From ac0cda8df81d5dc7d782ad8a32c0e5b064dd24ec Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 13 Sep 2018 13:48:41 -0400 Subject: [PATCH] add compile error for merging non- error sets closes #1509 --- src/analyze.cpp | 1 + src/ir.cpp | 12 ++++++++++++ test/compile_errors.zig | 13 +++++++++++++ 3 files changed, 26 insertions(+) diff --git a/src/analyze.cpp b/src/analyze.cpp index 78d907970eaf..9a9e9a051352 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -4069,6 +4069,7 @@ static void define_local_param_variables(CodeGen *g, ZigFn *fn_table_entry) { } bool resolve_inferred_error_set(CodeGen *g, ZigType *err_set_type, AstNode *source_node) { + assert(err_set_type->id == ZigTypeIdErrorSet); ZigFn *infer_fn = err_set_type->data.error_set.infer_fn; if (infer_fn != nullptr) { if (infer_fn->anal_state == FnAnalStateInvalid) { diff --git a/src/ir.cpp b/src/ir.cpp index 3a94cdef38d4..b432facb3620 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -12469,10 +12469,22 @@ static ZigType *ir_analyze_merge_error_sets(IrAnalyze *ira, IrInstructionBinOp * if (type_is_invalid(op1_type)) return ira->codegen->builtin_types.entry_invalid; + if (op1_type->id != ZigTypeIdErrorSet) { + ir_add_error(ira, instruction->op1, + buf_sprintf("expected error set type, found '%s'", buf_ptr(&op1_type->name))); + return ira->codegen->builtin_types.entry_invalid; + } + ZigType *op2_type = ir_resolve_type(ira, instruction->op2->other); if (type_is_invalid(op2_type)) return ira->codegen->builtin_types.entry_invalid; + if (op2_type->id != ZigTypeIdErrorSet) { + ir_add_error(ira, instruction->op2, + buf_sprintf("expected error set type, found '%s'", buf_ptr(&op2_type->name))); + return ira->codegen->builtin_types.entry_invalid; + } + if (type_is_global_error_set(op1_type) || type_is_global_error_set(op2_type)) { diff --git a/test/compile_errors.zig b/test/compile_errors.zig index dda45a5897f0..8fb64e21e28c 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -1,6 +1,19 @@ const tests = @import("tests.zig"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add( + "non error sets used in merge error sets operator", + \\export fn foo() void { + \\ const Errors = u8 || u16; + \\} + \\export fn bar() void { + \\ const Errors = error{} || u16; + \\} + , + ".tmp_source.zig:2:20: error: expected error set type, found 'u8'", + ".tmp_source.zig:5:31: error: expected error set type, found 'u16'", + ); + cases.add( "variable initialization compile error then referenced", \\fn Undeclared() type {