add compile error for non-optional types compared against null

closes #1539
This commit is contained in:
Andrew Kelley 2018-09-17 18:58:50 -04:00
parent 6c71e9a54d
commit 78a9a465a3
No known key found for this signature in database
GPG Key ID: 4E7CD66038A4D47C
2 changed files with 13 additions and 0 deletions

View File

@ -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) {

View File

@ -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 {