authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-08-12 15:28:05-07:00
committergravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-08-25 15:18:20-07:00
logce92ccccc961992c00a10e714ce9e799956c50f2
tree39d25560175d0787a0891c0f8fa25f68bb1ba7cd
parent472f3ac419386327a3ed464256509af456092785
signaturelock-open Commit is signed but in an unrecognized format.

sema: `resolve{Struct,Union}Inner` don't throw away Semas

before this, calls to `resolveTypeFieldsStruct` (now renamed to the more correct `resolveStructFieldTypes`) would just throw away the sema that `resolveStructInner` created and create its own. There is no reason to do this, and we fix it to preserve the sema through it all.

4 files changed, 64 insertions(+), 87 deletions(-)

src/InternPool.zig+2-2
......@@ -457,7 +457,7 @@ pub const Cau = struct {
457457 unwrapped.index);
458458 }
459459 };
460 fn unwrap(cau_index: Cau.Index, ip: *const InternPool) Unwrapped {
460 pub fn unwrap(cau_index: Cau.Index, ip: *const InternPool) Unwrapped {
461461 return .{
462462 .tid = @enumFromInt(@intFromEnum(cau_index) >> ip.tid_shift_31 & ip.getTidMask()),
463463 .index = @intFromEnum(cau_index) & ip.getIndexMask(u31),
......@@ -1464,7 +1464,7 @@ pub const NamespaceIndex = enum(u32) {
14641464 unwrapped.index);
14651465 }
14661466 };
1467 fn unwrap(namespace_index: NamespaceIndex, ip: *const InternPool) Unwrapped {
1467 pub fn unwrap(namespace_index: NamespaceIndex, ip: *const InternPool) Unwrapped {
14681468 const index = @intFromEnum(namespace_index) & ip.getIndexMask(u32);
14691469 return .{
14701470 .tid = @enumFromInt(@intFromEnum(namespace_index) >> ip.tid_shift_32 & ip.getTidMask()),
src/Sema.zig+47-80
......@@ -35277,7 +35277,7 @@ pub fn resolveStructAlignment(
3527735277 // might require explicit alignment.
3527835278 if (struct_type.assumePointerAlignedIfFieldTypesWip(ip, ptr_align)) return;
3527935279
35280 try sema.resolveTypeFieldsStruct(ty, struct_type);
35280 try sema.resolveStructFieldTypes(ty, struct_type);
3528135281
3528235282 // We'll guess "pointer-aligned", if the struct has an
3528335283 // underaligned pointer field then some allocations
......@@ -35316,7 +35316,7 @@ pub fn resolveStructLayout(sema: *Sema, ty: Type) SemaError!void {
3531635316 try sema.resolveTypeFieldsStruct(ty.toIntern(), struct_type);
3531735317
3531835318 if (struct_type.layout == .@"packed") {
35319 semaBackingIntType(pt, struct_type) catch |err| switch (err) {
35319 sema.backingIntType(struct_type) catch |err| switch (err) {
3532035320 error.OutOfMemory, error.AnalysisFail => |e| return e,
3532135321 error.ComptimeBreak, error.ComptimeReturn, error.GenericPoison => unreachable,
3532235322 };
......@@ -35444,38 +35444,26 @@ pub fn resolveStructLayout(sema: *Sema, ty: Type) SemaError!void {
3544435444 _ = try ty.comptimeOnlySema(pt);
3544535445}
3544635446
35447fn semaBackingIntType(pt: Zcu.PerThread, struct_type: InternPool.LoadedStructType) CompileError!void {
35447fn backingIntType(
35448 sema: *Sema,
35449 struct_type: InternPool.LoadedStructType,
35450) CompileError!void {
35451 const pt = sema.pt;
3544835452 const zcu = pt.zcu;
3544935453 const gpa = zcu.gpa;
3545035454 const ip = &zcu.intern_pool;
3545135455
3545235456 const cau_index = struct_type.cau.unwrap().?;
3545335457
35454 const zir = zcu.namespacePtr(struct_type.namespace.unwrap().?).fileScope(zcu).zir;
35455
3545635458 var analysis_arena = std.heap.ArenaAllocator.init(gpa);
3545735459 defer analysis_arena.deinit();
3545835460
3545935461 var comptime_err_ret_trace = std.ArrayList(LazySrcLoc).init(gpa);
3546035462 defer comptime_err_ret_trace.deinit();
3546135463
35462 var sema: Sema = .{
35463 .pt = pt,
35464 .gpa = gpa,
35465 .arena = analysis_arena.allocator(),
35466 .code = zir,
35467 .owner = AnalUnit.wrap(.{ .cau = cau_index }),
35468 .func_index = .none,
35469 .func_is_naked = false,
35470 .fn_ret_ty = Type.void,
35471 .fn_ret_ty_ies = null,
35472 .comptime_err_ret_trace = &comptime_err_ret_trace,
35473 };
35474 defer sema.deinit();
35475
3547635464 var block: Block = .{
3547735465 .parent = null,
35478 .sema = &sema,
35466 .sema = sema,
3547935467 .namespace = ip.getCau(cau_index).namespace,
3548035468 .instructions = .{},
3548135469 .inlining = null,
......@@ -35494,6 +35482,7 @@ fn semaBackingIntType(pt: Zcu.PerThread, struct_type: InternPool.LoadedStructTyp
3549435482 break :blk accumulator;
3549535483 };
3549635484
35485 const zir = zcu.namespacePtr(struct_type.namespace.unwrap().?).fileScope(zcu).zir;
3549735486 const zir_index = struct_type.zir_index.unwrap().?.resolve(ip) orelse return error.AnalysisFail;
3549835487 const extended = zir.instructions.items(.data)[@intFromEnum(zir_index)].extended;
3549935488 assert(extended.opcode == .struct_decl);
......@@ -35618,7 +35607,7 @@ pub fn resolveUnionAlignment(
3561835607 // might require explicit alignment.
3561935608 if (union_type.assumePointerAlignedIfFieldTypesWip(ip, ptr_align)) return;
3562035609
35621 try sema.resolveTypeFieldsUnion(ty, union_type);
35610 try sema.resolveUnionFieldTypes(ty, union_type);
3562235611
3562335612 var max_align: Alignment = .@"1";
3562435613 for (0..union_type.field_types.len) |field_index| {
......@@ -35642,7 +35631,7 @@ pub fn resolveUnionLayout(sema: *Sema, ty: Type) SemaError!void {
3564235631 const pt = sema.pt;
3564335632 const ip = &pt.zcu.intern_pool;
3564435633
35645 try sema.resolveTypeFieldsUnion(ty, ip.loadUnionType(ty.ip_index));
35634 try sema.resolveUnionFieldTypes(ty, ip.loadUnionType(ty.ip_index));
3564635635
3564735636 // Load again, since the tag type might have changed due to resolution.
3564835637 const union_type = ip.loadUnionType(ty.ip_index);
......@@ -35810,7 +35799,7 @@ pub fn resolveUnionFully(sema: *Sema, ty: Type) SemaError!void {
3581035799 _ = try ty.comptimeOnlySema(pt);
3581135800}
3581235801
35813pub fn resolveTypeFieldsStruct(
35802pub fn resolveStructFieldTypes(
3581435803 sema: *Sema,
3581535804 ty: InternPool.Index,
3581635805 struct_type: InternPool.LoadedStructType,
......@@ -35833,7 +35822,7 @@ pub fn resolveTypeFieldsStruct(
3583335822 }
3583435823 defer struct_type.clearFieldTypesWip(ip);
3583535824
35836 semaStructFields(pt, sema.arena, struct_type) catch |err| switch (err) {
35825 sema.structFields(struct_type) catch |err| switch (err) {
3583735826 error.AnalysisFail, error.OutOfMemory => |e| return e,
3583835827 error.ComptimeBreak, error.ComptimeReturn, error.GenericPoison => unreachable,
3583935828 };
......@@ -35862,14 +35851,14 @@ pub fn resolveStructFieldInits(sema: *Sema, ty: Type) SemaError!void {
3586235851 }
3586335852 defer struct_type.clearInitsWip(ip);
3586435853
35865 semaStructFieldInits(pt, sema.arena, struct_type) catch |err| switch (err) {
35854 sema.structFieldInits(struct_type) catch |err| switch (err) {
3586635855 error.AnalysisFail, error.OutOfMemory => |e| return e,
3586735856 error.ComptimeBreak, error.ComptimeReturn, error.GenericPoison => unreachable,
3586835857 };
3586935858 struct_type.setHaveFieldInits(ip);
3587035859}
3587135860
35872pub fn resolveTypeFieldsUnion(sema: *Sema, ty: Type, union_type: InternPool.LoadedUnionType) SemaError!void {
35861pub fn resolveUnionFieldTypes(sema: *Sema, ty: Type, union_type: InternPool.LoadedUnionType) SemaError!void {
3587335862 const pt = sema.pt;
3587435863 const zcu = pt.zcu;
3587535864 const ip = &zcu.intern_pool;
......@@ -35896,7 +35885,7 @@ pub fn resolveTypeFieldsUnion(sema: *Sema, ty: Type, union_type: InternPool.Load
3589635885
3589735886 union_type.setStatus(ip, .field_types_wip);
3589835887 errdefer union_type.setStatus(ip, .none);
35899 semaUnionFields(pt, sema.arena, ty.toIntern(), union_type) catch |err| switch (err) {
35888 sema.unionFields(ty.toIntern(), union_type) catch |err| switch (err) {
3590035889 error.AnalysisFail, error.OutOfMemory => |e| return e,
3590135890 error.ComptimeBreak, error.ComptimeReturn, error.GenericPoison => unreachable,
3590235891 };
......@@ -36099,11 +36088,11 @@ fn structZirInfo(zir: Zir, zir_index: Zir.Inst.Index) struct {
3609936088 return .{ fields_len, small, extra_index };
3610036089}
3610136090
36102fn semaStructFields(
36103 pt: Zcu.PerThread,
36104 arena: Allocator,
36091fn structFields(
36092 sema: *Sema,
3610536093 struct_type: InternPool.LoadedStructType,
3610636094) CompileError!void {
36095 const pt = sema.pt;
3610736096 const zcu = pt.zcu;
3610836097 const gpa = zcu.gpa;
3610936098 const ip = &zcu.intern_pool;
......@@ -36116,7 +36105,7 @@ fn semaStructFields(
3611636105
3611736106 if (fields_len == 0) switch (struct_type.layout) {
3611836107 .@"packed" => {
36119 try semaBackingIntType(pt, struct_type);
36108 try sema.backingIntType(struct_type);
3612036109 return;
3612136110 },
3612236111 .auto, .@"extern" => {
......@@ -36128,23 +36117,9 @@ fn semaStructFields(
3612836117 var comptime_err_ret_trace = std.ArrayList(LazySrcLoc).init(gpa);
3612936118 defer comptime_err_ret_trace.deinit();
3613036119
36131 var sema: Sema = .{
36132 .pt = pt,
36133 .gpa = gpa,
36134 .arena = arena,
36135 .code = zir,
36136 .owner = AnalUnit.wrap(.{ .cau = cau_index }),
36137 .func_index = .none,
36138 .func_is_naked = false,
36139 .fn_ret_ty = Type.void,
36140 .fn_ret_ty_ies = null,
36141 .comptime_err_ret_trace = &comptime_err_ret_trace,
36142 };
36143 defer sema.deinit();
36144
3614536120 var block_scope: Block = .{
3614636121 .parent = null,
36147 .sema = &sema,
36122 .sema = sema,
3614836123 .namespace = namespace_index,
3614936124 .instructions = .{},
3615036125 .inlining = null,
......@@ -36318,12 +36293,12 @@ fn semaStructFields(
3631836293 try sema.flushExports();
3631936294}
3632036295
36321// This logic must be kept in sync with `semaStructFields`
36322fn semaStructFieldInits(
36323 pt: Zcu.PerThread,
36324 arena: Allocator,
36296// This logic must be kept in sync with `structFields`
36297fn structFieldInits(
36298 sema: *Sema,
3632536299 struct_type: InternPool.LoadedStructType,
3632636300) CompileError!void {
36301 const pt = sema.pt;
3632736302 const zcu = pt.zcu;
3632836303 const gpa = zcu.gpa;
3632936304 const ip = &zcu.intern_pool;
......@@ -36339,23 +36314,9 @@ fn semaStructFieldInits(
3633936314 var comptime_err_ret_trace = std.ArrayList(LazySrcLoc).init(gpa);
3634036315 defer comptime_err_ret_trace.deinit();
3634136316
36342 var sema: Sema = .{
36343 .pt = pt,
36344 .gpa = gpa,
36345 .arena = arena,
36346 .code = zir,
36347 .owner = AnalUnit.wrap(.{ .cau = cau_index }),
36348 .func_index = .none,
36349 .func_is_naked = false,
36350 .fn_ret_ty = Type.void,
36351 .fn_ret_ty_ies = null,
36352 .comptime_err_ret_trace = &comptime_err_ret_trace,
36353 };
36354 defer sema.deinit();
36355
3635636317 var block_scope: Block = .{
3635736318 .parent = null,
36358 .sema = &sema,
36319 .sema = sema,
3635936320 .namespace = namespace_index,
3636036321 .instructions = .{},
3636136322 .inlining = null,
......@@ -36458,14 +36419,18 @@ fn semaStructFieldInits(
3645836419 try sema.flushExports();
3645936420}
3646036421
36461fn semaUnionFields(pt: Zcu.PerThread, arena: Allocator, union_ty: InternPool.Index, union_type: InternPool.LoadedUnionType) CompileError!void {
36422fn unionFields(
36423 sema: *Sema,
36424 union_ty: InternPool.Index,
36425 union_type: InternPool.LoadedUnionType,
36426) CompileError!void {
3646236427 const tracy = trace(@src());
3646336428 defer tracy.end();
3646436429
36430 const pt = sema.pt;
3646536431 const zcu = pt.zcu;
3646636432 const gpa = zcu.gpa;
3646736433 const ip = &zcu.intern_pool;
36468 const cau_index = union_type.cau;
3646936434 const zir = zcu.namespacePtr(union_type.namespace).fileScope(zcu).zir;
3647036435 const zir_index = union_type.zir_index.resolve(ip) orelse return error.AnalysisFail;
3647136436 const extended = zir.instructions.items(.data)[@intFromEnum(zir_index)].extended;
......@@ -36513,10 +36478,12 @@ fn semaUnionFields(pt: Zcu.PerThread, arena: Allocator, union_ty: InternPool.Ind
3651336478 var comptime_err_ret_trace = std.ArrayList(LazySrcLoc).init(gpa);
3651436479 defer comptime_err_ret_trace.deinit();
3651536480
36516 var sema: Sema = .{
36481 const cau_index = union_type.cau;
36482
36483 var inner_sema: Sema = .{
3651736484 .pt = pt,
3651836485 .gpa = gpa,
36519 .arena = arena,
36486 .arena = sema.arena,
3652036487 .code = zir,
3652136488 .owner = AnalUnit.wrap(.{ .cau = cau_index }),
3652236489 .func_index = .none,
......@@ -36525,11 +36492,11 @@ fn semaUnionFields(pt: Zcu.PerThread, arena: Allocator, union_ty: InternPool.Ind
3652536492 .fn_ret_ty_ies = null,
3652636493 .comptime_err_ret_trace = &comptime_err_ret_trace,
3652736494 };
36528 defer sema.deinit();
36495 defer inner_sema.deinit();
3652936496
3653036497 var block_scope: Block = .{
3653136498 .parent = null,
36532 .sema = &sema,
36499 .sema = &inner_sema,
3653336500 .namespace = union_type.namespace,
3653436501 .instructions = .{},
3653536502 .inlining = null,
......@@ -36672,7 +36639,10 @@ fn semaUnionFields(pt: Zcu.PerThread, arena: Allocator, union_ty: InternPool.Ind
3667236639
3667336640 if (enum_field_vals.capacity() > 0) {
3667436641 const enum_tag_val = if (tag_ref != .none) blk: {
36675 const val = try sema.semaUnionFieldVal(&block_scope, value_src, int_tag_ty, tag_ref);
36642 const coerced = try sema.coerce(&block_scope, int_tag_ty, tag_ref, value_src);
36643 const val = try sema.resolveConstDefinedValue(&block_scope, value_src, coerced, .{
36644 .needed_comptime_reason = "enum tag value must be comptime-known",
36645 });
3667636646 last_tag_val = val;
3667736647
3667836648 break :blk val;
......@@ -36692,7 +36662,11 @@ fn semaUnionFields(pt: Zcu.PerThread, arena: Allocator, union_ty: InternPool.Ind
3669236662 .offset = .{ .container_field_value = @intCast(gop.index) },
3669336663 };
3669436664 const msg = msg: {
36695 const msg = try sema.errMsg(value_src, "enum tag value {} already taken", .{enum_tag_val.fmtValueSema(pt, &sema)});
36665 const msg = try sema.errMsg(
36666 value_src,
36667 "enum tag value {} already taken",
36668 .{enum_tag_val.fmtValueSema(pt, sema)},
36669 );
3669636670 errdefer msg.destroy(gpa);
3669736671 try sema.errNote(other_value_src, msg, "other occurrence here", .{});
3669836672 break :msg msg;
......@@ -36832,13 +36806,6 @@ fn semaUnionFields(pt: Zcu.PerThread, arena: Allocator, union_ty: InternPool.Ind
3683236806 try sema.flushExports();
3683336807}
3683436808
36835fn semaUnionFieldVal(sema: *Sema, block: *Block, src: LazySrcLoc, int_tag_ty: Type, tag_ref: Air.Inst.Ref) CompileError!Value {
36836 const coerced = try sema.coerce(block, int_tag_ty, tag_ref, src);
36837 return sema.resolveConstDefinedValue(block, src, coerced, .{
36838 .needed_comptime_reason = "enum tag value must be comptime-known",
36839 });
36840}
36841
3684236809fn generateUnionTagTypeNumbered(
3684336810 sema: *Sema,
3684436811 block: *Block,
src/Type.zig+10-4
......@@ -3911,11 +3911,12 @@ fn resolveStructInner(
39113911 var comptime_err_ret_trace = std.ArrayList(Zcu.LazySrcLoc).init(gpa);
39123912 defer comptime_err_ret_trace.deinit();
39133913
3914 const zir = zcu.namespacePtr(struct_obj.namespace.unwrap().?).fileScope(zcu).zir;
39143915 var sema: Sema = .{
39153916 .pt = pt,
39163917 .gpa = gpa,
39173918 .arena = analysis_arena.allocator(),
3918 .code = undefined, // This ZIR will not be used.
3919 .code = zir,
39193920 .owner = owner,
39203921 .func_index = .none,
39213922 .func_is_naked = false,
......@@ -3925,8 +3926,10 @@ fn resolveStructInner(
39253926 };
39263927 defer sema.deinit();
39273928
3929 assert(sema.owner.unwrap().cau == struct_obj.cau.unwrap().?);
3930
39283931 (switch (resolution) {
3929 .fields => sema.resolveTypeFieldsStruct(ty.toIntern(), struct_obj),
3932 .fields => sema.resolveStructFieldTypes(ty.toIntern(), struct_obj),
39303933 .inits => sema.resolveStructFieldInits(ty),
39313934 .alignment => sema.resolveStructAlignment(ty.toIntern(), struct_obj),
39323935 .layout => sema.resolveStructLayout(ty),
......@@ -3964,11 +3967,12 @@ fn resolveUnionInner(
39643967 var comptime_err_ret_trace = std.ArrayList(Zcu.LazySrcLoc).init(gpa);
39653968 defer comptime_err_ret_trace.deinit();
39663969
3970 const zir = zcu.namespacePtr(union_obj.namespace).fileScope(zcu).zir;
39673971 var sema: Sema = .{
39683972 .pt = pt,
39693973 .gpa = gpa,
39703974 .arena = analysis_arena.allocator(),
3971 .code = undefined, // This ZIR will not be used.
3975 .code = zir,
39723976 .owner = owner,
39733977 .func_index = .none,
39743978 .func_is_naked = false,
......@@ -3978,8 +3982,10 @@ fn resolveUnionInner(
39783982 };
39793983 defer sema.deinit();
39803984
3985 assert(sema.owner.unwrap().cau == union_obj.cau);
3986
39813987 (switch (resolution) {
3982 .fields => sema.resolveTypeFieldsUnion(ty, union_obj),
3988 .fields => sema.resolveUnionFieldTypes(ty, union_obj),
39833989 .alignment => sema.resolveUnionAlignment(ty, union_obj),
39843990 .layout => sema.resolveUnionLayout(ty),
39853991 .full => sema.resolveUnionFully(ty),
src/target.zig+5-1
......@@ -526,7 +526,11 @@ pub fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBacken
526526pub inline fn backendSupportsFeature(backend: std.builtin.CompilerBackend, comptime feature: Feature) bool {
527527 return switch (feature) {
528528 .panic_fn => switch (backend) {
529 .stage2_c, .stage2_llvm, .stage2_x86_64, .stage2_riscv64 => true,
529 .stage2_c,
530 .stage2_llvm,
531 .stage2_x86_64,
532 .stage2_riscv64,
533 => true,
530534 else => false,
531535 },
532536 .panic_unwrap_error => switch (backend) {