Skip to content

Commit a503849

Browse files
committed
compiler: un-implement ziglang#19634
This commit reverts the handling of partially-undefined values in bitcasting to transform these bits into an arbitrary arithmetic value, like happens on `master` today. As @andrewrk rightly points out, ziglang#19634 has unfortunate consequences for the standard library, and likely requires more thought. To avoid a major breaking change, it has been decided to revert this design decision for now, and make a more informed decision further down the line.
1 parent 6c55960 commit a503849

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

lib/std/net.zig

-9
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,6 @@ pub const Ip4Address = extern struct {
278278
},
279279
};
280280
const out_ptr = mem.asBytes(&result.sa.addr);
281-
if (@inComptime()) {
282-
@memset(out_ptr, 0); // TODO: #19634
283-
}
284281

285282
var x: u8 = 0;
286283
var index: u8 = 0;
@@ -392,9 +389,6 @@ pub const Ip6Address = extern struct {
392389
.addr = undefined,
393390
},
394391
};
395-
if (@inComptime()) {
396-
@memset(std.mem.asBytes(&result.sa.addr), 0); // TODO: #19634
397-
}
398392
var ip_slice: *[16]u8 = result.sa.addr[0..];
399393

400394
var tail: [16]u8 = undefined;
@@ -513,9 +507,6 @@ pub const Ip6Address = extern struct {
513507
.addr = undefined,
514508
},
515509
};
516-
if (@inComptime()) {
517-
@memset(std.mem.asBytes(&result.sa.addr), 0); // TODO: #19634
518-
}
519510
var ip_slice: *[16]u8 = result.sa.addr[0..];
520511

521512
var tail: [16]u8 = undefined;

lib/std/packed_int_array.zig

-8
Original file line numberDiff line numberDiff line change
@@ -214,21 +214,13 @@ pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: Endian, comptim
214214
/// or, more likely, an array literal.
215215
pub fn init(ints: [int_count]Int) Self {
216216
var self: Self = undefined;
217-
if (@inComptime()) {
218-
// TODO: #19634
219-
@memset(&self.bytes, 0xAA);
220-
}
221217
for (ints, 0..) |int, i| self.set(i, int);
222218
return self;
223219
}
224220

225221
/// Initialize all entries of a packed array to the same value.
226222
pub fn initAllTo(int: Int) Self {
227223
var self: Self = undefined;
228-
if (@inComptime()) {
229-
// TODO: #19634
230-
@memset(&self.bytes, 0xAA);
231-
}
232224
self.setAll(int);
233225
return self;
234226
}

src/Sema/bitcast.zig

+16-4
Original file line numberDiff line numberDiff line change
@@ -681,12 +681,24 @@ const PackValueBits = struct {
681681
const vals, const bit_offset = pack.prepareBits(want_ty.bitSize(zcu));
682682

683683
for (vals) |val| {
684-
if (Value.fromInterned(val).isUndef(zcu)) {
685-
// The value contains undef bits, so is considered entirely undef.
686-
return zcu.undefValue(want_ty);
687-
}
684+
if (!Value.fromInterned(val).isUndef(zcu)) break;
685+
} else {
686+
// All bits of the value are `undefined`.
687+
return zcu.undefValue(want_ty);
688688
}
689689

690+
// TODO: we need to decide how to handle partially-undef values here.
691+
// Currently, a value with some undefined bits becomes `0xAA` so that we
692+
// preserve the well-defined bits, because we can't currently represent
693+
// a partially-undefined primitive (e.g. an int with some undef bits).
694+
// In future, we probably want to take one of these two routes:
695+
// * Define that if any bits are `undefined`, the entire value is `undefined`.
696+
// This is a major breaking change, and probably a footgun.
697+
// * Introduce tracking for partially-undef values at comptime.
698+
// This would complicate a lot of operations in Sema, such as basic
699+
// arithmetic.
700+
// This design complexity is tracked by #19634.
701+
690702
ptr_cast: {
691703
if (vals.len != 1) break :ptr_cast;
692704
const val = Value.fromInterned(vals[0]);

0 commit comments

Comments
 (0)