authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-13 13:48:41-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-13 13:48:41-04:00
logac0cda8df81d5dc7d782ad8a32c0e5b064dd24ec
treed0eef967a9851bb83b8043ad6bb73dbb46ed8a00
parent22e39e1e5ab42c2c3d33eeb6797f5835b2004fe3
signaturelock-open Commit is signed but in an unrecognized format.

add compile error for merging non- error sets

closes #1509

3 files changed, 26 insertions(+), 0 deletions(-)

src/analyze.cpp+1
...@@ -4069,6 +4069,7 @@ static void define_local_param_variables(CodeGen *g, ZigFn *fn_table_entry) {...@@ -4069,6 +4069,7 @@ static void define_local_param_variables(CodeGen *g, ZigFn *fn_table_entry) {
4069}4069}
40704070
4071bool resolve_inferred_error_set(CodeGen *g, ZigType *err_set_type, AstNode *source_node) {4071bool resolve_inferred_error_set(CodeGen *g, ZigType *err_set_type, AstNode *source_node) {
4072 assert(err_set_type->id == ZigTypeIdErrorSet);
4072 ZigFn *infer_fn = err_set_type->data.error_set.infer_fn;4073 ZigFn *infer_fn = err_set_type->data.error_set.infer_fn;
4073 if (infer_fn != nullptr) {4074 if (infer_fn != nullptr) {
4074 if (infer_fn->anal_state == FnAnalStateInvalid) {4075 if (infer_fn->anal_state == FnAnalStateInvalid) {
src/ir.cpp+12
...@@ -12469,10 +12469,22 @@ static ZigType *ir_analyze_merge_error_sets(IrAnalyze *ira, IrInstructionBinOp *...@@ -12469,10 +12469,22 @@ static ZigType *ir_analyze_merge_error_sets(IrAnalyze *ira, IrInstructionBinOp *
12469 if (type_is_invalid(op1_type))12469 if (type_is_invalid(op1_type))
12470 return ira->codegen->builtin_types.entry_invalid;12470 return ira->codegen->builtin_types.entry_invalid;
1247112471
12472 if (op1_type->id != ZigTypeIdErrorSet) {
12473 ir_add_error(ira, instruction->op1,
12474 buf_sprintf("expected error set type, found '%s'", buf_ptr(&op1_type->name)));
12475 return ira->codegen->builtin_types.entry_invalid;
12476 }
12477
12472 ZigType *op2_type = ir_resolve_type(ira, instruction->op2->other);12478 ZigType *op2_type = ir_resolve_type(ira, instruction->op2->other);
12473 if (type_is_invalid(op2_type))12479 if (type_is_invalid(op2_type))
12474 return ira->codegen->builtin_types.entry_invalid;12480 return ira->codegen->builtin_types.entry_invalid;
1247512481
12482 if (op2_type->id != ZigTypeIdErrorSet) {
12483 ir_add_error(ira, instruction->op2,
12484 buf_sprintf("expected error set type, found '%s'", buf_ptr(&op2_type->name)));
12485 return ira->codegen->builtin_types.entry_invalid;
12486 }
12487
12476 if (type_is_global_error_set(op1_type) ||12488 if (type_is_global_error_set(op1_type) ||
12477 type_is_global_error_set(op2_type))12489 type_is_global_error_set(op2_type))
12478 {12490 {
test/compile_errors.zig+13
...@@ -1,6 +1,19 @@...@@ -1,6 +1,19 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: *tests.CompileErrorContext) void {3pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.add(
5 "non error sets used in merge error sets operator",
6 \\export fn foo() void {
7 \\ const Errors = u8 || u16;
8 \\}
9 \\export fn bar() void {
10 \\ const Errors = error{} || u16;
11 \\}
12 ,
13 ".tmp_source.zig:2:20: error: expected error set type, found 'u8'",
14 ".tmp_source.zig:5:31: error: expected error set type, found 'u16'",
15 );
16
4 cases.add(17 cases.add(
5 "variable initialization compile error then referenced",18 "variable initialization compile error then referenced",
6 \\fn Undeclared() type {19 \\fn Undeclared() type {