From 6cee98eb3074fcb99297f23f30e3a230a14e8db7 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 25 Jul 2023 10:46:49 -0700 Subject: [PATCH] frontend: forbid packed and extern tuples --- src/AstGen.zig | 7 +++++++ src/Sema.zig | 6 ++++++ src/codegen/llvm/Builder.zig | 4 ++-- test/behavior/tuple_declarations.zig | 21 --------------------- test/cases/compile_errors/reify_struct.zig | 4 ++-- 5 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/AstGen.zig b/src/AstGen.zig index f7b81765b8..3a3dfa8160 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -4687,6 +4687,13 @@ fn structDeclInner( const container_field = tree.fullContainerField(member_node) orelse continue; if (container_field.ast.tuple_like) break true; } else false; + + if (is_tuple) switch (layout) { + .Auto => {}, + .Extern => return astgen.failNode(node, "extern tuples are not supported", .{}), + .Packed => return astgen.failNode(node, "packed tuples are not supported", .{}), + }; + if (is_tuple) for (container_decl.ast.members) |member_node| { switch (node_tags[member_node]) { .container_field_init, diff --git a/src/Sema.zig b/src/Sema.zig index 2f068bd98f..3ae2ab96bd 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -20391,6 +20391,12 @@ fn reifyStruct( const gpa = sema.gpa; const ip = &mod.intern_pool; + if (is_tuple) switch (layout) { + .Extern => return sema.fail(block, src, "extern tuples are not supported", .{}), + .Packed => return sema.fail(block, src, "packed tuples are not supported", .{}), + .Auto => {}, + }; + // Because these three things each reference each other, `undefined` // placeholders are used before being set after the struct type gains an // InternPool index. diff --git a/src/codegen/llvm/Builder.zig b/src/codegen/llvm/Builder.zig index 4abe7902ef..3e6cf31d69 100644 --- a/src/codegen/llvm/Builder.zig +++ b/src/codegen/llvm/Builder.zig @@ -4164,8 +4164,8 @@ pub const WipFunction = struct { @memcpy(extra.trail.nextMut(incoming_len, Block.Index, wip), blocks); if (wip.builder.useLibLlvm()) { const ExpectedContents = extern struct { - [expected_incoming_len]*llvm.Value, - [expected_incoming_len]*llvm.BasicBlock, + values: [expected_incoming_len]*llvm.Value, + blocks: [expected_incoming_len]*llvm.BasicBlock, }; var stack align(@alignOf(ExpectedContents)) = std.heap.stackFallback(@sizeOf(ExpectedContents), wip.builder.gpa); diff --git a/test/behavior/tuple_declarations.zig b/test/behavior/tuple_declarations.zig index 84b04d3e53..1936d77043 100644 --- a/test/behavior/tuple_declarations.zig +++ b/test/behavior/tuple_declarations.zig @@ -31,27 +31,6 @@ test "tuple declaration type info" { try expect(!info.fields[1].is_comptime); try expect(info.fields[1].alignment == @alignOf([]const u8)); } - { - const T = packed struct(u32) { u1, u30, u1 }; - const info = @typeInfo(T).Struct; - - try expect(std.mem.endsWith(u8, @typeName(T), "test.tuple declaration type info.T")); - - try expect(info.layout == .Packed); - try expect(info.backing_integer == u32); - try expect(info.fields.len == 3); - try expect(info.decls.len == 0); - try expect(info.is_tuple); - - try expectEqualStrings(info.fields[0].name, "0"); - try expect(info.fields[0].type == u1); - - try expectEqualStrings(info.fields[1].name, "1"); - try expect(info.fields[1].type == u30); - - try expectEqualStrings(info.fields[2].name, "2"); - try expect(info.fields[2].type == u1); - } } test "Tuple declaration usage" { diff --git a/test/cases/compile_errors/reify_struct.zig b/test/cases/compile_errors/reify_struct.zig index 42b8e42af5..f20ea15048 100644 --- a/test/cases/compile_errors/reify_struct.zig +++ b/test/cases/compile_errors/reify_struct.zig @@ -51,7 +51,7 @@ comptime { .alignment = 4, }}, .decls = &.{}, - .is_tuple = true, + .is_tuple = false, } }); } comptime { @@ -65,7 +65,7 @@ comptime { .alignment = 4, }}, .decls = &.{}, - .is_tuple = true, + .is_tuple = false, } }); }