authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-30 19:08:57-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-30 19:08:57-05:00
loga05150e92d5925f14591d96220de3bb341be1154
tree55d6d955dbaacf58c426106cca8f280736c3fd32
parenta3d04b92287082f9f51c906075d42d53a552c19d
signature Commit is signed but in an unrecognized format.

fix comparing comptime_int against undefined literal

closes #4004

4 files changed, 19 insertions(+), 2 deletions(-)

src/analyze.cpp+1-1
......@@ -9243,6 +9243,7 @@ bool type_is_numeric(ZigType *ty) {
92439243 case ZigTypeIdComptimeInt:
92449244 case ZigTypeIdInt:
92459245 case ZigTypeIdFloat:
9246 case ZigTypeIdUndefined:
92469247 return true;
92479248
92489249 case ZigTypeIdVector:
......@@ -9255,7 +9256,6 @@ bool type_is_numeric(ZigType *ty) {
92559256 case ZigTypeIdPointer:
92569257 case ZigTypeIdArray:
92579258 case ZigTypeIdStruct:
9258 case ZigTypeIdUndefined:
92599259 case ZigTypeIdNull:
92609260 case ZigTypeIdOptional:
92619261 case ZigTypeIdErrorUnion:
src/ir.cpp+3-1
......@@ -14647,7 +14647,9 @@ never_mind_just_calculate_it_normally:
1464714647 }
1464814648
1464914649
14650 if (op1_val->special == ConstValSpecialUndef || op2_val->special == ConstValSpecialUndef) {
14650 if (op1_val->special == ConstValSpecialUndef || op2_val->special == ConstValSpecialUndef ||
14651 op1_val->type->id == ZigTypeIdUndefined || op2_val->type->id == ZigTypeIdUndefined)
14652 {
1465114653 out_val->special = ConstValSpecialUndef;
1465214654 return nullptr;
1465314655 }
test/compile_errors.zig+8
......@@ -2,6 +2,14 @@ const tests = @import("tests.zig");
22const builtin = @import("builtin");
33
44pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add("comparing against undefined produces undefined value",
6 \\export fn entry() void {
7 \\ if (2 == undefined) {}
8 \\}
9 , &[_][]const u8{
10 "tmp.zig:2:11: error: use of undefined value here causes undefined behavior",
11 });
12
513 cases.add("comptime ptrcast of zero-sized type",
614 \\fn foo() void {
715 \\ const node: struct {} = undefined;
test/stage1/behavior/math.zig+7
......@@ -671,3 +671,10 @@ test "vector comparison" {
671671 S.doTheTest();
672672 comptime S.doTheTest();
673673}
674
675test "compare undefined literal with comptime_int" {
676 var x = undefined == 1;
677 // x is now undefined with type bool
678 x = true;
679 expect(x);
680}