authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-17 18:58:50-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-17 18:58:50-04:00
log78a9a465a372d25ef6f9a1f6bb1018160b8c94e1
tree3087d77f8411acc3227f3e0b04c1b3be6c8457b6
parent6c71e9a54da068202794c309e895b3f9ccfeb161
signaturelock-open Commit is signed but in an unrecognized format.

add compile error for non-optional types compared against null

closes #1539

2 files changed, 13 insertions(+), 0 deletions(-)

src/ir.cpp+3
......@@ -11571,6 +11571,9 @@ static ZigType *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *bin_op
1157111571 ir_link_new_instruction(is_non_null, &bin_op_instruction->base);
1157211572 }
1157311573 return ira->codegen->builtin_types.entry_bool;
11574 } else if (op1->value.type->id == ZigTypeIdNull || op2->value.type->id == ZigTypeIdNull) {
11575 ir_add_error_node(ira, source_node, buf_sprintf("comparison against null can only be done with optionals"));
11576 return ira->codegen->builtin_types.entry_invalid;
1157411577 }
1157511578
1157611579 if (op1->value.type->id == ZigTypeIdErrorSet && op2->value.type->id == ZigTypeIdErrorSet) {
test/compile_errors.zig+10
......@@ -1,6 +1,16 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.add(
5 "comparing a non-optional pointer against null",
6 \\export fn entry() void {
7 \\ var x: i32 = 1;
8 \\ _ = &x == null;
9 \\}
10 ,
11 ".tmp_source.zig:3:12: error: comparison against null can only be done with optionals",
12 );
13
414 cases.add(
515 "non error sets used in merge error sets operator",
616 \\export fn foo() void {