Skip to content

Commit

Permalink
add compile error for merging non- error sets
Browse files Browse the repository at this point in the history
closes #1509
  • Loading branch information
andrewrk committed Sep 13, 2018
1 parent 22e39e1 commit ac0cda8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/analyze.cpp
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions src/ir.cpp
Expand Up @@ -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))
{
Expand Down
13 changes: 13 additions & 0 deletions 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 {
Expand Down

0 comments on commit ac0cda8

Please sign in to comment.