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) {...@@ -9243,6 +9243,7 @@ bool type_is_numeric(ZigType *ty) {
9243 case ZigTypeIdComptimeInt:9243 case ZigTypeIdComptimeInt:
9244 case ZigTypeIdInt:9244 case ZigTypeIdInt:
9245 case ZigTypeIdFloat:9245 case ZigTypeIdFloat:
9246 case ZigTypeIdUndefined:
9246 return true;9247 return true;
92479248
9248 case ZigTypeIdVector:9249 case ZigTypeIdVector:
...@@ -9255,7 +9256,6 @@ bool type_is_numeric(ZigType *ty) {...@@ -9255,7 +9256,6 @@ bool type_is_numeric(ZigType *ty) {
9255 case ZigTypeIdPointer:9256 case ZigTypeIdPointer:
9256 case ZigTypeIdArray:9257 case ZigTypeIdArray:
9257 case ZigTypeIdStruct:9258 case ZigTypeIdStruct:
9258 case ZigTypeIdUndefined:
9259 case ZigTypeIdNull:9259 case ZigTypeIdNull:
9260 case ZigTypeIdOptional:9260 case ZigTypeIdOptional:
9261 case ZigTypeIdErrorUnion:9261 case ZigTypeIdErrorUnion:
src/ir.cpp+3-1
...@@ -14647,7 +14647,9 @@ never_mind_just_calculate_it_normally:...@@ -14647,7 +14647,9 @@ never_mind_just_calculate_it_normally:
14647 }14647 }
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 {
14651 out_val->special = ConstValSpecialUndef;14653 out_val->special = ConstValSpecialUndef;
14652 return nullptr;14654 return nullptr;
14653 }14655 }
test/compile_errors.zig+8
...@@ -2,6 +2,14 @@ const tests = @import("tests.zig");...@@ -2,6 +2,14 @@ const tests = @import("tests.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub 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
5 cases.add("comptime ptrcast of zero-sized type",13 cases.add("comptime ptrcast of zero-sized type",
6 \\fn foo() void {14 \\fn foo() void {
7 \\ const node: struct {} = undefined;15 \\ const node: struct {} = undefined;
test/stage1/behavior/math.zig+7
...@@ -671,3 +671,10 @@ test "vector comparison" {...@@ -671,3 +671,10 @@ test "vector comparison" {
671 S.doTheTest();671 S.doTheTest();
672 comptime S.doTheTest();672 comptime S.doTheTest();
673}673}
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}