From 27274d4fdea8061d7afacbb2b179fd49fbaca125 Mon Sep 17 00:00:00 2001 From: mlugg Date: Sun, 12 Jan 2025 23:45:22 +0000 Subject: [PATCH] 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. --- src/Type.zig | 6 +----- .../dereference_bad_pointer_via_array_mul.zig | 11 +++++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 test/cases/compile_errors/dereference_bad_pointer_via_array_mul.zig diff --git a/src/Type.zig b/src/Type.zig index b47a45d1fc..bfc883bede 100644 --- a/src/Type.zig +++ b/src/Type.zig @@ -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) { diff --git a/test/cases/compile_errors/dereference_bad_pointer_via_array_mul.zig b/test/cases/compile_errors/dereference_bad_pointer_via_array_mul.zig new file mode 100644 index 0000000000..cc54eeacb8 --- /dev/null +++ b/test/cases/compile_errors/dereference_bad_pointer_via_array_mul.zig @@ -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