authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-02-22 22:06:08+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-02-26 00:17:09+00:00
log84da520c44e9d944fec4f8f54655b1942acb36c7
tree33382d7ffd921bd2a749aa444aa0bc24c5985d08
parente0a955afb3c8768fb52b56c342f171bc1d0d6066
signaturelock-open Commit is signed but in an unrecognized format.

Sema: remove legacy coercion

This was meant to be removed in #21817, but was somehow missed.

2 files changed, 23 insertions(+), 110 deletions(-)

src/Sema.zig+2-110
......@@ -30053,8 +30053,8 @@ fn coerceExtra(
3005330053 else => {},
3005430054 },
3005530055 .@"struct" => blk: {
30056 if (inst_ty.isTuple(zcu)) {
30057 return sema.coerceTupleToStruct(block, dest_ty, inst, inst_src) catch |err| switch (err) {
30056 if (dest_ty.isTuple(zcu) and inst_ty.isTuple(zcu)) {
30057 return sema.coerceTupleToTuple(block, dest_ty, inst, inst_src) catch |err| switch (err) {
3005830058 error.NotCoercible => break :blk,
3005930059 else => |e| return e,
3006030060 };
......@@ -32135,114 +32135,6 @@ fn coerceTupleToArrayPtrs(
3213532135 return ptr_array;
3213632136}
3213732137
32138/// Handles both tuples and anon struct literals. Coerces field-wise. Reports
32139/// errors for both extra fields and missing fields.
32140fn coerceTupleToStruct(
32141 sema: *Sema,
32142 block: *Block,
32143 struct_ty: Type,
32144 inst: Air.Inst.Ref,
32145 inst_src: LazySrcLoc,
32146) !Air.Inst.Ref {
32147 const pt = sema.pt;
32148 const zcu = pt.zcu;
32149 const ip = &zcu.intern_pool;
32150 try struct_ty.resolveFields(pt);
32151 try struct_ty.resolveStructFieldInits(pt);
32152
32153 if (struct_ty.isTuple(zcu)) {
32154 return sema.coerceTupleToTuple(block, struct_ty, inst, inst_src);
32155 }
32156
32157 const struct_type = zcu.typeToStruct(struct_ty).?;
32158 const field_vals = try sema.arena.alloc(InternPool.Index, struct_type.field_types.len);
32159 const field_refs = try sema.arena.alloc(Air.Inst.Ref, field_vals.len);
32160 @memset(field_refs, .none);
32161
32162 const inst_ty = sema.typeOf(inst);
32163 var runtime_src: ?LazySrcLoc = null;
32164 const field_count = switch (ip.indexToKey(inst_ty.toIntern())) {
32165 .tuple_type => |tuple| tuple.types.len,
32166 .struct_type => ip.loadStructType(inst_ty.toIntern()).field_types.len,
32167 else => unreachable,
32168 };
32169 for (0..field_count) |tuple_field_index| {
32170 const field_src = inst_src; // TODO better source location
32171 const field_name = inst_ty.structFieldName(tuple_field_index, zcu).unwrap() orelse
32172 try ip.getOrPutStringFmt(sema.gpa, pt.tid, "{d}", .{tuple_field_index}, .no_embedded_nulls);
32173
32174 const struct_field_index = try sema.structFieldIndex(block, struct_ty, field_name, field_src);
32175 const struct_field_ty = Type.fromInterned(struct_type.field_types.get(ip)[struct_field_index]);
32176 const elem_ref = try sema.tupleField(block, inst_src, inst, field_src, @intCast(tuple_field_index));
32177 const coerced = try sema.coerce(block, struct_field_ty, elem_ref, field_src);
32178 field_refs[struct_field_index] = coerced;
32179 if (struct_type.fieldIsComptime(ip, struct_field_index)) {
32180 const init_val = try sema.resolveValue(coerced) orelse {
32181 return sema.failWithNeededComptime(block, field_src, .{ .simple = .stored_to_comptime_field });
32182 };
32183
32184 const field_init = Value.fromInterned(struct_type.field_inits.get(ip)[struct_field_index]);
32185 if (!init_val.eql(field_init, struct_field_ty, pt.zcu)) {
32186 return sema.failWithInvalidComptimeFieldStore(block, field_src, inst_ty, tuple_field_index);
32187 }
32188 }
32189 if (runtime_src == null) {
32190 if (try sema.resolveValue(coerced)) |field_val| {
32191 field_vals[struct_field_index] = field_val.toIntern();
32192 } else {
32193 runtime_src = field_src;
32194 }
32195 }
32196 }
32197
32198 // Populate default field values and report errors for missing fields.
32199 var root_msg: ?*Zcu.ErrorMsg = null;
32200 errdefer if (root_msg) |msg| msg.destroy(sema.gpa);
32201
32202 for (field_refs, 0..) |*field_ref, i| {
32203 if (field_ref.* != .none) continue;
32204
32205 const field_name = struct_type.field_names.get(ip)[i];
32206 const field_default_val = struct_type.fieldInit(ip, i);
32207 const field_src = inst_src; // TODO better source location
32208 if (field_default_val == .none) {
32209 const template = "missing struct field: {}";
32210 const args = .{field_name.fmt(ip)};
32211 if (root_msg) |msg| {
32212 try sema.errNote(field_src, msg, template, args);
32213 } else {
32214 root_msg = try sema.errMsg(field_src, template, args);
32215 }
32216 continue;
32217 }
32218 if (runtime_src == null) {
32219 field_vals[i] = field_default_val;
32220 } else {
32221 field_ref.* = Air.internedToRef(field_default_val);
32222 }
32223 }
32224
32225 if (root_msg) |msg| {
32226 try sema.addDeclaredHereNote(msg, struct_ty);
32227 root_msg = null;
32228 return sema.failWithOwnedErrorMsg(block, msg);
32229 }
32230
32231 if (runtime_src) |rs| {
32232 try sema.requireRuntimeBlock(block, inst_src, rs);
32233 return block.addAggregateInit(struct_ty, field_refs);
32234 }
32235
32236 const struct_val = try pt.intern(.{ .aggregate = .{
32237 .ty = struct_ty.toIntern(),
32238 .storage = .{ .elems = field_vals },
32239 } });
32240 // TODO: figure out InternPool removals for incremental compilation
32241 //errdefer ip.remove(struct_val);
32242
32243 return Air.internedToRef(struct_val);
32244}
32245
3224632138fn coerceTupleToTuple(
3224732139 sema: *Sema,
3224832140 block: *Block,
test/cases/compile_errors/coerce_empty_tuple_to_struct.zig created+21
......@@ -0,0 +1,21 @@
1const empty = .{};
2
3const Foo = struct {};
4const foo: Foo = empty;
5
6const Bar = struct { a: u32 };
7const bar: Bar = empty;
8
9comptime {
10 _ = foo;
11}
12comptime {
13 _ = bar;
14}
15
16// error
17//
18// :4:18: error: expected type 'tmp.Foo', found '@TypeOf(.{})'
19// :3:13: note: struct declared here
20// :7:18: error: expected type 'tmp.Bar', found '@TypeOf(.{})'
21// :6:13: note: struct declared here