authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-12 14:33:17-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-12 14:33:17-07:00
log70d8baaec11ca370b73fce72d7f3dfce2277455b
tree96cd79022024f6a65584dd0aab52957d5fb850be
parent7048e93665543005ed3dabd2e7637f01c584006a

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.

2 files changed, 10 insertions(+), 34 deletions(-)

src/Sema.zig+10-24
......@@ -8898,16 +8898,10 @@ fn analyzeErrUnionCode(sema: *Sema, block: *Block, src: LazySrcLoc, operand: Air
88988898 const result_ty = operand_ty.errorUnionSet(mod);
88998899
89008900 if (try sema.resolveDefinedValue(block, src, operand)) |val| {
8901 switch (mod.intern_pool.indexToKey(val.toIntern()).error_union.val) {
8902 .err_name => |err_name| return Air.internedToRef((try mod.intern(.{ .err = .{
8903 .ty = result_ty.toIntern(),
8904 .name = err_name,
8905 } }))),
8906 .payload => |payload| {
8907 assert(payload.toValue().isUndef(mod));
8908 return mod.undefRef(result_ty);
8909 },
8910 }
8901 return Air.internedToRef((try mod.intern(.{ .err = .{
8902 .ty = result_ty.toIntern(),
8903 .name = mod.intern_pool.indexToKey(val.toIntern()).error_union.val.err_name,
8904 } })));
89118905 }
89128906
89138907 try sema.requireRuntimeBlock(block, src, null);
......@@ -16419,7 +16413,6 @@ fn zirCmp(
1641916413 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1642016414 const lhs = try sema.resolveInst(extra.lhs);
1642116415 const rhs = try sema.resolveInst(extra.rhs);
16422
1642316416 return sema.analyzeCmp(block, src, lhs, rhs, op, lhs_src, rhs_src, false);
1642416417}
1642516418
......@@ -16451,16 +16444,10 @@ fn analyzeCmp(
1645116444 return sema.cmpNumeric(block, src, lhs, rhs, op, lhs_src, rhs_src);
1645216445 }
1645316446 if (is_equality_cmp and lhs_ty.zigTypeTag(mod) == .ErrorUnion and rhs_ty.zigTypeTag(mod) == .ErrorSet) {
16454 if (try sema.resolveValue(lhs)) |lhs_val| {
16455 if (lhs_val.isUndef(mod)) return mod.undefRef(Type.bool);
16456 }
1645716447 const casted_lhs = try sema.analyzeErrUnionCode(block, lhs_src, lhs);
1645816448 return sema.cmpSelf(block, src, casted_lhs, rhs, op, lhs_src, rhs_src);
1645916449 }
1646016450 if (is_equality_cmp and lhs_ty.zigTypeTag(mod) == .ErrorSet and rhs_ty.zigTypeTag(mod) == .ErrorUnion) {
16461 if (try sema.resolveValue(rhs)) |rhs_val| {
16462 if (rhs_val.isUndef(mod)) return mod.undefRef(Type.bool);
16463 }
1646416451 const casted_rhs = try sema.analyzeErrUnionCode(block, rhs_src, rhs);
1646516452 return sema.cmpSelf(block, src, lhs, casted_rhs, op, lhs_src, rhs_src);
1646616453 }
......@@ -16524,9 +16511,11 @@ fn cmpSelf(
1652416511 } else {
1652516512 // For bools, we still check the other operand, because we can lower
1652616513 // bool eq/neq more efficiently.
16527 if (try sema.resolveValue(casted_rhs)) |rhs_val| {
16528 if (rhs_val.isUndef(mod)) return mod.undefRef(Type.bool);
16529 if (resolved_type.zigTypeTag(mod) == .Bool) return sema.runtimeBoolCmp(block, src, op, casted_lhs, rhs_val.toBool(), lhs_src);
16514 if (resolved_type.zigTypeTag(mod) == .Bool) {
16515 if (try sema.resolveValue(casted_rhs)) |rhs_val| {
16516 if (rhs_val.isUndef(mod)) return mod.undefRef(Type.bool);
16517 return sema.runtimeBoolCmp(block, src, op, casted_lhs, rhs_val.toBool(), lhs_src);
16518 }
1653016519 }
1653116520 break :src lhs_src;
1653216521 }
......@@ -38011,10 +38000,7 @@ fn compareVector(
3801138000 const lhs_elem = try lhs.elemValue(sema.mod, i);
3801238001 const rhs_elem = try rhs.elemValue(sema.mod, i);
3801338002 const res_bool = try sema.compareScalar(lhs_elem, op, rhs_elem, ty.scalarType(mod));
38014 scalar.* = if (lhs_elem.isUndef(mod) or rhs_elem.isUndef(mod))
38015 Air.refToInterned(try mod.undefRef(Type.bool)).?
38016 else
38017 try Value.makeBool(res_bool).intern(Type.bool, mod);
38003 scalar.* = try Value.makeBool(res_bool).intern(Type.bool, mod);
3801838004 }
3801938005 return (try mod.intern(.{ .aggregate = .{
3802038006 .ty = (try mod.vectorType(.{ .len = ty.vectorLen(mod), .child = .bool_type })).toIntern(),
test/cases/compile_errors/comparison_with_comptime-known_undefined.zig deleted-10
......@@ -1,10 +0,0 @@
1pub fn entry() void {
2 var foo: ?*i32 = undefined;
3 if (foo == undefined) {}
4}
5
6// error
7// backend=stage2
8// target=native
9//
10// :3:13: error: use of undefined value here causes undefined behavior