authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-03-17 13:23:54+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-03-17 14:42:12+02:00
logfec4b7ef5c6a9fc2da1708be6e5be0a619d4b948
tree0597cfbca81a6370617fcefb6833d2a4c9afe967
parentf983adfc1076adc8509458c4bb64102c797041ff

Sema: fix spurious type has no namespace error

Closes #19232

4 files changed, 152 insertions(+), 141 deletions(-)

src/Sema.zig+116-132
...@@ -6267,7 +6267,7 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void...@@ -6267,7 +6267,7 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
6267 const decl_name = try mod.intern_pool.getOrPutString(mod.gpa, sema.code.nullTerminatedString(extra.decl_name));6267 const decl_name = try mod.intern_pool.getOrPutString(mod.gpa, sema.code.nullTerminatedString(extra.decl_name));
6268 const decl_index = if (extra.namespace != .none) index_blk: {6268 const decl_index = if (extra.namespace != .none) index_blk: {
6269 const container_ty = try sema.resolveType(block, operand_src, extra.namespace);6269 const container_ty = try sema.resolveType(block, operand_src, extra.namespace);
6270 const container_namespace = container_ty.getNamespaceIndex(mod).unwrap().?;6270 const container_namespace = container_ty.getNamespaceIndex(mod);
62716271
6272 const maybe_index = try sema.lookupInNamespace(block, operand_src, container_namespace, decl_name, false);6272 const maybe_index = try sema.lookupInNamespace(block, operand_src, container_namespace, decl_name, false);
6273 break :index_blk maybe_index orelse6273 break :index_blk maybe_index orelse
...@@ -6632,7 +6632,7 @@ fn lookupIdentifier(sema: *Sema, block: *Block, src: LazySrcLoc, name: InternPoo...@@ -6632,7 +6632,7 @@ fn lookupIdentifier(sema: *Sema, block: *Block, src: LazySrcLoc, name: InternPoo
6632 const mod = sema.mod;6632 const mod = sema.mod;
6633 var namespace = block.namespace;6633 var namespace = block.namespace;
6634 while (true) {6634 while (true) {
6635 if (try sema.lookupInNamespace(block, src, namespace, name, false)) |decl_index| {6635 if (try sema.lookupInNamespace(block, src, namespace.toOptional(), name, false)) |decl_index| {
6636 return decl_index;6636 return decl_index;
6637 }6637 }
6638 namespace = mod.namespacePtr(namespace).parent.unwrap() orelse break;6638 namespace = mod.namespacePtr(namespace).parent.unwrap() orelse break;
...@@ -6646,12 +6646,13 @@ fn lookupInNamespace(...@@ -6646,12 +6646,13 @@ fn lookupInNamespace(
6646 sema: *Sema,6646 sema: *Sema,
6647 block: *Block,6647 block: *Block,
6648 src: LazySrcLoc,6648 src: LazySrcLoc,
6649 namespace_index: InternPool.NamespaceIndex,6649 opt_namespace_index: InternPool.OptionalNamespaceIndex,
6650 ident_name: InternPool.NullTerminatedString,6650 ident_name: InternPool.NullTerminatedString,
6651 observe_usingnamespace: bool,6651 observe_usingnamespace: bool,
6652) CompileError!?InternPool.DeclIndex {6652) CompileError!?InternPool.DeclIndex {
6653 const mod = sema.mod;6653 const mod = sema.mod;
66546654
6655 const namespace_index = opt_namespace_index.unwrap() orelse return null;
6655 const namespace = mod.namespacePtr(namespace_index);6656 const namespace = mod.namespacePtr(namespace_index);
6656 const namespace_decl = mod.declPtr(namespace.decl_index);6657 const namespace_decl = mod.declPtr(namespace.decl_index);
6657 if (namespace_decl.analysis == .file_failure) {6658 if (namespace_decl.analysis == .file_failure) {
...@@ -6696,7 +6697,7 @@ fn lookupInNamespace(...@@ -6696,7 +6697,7 @@ fn lookupInNamespace(
6696 }6697 }
6697 try sema.ensureDeclAnalyzed(sub_usingnamespace_decl_index);6698 try sema.ensureDeclAnalyzed(sub_usingnamespace_decl_index);
6698 const ns_ty = sub_usingnamespace_decl.val.toType();6699 const ns_ty = sub_usingnamespace_decl.val.toType();
6699 const sub_ns = ns_ty.getNamespace(mod).?;6700 const sub_ns = mod.namespacePtrUnwrap(ns_ty.getNamespaceIndex(mod)) orelse continue;
6700 try checked_namespaces.put(gpa, sub_ns, src_file == sub_usingnamespace_decl.getFileScope(mod));6701 try checked_namespaces.put(gpa, sub_ns, src_file == sub_usingnamespace_decl.getFileScope(mod));
6701 }6702 }
6702 }6703 }
...@@ -13700,8 +13701,7 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -13700,8 +13701,7 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
13700 } });13701 } });
13701 }13702 }
1370213703
13703 const namespace = container_type.getNamespaceIndex(mod).unwrap() orelse13704 const namespace = container_type.getNamespaceIndex(mod);
13704 return .bool_false;
13705 if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl_index| {13705 if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl_index| {
13706 const decl = mod.declPtr(decl_index);13706 const decl = mod.declPtr(decl_index);
13707 if (decl.is_pub or decl.getFileScope(mod) == block.getFileScope(mod)) {13707 if (decl.is_pub or decl.getFileScope(mod) == block.getFileScope(mod)) {
...@@ -17535,7 +17535,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17535,7 +17535,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
17535 const fn_info_decl_index = (try sema.namespaceLookup(17535 const fn_info_decl_index = (try sema.namespaceLookup(
17536 block,17536 block,
17537 src,17537 src,
17538 type_info_ty.getNamespaceIndex(mod).unwrap().?,17538 type_info_ty.getNamespaceIndex(mod),
17539 try ip.getOrPutString(gpa, "Fn"),17539 try ip.getOrPutString(gpa, "Fn"),
17540 )).?;17540 )).?;
17541 try sema.ensureDeclAnalyzed(fn_info_decl_index);17541 try sema.ensureDeclAnalyzed(fn_info_decl_index);
...@@ -17545,7 +17545,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17545,7 +17545,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
17545 const param_info_decl_index = (try sema.namespaceLookup(17545 const param_info_decl_index = (try sema.namespaceLookup(
17546 block,17546 block,
17547 src,17547 src,
17548 fn_info_ty.getNamespaceIndex(mod).unwrap().?,17548 fn_info_ty.getNamespaceIndex(mod),
17549 try ip.getOrPutString(gpa, "Param"),17549 try ip.getOrPutString(gpa, "Param"),
17550 )).?;17550 )).?;
17551 try sema.ensureDeclAnalyzed(param_info_decl_index);17551 try sema.ensureDeclAnalyzed(param_info_decl_index);
...@@ -17647,7 +17647,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17647,7 +17647,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
17647 const int_info_decl_index = (try sema.namespaceLookup(17647 const int_info_decl_index = (try sema.namespaceLookup(
17648 block,17648 block,
17649 src,17649 src,
17650 type_info_ty.getNamespaceIndex(mod).unwrap().?,17650 type_info_ty.getNamespaceIndex(mod),
17651 try ip.getOrPutString(gpa, "Int"),17651 try ip.getOrPutString(gpa, "Int"),
17652 )).?;17652 )).?;
17653 try sema.ensureDeclAnalyzed(int_info_decl_index);17653 try sema.ensureDeclAnalyzed(int_info_decl_index);
...@@ -17675,7 +17675,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17675,7 +17675,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
17675 const float_info_decl_index = (try sema.namespaceLookup(17675 const float_info_decl_index = (try sema.namespaceLookup(
17676 block,17676 block,
17677 src,17677 src,
17678 type_info_ty.getNamespaceIndex(mod).unwrap().?,17678 type_info_ty.getNamespaceIndex(mod),
17679 try ip.getOrPutString(gpa, "Float"),17679 try ip.getOrPutString(gpa, "Float"),
17680 )).?;17680 )).?;
17681 try sema.ensureDeclAnalyzed(float_info_decl_index);17681 try sema.ensureDeclAnalyzed(float_info_decl_index);
...@@ -17707,7 +17707,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17707,7 +17707,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
17707 const decl_index = (try sema.namespaceLookup(17707 const decl_index = (try sema.namespaceLookup(
17708 block,17708 block,
17709 src,17709 src,
17710 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod).unwrap().?,17710 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod),
17711 try ip.getOrPutString(gpa, "Pointer"),17711 try ip.getOrPutString(gpa, "Pointer"),
17712 )).?;17712 )).?;
17713 try sema.ensureDeclAnalyzed(decl_index);17713 try sema.ensureDeclAnalyzed(decl_index);
...@@ -17718,7 +17718,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17718,7 +17718,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
17718 const decl_index = (try sema.namespaceLookup(17718 const decl_index = (try sema.namespaceLookup(
17719 block,17719 block,
17720 src,17720 src,
17721 pointer_ty.getNamespaceIndex(mod).unwrap().?,17721 pointer_ty.getNamespaceIndex(mod),
17722 try ip.getOrPutString(gpa, "Size"),17722 try ip.getOrPutString(gpa, "Size"),
17723 )).?;17723 )).?;
17724 try sema.ensureDeclAnalyzed(decl_index);17724 try sema.ensureDeclAnalyzed(decl_index);
...@@ -17761,7 +17761,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17761,7 +17761,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
17761 const array_field_ty_decl_index = (try sema.namespaceLookup(17761 const array_field_ty_decl_index = (try sema.namespaceLookup(
17762 block,17762 block,
17763 src,17763 src,
17764 type_info_ty.getNamespaceIndex(mod).unwrap().?,17764 type_info_ty.getNamespaceIndex(mod),
17765 try ip.getOrPutString(gpa, "Array"),17765 try ip.getOrPutString(gpa, "Array"),
17766 )).?;17766 )).?;
17767 try sema.ensureDeclAnalyzed(array_field_ty_decl_index);17767 try sema.ensureDeclAnalyzed(array_field_ty_decl_index);
...@@ -17792,7 +17792,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17792,7 +17792,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
17792 const vector_field_ty_decl_index = (try sema.namespaceLookup(17792 const vector_field_ty_decl_index = (try sema.namespaceLookup(
17793 block,17793 block,
17794 src,17794 src,
17795 type_info_ty.getNamespaceIndex(mod).unwrap().?,17795 type_info_ty.getNamespaceIndex(mod),
17796 try ip.getOrPutString(gpa, "Vector"),17796 try ip.getOrPutString(gpa, "Vector"),
17797 )).?;17797 )).?;
17798 try sema.ensureDeclAnalyzed(vector_field_ty_decl_index);17798 try sema.ensureDeclAnalyzed(vector_field_ty_decl_index);
...@@ -17821,7 +17821,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17821,7 +17821,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
17821 const optional_field_ty_decl_index = (try sema.namespaceLookup(17821 const optional_field_ty_decl_index = (try sema.namespaceLookup(
17822 block,17822 block,
17823 src,17823 src,
17824 type_info_ty.getNamespaceIndex(mod).unwrap().?,17824 type_info_ty.getNamespaceIndex(mod),
17825 try ip.getOrPutString(gpa, "Optional"),17825 try ip.getOrPutString(gpa, "Optional"),
17826 )).?;17826 )).?;
17827 try sema.ensureDeclAnalyzed(optional_field_ty_decl_index);17827 try sema.ensureDeclAnalyzed(optional_field_ty_decl_index);
...@@ -17848,7 +17848,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17848,7 +17848,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
17848 const set_field_ty_decl_index = (try sema.namespaceLookup(17848 const set_field_ty_decl_index = (try sema.namespaceLookup(
17849 block,17849 block,
17850 src,17850 src,
17851 type_info_ty.getNamespaceIndex(mod).unwrap().?,17851 type_info_ty.getNamespaceIndex(mod),
17852 try ip.getOrPutString(gpa, "Error"),17852 try ip.getOrPutString(gpa, "Error"),
17853 )).?;17853 )).?;
17854 try sema.ensureDeclAnalyzed(set_field_ty_decl_index);17854 try sema.ensureDeclAnalyzed(set_field_ty_decl_index);
...@@ -17953,7 +17953,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17953,7 +17953,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
17953 const error_union_field_ty_decl_index = (try sema.namespaceLookup(17953 const error_union_field_ty_decl_index = (try sema.namespaceLookup(
17954 block,17954 block,
17955 src,17955 src,
17956 type_info_ty.getNamespaceIndex(mod).unwrap().?,17956 type_info_ty.getNamespaceIndex(mod),
17957 try ip.getOrPutString(gpa, "ErrorUnion"),17957 try ip.getOrPutString(gpa, "ErrorUnion"),
17958 )).?;17958 )).?;
17959 try sema.ensureDeclAnalyzed(error_union_field_ty_decl_index);17959 try sema.ensureDeclAnalyzed(error_union_field_ty_decl_index);
...@@ -17983,7 +17983,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17983,7 +17983,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
17983 const enum_field_ty_decl_index = (try sema.namespaceLookup(17983 const enum_field_ty_decl_index = (try sema.namespaceLookup(
17984 block,17984 block,
17985 src,17985 src,
17986 type_info_ty.getNamespaceIndex(mod).unwrap().?,17986 type_info_ty.getNamespaceIndex(mod),
17987 try ip.getOrPutString(gpa, "EnumField"),17987 try ip.getOrPutString(gpa, "EnumField"),
17988 )).?;17988 )).?;
17989 try sema.ensureDeclAnalyzed(enum_field_ty_decl_index);17989 try sema.ensureDeclAnalyzed(enum_field_ty_decl_index);
...@@ -18074,7 +18074,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -18074,7 +18074,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
18074 const type_enum_ty_decl_index = (try sema.namespaceLookup(18074 const type_enum_ty_decl_index = (try sema.namespaceLookup(
18075 block,18075 block,
18076 src,18076 src,
18077 type_info_ty.getNamespaceIndex(mod).unwrap().?,18077 type_info_ty.getNamespaceIndex(mod),
18078 try ip.getOrPutString(gpa, "Enum"),18078 try ip.getOrPutString(gpa, "Enum"),
18079 )).?;18079 )).?;
18080 try sema.ensureDeclAnalyzed(type_enum_ty_decl_index);18080 try sema.ensureDeclAnalyzed(type_enum_ty_decl_index);
...@@ -18106,7 +18106,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -18106,7 +18106,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
18106 const type_union_ty_decl_index = (try sema.namespaceLookup(18106 const type_union_ty_decl_index = (try sema.namespaceLookup(
18107 block,18107 block,
18108 src,18108 src,
18109 type_info_ty.getNamespaceIndex(mod).unwrap().?,18109 type_info_ty.getNamespaceIndex(mod),
18110 try ip.getOrPutString(gpa, "Union"),18110 try ip.getOrPutString(gpa, "Union"),
18111 )).?;18111 )).?;
18112 try sema.ensureDeclAnalyzed(type_union_ty_decl_index);18112 try sema.ensureDeclAnalyzed(type_union_ty_decl_index);
...@@ -18118,7 +18118,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -18118,7 +18118,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
18118 const union_field_ty_decl_index = (try sema.namespaceLookup(18118 const union_field_ty_decl_index = (try sema.namespaceLookup(
18119 block,18119 block,
18120 src,18120 src,
18121 type_info_ty.getNamespaceIndex(mod).unwrap().?,18121 type_info_ty.getNamespaceIndex(mod),
18122 try ip.getOrPutString(gpa, "UnionField"),18122 try ip.getOrPutString(gpa, "UnionField"),
18123 )).?;18123 )).?;
18124 try sema.ensureDeclAnalyzed(union_field_ty_decl_index);18124 try sema.ensureDeclAnalyzed(union_field_ty_decl_index);
...@@ -18220,7 +18220,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -18220,7 +18220,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
18220 const decl_index = (try sema.namespaceLookup(18220 const decl_index = (try sema.namespaceLookup(
18221 block,18221 block,
18222 src,18222 src,
18223 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod).unwrap().?,18223 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod),
18224 try ip.getOrPutString(gpa, "ContainerLayout"),18224 try ip.getOrPutString(gpa, "ContainerLayout"),
18225 )).?;18225 )).?;
18226 try sema.ensureDeclAnalyzed(decl_index);18226 try sema.ensureDeclAnalyzed(decl_index);
...@@ -18253,7 +18253,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -18253,7 +18253,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
18253 const type_struct_ty_decl_index = (try sema.namespaceLookup(18253 const type_struct_ty_decl_index = (try sema.namespaceLookup(
18254 block,18254 block,
18255 src,18255 src,
18256 type_info_ty.getNamespaceIndex(mod).unwrap().?,18256 type_info_ty.getNamespaceIndex(mod),
18257 try ip.getOrPutString(gpa, "Struct"),18257 try ip.getOrPutString(gpa, "Struct"),
18258 )).?;18258 )).?;
18259 try sema.ensureDeclAnalyzed(type_struct_ty_decl_index);18259 try sema.ensureDeclAnalyzed(type_struct_ty_decl_index);
...@@ -18265,7 +18265,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -18265,7 +18265,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
18265 const struct_field_ty_decl_index = (try sema.namespaceLookup(18265 const struct_field_ty_decl_index = (try sema.namespaceLookup(
18266 block,18266 block,
18267 src,18267 src,
18268 type_info_ty.getNamespaceIndex(mod).unwrap().?,18268 type_info_ty.getNamespaceIndex(mod),
18269 try ip.getOrPutString(gpa, "StructField"),18269 try ip.getOrPutString(gpa, "StructField"),
18270 )).?;18270 )).?;
18271 try sema.ensureDeclAnalyzed(struct_field_ty_decl_index);18271 try sema.ensureDeclAnalyzed(struct_field_ty_decl_index);
...@@ -18450,7 +18450,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -18450,7 +18450,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
18450 const decl_index = (try sema.namespaceLookup(18450 const decl_index = (try sema.namespaceLookup(
18451 block,18451 block,
18452 src,18452 src,
18453 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod).unwrap().?,18453 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod),
18454 try ip.getOrPutString(gpa, "ContainerLayout"),18454 try ip.getOrPutString(gpa, "ContainerLayout"),
18455 )).?;18455 )).?;
18456 try sema.ensureDeclAnalyzed(decl_index);18456 try sema.ensureDeclAnalyzed(decl_index);
...@@ -18486,7 +18486,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -18486,7 +18486,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
18486 const type_opaque_ty_decl_index = (try sema.namespaceLookup(18486 const type_opaque_ty_decl_index = (try sema.namespaceLookup(
18487 block,18487 block,
18488 src,18488 src,
18489 type_info_ty.getNamespaceIndex(mod).unwrap().?,18489 type_info_ty.getNamespaceIndex(mod),
18490 try ip.getOrPutString(gpa, "Opaque"),18490 try ip.getOrPutString(gpa, "Opaque"),
18491 )).?;18491 )).?;
18492 try sema.ensureDeclAnalyzed(type_opaque_ty_decl_index);18492 try sema.ensureDeclAnalyzed(type_opaque_ty_decl_index);
...@@ -18529,7 +18529,7 @@ fn typeInfoDecls(...@@ -18529,7 +18529,7 @@ fn typeInfoDecls(
18529 const declaration_ty_decl_index = (try sema.namespaceLookup(18529 const declaration_ty_decl_index = (try sema.namespaceLookup(
18530 block,18530 block,
18531 src,18531 src,
18532 type_info_ty.getNamespaceIndex(mod).unwrap().?,18532 type_info_ty.getNamespaceIndex(mod),
18533 try mod.intern_pool.getOrPutString(gpa, "Declaration"),18533 try mod.intern_pool.getOrPutString(gpa, "Declaration"),
18534 )).?;18534 )).?;
18535 try sema.ensureDeclAnalyzed(declaration_ty_decl_index);18535 try sema.ensureDeclAnalyzed(declaration_ty_decl_index);
...@@ -18544,10 +18544,7 @@ fn typeInfoDecls(...@@ -18544,10 +18544,7 @@ fn typeInfoDecls(
18544 var seen_namespaces = std.AutoHashMap(*Namespace, void).init(gpa);18544 var seen_namespaces = std.AutoHashMap(*Namespace, void).init(gpa);
18545 defer seen_namespaces.deinit();18545 defer seen_namespaces.deinit();
1854618546
18547 if (opt_namespace.unwrap()) |namespace_index| {18547 try sema.typeInfoNamespaceDecls(block, opt_namespace, declaration_ty, &decl_vals, &seen_namespaces);
18548 const namespace = mod.namespacePtr(namespace_index);
18549 try sema.typeInfoNamespaceDecls(block, namespace, declaration_ty, &decl_vals, &seen_namespaces);
18550 }
1855118548
18552 const array_decl_ty = try mod.arrayType(.{18549 const array_decl_ty = try mod.arrayType(.{
18553 .len = decl_vals.items.len,18550 .len = decl_vals.items.len,
...@@ -18580,23 +18577,27 @@ fn typeInfoDecls(...@@ -18580,23 +18577,27 @@ fn typeInfoDecls(
18580fn typeInfoNamespaceDecls(18577fn typeInfoNamespaceDecls(
18581 sema: *Sema,18578 sema: *Sema,
18582 block: *Block,18579 block: *Block,
18583 namespace: *Namespace,18580 opt_namespace_index: InternPool.OptionalNamespaceIndex,
18584 declaration_ty: Type,18581 declaration_ty: Type,
18585 decl_vals: *std.ArrayList(InternPool.Index),18582 decl_vals: *std.ArrayList(InternPool.Index),
18586 seen_namespaces: *std.AutoHashMap(*Namespace, void),18583 seen_namespaces: *std.AutoHashMap(*Namespace, void),
18587) !void {18584) !void {
18588 const mod = sema.mod;18585 const mod = sema.mod;
18589 const ip = &mod.intern_pool;18586 const ip = &mod.intern_pool;
18587
18588 const namespace_index = opt_namespace_index.unwrap() orelse return;
18589 const namespace = mod.namespacePtr(namespace_index);
18590
18590 const gop = try seen_namespaces.getOrPut(namespace);18591 const gop = try seen_namespaces.getOrPut(namespace);
18591 if (gop.found_existing) return;18592 if (gop.found_existing) return;
18593
18592 const decls = namespace.decls.keys();18594 const decls = namespace.decls.keys();
18593 for (decls) |decl_index| {18595 for (decls) |decl_index| {
18594 const decl = mod.declPtr(decl_index);18596 const decl = mod.declPtr(decl_index);
18595 if (decl.kind == .@"usingnamespace") {18597 if (decl.kind == .@"usingnamespace") {
18596 if (decl.analysis == .in_progress) continue;18598 if (decl.analysis == .in_progress) continue;
18597 try mod.ensureDeclAnalyzed(decl_index);18599 try mod.ensureDeclAnalyzed(decl_index);
18598 const new_ns = decl.val.toType().getNamespace(mod).?;18600 try sema.typeInfoNamespaceDecls(block, decl.val.toType().getNamespaceIndex(mod), declaration_ty, decl_vals, seen_namespaces);
18599 try sema.typeInfoNamespaceDecls(block, new_ns, declaration_ty, decl_vals, seen_namespaces);
18600 continue;18601 continue;
18601 }18602 }
18602 if (decl.kind != .named or !decl.is_pub) continue;18603 if (decl.kind != .named or !decl.is_pub) continue;
...@@ -26585,7 +26586,7 @@ fn preparePanicId(sema: *Sema, block: *Block, panic_id: Module.PanicId) !InternP...@@ -26585,7 +26586,7 @@ fn preparePanicId(sema: *Sema, block: *Block, panic_id: Module.PanicId) !InternP
26585 const msg_decl_index = (sema.namespaceLookup(26586 const msg_decl_index = (sema.namespaceLookup(
26586 block,26587 block,
26587 .unneeded,26588 .unneeded,
26588 panic_messages_ty.getNamespaceIndex(mod).unwrap().?,26589 panic_messages_ty.getNamespaceIndex(mod),
26589 try mod.intern_pool.getOrPutString(gpa, @tagName(panic_id)),26590 try mod.intern_pool.getOrPutString(gpa, @tagName(panic_id)),
26590 ) catch |err| switch (err) {26591 ) catch |err| switch (err) {
26591 error.AnalysisFail, error.NeededSourceLocation => @panic("std.builtin.panic_messages is corrupt"),26592 error.AnalysisFail, error.NeededSourceLocation => @panic("std.builtin.panic_messages is corrupt"),
...@@ -27004,10 +27005,8 @@ fn fieldVal(...@@ -27004,10 +27005,8 @@ fn fieldVal(
27004 } })));27005 } })));
27005 },27006 },
27006 .Union => {27007 .Union => {
27007 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {27008 if (try sema.namespaceLookupVal(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27008 if (try sema.namespaceLookupVal(block, src, namespace, field_name)) |inst| {27009 return inst;
27009 return inst;
27010 }
27011 }27010 }
27012 try sema.resolveTypeFields(child_type);27011 try sema.resolveTypeFields(child_type);
27013 if (child_type.unionTagType(mod)) |enum_ty| {27012 if (child_type.unionTagType(mod)) |enum_ty| {
...@@ -27019,10 +27018,8 @@ fn fieldVal(...@@ -27019,10 +27018,8 @@ fn fieldVal(
27019 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);27018 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
27020 },27019 },
27021 .Enum => {27020 .Enum => {
27022 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {27021 if (try sema.namespaceLookupVal(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27023 if (try sema.namespaceLookupVal(block, src, namespace, field_name)) |inst| {27022 return inst;
27024 return inst;
27025 }
27026 }27023 }
27027 const field_index_usize = child_type.enumFieldIndex(field_name, mod) orelse27024 const field_index_usize = child_type.enumFieldIndex(field_name, mod) orelse
27028 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);27025 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
...@@ -27031,10 +27028,8 @@ fn fieldVal(...@@ -27031,10 +27028,8 @@ fn fieldVal(
27031 return Air.internedToRef(enum_val.toIntern());27028 return Air.internedToRef(enum_val.toIntern());
27032 },27029 },
27033 .Struct, .Opaque => {27030 .Struct, .Opaque => {
27034 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {27031 if (try sema.namespaceLookupVal(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27035 if (try sema.namespaceLookupVal(block, src, namespace, field_name)) |inst| {27032 return inst;
27036 return inst;
27037 }
27038 }27033 }
27039 return sema.failWithBadMemberAccess(block, child_type, src, field_name);27034 return sema.failWithBadMemberAccess(block, child_type, src, field_name);
27040 },27035 },
...@@ -27230,10 +27225,8 @@ fn fieldPtr(...@@ -27230,10 +27225,8 @@ fn fieldPtr(
27230 } }));27225 } }));
27231 },27226 },
27232 .Union => {27227 .Union => {
27233 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {27228 if (try sema.namespaceLookupRef(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27234 if (try sema.namespaceLookupRef(block, src, namespace, field_name)) |inst| {27229 return inst;
27235 return inst;
27236 }
27237 }27230 }
27238 try sema.resolveTypeFields(child_type);27231 try sema.resolveTypeFields(child_type);
27239 if (child_type.unionTagType(mod)) |enum_ty| {27232 if (child_type.unionTagType(mod)) |enum_ty| {
...@@ -27246,10 +27239,8 @@ fn fieldPtr(...@@ -27246,10 +27239,8 @@ fn fieldPtr(
27246 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);27239 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
27247 },27240 },
27248 .Enum => {27241 .Enum => {
27249 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {27242 if (try sema.namespaceLookupRef(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27250 if (try sema.namespaceLookupRef(block, src, namespace, field_name)) |inst| {27243 return inst;
27251 return inst;
27252 }
27253 }27244 }
27254 const field_index = child_type.enumFieldIndex(field_name, mod) orelse {27245 const field_index = child_type.enumFieldIndex(field_name, mod) orelse {
27255 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);27246 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
...@@ -27259,10 +27250,8 @@ fn fieldPtr(...@@ -27259,10 +27250,8 @@ fn fieldPtr(
27259 return anonDeclRef(sema, idx_val.toIntern());27250 return anonDeclRef(sema, idx_val.toIntern());
27260 },27251 },
27261 .Struct, .Opaque => {27252 .Struct, .Opaque => {
27262 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {27253 if (try sema.namespaceLookupRef(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27263 if (try sema.namespaceLookupRef(block, src, namespace, field_name)) |inst| {27254 return inst;
27264 return inst;
27265 }
27266 }27255 }
27267 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);27256 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
27268 },27257 },
...@@ -27375,73 +27364,68 @@ fn fieldCallBind(...@@ -27375,73 +27364,68 @@ fn fieldCallBind(
27375 }27364 }
2737627365
27377 // If we get here, we need to look for a decl in the struct type instead.27366 // If we get here, we need to look for a decl in the struct type instead.
27378 const found_decl = switch (concrete_ty.zigTypeTag(mod)) {27367 const found_decl = found_decl: {
27379 .Struct, .Opaque, .Union, .Enum => found_decl: {27368 const namespace = concrete_ty.getNamespace(mod) orelse
27380 if (concrete_ty.getNamespaceIndex(mod).unwrap()) |namespace| {27369 break :found_decl null;
27381 if (try sema.namespaceLookup(block, src, namespace, field_name)) |decl_idx| {27370 const decl_idx = (try sema.namespaceLookup(block, src, namespace, field_name)) orelse
27382 try sema.addReferencedBy(block, src, decl_idx);27371 break :found_decl null;
27383 const decl_val = try sema.analyzeDeclVal(block, src, decl_idx);27372
27384 const decl_type = sema.typeOf(decl_val);27373 try sema.addReferencedBy(block, src, decl_idx);
27385 if (mod.typeToFunc(decl_type)) |func_type| f: {27374 const decl_val = try sema.analyzeDeclVal(block, src, decl_idx);
27386 if (func_type.param_types.len == 0) break :f;27375 const decl_type = sema.typeOf(decl_val);
2738727376 if (mod.typeToFunc(decl_type)) |func_type| f: {
27388 const first_param_type = Type.fromInterned(func_type.param_types.get(ip)[0]);27377 if (func_type.param_types.len == 0) break :f;
27389 // zig fmt: off27378
27390 if (first_param_type.isGenericPoison() or (27379 const first_param_type = Type.fromInterned(func_type.param_types.get(ip)[0]);
27391 first_param_type.zigTypeTag(mod) == .Pointer and27380 if (first_param_type.isGenericPoison() or
27392 (first_param_type.ptrSize(mod) == .One or27381 (first_param_type.zigTypeTag(mod) == .Pointer and
27393 first_param_type.ptrSize(mod) == .C) and27382 (first_param_type.ptrSize(mod) == .One or
27394 first_param_type.childType(mod).eql(concrete_ty, mod)))27383 first_param_type.ptrSize(mod) == .C) and
27395 {27384 first_param_type.childType(mod).eql(concrete_ty, mod)))
27396 // zig fmt: on27385 {
27397 // Note that if the param type is generic poison, we know that it must27386 // Note that if the param type is generic poison, we know that it must
27398 // specifically be `anytype` since it's the first parameter, meaning we27387 // specifically be `anytype` since it's the first parameter, meaning we
27399 // can safely assume it can be a pointer.27388 // can safely assume it can be a pointer.
27400 // TODO: bound fn calls on rvalues should probably27389 // TODO: bound fn calls on rvalues should probably
27401 // generate a by-value argument somehow.27390 // generate a by-value argument somehow.
27402 return .{ .method = .{27391 return .{ .method = .{
27403 .func_inst = decl_val,27392 .func_inst = decl_val,
27404 .arg0_inst = object_ptr,27393 .arg0_inst = object_ptr,
27405 } };27394 } };
27406 } else if (first_param_type.eql(concrete_ty, mod)) {27395 } else if (first_param_type.eql(concrete_ty, mod)) {
27407 const deref = try sema.analyzeLoad(block, src, object_ptr, src);27396 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
27408 return .{ .method = .{27397 return .{ .method = .{
27409 .func_inst = decl_val,27398 .func_inst = decl_val,
27410 .arg0_inst = deref,27399 .arg0_inst = deref,
27411 } };27400 } };
27412 } else if (first_param_type.zigTypeTag(mod) == .Optional) {27401 } else if (first_param_type.zigTypeTag(mod) == .Optional) {
27413 const child = first_param_type.optionalChild(mod);27402 const child = first_param_type.optionalChild(mod);
27414 if (child.eql(concrete_ty, mod)) {27403 if (child.eql(concrete_ty, mod)) {
27415 const deref = try sema.analyzeLoad(block, src, object_ptr, src);27404 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
27416 return .{ .method = .{27405 return .{ .method = .{
27417 .func_inst = decl_val,27406 .func_inst = decl_val,
27418 .arg0_inst = deref,27407 .arg0_inst = deref,
27419 } };27408 } };
27420 } else if (child.zigTypeTag(mod) == .Pointer and27409 } else if (child.zigTypeTag(mod) == .Pointer and
27421 child.ptrSize(mod) == .One and27410 child.ptrSize(mod) == .One and
27422 child.childType(mod).eql(concrete_ty, mod))27411 child.childType(mod).eql(concrete_ty, mod))
27423 {27412 {
27424 return .{ .method = .{27413 return .{ .method = .{
27425 .func_inst = decl_val,27414 .func_inst = decl_val,
27426 .arg0_inst = object_ptr,27415 .arg0_inst = object_ptr,
27427 } };27416 } };
27428 }
27429 } else if (first_param_type.zigTypeTag(mod) == .ErrorUnion and
27430 first_param_type.errorUnionPayload(mod).eql(concrete_ty, mod))
27431 {
27432 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
27433 return .{ .method = .{
27434 .func_inst = decl_val,
27435 .arg0_inst = deref,
27436 } };
27437 }
27438 }
27439 break :found_decl decl_idx;
27440 }27417 }
27418 } else if (first_param_type.zigTypeTag(mod) == .ErrorUnion and
27419 first_param_type.errorUnionPayload(mod).eql(concrete_ty, mod))
27420 {
27421 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
27422 return .{ .method = .{
27423 .func_inst = decl_val,
27424 .arg0_inst = deref,
27425 } };
27441 }27426 }
27442 break :found_decl null;27427 }
27443 },27428 break :found_decl decl_idx;
27444 else => null,
27445 };27429 };
2744627430
27447 const msg = msg: {27431 const msg = msg: {
...@@ -27507,12 +27491,12 @@ fn namespaceLookup(...@@ -27507,12 +27491,12 @@ fn namespaceLookup(
27507 sema: *Sema,27491 sema: *Sema,
27508 block: *Block,27492 block: *Block,
27509 src: LazySrcLoc,27493 src: LazySrcLoc,
27510 namespace: InternPool.NamespaceIndex,27494 opt_namespace: InternPool.OptionalNamespaceIndex,
27511 decl_name: InternPool.NullTerminatedString,27495 decl_name: InternPool.NullTerminatedString,
27512) CompileError!?InternPool.DeclIndex {27496) CompileError!?InternPool.DeclIndex {
27513 const mod = sema.mod;27497 const mod = sema.mod;
27514 const gpa = sema.gpa;27498 const gpa = sema.gpa;
27515 if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl_index| {27499 if (try sema.lookupInNamespace(block, src, opt_namespace, decl_name, true)) |decl_index| {
27516 const decl = mod.declPtr(decl_index);27500 const decl = mod.declPtr(decl_index);
27517 if (!decl.is_pub and decl.getFileScope(mod) != block.getFileScope(mod)) {27501 if (!decl.is_pub and decl.getFileScope(mod) != block.getFileScope(mod)) {
27518 const msg = msg: {27502 const msg = msg: {
...@@ -27534,10 +27518,10 @@ fn namespaceLookupRef(...@@ -27534,10 +27518,10 @@ fn namespaceLookupRef(
27534 sema: *Sema,27518 sema: *Sema,
27535 block: *Block,27519 block: *Block,
27536 src: LazySrcLoc,27520 src: LazySrcLoc,
27537 namespace: InternPool.NamespaceIndex,27521 opt_namespace: InternPool.OptionalNamespaceIndex,
27538 decl_name: InternPool.NullTerminatedString,27522 decl_name: InternPool.NullTerminatedString,
27539) CompileError!?Air.Inst.Ref {27523) CompileError!?Air.Inst.Ref {
27540 const decl = (try sema.namespaceLookup(block, src, namespace, decl_name)) orelse return null;27524 const decl = (try sema.namespaceLookup(block, src, opt_namespace, decl_name)) orelse return null;
27541 try sema.addReferencedBy(block, src, decl);27525 try sema.addReferencedBy(block, src, decl);
27542 return try sema.analyzeDeclRef(decl);27526 return try sema.analyzeDeclRef(decl);
27543}27527}
...@@ -27546,10 +27530,10 @@ fn namespaceLookupVal(...@@ -27546,10 +27530,10 @@ fn namespaceLookupVal(
27546 sema: *Sema,27530 sema: *Sema,
27547 block: *Block,27531 block: *Block,
27548 src: LazySrcLoc,27532 src: LazySrcLoc,
27549 namespace: InternPool.NamespaceIndex,27533 opt_namespace: InternPool.OptionalNamespaceIndex,
27550 decl_name: InternPool.NullTerminatedString,27534 decl_name: InternPool.NullTerminatedString,
27551) CompileError!?Air.Inst.Ref {27535) CompileError!?Air.Inst.Ref {
27552 const decl = (try sema.namespaceLookup(block, src, namespace, decl_name)) orelse return null;27536 const decl = (try sema.namespaceLookup(block, src, opt_namespace, decl_name)) orelse return null;
27553 return try sema.analyzeDeclVal(block, src, decl);27537 return try sema.analyzeDeclVal(block, src, decl);
27554}27538}
2755527539
...@@ -37602,7 +37586,7 @@ fn getBuiltinDecl(sema: *Sema, block: *Block, name: []const u8) CompileError!Int...@@ -37602,7 +37586,7 @@ fn getBuiltinDecl(sema: *Sema, block: *Block, name: []const u8) CompileError!Int
37602 const opt_builtin_inst = (try sema.namespaceLookupRef(37586 const opt_builtin_inst = (try sema.namespaceLookupRef(
37603 block,37587 block,
37604 src,37588 src,
37605 mod.declPtr(std_file.root_decl.unwrap().?).src_namespace,37589 mod.declPtr(std_file.root_decl.unwrap().?).src_namespace.toOptional(),
37606 try ip.getOrPutString(gpa, "builtin"),37590 try ip.getOrPutString(gpa, "builtin"),
37607 )) orelse @panic("lib/std.zig is corrupt and missing 'builtin'");37591 )) orelse @panic("lib/std.zig is corrupt and missing 'builtin'");
37608 const builtin_inst = try sema.analyzeLoad(block, src, opt_builtin_inst, src);37592 const builtin_inst = try sema.analyzeLoad(block, src, opt_builtin_inst, src);
...@@ -37613,7 +37597,7 @@ fn getBuiltinDecl(sema: *Sema, block: *Block, name: []const u8) CompileError!Int...@@ -37613,7 +37597,7 @@ fn getBuiltinDecl(sema: *Sema, block: *Block, name: []const u8) CompileError!Int
37613 const decl_index = (try sema.namespaceLookup(37597 const decl_index = (try sema.namespaceLookup(
37614 block,37598 block,
37615 src,37599 src,
37616 builtin_ty.getNamespaceIndex(mod).unwrap().?,37600 builtin_ty.getNamespaceIndex(mod),
37617 try ip.getOrPutString(gpa, name),37601 try ip.getOrPutString(gpa, name),
37618 )) orelse std.debug.panic("lib/std/builtin.zig is corrupt and missing '{s}'", .{name});37602 )) orelse std.debug.panic("lib/std/builtin.zig is corrupt and missing '{s}'", .{name});
37619 return decl_index;37603 return decl_index;
src/codegen/llvm.zig+1-1
...@@ -2852,7 +2852,7 @@ pub const Object = struct {...@@ -2852,7 +2852,7 @@ pub const Object = struct {
2852 const stack_trace_str = try mod.intern_pool.getOrPutString(mod.gpa, "StackTrace");2852 const stack_trace_str = try mod.intern_pool.getOrPutString(mod.gpa, "StackTrace");
2853 // buffer is only used for int_type, `builtin` is a struct.2853 // buffer is only used for int_type, `builtin` is a struct.
2854 const builtin_ty = mod.declPtr(builtin_decl).val.toType();2854 const builtin_ty = mod.declPtr(builtin_decl).val.toType();
2855 const builtin_namespace = builtin_ty.getNamespace(mod).?;2855 const builtin_namespace = mod.namespacePtrUnwrap(builtin_ty.getNamespaceIndex(mod)).?;
2856 const stack_trace_decl_index = builtin_namespace.decls.getKeyAdapted(stack_trace_str, Module.DeclAdapter{ .zcu = mod }).?;2856 const stack_trace_decl_index = builtin_namespace.decls.getKeyAdapted(stack_trace_str, Module.DeclAdapter{ .zcu = mod }).?;
2857 const stack_trace_decl = mod.declPtr(stack_trace_decl_index);2857 const stack_trace_decl = mod.declPtr(stack_trace_decl_index);
28582858
src/type.zig+26-8
...@@ -2845,22 +2845,40 @@ pub const Type = struct {...@@ -2845,22 +2845,40 @@ pub const Type = struct {
2845 };2845 };
2846 }2846 }
28472847
2848 /// Asserts that the type can have a namespace.
2849 pub fn getNamespaceIndex(ty: Type, zcu: *Zcu) InternPool.OptionalNamespaceIndex {
2850 return ty.getNamespace(zcu).?;
2851 }
2852
2848 /// Returns null if the type has no namespace.2853 /// Returns null if the type has no namespace.
2849 pub fn getNamespaceIndex(ty: Type, mod: *Module) InternPool.OptionalNamespaceIndex {2854 pub fn getNamespace(ty: Type, zcu: *Zcu) ?InternPool.OptionalNamespaceIndex {
2850 const ip = &mod.intern_pool;2855 const ip = &zcu.intern_pool;
2851 return switch (ip.indexToKey(ty.toIntern())) {2856 return switch (ip.indexToKey(ty.toIntern())) {
2852 .opaque_type => ip.loadOpaqueType(ty.toIntern()).namespace,2857 .opaque_type => ip.loadOpaqueType(ty.toIntern()).namespace,
2853 .struct_type => ip.loadStructType(ty.toIntern()).namespace,2858 .struct_type => ip.loadStructType(ty.toIntern()).namespace,
2854 .union_type => ip.loadUnionType(ty.toIntern()).namespace,2859 .union_type => ip.loadUnionType(ty.toIntern()).namespace,
2855 .enum_type => ip.loadEnumType(ty.toIntern()).namespace,2860 .enum_type => ip.loadEnumType(ty.toIntern()).namespace,
28562861
2857 else => .none,2862 .anon_struct_type => .none,
2858 };2863 .simple_type => |s| switch (s) {
2859 }2864 .anyopaque,
2865 .atomic_order,
2866 .atomic_rmw_op,
2867 .calling_convention,
2868 .address_space,
2869 .float_mode,
2870 .reduce_op,
2871 .call_modifier,
2872 .prefetch_options,
2873 .export_options,
2874 .extern_options,
2875 .type_info,
2876 => .none,
2877 else => null,
2878 },
28602879
2861 /// Returns null if the type has no namespace.2880 else => null,
2862 pub fn getNamespace(ty: Type, mod: *Module) ?*Module.Namespace {2881 };
2863 return if (getNamespaceIndex(ty, mod).unwrap()) |i| mod.namespacePtr(i) else null;
2864 }2882 }
28652883
2866 // Works for vectors and vectors of integers.2884 // Works for vectors and vectors of integers.
test/behavior/usingnamespace.zig+9
...@@ -97,3 +97,12 @@ test "container member access usingnamespace decls" {...@@ -97,3 +97,12 @@ test "container member access usingnamespace decls" {
97 var foo = Bar{};97 var foo = Bar{};
98 foo.two();98 foo.two();
99}99}
100
101usingnamespace opaque {};
102
103usingnamespace @Type(.{ .Struct = .{
104 .layout = .auto,
105 .fields = &.{},
106 .decls = &.{},
107 .is_tuple = false,
108} });