Commit ac0cda8df8

Andrew Kelley <superjoe30@gmail.com>
2018-09-13 19:48:41
add compile error for merging non- error sets
closes #1509
1 parent 22e39e1
Changed files (3)
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) {
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))
     {
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 {