diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2024-10-26 23:13:58 +0100 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2024-10-31 20:42:53 +0000 |
| commit | d11bbde5f9c64ef58405604601d55f88bb5d5f3a (patch) | |
| tree | 4e05b679cd791e90db68f3f2198294667a6fc57c /src/codegen/c.zig | |
| parent | a916bc7fdd3975a9e2ef13c44f814c71ce017193 (diff) | |
| download | zig-d11bbde5f9c64ef58405604601d55f88bb5d5f3a.tar.gz zig-d11bbde5f9c64ef58405604601d55f88bb5d5f3a.zip | |
compiler: remove anonymous struct types, unify all tuples
This commit reworks how anonymous struct literals and tuples work.
Previously, an untyped anonymous struct literal
(e.g. `const x = .{ .a = 123 }`) was given an "anonymous struct type",
which is a special kind of struct which coerces using structural
equivalence. This mechanism was a holdover from before we used
RLS / result types as the primary mechanism of type inference. This
commit changes the language so that the type assigned here is a "normal"
struct type. It uses a form of equivalence based on the AST node and the
type's structure, much like a reified (`@Type`) type.
Additionally, tuples have been simplified. The distinction between
"simple" and "complex" tuple types is eliminated. All tuples, even those
explicitly declared using `struct { ... }` syntax, use structural
equivalence, and do not undergo staged type resolution. Tuples are very
restricted: they cannot have non-`auto` layouts, cannot have aligned
fields, and cannot have default values with the exception of `comptime`
fields. Tuples currently do not have optimized layout, but this can be
changed in the future.
This change simplifies the language, and fixes some problematic
coercions through pointers which led to unintuitive behavior.
Resolves: #16865
Diffstat (limited to 'src/codegen/c.zig')
| -rw-r--r-- | src/codegen/c.zig | 41 |
1 files changed, 16 insertions, 25 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig index be8d2ad2a5..4d55637f27 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -891,7 +891,7 @@ pub const DeclGen = struct { .error_union_type, .simple_type, .struct_type, - .anon_struct_type, + .tuple_type, .union_type, .opaque_type, .enum_type, @@ -908,7 +908,7 @@ pub const DeclGen = struct { .undefined => unreachable, .void => unreachable, .null => unreachable, - .empty_struct => unreachable, + .empty_tuple => unreachable, .@"unreachable" => unreachable, .generic_poison => unreachable, @@ -1194,7 +1194,7 @@ pub const DeclGen = struct { try writer.writeByte('}'); } }, - .anon_struct_type => |tuple| { + .tuple_type => |tuple| { if (!location.isInitializer()) { try writer.writeByte('('); try dg.renderCType(writer, ctype); @@ -1605,7 +1605,7 @@ pub const DeclGen = struct { }), } }, - .anon_struct_type => |anon_struct_info| { + .tuple_type => |tuple_info| { if (!location.isInitializer()) { try writer.writeByte('('); try dg.renderCType(writer, ctype); @@ -1614,9 +1614,9 @@ pub const DeclGen = struct { try writer.writeByte('{'); var need_comma = false; - for (0..anon_struct_info.types.len) |field_index| { - if (anon_struct_info.values.get(ip)[field_index] != .none) continue; - const field_ty = Type.fromInterned(anon_struct_info.types.get(ip)[field_index]); + for (0..tuple_info.types.len) |field_index| { + if (tuple_info.values.get(ip)[field_index] != .none) continue; + const field_ty = Type.fromInterned(tuple_info.types.get(ip)[field_index]); if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; if (need_comma) try writer.writeByte(','); @@ -5411,9 +5411,9 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { const input_val = try f.resolveInst(input); try writer.print("{s}(", .{fmtStringLiteral(if (is_reg) "r" else constraint, null)}); try f.writeCValue(writer, if (asmInputNeedsLocal(f, constraint, input_val)) local: { - const input_local = .{ .local = locals_index }; + const input_local_idx = locals_index; locals_index += 1; - break :local input_local; + break :local .{ .local = input_local_idx }; } else input_val, .Other); try writer.writeByte(')'); } @@ -5651,15 +5651,12 @@ fn fieldLocation( .begin, }; }, - .anon_struct_type => |anon_struct_info| return if (!container_ty.hasRuntimeBitsIgnoreComptime(zcu)) + .tuple_type => return if (!container_ty.hasRuntimeBitsIgnoreComptime(zcu)) .begin else if (!field_ptr_ty.childType(zcu).hasRuntimeBitsIgnoreComptime(zcu)) .{ .byte_offset = container_ty.structFieldOffset(field_index, zcu) } else - .{ .field = if (anon_struct_info.fieldName(ip, field_index).unwrap()) |field_name| - .{ .identifier = field_name.toSlice(ip) } - else - .{ .field = field_index } }, + .{ .field = .{ .field = field_index } }, .union_type => { const loaded_union = ip.loadUnionType(container_ty.toIntern()); switch (loaded_union.flagsUnordered(ip).layout) { @@ -5892,10 +5889,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { }, } }, - .anon_struct_type => |anon_struct_info| if (anon_struct_info.fieldName(ip, extra.field_index).unwrap()) |field_name| - .{ .identifier = field_name.toSlice(ip) } - else - .{ .field = extra.field_index }, + .tuple_type => .{ .field = extra.field_index }, .union_type => field_name: { const loaded_union = ip.loadUnionType(struct_ty.toIntern()); switch (loaded_union.flagsUnordered(ip).layout) { @@ -7366,16 +7360,13 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { }, } }, - .anon_struct_type => |anon_struct_info| for (0..anon_struct_info.types.len) |field_index| { - if (anon_struct_info.values.get(ip)[field_index] != .none) continue; - const field_ty = Type.fromInterned(anon_struct_info.types.get(ip)[field_index]); + .tuple_type => |tuple_info| for (0..tuple_info.types.len) |field_index| { + if (tuple_info.values.get(ip)[field_index] != .none) continue; + const field_ty = Type.fromInterned(tuple_info.types.get(ip)[field_index]); if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; const a = try Assignment.start(f, writer, try f.ctypeFromType(field_ty, .complete)); - try f.writeCValueMember(writer, local, if (anon_struct_info.fieldName(ip, field_index).unwrap()) |field_name| - .{ .identifier = field_name.toSlice(ip) } - else - .{ .field = field_index }); + try f.writeCValueMember(writer, local, .{ .field = field_index }); try a.assign(f, writer); try f.writeCValue(writer, resolved_elements[field_index], .Other); try a.end(f, writer); |
