Skip to content

Fix unreachable on bin op between numeric and undefined #4005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
8 changes: 8 additions & 0 deletions test/compile_errors.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down