Type: struct {} does not have a well-defined layout

`Type.hasWellDefinedLayout` was in disagreement with pointer loading
logic about auto-layout structs with zero fields, `struct {}`. For
consistency, these types should not have a well-defined layout.

This is technically a breaking change.
This commit is contained in:
mlugg 2025-01-12 23:45:22 +00:00 committed by Matthew Lugg
parent affe45b31f
commit 27274d4fde
2 changed files with 12 additions and 5 deletions

View File

@ -723,11 +723,7 @@ pub fn hasWellDefinedLayout(ty: Type, zcu: *const Zcu) bool {
.generic_poison,
=> false,
},
.struct_type => {
const struct_type = ip.loadStructType(ty.toIntern());
// Struct with no fields have a well-defined layout of no bits.
return struct_type.layout != .auto or struct_type.field_types.len == 0;
},
.struct_type => ip.loadStructType(ty.toIntern()).layout != .auto,
.union_type => {
const union_type = ip.loadUnionType(ty.toIntern());
return switch (union_type.flagsUnordered(ip).runtime_tag) {

View File

@ -0,0 +1,11 @@
const A = struct {};
const B = struct {};
comptime {
const val: [1]A = .{.{}};
const ptr: *const [1]B = @ptrCast(&val);
_ = ptr ** 2;
}
// error
//
// :6:9: error: comptime dereference requires '[1]tmp.B' to have a well-defined layout