Skip to content

Commit d11bbde

Browse files
committed
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
1 parent a916bc7 commit d11bbde

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1066
-1313
lines changed

lib/compiler/aro/aro/Builtins.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ fn createType(desc: TypeDescription, it: *TypeDescription.TypeIterator, comp: *c
157157
.len = element_count,
158158
.elem = child_ty,
159159
};
160-
const vector_ty = .{ .specifier = .vector, .data = .{ .array = arr_ty } };
160+
const vector_ty: Type = .{ .specifier = .vector, .data = .{ .array = arr_ty } };
161161
builder.specifier = Type.Builder.fromType(vector_ty);
162162
},
163163
.q => {

lib/compiler/aro/aro/Parser.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8095,7 +8095,7 @@ fn primaryExpr(p: *Parser) Error!Result {
80958095

80968096
fn makePredefinedIdentifier(p: *Parser, strings_top: usize) !Result {
80978097
const end: u32 = @intCast(p.strings.items.len);
8098-
const elem_ty = .{ .specifier = .char, .qual = .{ .@"const" = true } };
8098+
const elem_ty: Type = .{ .specifier = .char, .qual = .{ .@"const" = true } };
80998099
const arr_ty = try p.arena.create(Type.Array);
81008100
arr_ty.* = .{ .elem = elem_ty, .len = end - strings_top };
81018101
const ty: Type = .{ .specifier = .array, .data = .{ .array = arr_ty } };

lib/compiler/aro/aro/text_literal.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ pub const Parser = struct {
188188
pub fn err(self: *Parser, tag: Diagnostics.Tag, extra: Diagnostics.Message.Extra) void {
189189
if (self.errored) return;
190190
self.errored = true;
191-
const diagnostic = .{ .tag = tag, .extra = extra };
191+
const diagnostic: CharDiagnostic = .{ .tag = tag, .extra = extra };
192192
if (self.errors_len == self.errors_buffer.len) {
193193
self.errors_buffer[self.errors_buffer.len - 1] = diagnostic;
194194
} else {

lib/compiler/aro_translate_c.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ fn transType(c: *Context, scope: *Scope, raw_ty: Type, qual_handling: Type.QualH
749749
const is_const = is_fn_proto or child_type.isConst();
750750
const is_volatile = child_type.qual.@"volatile";
751751
const elem_type = try transType(c, scope, child_type, qual_handling, source_loc);
752-
const ptr_info = .{
752+
const ptr_info: @FieldType(ast.Payload.Pointer, "data") = .{
753753
.is_const = is_const,
754754
.is_volatile = is_volatile,
755755
.elem_type = elem_type,

lib/compiler/test_runner.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const io = std.io;
66
const testing = std.testing;
77
const assert = std.debug.assert;
88

9-
pub const std_options = .{
9+
pub const std_options: std.Options = .{
1010
.logFn = log,
1111
};
1212

lib/std/SemanticVersion.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ test "precedence" {
299299

300300
test "zig_version" {
301301
// An approximate Zig build that predates this test.
302-
const older_version = .{ .major = 0, .minor = 8, .patch = 0, .pre = "dev.874" };
302+
const older_version: Version = .{ .major = 0, .minor = 8, .patch = 0, .pre = "dev.874" };
303303

304304
// Simulated compatibility check using Zig version.
305305
const compatible = comptime @import("builtin").zig_version.order(older_version) == .gt;

lib/std/Target.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ pub const Os = struct {
509509
.max = .{ .major = 6, .minor = 10, .patch = 3 },
510510
},
511511
.glibc = blk: {
512-
const default_min = .{ .major = 2, .minor = 28, .patch = 0 };
512+
const default_min: std.SemanticVersion = .{ .major = 2, .minor = 28, .patch = 0 };
513513

514514
for (std.zig.target.available_libcs) |libc| {
515515
// We don't know the ABI here. We can get away with not checking it

lib/std/array_list.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
100100
/// of this ArrayList. Empties this ArrayList.
101101
pub fn moveToUnmanaged(self: *Self) ArrayListAlignedUnmanaged(T, alignment) {
102102
const allocator = self.allocator;
103-
const result = .{ .items = self.items, .capacity = self.capacity };
103+
const result: ArrayListAlignedUnmanaged(T, alignment) = .{ .items = self.items, .capacity = self.capacity };
104104
self.* = init(allocator);
105105
return result;
106106
}

lib/std/crypto/phc_encoding.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@ fn kvSplit(str: []const u8) !struct { key: []const u8, value: []const u8 } {
258258
var it = mem.splitScalar(u8, str, kv_delimiter_scalar);
259259
const key = it.first();
260260
const value = it.next() orelse return Error.InvalidEncoding;
261-
const ret = .{ .key = key, .value = value };
262-
return ret;
261+
return .{ .key = key, .value = value };
263262
}
264263

265264
test "phc format - encoding/decoding" {

lib/std/meta.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ fn CreateUniqueTuple(comptime N: comptime_int, comptime types: [N]type) type {
10181018
.type = T,
10191019
.default_value = null,
10201020
.is_comptime = false,
1021-
.alignment = if (@sizeOf(T) > 0) @alignOf(T) else 0,
1021+
.alignment = 0,
10221022
};
10231023
}
10241024

0 commit comments

Comments
 (0)