diff --git a/src/InternPool.zig b/src/InternPool.zig index 2e704945f9a2..08168900685d 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -2885,7 +2885,7 @@ pub const LoadedUnionType = struct { /// The `Cau` within which type resolution occurs. cau: Cau.Index, /// Represents the declarations inside this union. - namespace: OptionalNamespaceIndex, + namespace: NamespaceIndex, /// The enum tag type. enum_tag_ty: Index, /// List of field types in declaration order. @@ -3219,7 +3219,7 @@ pub const LoadedStructType = struct { name: NullTerminatedString, /// The `Cau` within which type resolution occurs. `none` when the struct is `@TypeOf(.{})`. cau: Cau.Index.Optional, - /// `none` when the struct has no declarations. + /// `none` when the struct is `@TypeOf(.{})`. namespace: OptionalNamespaceIndex, /// Index of the `struct_decl` or `reify` ZIR instruction. /// Only `none` when the struct is `@TypeOf(.{})`. @@ -3834,6 +3834,7 @@ pub fn loadStructType(ip: *const InternPool, index: Index) LoadedStructType { }; const name: NullTerminatedString = @enumFromInt(extra_items[item.data + std.meta.fieldIndex(Tag.TypeStruct, "name").?]); const cau: Cau.Index = @enumFromInt(extra_items[item.data + std.meta.fieldIndex(Tag.TypeStruct, "cau").?]); + const namespace: NamespaceIndex = @enumFromInt(extra_items[item.data + std.meta.fieldIndex(Tag.TypeStruct, "namespace").?]); const zir_index: TrackedInst.Index = @enumFromInt(extra_items[item.data + std.meta.fieldIndex(Tag.TypeStruct, "zir_index").?]); const fields_len = extra_items[item.data + std.meta.fieldIndex(Tag.TypeStruct, "fields_len").?]; const flags: Tag.TypeStruct.Flags = @bitCast(@atomicLoad(u32, &extra_items[item.data + std.meta.fieldIndex(Tag.TypeStruct, "flags").?], .unordered)); @@ -3878,11 +3879,6 @@ pub fn loadStructType(ip: *const InternPool, index: Index) LoadedStructType { extra_index += fields_len; break :i inits; } else Index.Slice.empty; - const namespace: OptionalNamespaceIndex = if (flags.has_namespace) n: { - const n: NamespaceIndex = @enumFromInt(extra_list.view().items(.@"0")[extra_index]); - extra_index += 1; - break :n n.toOptional(); - } else .none; const aligns: Alignment.Slice = if (flags.any_aligned_fields) a: { const a: Alignment.Slice = .{ .tid = unwrapped_index.tid, @@ -3925,7 +3921,7 @@ pub fn loadStructType(ip: *const InternPool, index: Index) LoadedStructType { .extra_index = item.data, .name = name, .cau = cau.toOptional(), - .namespace = namespace, + .namespace = namespace.toOptional(), .zir_index = zir_index.toOptional(), .layout = if (flags.is_extern) .@"extern" else .auto, .field_names = names, @@ -3944,7 +3940,7 @@ pub fn loadStructType(ip: *const InternPool, index: Index) LoadedStructType { const cau: Cau.Index = @enumFromInt(extra_items[item.data + std.meta.fieldIndex(Tag.TypeStructPacked, "cau").?]); const zir_index: TrackedInst.Index = @enumFromInt(extra_items[item.data + std.meta.fieldIndex(Tag.TypeStructPacked, "zir_index").?]); const fields_len = extra_items[item.data + std.meta.fieldIndex(Tag.TypeStructPacked, "fields_len").?]; - const namespace: OptionalNamespaceIndex = @enumFromInt(extra_items[item.data + std.meta.fieldIndex(Tag.TypeStructPacked, "namespace").?]); + const namespace: NamespaceIndex = @enumFromInt(extra_items[item.data + std.meta.fieldIndex(Tag.TypeStructPacked, "namespace").?]); const names_map: MapIndex = @enumFromInt(extra_items[item.data + std.meta.fieldIndex(Tag.TypeStructPacked, "names_map").?]); const flags: Tag.TypeStructPacked.Flags = @bitCast(@atomicLoad(u32, &extra_items[item.data + std.meta.fieldIndex(Tag.TypeStructPacked, "flags").?], .unordered)); var extra_index = item.data + @as(u32, @typeInfo(Tag.TypeStructPacked).Struct.fields.len); @@ -3989,7 +3985,7 @@ pub fn loadStructType(ip: *const InternPool, index: Index) LoadedStructType { .extra_index = item.data, .name = name, .cau = cau.toOptional(), - .namespace = namespace, + .namespace = namespace.toOptional(), .zir_index = zir_index.toOptional(), .layout = .@"packed", .field_names = field_names, @@ -4015,7 +4011,7 @@ const LoadedEnumType = struct { /// `null` if this is a generated tag type. cau: Cau.Index.Optional, /// Represents the declarations inside this enum. - namespace: OptionalNamespaceIndex, + namespace: NamespaceIndex, /// An integer type which is used for the numerical value of the enum. /// This field is present regardless of whether the enum has an /// explicitly provided tag type or auto-numbered. @@ -4172,7 +4168,7 @@ pub fn loadEnumType(ip: *const InternPool, index: Index) LoadedEnumType { /// Note that this type doubles as the payload for `Tag.type_opaque`. pub const LoadedOpaqueType = struct { /// Contains the declarations inside this opaque. - namespace: OptionalNamespaceIndex, + namespace: NamespaceIndex, // TODO: the non-fqn will be needed by the new dwarf structure /// The name of this opaque type. name: NullTerminatedString, @@ -5324,7 +5320,7 @@ pub const Tag = enum(u8) { /// Only valid after .have_layout padding: u32, cau: Cau.Index, - namespace: OptionalNamespaceIndex, + namespace: NamespaceIndex, /// The enum that provides the list of field names and values. tag_ty: Index, zir_index: TrackedInst.Index, @@ -5357,7 +5353,7 @@ pub const Tag = enum(u8) { cau: Cau.Index, zir_index: TrackedInst.Index, fields_len: u32, - namespace: OptionalNamespaceIndex, + namespace: NamespaceIndex, backing_int_ty: Index, names_map: MapIndex, flags: Flags, @@ -5396,19 +5392,18 @@ pub const Tag = enum(u8) { /// name: NullTerminatedString // for each field in declared order /// 5. if any_default_inits: /// init: Index // for each field in declared order - /// 6. if has_namespace: - /// namespace: NamespaceIndex - /// 7. if any_aligned_fields: + /// 6. if any_aligned_fields: /// align: Alignment // for each field in declared order - /// 8. if any_comptime_fields: + /// 7. if any_comptime_fields: /// field_is_comptime_bits: u32 // minimal number of u32s needed, LSB is field 0 - /// 9. if not is_extern: + /// 8. if not is_extern: /// field_index: RuntimeOrder // for each field in runtime order - /// 10. field_offset: u32 // for each field in declared order, undef until layout_resolved + /// 9. field_offset: u32 // for each field in declared order, undef until layout_resolved pub const TypeStruct = struct { name: NullTerminatedString, cau: Cau.Index, zir_index: TrackedInst.Index, + namespace: NamespaceIndex, fields_len: u32, flags: Flags, size: u32, @@ -5421,7 +5416,6 @@ pub const Tag = enum(u8) { is_tuple: bool = false, assumed_runtime_bits: bool = false, assumed_pointer_aligned: bool = false, - has_namespace: bool = false, any_comptime_fields: bool = false, any_default_inits: bool = false, any_aligned_fields: bool = false, @@ -5444,7 +5438,7 @@ pub const Tag = enum(u8) { // which `layout_resolved` does not ensure. fully_resolved: bool = false, is_reified: bool = false, - _: u6 = 0, + _: u7 = 0, }; }; @@ -5453,7 +5447,7 @@ pub const Tag = enum(u8) { pub const TypeOpaque = struct { name: NullTerminatedString, /// Contains the declarations inside this opaque. - namespace: OptionalNamespaceIndex, + namespace: NamespaceIndex, /// The index of the `opaque_decl` instruction. zir_index: TrackedInst.Index, /// `std.math.maxInt(u32)` indicates this type is reified. @@ -5762,8 +5756,7 @@ pub const EnumExplicit = struct { name: NullTerminatedString, /// `std.math.maxInt(u32)` indicates this type is reified. captures_len: u32, - /// This may be `none` if there are no declarations. - namespace: OptionalNamespaceIndex, + namespace: NamespaceIndex, /// An integer type which is used for the numerical value of the enum, which /// has been explicitly provided by the enum declaration. int_tag_type: Index, @@ -5789,8 +5782,7 @@ pub const EnumAuto = struct { name: NullTerminatedString, /// `std.math.maxInt(u32)` indicates this type is reified. captures_len: u32, - /// This may be `none` if there are no declarations. - namespace: OptionalNamespaceIndex, + namespace: NamespaceIndex, /// An integer type which is used for the numerical value of the enum, which /// was inferred by Zig based on the number of tags. int_tag_type: Index, @@ -7854,7 +7846,6 @@ pub const UnionTypeInit = struct { assumed_pointer_aligned: bool, alignment: Alignment, }, - has_namespace: bool, fields_len: u32, enum_tag_ty: Index, /// May have length 0 which leaves the values unset until later. @@ -7930,7 +7921,7 @@ pub fn getUnionType( .padding = std.math.maxInt(u32), .name = undefined, // set by `finish` .cau = undefined, // set by `finish` - .namespace = .none, // set by `finish` + .namespace = undefined, // set by `finish` .tag_ty = ini.enum_tag_ty, .zir_index = switch (ini.key) { inline else => |x| x.zir_index, @@ -7978,10 +7969,7 @@ pub fn getUnionType( .index = gop.put(), .type_name_extra_index = extra_index + std.meta.fieldIndex(Tag.TypeUnion, "name").?, .cau_extra_index = extra_index + std.meta.fieldIndex(Tag.TypeUnion, "cau").?, - .namespace_extra_index = if (ini.has_namespace) - extra_index + std.meta.fieldIndex(Tag.TypeUnion, "namespace").? - else - null, + .namespace_extra_index = extra_index + std.meta.fieldIndex(Tag.TypeUnion, "namespace").?, } }; } @@ -7990,7 +7978,7 @@ pub const WipNamespaceType = struct { index: Index, type_name_extra_index: u32, cau_extra_index: ?u32, - namespace_extra_index: ?u32, + namespace_extra_index: u32, pub fn setName( wip: WipNamespaceType, @@ -8006,7 +7994,7 @@ pub const WipNamespaceType = struct { wip: WipNamespaceType, ip: *InternPool, analysis_owner: Cau.Index.Optional, - namespace: OptionalNamespaceIndex, + namespace: NamespaceIndex, ) Index { const extra = ip.getLocalShared(wip.tid).extra.acquire(); const extra_items = extra.view().items(.@"0"); @@ -8017,11 +8005,7 @@ pub const WipNamespaceType = struct { assert(analysis_owner == .none); } - if (wip.namespace_extra_index) |i| { - extra_items[i] = @intFromEnum(namespace.unwrap().?); - } else { - assert(namespace == .none); - } + extra_items[wip.namespace_extra_index] = @intFromEnum(namespace); return wip.index; } @@ -8046,7 +8030,6 @@ pub const StructTypeInit = struct { any_default_inits: bool, inits_resolved: bool, any_aligned_fields: bool, - has_namespace: bool, key: union(enum) { declared: struct { zir_index: TrackedInst.Index, @@ -8109,7 +8092,7 @@ pub fn getStructType( .cau = undefined, // set by `finish` .zir_index = zir_index, .fields_len = ini.fields_len, - .namespace = .none, + .namespace = undefined, // set by `finish` .backing_int_ty = .none, .names_map = names_map, .flags = .{ @@ -8142,10 +8125,7 @@ pub fn getStructType( .index = gop.put(), .type_name_extra_index = extra_index + std.meta.fieldIndex(Tag.TypeStructPacked, "name").?, .cau_extra_index = extra_index + std.meta.fieldIndex(Tag.TypeStructPacked, "cau").?, - .namespace_extra_index = if (ini.has_namespace) - extra_index + std.meta.fieldIndex(Tag.TypeStructPacked, "namespace").? - else - null, + .namespace_extra_index = extra_index + std.meta.fieldIndex(Tag.TypeStructPacked, "namespace").?, } }; }, }; @@ -8164,11 +8144,12 @@ pub fn getStructType( // zig fmt: on (ini.fields_len * 5) + // types, names, inits, runtime order, offsets align_elements_len + comptime_elements_len + - 2); // names_map + namespace + 1); // names_map const extra_index = addExtraAssumeCapacity(extra, Tag.TypeStruct{ .name = undefined, // set by `finish` .cau = undefined, // set by `finish` .zir_index = zir_index, + .namespace = undefined, // set by `finish` .fields_len = ini.fields_len, .size = std.math.maxInt(u32), .flags = .{ @@ -8179,7 +8160,6 @@ pub fn getStructType( .is_tuple = ini.is_tuple, .assumed_runtime_bits = false, .assumed_pointer_aligned = false, - .has_namespace = ini.has_namespace, .any_comptime_fields = ini.any_comptime_fields, .any_default_inits = ini.any_default_inits, .any_aligned_fields = ini.any_aligned_fields, @@ -8215,10 +8195,6 @@ pub fn getStructType( if (ini.any_default_inits) { extra.appendNTimesAssumeCapacity(.{@intFromEnum(Index.none)}, ini.fields_len); } - const namespace_extra_index: ?u32 = if (ini.has_namespace) i: { - extra.appendAssumeCapacity(undefined); // set by `finish` - break :i @intCast(extra.mutate.len - 1); - } else null; if (ini.any_aligned_fields) { extra.appendNTimesAssumeCapacity(.{align_element}, align_elements_len); } @@ -8234,7 +8210,7 @@ pub fn getStructType( .index = gop.put(), .type_name_extra_index = extra_index + std.meta.fieldIndex(Tag.TypeStruct, "name").?, .cau_extra_index = extra_index + std.meta.fieldIndex(Tag.TypeStruct, "cau").?, - .namespace_extra_index = namespace_extra_index, + .namespace_extra_index = extra_index + std.meta.fieldIndex(Tag.TypeStruct, "namespace").?, } }; } @@ -9002,7 +8978,6 @@ fn finishFuncInstance( } pub const EnumTypeInit = struct { - has_namespace: bool, has_values: bool, tag_mode: LoadedEnumType.TagMode, fields_len: u32, @@ -9024,7 +8999,7 @@ pub const WipEnumType = struct { tag_ty_index: u32, type_name_extra_index: u32, cau_extra_index: u32, - namespace_extra_index: ?u32, + namespace_extra_index: u32, names_map: MapIndex, names_start: u32, values_map: OptionalMapIndex, @@ -9044,18 +9019,13 @@ pub const WipEnumType = struct { wip: WipEnumType, ip: *InternPool, analysis_owner: Cau.Index, - namespace: OptionalNamespaceIndex, + namespace: NamespaceIndex, ) void { const extra = ip.getLocalShared(wip.tid).extra.acquire(); const extra_items = extra.view().items(.@"0"); extra_items[wip.cau_extra_index] = @intFromEnum(analysis_owner); - - if (wip.namespace_extra_index) |i| { - extra_items[i] = @intFromEnum(namespace.unwrap().?); - } else { - assert(namespace == .none); - } + extra_items[wip.namespace_extra_index] = @intFromEnum(namespace); } pub fn setTagTy(wip: WipEnumType, ip: *InternPool, tag_ty: Index) void { @@ -9153,7 +9123,7 @@ pub fn getEnumType( .declared => |d| @intCast(d.captures.len), .reified => std.math.maxInt(u32), }, - .namespace = .none, + .namespace = undefined, // set by `prepare` .int_tag_type = .none, // set by `prepare` .fields_len = ini.fields_len, .names_map = names_map, @@ -9179,7 +9149,7 @@ pub fn getEnumType( .tag_ty_index = extra_index + std.meta.fieldIndex(EnumAuto, "int_tag_type").?, .type_name_extra_index = extra_index + std.meta.fieldIndex(EnumAuto, "name").?, .cau_extra_index = @intCast(cau_extra_index), - .namespace_extra_index = if (ini.has_namespace) extra_index + std.meta.fieldIndex(EnumAuto, "namespace").? else null, + .namespace_extra_index = extra_index + std.meta.fieldIndex(EnumAuto, "namespace").?, .names_map = names_map, .names_start = @intCast(names_start), .values_map = .none, @@ -9213,7 +9183,7 @@ pub fn getEnumType( .declared => |d| @intCast(d.captures.len), .reified => std.math.maxInt(u32), }, - .namespace = .none, + .namespace = undefined, // set by `prepare` .int_tag_type = .none, // set by `prepare` .fields_len = ini.fields_len, .names_map = names_map, @@ -9248,7 +9218,7 @@ pub fn getEnumType( .tag_ty_index = extra_index + std.meta.fieldIndex(EnumExplicit, "int_tag_type").?, .type_name_extra_index = extra_index + std.meta.fieldIndex(EnumExplicit, "name").?, .cau_extra_index = @intCast(cau_extra_index), - .namespace_extra_index = if (ini.has_namespace) extra_index + std.meta.fieldIndex(EnumExplicit, "namespace").? else null, + .namespace_extra_index = extra_index + std.meta.fieldIndex(EnumExplicit, "namespace").?, .names_map = names_map, .names_start = @intCast(names_start), .values_map = values_map, @@ -9265,6 +9235,7 @@ const GeneratedTagEnumTypeInit = struct { names: []const NullTerminatedString, values: []const Index, tag_mode: LoadedEnumType.TagMode, + parent_namespace: NamespaceIndex, }; /// Creates an enum type which was automatically-generated as the tag type of a @@ -9291,6 +9262,18 @@ pub fn getGeneratedTagEnumType( const fields_len: u32 = @intCast(ini.names.len); + // Predict the index the enum will live at so we can construct the namespace before releasing the shard's mutex. + const enum_index = Index.Unwrapped.wrap(.{ + .tid = tid, + .index = items.mutate.len, + }, ip); + const namespace = try ip.createNamespace(gpa, tid, .{ + .parent = ini.parent_namespace.toOptional(), + .owner_type = enum_index, + .file_scope = ip.namespacePtr(ini.parent_namespace).file_scope, + }); + errdefer ip.destroyNamespace(tid, namespace); + const prev_extra_len = extra.mutate.len; switch (ini.tag_mode) { .auto => { @@ -9302,7 +9285,7 @@ pub fn getGeneratedTagEnumType( .data = addExtraAssumeCapacity(extra, EnumAuto{ .name = ini.name, .captures_len = 0, - .namespace = .none, + .namespace = namespace, .int_tag_type = ini.tag_ty, .fields_len = fields_len, .names_map = names_map, @@ -9335,7 +9318,7 @@ pub fn getGeneratedTagEnumType( .data = addExtraAssumeCapacity(extra, EnumExplicit{ .name = ini.name, .captures_len = 0, - .namespace = .none, + .namespace = namespace, .int_tag_type = ini.tag_ty, .fields_len = fields_len, .names_map = names_map, @@ -9360,11 +9343,11 @@ pub fn getGeneratedTagEnumType( .generated_tag = .{ .union_type = ini.owner_union_ty }, } }); defer gop.deinit(); - return gop.put(); + assert(gop.put() == enum_index); + return enum_index; } pub const OpaqueTypeInit = struct { - has_namespace: bool, key: union(enum) { declared: struct { zir_index: TrackedInst.Index, @@ -9407,7 +9390,7 @@ pub fn getOpaqueType( }); const extra_index = addExtraAssumeCapacity(extra, Tag.TypeOpaque{ .name = undefined, // set by `finish` - .namespace = .none, + .namespace = undefined, // set by `finish` .zir_index = switch (ini.key) { inline else => |x| x.zir_index, }, @@ -9430,10 +9413,7 @@ pub fn getOpaqueType( .index = gop.put(), .type_name_extra_index = extra_index + std.meta.fieldIndex(Tag.TypeOpaque, "name").?, .cau_extra_index = null, // opaques do not undergo type resolution - .namespace_extra_index = if (ini.has_namespace) - extra_index + std.meta.fieldIndex(Tag.TypeOpaque, "namespace").? - else - null, + .namespace_extra_index = extra_index + std.meta.fieldIndex(Tag.TypeOpaque, "namespace").?, }, }; } @@ -10332,7 +10312,6 @@ fn dumpStatsFallible(ip: *const InternPool, arena: Allocator) anyerror!void { } if (info.flags.any_default_inits) ints += info.fields_len; // inits - ints += @intFromBool(info.flags.has_namespace); // namespace if (info.flags.any_aligned_fields) ints += (info.fields_len + 3) / 4; // aligns if (info.flags.any_comptime_fields) diff --git a/src/Sema.zig b/src/Sema.zig index 43afe5683b40..149838bc1542 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -2802,7 +2802,6 @@ fn zirStructDecl( .any_default_inits = small.any_default_inits, .inits_resolved = false, .any_aligned_fields = small.any_aligned_fields, - .has_namespace = true or decls_len > 0, // TODO: see below .key = .{ .declared = .{ .zir_index = tracked_inst, .captures = captures, @@ -2825,15 +2824,14 @@ fn zirStructDecl( wip_ty.index, )); - // TODO: if AstGen tells us `@This` was not used in the fields, we can elide the namespace. - const new_namespace_index: InternPool.OptionalNamespaceIndex = if (true or decls_len > 0) (try pt.createNamespace(.{ + const new_namespace_index: InternPool.NamespaceIndex = try pt.createNamespace(.{ .parent = block.namespace.toOptional(), .owner_type = wip_ty.index, .file_scope = block.getFileScopeIndex(mod), - })).toOptional() else .none; - errdefer if (new_namespace_index.unwrap()) |ns| pt.destroyNamespace(ns); + }); + errdefer pt.destroyNamespace(new_namespace_index); - const new_cau_index = try ip.createTypeCau(gpa, pt.tid, tracked_inst, new_namespace_index.unwrap() orelse block.namespace, wip_ty.index); + const new_cau_index = try ip.createTypeCau(gpa, pt.tid, tracked_inst, new_namespace_index, wip_ty.index); if (pt.zcu.comp.incremental) { try ip.addDependency( @@ -2843,10 +2841,8 @@ fn zirStructDecl( ); } - if (new_namespace_index.unwrap()) |ns| { - const decls = sema.code.bodySlice(extra_index, decls_len); - try pt.scanNamespace(ns, decls); - } + const decls = sema.code.bodySlice(extra_index, decls_len); + try pt.scanNamespace(new_namespace_index, decls); try mod.comp.queueJob(.{ .resolve_type_fully = wip_ty.index }); try sema.addReferenceEntry(src, AnalUnit.wrap(.{ .cau = new_cau_index })); @@ -3015,7 +3011,6 @@ fn zirEnumDecl( } else false; const enum_init: InternPool.EnumTypeInit = .{ - .has_namespace = true or decls_len > 0, // TODO: see below .has_values = any_values, .tag_mode = if (small.nonexhaustive) .nonexhaustive @@ -3052,15 +3047,14 @@ fn zirEnumDecl( ); wip_ty.setName(ip, type_name); - // TODO: if AstGen tells us `@This` was not used in the fields, we can elide the namespace. - const new_namespace_index: InternPool.OptionalNamespaceIndex = if (true or decls_len > 0) (try pt.createNamespace(.{ + const new_namespace_index: InternPool.NamespaceIndex = try pt.createNamespace(.{ .parent = block.namespace.toOptional(), .owner_type = wip_ty.index, .file_scope = block.getFileScopeIndex(mod), - })).toOptional() else .none; - errdefer if (!done) if (new_namespace_index.unwrap()) |ns| pt.destroyNamespace(ns); + }); + errdefer if (!done) pt.destroyNamespace(new_namespace_index); - const new_cau_index = try ip.createTypeCau(gpa, pt.tid, tracked_inst, new_namespace_index.unwrap() orelse block.namespace, wip_ty.index); + const new_cau_index = try ip.createTypeCau(gpa, pt.tid, tracked_inst, new_namespace_index, wip_ty.index); if (pt.zcu.comp.incremental) { try mod.intern_pool.addDependency( @@ -3070,9 +3064,7 @@ fn zirEnumDecl( ); } - if (new_namespace_index.unwrap()) |ns| { - try pt.scanNamespace(ns, decls); - } + try pt.scanNamespace(new_namespace_index, decls); try sema.addReferenceEntry(src, AnalUnit.wrap(.{ .cau = new_cau_index })); try sema.declareDependency(.{ .interned = wip_ty.index }); @@ -3098,7 +3090,7 @@ fn zirEnumDecl( var enum_block: Block = .{ .parent = null, .sema = sema, - .namespace = new_namespace_index.unwrap() orelse block.namespace, + .namespace = new_namespace_index, .instructions = .{}, .inlining = null, .is_comptime = true, @@ -3284,7 +3276,6 @@ fn zirUnionDecl( .assumed_pointer_aligned = false, .alignment = .none, }, - .has_namespace = true or decls_len != 0, // TODO: see below .fields_len = fields_len, .enum_tag_ty = .none, // set later .field_types = &.{}, // set later @@ -3311,15 +3302,14 @@ fn zirUnionDecl( wip_ty.index, )); - // TODO: if AstGen tells us `@This` was not used in the fields, we can elide the namespace. - const new_namespace_index: InternPool.OptionalNamespaceIndex = if (true or decls_len > 0) (try pt.createNamespace(.{ + const new_namespace_index: InternPool.NamespaceIndex = try pt.createNamespace(.{ .parent = block.namespace.toOptional(), .owner_type = wip_ty.index, .file_scope = block.getFileScopeIndex(mod), - })).toOptional() else .none; - errdefer if (new_namespace_index.unwrap()) |ns| pt.destroyNamespace(ns); + }); + errdefer pt.destroyNamespace(new_namespace_index); - const new_cau_index = try ip.createTypeCau(gpa, pt.tid, tracked_inst, new_namespace_index.unwrap() orelse block.namespace, wip_ty.index); + const new_cau_index = try ip.createTypeCau(gpa, pt.tid, tracked_inst, new_namespace_index, wip_ty.index); if (pt.zcu.comp.incremental) { try mod.intern_pool.addDependency( @@ -3329,10 +3319,8 @@ fn zirUnionDecl( ); } - if (new_namespace_index.unwrap()) |ns| { - const decls = sema.code.bodySlice(extra_index, decls_len); - try pt.scanNamespace(ns, decls); - } + const decls = sema.code.bodySlice(extra_index, decls_len); + try pt.scanNamespace(new_namespace_index, decls); try mod.comp.queueJob(.{ .resolve_type_fully = wip_ty.index }); try sema.addReferenceEntry(src, AnalUnit.wrap(.{ .cau = new_cau_index })); @@ -3377,7 +3365,6 @@ fn zirOpaqueDecl( extra_index += captures_len; const opaque_init: InternPool.OpaqueTypeInit = .{ - .has_namespace = decls_len != 0, .key = .{ .declared = .{ .zir_index = tracked_inst, .captures = captures, @@ -3399,17 +3386,15 @@ fn zirOpaqueDecl( wip_ty.index, )); - const new_namespace_index: InternPool.OptionalNamespaceIndex = if (decls_len > 0) (try pt.createNamespace(.{ + const new_namespace_index: InternPool.NamespaceIndex = try pt.createNamespace(.{ .parent = block.namespace.toOptional(), .owner_type = wip_ty.index, .file_scope = block.getFileScopeIndex(mod), - })).toOptional() else .none; - errdefer if (new_namespace_index.unwrap()) |ns| pt.destroyNamespace(ns); + }); + errdefer pt.destroyNamespace(new_namespace_index); - if (new_namespace_index.unwrap()) |ns| { - const decls = sema.code.bodySlice(extra_index, decls_len); - try pt.scanNamespace(ns, decls); - } + const decls = sema.code.bodySlice(extra_index, decls_len); + try pt.scanNamespace(new_namespace_index, decls); return Air.internedToRef(wip_ty.finish(ip, .none, new_namespace_index)); } @@ -6749,7 +6734,7 @@ fn lookupIdentifier(sema: *Sema, block: *Block, src: LazySrcLoc, name: InternPoo const mod = pt.zcu; var namespace = block.namespace; while (true) { - if (try sema.lookupInNamespace(block, src, namespace.toOptional(), name, false)) |lookup| { + if (try sema.lookupInNamespace(block, src, namespace, name, false)) |lookup| { assert(lookup.accessible); return lookup.nav; } @@ -6764,7 +6749,7 @@ fn lookupInNamespace( sema: *Sema, block: *Block, src: LazySrcLoc, - opt_namespace_index: InternPool.OptionalNamespaceIndex, + namespace_index: InternPool.NamespaceIndex, ident_name: InternPool.NullTerminatedString, observe_usingnamespace: bool, ) CompileError!?struct { @@ -6777,7 +6762,6 @@ fn lookupInNamespace( const zcu = pt.zcu; const ip = &zcu.intern_pool; - const namespace_index = opt_namespace_index.unwrap() orelse return null; const namespace = zcu.namespacePtr(namespace_index); const adapter: Zcu.Namespace.NameAdapter = .{ .zcu = zcu }; @@ -6816,7 +6800,7 @@ fn lookupInNamespace( for (usingnamespaces.items) |sub_ns_nav| { try sema.ensureNavResolved(src, sub_ns_nav); const sub_ns_ty = Type.fromInterned(ip.getNav(sub_ns_nav).status.resolved.val); - const sub_ns = zcu.namespacePtrUnwrap(sub_ns_ty.getNamespaceIndex(zcu)) orelse continue; + const sub_ns = zcu.namespacePtr(sub_ns_ty.getNamespaceIndex(zcu)); try checked_namespaces.put(gpa, sub_ns, {}); } } @@ -13952,7 +13936,7 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air } }); } - const namespace = container_type.getNamespaceIndex(mod); + const namespace = container_type.getNamespace(mod).unwrap() orelse return .bool_false; if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |lookup| { if (lookup.accessible) { return .bool_true; @@ -18407,7 +18391,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai } }); }; - const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ip.loadEnumType(ty.toIntern()).namespace); + const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ip.loadEnumType(ty.toIntern()).namespace.toOptional()); const type_enum_ty = t: { const nav = try sema.namespaceLookup( @@ -18548,7 +18532,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai } }); }; - const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ty.getNamespaceIndex(mod)); + const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ty.getNamespaceIndex(mod).toOptional()); const enum_tag_ty_val = try pt.intern(.{ .opt = .{ .ty = (try pt.optionalType(.type_type)).toIntern(), @@ -18775,7 +18759,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai } }); }; - const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ty.getNamespaceIndex(mod)); + const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ty.getNamespace(mod)); const backing_integer_val = try pt.intern(.{ .opt = .{ .ty = (try pt.optionalType(.type_type)).toIntern(), @@ -18832,7 +18816,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai }; try ty.resolveFields(pt); - const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ty.getNamespaceIndex(mod)); + const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ty.getNamespace(mod)); const field_values = .{ // decls: []const Declaration, @@ -18980,7 +18964,7 @@ fn typeInfoNamespaceDecls( } try sema.ensureNavResolved(src, nav); const namespace_ty = Type.fromInterned(ip.getNav(nav).status.resolved.val); - try sema.typeInfoNamespaceDecls(block, src, namespace_ty.getNamespaceIndex(zcu), declaration_ty, decl_vals, seen_namespaces); + try sema.typeInfoNamespaceDecls(block, src, namespace_ty.getNamespaceIndex(zcu).toOptional(), declaration_ty, decl_vals, seen_namespaces); } } @@ -21769,7 +21753,6 @@ fn zirReify( } const wip_ty = switch (try ip.getOpaqueType(gpa, pt.tid, .{ - .has_namespace = false, .key = .{ .reified = .{ .zir_index = try block.trackZir(inst), } }, @@ -21787,7 +21770,13 @@ fn zirReify( wip_ty.index, )); - return Air.internedToRef(wip_ty.finish(ip, .none, .none)); + const new_namespace_index = try pt.createNamespace(.{ + .parent = block.namespace.toOptional(), + .owner_type = wip_ty.index, + .file_scope = block.getFileScopeIndex(mod), + }); + + return Air.internedToRef(wip_ty.finish(ip, .none, new_namespace_index)); }, .Union => { const struct_type = ip.loadStructType(ip.typeOf(union_val.val)); @@ -21959,7 +21948,6 @@ fn reifyEnum( const tracked_inst = try block.trackZir(inst); const wip_ty = switch (try ip.getEnumType(gpa, pt.tid, .{ - .has_namespace = false, .has_values = true, .tag_mode = if (is_exhaustive) .explicit else .nonexhaustive, .fields_len = fields_len, @@ -21985,9 +21973,15 @@ fn reifyEnum( wip_ty.index, )); - const new_cau_index = try ip.createTypeCau(gpa, pt.tid, tracked_inst, block.namespace, wip_ty.index); + const new_namespace_index = try pt.createNamespace(.{ + .parent = block.namespace.toOptional(), + .owner_type = wip_ty.index, + .file_scope = block.getFileScopeIndex(mod), + }); + + const new_cau_index = try ip.createTypeCau(gpa, pt.tid, tracked_inst, new_namespace_index, wip_ty.index); - wip_ty.prepare(ip, new_cau_index, .none); + wip_ty.prepare(ip, new_cau_index, new_namespace_index); wip_ty.setTagTy(ip, tag_ty.toIntern()); for (0..fields_len) |field_idx| { @@ -22110,7 +22104,6 @@ fn reifyUnion( .assumed_pointer_aligned = false, .alignment = .none, }, - .has_namespace = false, .fields_len = fields_len, .enum_tag_ty = .none, // set later because not yet validated .field_types = &.{}, // set later @@ -22225,7 +22218,7 @@ fn reifyUnion( } } - const enum_tag_ty = try sema.generateUnionTagTypeSimple(field_names.keys(), wip_ty.index, type_name); + const enum_tag_ty = try sema.generateUnionTagTypeSimple(block, field_names.keys(), wip_ty.index, type_name); break :tag_ty .{ enum_tag_ty, false }; }; errdefer if (!has_explicit_tag) ip.remove(pt.tid, enum_tag_ty); // remove generated tag type on error @@ -22272,11 +22265,17 @@ fn reifyUnion( loaded_union.setTagType(ip, enum_tag_ty); loaded_union.setStatus(ip, .have_field_types); - const new_cau_index = try ip.createTypeCau(gpa, pt.tid, tracked_inst, block.namespace, wip_ty.index); + const new_namespace_index = try pt.createNamespace(.{ + .parent = block.namespace.toOptional(), + .owner_type = wip_ty.index, + .file_scope = block.getFileScopeIndex(mod), + }); + + const new_cau_index = try ip.createTypeCau(gpa, pt.tid, tracked_inst, new_namespace_index, wip_ty.index); try mod.comp.queueJob(.{ .resolve_type_fully = wip_ty.index }); try sema.addReferenceEntry(src, AnalUnit.wrap(.{ .cau = new_cau_index })); - return Air.internedToRef(wip_ty.finish(ip, new_cau_index.toOptional(), .none)); + return Air.internedToRef(wip_ty.finish(ip, new_cau_index.toOptional(), new_namespace_index)); } fn reifyStruct( @@ -22369,7 +22368,6 @@ fn reifyStruct( .any_default_inits = any_default_inits, .any_aligned_fields = any_aligned_fields, .inits_resolved = true, - .has_namespace = false, .key = .{ .reified = .{ .zir_index = tracked_inst, .type_hash = hasher.final(), @@ -22540,11 +22538,17 @@ fn reifyStruct( } } - const new_cau_index = try ip.createTypeCau(gpa, pt.tid, tracked_inst, block.namespace, wip_ty.index); + const new_namespace_index = try pt.createNamespace(.{ + .parent = block.namespace.toOptional(), + .owner_type = wip_ty.index, + .file_scope = block.getFileScopeIndex(mod), + }); + + const new_cau_index = try ip.createTypeCau(gpa, pt.tid, tracked_inst, new_namespace_index, wip_ty.index); try mod.comp.queueJob(.{ .resolve_type_fully = wip_ty.index }); try sema.addReferenceEntry(src, AnalUnit.wrap(.{ .cau = new_cau_index })); - return Air.internedToRef(wip_ty.finish(ip, new_cau_index.toOptional(), .none)); + return Air.internedToRef(wip_ty.finish(ip, new_cau_index.toOptional(), new_namespace_index)); } fn resolveVaListRef(sema: *Sema, block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref) CompileError!Air.Inst.Ref { @@ -27993,7 +27997,7 @@ fn fieldCallBind( // If we get here, we need to look for a decl in the struct type instead. const found_nav = found_nav: { - const namespace = concrete_ty.getNamespace(zcu) orelse + const namespace = concrete_ty.getNamespace(zcu).unwrap() orelse break :found_nav null; const nav_index = try sema.namespaceLookup(block, src, namespace, field_name) orelse break :found_nav null; @@ -28125,13 +28129,13 @@ fn namespaceLookup( sema: *Sema, block: *Block, src: LazySrcLoc, - opt_namespace: InternPool.OptionalNamespaceIndex, + namespace: InternPool.NamespaceIndex, decl_name: InternPool.NullTerminatedString, ) CompileError!?InternPool.Nav.Index { const pt = sema.pt; const zcu = pt.zcu; const gpa = sema.gpa; - if (try sema.lookupInNamespace(block, src, opt_namespace, decl_name, true)) |lookup| { + if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |lookup| { if (!lookup.accessible) { return sema.failWithOwnedErrorMsg(block, msg: { const msg = try sema.errMsg(src, "'{}' is not marked 'pub'", .{ @@ -28151,10 +28155,10 @@ fn namespaceLookupRef( sema: *Sema, block: *Block, src: LazySrcLoc, - opt_namespace: InternPool.OptionalNamespaceIndex, + namespace: InternPool.NamespaceIndex, decl_name: InternPool.NullTerminatedString, ) CompileError!?Air.Inst.Ref { - const nav = try sema.namespaceLookup(block, src, opt_namespace, decl_name) orelse return null; + const nav = try sema.namespaceLookup(block, src, namespace, decl_name) orelse return null; return try sema.analyzeNavRef(src, nav); } @@ -28162,10 +28166,10 @@ fn namespaceLookupVal( sema: *Sema, block: *Block, src: LazySrcLoc, - opt_namespace: InternPool.OptionalNamespaceIndex, + namespace: InternPool.NamespaceIndex, decl_name: InternPool.NullTerminatedString, ) CompileError!?Air.Inst.Ref { - const nav = try sema.namespaceLookup(block, src, opt_namespace, decl_name) orelse return null; + const nav = try sema.namespaceLookup(block, src, namespace, decl_name) orelse return null; return try sema.analyzeNavVal(block, src, nav); } @@ -36393,7 +36397,7 @@ fn semaUnionFields(pt: Zcu.PerThread, arena: Allocator, union_ty: InternPool.Ind const gpa = zcu.gpa; const ip = &zcu.intern_pool; const cau_index = union_type.cau; - const zir = zcu.namespacePtr(union_type.namespace.unwrap().?).fileScope(zcu).zir; + const zir = zcu.namespacePtr(union_type.namespace).fileScope(zcu).zir; const zir_index = union_type.zir_index.resolve(ip); const extended = zir.instructions.items(.data)[@intFromEnum(zir_index)].extended; assert(extended.opcode == .union_decl); @@ -36457,7 +36461,7 @@ fn semaUnionFields(pt: Zcu.PerThread, arena: Allocator, union_ty: InternPool.Ind var block_scope: Block = .{ .parent = null, .sema = &sema, - .namespace = union_type.namespace.unwrap().?, + .namespace = union_type.namespace, .instructions = .{}, .inlining = null, .is_comptime = true, @@ -36749,10 +36753,10 @@ fn semaUnionFields(pt: Zcu.PerThread, arena: Allocator, union_ty: InternPool.Ind return sema.failWithOwnedErrorMsg(&block_scope, msg); } } else if (enum_field_vals.count() > 0) { - const enum_ty = try sema.generateUnionTagTypeNumbered(enum_field_names, enum_field_vals.keys(), union_ty, union_type.name); + const enum_ty = try sema.generateUnionTagTypeNumbered(&block_scope, enum_field_names, enum_field_vals.keys(), union_ty, union_type.name); union_type.setTagType(ip, enum_ty); } else { - const enum_ty = try sema.generateUnionTagTypeSimple(enum_field_names, union_ty, union_type.name); + const enum_ty = try sema.generateUnionTagTypeSimple(&block_scope, enum_field_names, union_ty, union_type.name); union_type.setTagType(ip, enum_ty); } @@ -36768,6 +36772,7 @@ fn semaUnionFieldVal(sema: *Sema, block: *Block, src: LazySrcLoc, int_tag_ty: Ty fn generateUnionTagTypeNumbered( sema: *Sema, + block: *Block, enum_field_names: []const InternPool.NullTerminatedString, enum_field_vals: []const InternPool.Index, union_type: InternPool.Index, @@ -36796,6 +36801,7 @@ fn generateUnionTagTypeNumbered( .names = enum_field_names, .values = enum_field_vals, .tag_mode = .explicit, + .parent_namespace = block.namespace, }); return enum_ty; @@ -36803,6 +36809,7 @@ fn generateUnionTagTypeNumbered( fn generateUnionTagTypeSimple( sema: *Sema, + block: *Block, enum_field_names: []const InternPool.NullTerminatedString, union_type: InternPool.Index, union_name: InternPool.NullTerminatedString, @@ -36830,6 +36837,7 @@ fn generateUnionTagTypeSimple( .names = enum_field_names, .values = &.{}, .tag_mode = .auto, + .parent_namespace = block.namespace, }); return enum_ty; diff --git a/src/Type.zig b/src/Type.zig index 4e007c9bbbf9..00f70239a99e 100644 --- a/src/Type.zig +++ b/src/Type.zig @@ -2964,59 +2964,25 @@ pub fn indexableHasLen(ty: Type, mod: *Module) bool { } /// Asserts that the type can have a namespace. -pub fn getNamespaceIndex(ty: Type, zcu: *Zcu) InternPool.OptionalNamespaceIndex { - return ty.getNamespace(zcu).?; +pub fn getNamespaceIndex(ty: Type, zcu: *Zcu) InternPool.NamespaceIndex { + return ty.getNamespace(zcu).unwrap().?; } /// Returns null if the type has no namespace. -pub fn getNamespace(ty: Type, zcu: *Zcu) ?InternPool.OptionalNamespaceIndex { +pub fn getNamespace(ty: Type, zcu: *Zcu) InternPool.OptionalNamespaceIndex { const ip = &zcu.intern_pool; return switch (ip.indexToKey(ty.toIntern())) { - .opaque_type => ip.loadOpaqueType(ty.toIntern()).namespace, + .opaque_type => ip.loadOpaqueType(ty.toIntern()).namespace.toOptional(), .struct_type => ip.loadStructType(ty.toIntern()).namespace, - .union_type => ip.loadUnionType(ty.toIntern()).namespace, - .enum_type => ip.loadEnumType(ty.toIntern()).namespace, - - .anon_struct_type => .none, - .simple_type => |s| switch (s) { - .anyopaque, - .atomic_order, - .atomic_rmw_op, - .calling_convention, - .address_space, - .float_mode, - .reduce_op, - .call_modifier, - .prefetch_options, - .export_options, - .extern_options, - .type_info, - => .none, - else => null, - }, - - else => null, + .union_type => ip.loadUnionType(ty.toIntern()).namespace.toOptional(), + .enum_type => ip.loadEnumType(ty.toIntern()).namespace.toOptional(), + else => .none, }; } // TODO: new dwarf structure will also need the enclosing code block for types created in imperative scopes -pub fn getParentNamespace(ty: Type, zcu: *Zcu) ?InternPool.OptionalNamespaceIndex { - const ip = &zcu.intern_pool; - const cau = switch (ip.indexToKey(ty.toIntern())) { - .struct_type => ip.loadStructType(ty.toIntern()).cau, - .union_type => ip.loadUnionType(ty.toIntern()).cau.toOptional(), - .enum_type => |e| switch (e) { - .declared, .reified => ip.loadEnumType(ty.toIntern()).cau, - .generated_tag => |gt| ip.loadUnionType(gt.union_type).cau.toOptional(), - .empty_struct => unreachable, - }, - // TODO: this doesn't handle opaque types with empty namespaces - .opaque_type => return ip.namespacePtr(ip.loadOpaqueType(ty.toIntern()).namespace.unwrap().?).parent, - else => return null, - }; - return ip.namespacePtr(ip.getCau(cau.unwrap() orelse return .none).namespace) - // TODO: I thought the cau contained the parent namespace based on "analyzed within" but alas - .parent; +pub fn getParentNamespace(ty: Type, zcu: *Zcu) InternPool.OptionalNamespaceIndex { + return zcu.namespacePtr(ty.getNamespace(zcu).unwrap() orelse return .none).parent; } // Works for vectors and vectors of integers. diff --git a/src/Zcu/PerThread.zig b/src/Zcu/PerThread.zig index 87d9e23dfd65..d96b950850bb 100644 --- a/src/Zcu/PerThread.zig +++ b/src/Zcu/PerThread.zig @@ -886,7 +886,6 @@ fn createFileRootStruct( .any_default_inits = small.any_default_inits, .inits_resolved = false, .any_aligned_fields = small.any_aligned_fields, - .has_namespace = true, .key = .{ .declared = .{ .zir_index = tracked_inst, .captures = &.{}, @@ -912,7 +911,7 @@ fn createFileRootStruct( try pt.scanNamespace(namespace_index, decls); try zcu.comp.queueJob(.{ .resolve_type_fully = wip_ty.index }); zcu.setFileRootType(file_index, wip_ty.index); - return wip_ty.finish(ip, new_cau_index.toOptional(), namespace_index.toOptional()); + return wip_ty.finish(ip, new_cau_index.toOptional(), namespace_index); } /// Re-analyze the root type of a file on an incremental update. @@ -925,7 +924,7 @@ fn semaFileUpdate(pt: Zcu.PerThread, file_index: Zcu.File.Index, type_outdated: const ip = &zcu.intern_pool; const file = zcu.fileByIndex(file_index); const file_root_type = zcu.fileRootType(file_index); - const namespace_index = Type.fromInterned(file_root_type).getNamespaceIndex(zcu).unwrap().?; + const namespace_index = Type.fromInterned(file_root_type).getNamespaceIndex(zcu); assert(file_root_type != .none); @@ -1082,12 +1081,12 @@ fn semaCau(pt: Zcu.PerThread, cau_index: InternPool.Cau.Index) !SemaCauResult { const nav_name = ip.getNav(nav).name; const std_file_imported = try pt.importPkg(zcu.std_mod); const std_type = Type.fromInterned(zcu.fileRootType(std_file_imported.file_index)); - const std_namespace = zcu.namespacePtr(std_type.getNamespace(zcu).?.unwrap().?); + const std_namespace = zcu.namespacePtr(std_type.getNamespace(zcu).unwrap().?); const builtin_str = try ip.getOrPutString(gpa, pt.tid, "builtin", .no_embedded_nulls); const builtin_nav = ip.getNav(std_namespace.pub_decls.getKeyAdapted(builtin_str, Zcu.Namespace.NameAdapter{ .zcu = zcu }) orelse break :ip_index .none); const builtin_namespace = switch (builtin_nav.status) { .unresolved => break :ip_index .none, - .resolved => |r| Type.fromInterned(r.val).getNamespace(zcu).?.unwrap().?, + .resolved => |r| Type.fromInterned(r.val).getNamespace(zcu).unwrap().?, }; if (cau.namespace != builtin_namespace) break :ip_index .none; // We're in builtin.zig. This could be a builtin we need to add to a specific InternPool index. @@ -1231,7 +1230,7 @@ fn semaCau(pt: Zcu.PerThread, cau_index: InternPool.Cau.Index) !SemaCauResult { if (decl_ty.toIntern() != .type_type) { return sema.fail(&block, ty_src, "expected type, found {}", .{decl_ty.fmt(pt)}); } - if (decl_val.toType().getNamespace(zcu) == null) { + if (decl_val.toType().getNamespace(zcu) == .none) { return sema.fail(&block, ty_src, "type {} has no namespace", .{decl_val.toType().fmt(pt)}); } ip.resolveNavValue(nav_index, .{ @@ -2431,7 +2430,7 @@ pub fn populateTestFunctions( error.OutOfMemory => |e| return e, }; const builtin_root_type = Type.fromInterned(zcu.fileRootType(builtin_file_index)); - const builtin_namespace = builtin_root_type.getNamespace(zcu).?.unwrap().?; + const builtin_namespace = builtin_root_type.getNamespace(zcu).unwrap().?; const nav_index = zcu.namespacePtr(builtin_namespace).pub_decls.getKeyAdapted( try ip.getOrPutString(gpa, pt.tid, "test_functions", .no_embedded_nulls), Zcu.Namespace.NameAdapter{ .zcu = zcu }, @@ -3195,14 +3194,13 @@ pub fn getBuiltinNav(pt: Zcu.PerThread, name: []const u8) Allocator.Error!Intern const ip = &zcu.intern_pool; const std_file_imported = pt.importPkg(zcu.std_mod) catch @panic("failed to import lib/std.zig"); const std_type = Type.fromInterned(zcu.fileRootType(std_file_imported.file_index)); - const std_namespace = zcu.namespacePtr(std_type.getNamespace(zcu).?.unwrap().?); + const std_namespace = zcu.namespacePtr(std_type.getNamespace(zcu).unwrap().?); const builtin_str = try ip.getOrPutString(gpa, pt.tid, "builtin", .no_embedded_nulls); const builtin_nav = std_namespace.pub_decls.getKeyAdapted(builtin_str, Zcu.Namespace.NameAdapter{ .zcu = zcu }) orelse @panic("lib/std.zig is corrupt and missing 'builtin'"); pt.ensureCauAnalyzed(ip.getNav(builtin_nav).analysis_owner.unwrap().?) catch @panic("std.builtin is corrupt"); const builtin_type = Type.fromInterned(ip.getNav(builtin_nav).status.resolved.val); - const builtin_namespace_index = (if (builtin_type.getNamespace(zcu)) |n| n.unwrap() else null) orelse @panic("std.builtin is corrupt"); - const builtin_namespace = zcu.namespacePtr(builtin_namespace_index); + const builtin_namespace = zcu.namespacePtr(builtin_type.getNamespace(zcu).unwrap() orelse @panic("std.builtin is corrupt")); const name_str = try ip.getOrPutString(gpa, pt.tid, name, .no_embedded_nulls); return builtin_namespace.pub_decls.getKeyAdapted(name_str, Zcu.Namespace.NameAdapter{ .zcu = zcu }) orelse @panic("lib/std/builtin.zig is corrupt"); } diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index bc114d8b2f6d..6f26a5b1b63f 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -1956,7 +1956,7 @@ pub const Object = struct { } const file = try o.getDebugFile(ty.typeDeclInstAllowGeneratedTag(zcu).?.resolveFull(ip).file); - const scope = if (ty.getParentNamespace(zcu).?.unwrap()) |parent_namespace| + const scope = if (ty.getParentNamespace(zcu).unwrap()) |parent_namespace| try o.namespaceToDebugScope(parent_namespace) else file; @@ -2134,7 +2134,7 @@ pub const Object = struct { defer gpa.free(name); const file = try o.getDebugFile(ty.typeDeclInstAllowGeneratedTag(zcu).?.resolveFull(ip).file); - const scope = if (ty.getParentNamespace(zcu).?.unwrap()) |parent_namespace| + const scope = if (ty.getParentNamespace(zcu).unwrap()) |parent_namespace| try o.namespaceToDebugScope(parent_namespace) else file; @@ -2769,7 +2769,7 @@ pub const Object = struct { const zcu = o.pt.zcu; const ip = &zcu.intern_pool; const file = try o.getDebugFile(ty.typeDeclInstAllowGeneratedTag(zcu).?.resolveFull(ip).file); - const scope = if (ty.getParentNamespace(zcu).?.unwrap()) |parent_namespace| + const scope = if (ty.getParentNamespace(zcu).unwrap()) |parent_namespace| try o.namespaceToDebugScope(parent_namespace) else file; @@ -2795,13 +2795,13 @@ pub const Object = struct { const builtin_str = try ip.getOrPutString(zcu.gpa, pt.tid, "builtin", .no_embedded_nulls); const std_file_root_type = Type.fromInterned(zcu.fileRootType(std_file_imported.file_index)); - const std_namespace = ip.namespacePtr(std_file_root_type.getNamespaceIndex(zcu).unwrap().?); + const std_namespace = ip.namespacePtr(std_file_root_type.getNamespaceIndex(zcu)); const builtin_nav = std_namespace.pub_decls.getKeyAdapted(builtin_str, Zcu.Namespace.NameAdapter{ .zcu = zcu }).?; const stack_trace_str = try ip.getOrPutString(zcu.gpa, pt.tid, "StackTrace", .no_embedded_nulls); // buffer is only used for int_type, `builtin` is a struct. const builtin_ty = zcu.navValue(builtin_nav).toType(); - const builtin_namespace = zcu.namespacePtrUnwrap(builtin_ty.getNamespaceIndex(zcu)).?; + const builtin_namespace = zcu.namespacePtr(builtin_ty.getNamespaceIndex(zcu)); const stack_trace_nav = builtin_namespace.pub_decls.getKeyAdapted(stack_trace_str, Zcu.Namespace.NameAdapter{ .zcu = zcu }).?; // Sema should have ensured that StackTrace was analyzed.