Commit 70d8baaec1

Andrew Kelley <andrew@ziglang.org>
2023-11-12 22:33:17
Revert "Sema: fix comparison with undefined"
This reverts commit 547481c31c8a538a7badbdce66d81820177ce87f. There is a comment that did not get addressed with this patch, and the required test cases are not added. Reopens #17798.
1 parent 7048e93
Changed files (2)
src/Sema.zig
@@ -8898,16 +8898,10 @@ fn analyzeErrUnionCode(sema: *Sema, block: *Block, src: LazySrcLoc, operand: Air
     const result_ty = operand_ty.errorUnionSet(mod);
 
     if (try sema.resolveDefinedValue(block, src, operand)) |val| {
-        switch (mod.intern_pool.indexToKey(val.toIntern()).error_union.val) {
-            .err_name => |err_name| return Air.internedToRef((try mod.intern(.{ .err = .{
-                .ty = result_ty.toIntern(),
-                .name = err_name,
-            } }))),
-            .payload => |payload| {
-                assert(payload.toValue().isUndef(mod));
-                return mod.undefRef(result_ty);
-            },
-        }
+        return Air.internedToRef((try mod.intern(.{ .err = .{
+            .ty = result_ty.toIntern(),
+            .name = mod.intern_pool.indexToKey(val.toIntern()).error_union.val.err_name,
+        } })));
     }
 
     try sema.requireRuntimeBlock(block, src, null);
@@ -16419,7 +16413,6 @@ fn zirCmp(
     const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
     const lhs = try sema.resolveInst(extra.lhs);
     const rhs = try sema.resolveInst(extra.rhs);
-
     return sema.analyzeCmp(block, src, lhs, rhs, op, lhs_src, rhs_src, false);
 }
 
@@ -16451,16 +16444,10 @@ fn analyzeCmp(
         return sema.cmpNumeric(block, src, lhs, rhs, op, lhs_src, rhs_src);
     }
     if (is_equality_cmp and lhs_ty.zigTypeTag(mod) == .ErrorUnion and rhs_ty.zigTypeTag(mod) == .ErrorSet) {
-        if (try sema.resolveValue(lhs)) |lhs_val| {
-            if (lhs_val.isUndef(mod)) return mod.undefRef(Type.bool);
-        }
         const casted_lhs = try sema.analyzeErrUnionCode(block, lhs_src, lhs);
         return sema.cmpSelf(block, src, casted_lhs, rhs, op, lhs_src, rhs_src);
     }
     if (is_equality_cmp and lhs_ty.zigTypeTag(mod) == .ErrorSet and rhs_ty.zigTypeTag(mod) == .ErrorUnion) {
-        if (try sema.resolveValue(rhs)) |rhs_val| {
-            if (rhs_val.isUndef(mod)) return mod.undefRef(Type.bool);
-        }
         const casted_rhs = try sema.analyzeErrUnionCode(block, rhs_src, rhs);
         return sema.cmpSelf(block, src, lhs, casted_rhs, op, lhs_src, rhs_src);
     }
@@ -16524,9 +16511,11 @@ fn cmpSelf(
         } else {
             // For bools, we still check the other operand, because we can lower
             // bool eq/neq more efficiently.
-            if (try sema.resolveValue(casted_rhs)) |rhs_val| {
-                if (rhs_val.isUndef(mod)) return mod.undefRef(Type.bool);
-                if (resolved_type.zigTypeTag(mod) == .Bool) return sema.runtimeBoolCmp(block, src, op, casted_lhs, rhs_val.toBool(), lhs_src);
+            if (resolved_type.zigTypeTag(mod) == .Bool) {
+                if (try sema.resolveValue(casted_rhs)) |rhs_val| {
+                    if (rhs_val.isUndef(mod)) return mod.undefRef(Type.bool);
+                    return sema.runtimeBoolCmp(block, src, op, casted_lhs, rhs_val.toBool(), lhs_src);
+                }
             }
             break :src lhs_src;
         }
@@ -38011,10 +38000,7 @@ fn compareVector(
         const lhs_elem = try lhs.elemValue(sema.mod, i);
         const rhs_elem = try rhs.elemValue(sema.mod, i);
         const res_bool = try sema.compareScalar(lhs_elem, op, rhs_elem, ty.scalarType(mod));
-        scalar.* = if (lhs_elem.isUndef(mod) or rhs_elem.isUndef(mod))
-            Air.refToInterned(try mod.undefRef(Type.bool)).?
-        else
-            try Value.makeBool(res_bool).intern(Type.bool, mod);
+        scalar.* = try Value.makeBool(res_bool).intern(Type.bool, mod);
     }
     return (try mod.intern(.{ .aggregate = .{
         .ty = (try mod.vectorType(.{ .len = ty.vectorLen(mod), .child = .bool_type })).toIntern(),
test/cases/compile_errors/comparison_with_comptime-known_undefined.zig
@@ -1,10 +0,0 @@
-pub fn entry() void {
-    var foo: ?*i32 = undefined;
-    if (foo == undefined) {}
-}
-
-// error
-// backend=stage2
-// target=native
-//
-// :3:13: error: use of undefined value here causes undefined behavior