Skip to content

Commit

Permalink
add compile error for non-optional types compared against null
Browse files Browse the repository at this point in the history
closes #1539
  • Loading branch information
andrewrk committed Sep 17, 2018
1 parent 6c71e9a commit 78a9a46
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11571,6 +11571,9 @@ static ZigType *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *bin_op
ir_link_new_instruction(is_non_null, &bin_op_instruction->base);
}
return ira->codegen->builtin_types.entry_bool;
} else if (op1->value.type->id == ZigTypeIdNull || op2->value.type->id == ZigTypeIdNull) {
ir_add_error_node(ira, source_node, buf_sprintf("comparison against null can only be done with optionals"));
return ira->codegen->builtin_types.entry_invalid;
}

if (op1->value.type->id == ZigTypeIdErrorSet && op2->value.type->id == ZigTypeIdErrorSet) {
Expand Down
10 changes: 10 additions & 0 deletions test/compile_errors.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
const tests = @import("tests.zig");

pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add(
"comparing a non-optional pointer against null",
\\export fn entry() void {
\\ var x: i32 = 1;
\\ _ = &x == null;
\\}
,
".tmp_source.zig:3:12: error: comparison against null can only be done with optionals",
);

cases.add(
"non error sets used in merge error sets operator",
\\export fn foo() void {
Expand Down

0 comments on commit 78a9a46

Please sign in to comment.