diff --git a/src/ir.cpp b/src/ir.cpp index a862ff1068fc..4db12f9ce9b8 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -15118,6 +15118,11 @@ static IrInstruction *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp * ir_add_error_node(ira, source_node, buf_sprintf("comparison of '%s' with null", buf_ptr(&non_null_type->name))); return ira->codegen->invalid_instruction; + } else if (op1->value->type->id == ZigTypeIdUndefined || op2->value->type->id == ZigTypeIdUndefined) { + ZigType *other_type = (op1->value->type->id == ZigTypeIdUndefined) ? op2->value->type : op1->value->type; + ir_add_error_node(ira, source_node, buf_sprintf("comparison of '%s' with undefined", + buf_ptr(&other_type->name))); + return ira->codegen->invalid_instruction; } else if (is_equality_cmp && ( (op1->value->type->id == ZigTypeIdEnumLiteral && op2->value->type->id == ZigTypeIdUnion) || (op2->value->type->id == ZigTypeIdEnumLiteral && op1->value->type->id == ZigTypeIdUnion))) diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 450f91b9be5d..029c35754031 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2,6 +2,14 @@ const tests = @import("tests.zig"); const builtin = @import("builtin"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add("comparing against undefined", + \\pub fn main() void { + \\ _ = 2 == undefined; + \\} + , &[_][]const u8{ + "tmp.zig:2:11: error: comparison of 'comptime_int' with undefined", + }); + cases.add("slice sentinel mismatch", \\fn foo() [:0]u8 { \\ var x: []u8 = undefined;