From a05150e92d5925f14591d96220de3bb341be1154 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 30 Dec 2019 19:08:57 -0500 Subject: [PATCH] fix comparing comptime_int against undefined literal closes #4004 --- src/analyze.cpp | 2 +- src/ir.cpp | 4 +++- test/compile_errors.zig | 8 ++++++++ test/stage1/behavior/math.zig | 7 +++++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index dacb1d2b70f0219e2c8d75fb71eb4bdad8128fd6..8be25b759251c41ec0d33c63b63df270564d14b4 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -9243,6 +9243,7 @@ bool type_is_numeric(ZigType *ty) { case ZigTypeIdComptimeInt: case ZigTypeIdInt: case ZigTypeIdFloat: + case ZigTypeIdUndefined: return true; case ZigTypeIdVector: @@ -9255,7 +9256,6 @@ bool type_is_numeric(ZigType *ty) { case ZigTypeIdPointer: case ZigTypeIdArray: case ZigTypeIdStruct: - case ZigTypeIdUndefined: case ZigTypeIdNull: case ZigTypeIdOptional: case ZigTypeIdErrorUnion: diff --git a/src/ir.cpp b/src/ir.cpp index 149c41dfec222f6628c3ff40458ad285a187eea5..bf5a297a811c05aab40e89ff62e012a99263ef4d 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -14647,7 +14647,9 @@ never_mind_just_calculate_it_normally: } - if (op1_val->special == ConstValSpecialUndef || op2_val->special == ConstValSpecialUndef) { + if (op1_val->special == ConstValSpecialUndef || op2_val->special == ConstValSpecialUndef || + op1_val->type->id == ZigTypeIdUndefined || op2_val->type->id == ZigTypeIdUndefined) + { out_val->special = ConstValSpecialUndef; return nullptr; } diff --git a/test/compile_errors.zig b/test/compile_errors.zig index a653e1662ec3af7094d92faa27b7e9d32cd6d9f2..1c09ed9edf87697565b1b04b0e1a64caba428819 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 produces undefined value", + \\export fn entry() void { + \\ if (2 == undefined) {} + \\} + , &[_][]const u8{ + "tmp.zig:2:11: error: use of undefined value here causes undefined behavior", + }); + cases.add("comptime ptrcast of zero-sized type", \\fn foo() void { \\ const node: struct {} = undefined; diff --git a/test/stage1/behavior/math.zig b/test/stage1/behavior/math.zig index 3a73e776eb1cf35578fcf36c9d809efc73b835be..90260d4e021a439837b95ecf21f1b78044a2cacf 100644 --- a/test/stage1/behavior/math.zig +++ b/test/stage1/behavior/math.zig @@ -671,3 +671,10 @@ test "vector comparison" { S.doTheTest(); comptime S.doTheTest(); } + +test "compare undefined literal with comptime_int" { + var x = undefined == 1; + // x is now undefined with type bool + x = true; + expect(x); +} -- 2.54.0