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
62676267 const decl_name = try mod.intern_pool.getOrPutString(mod.gpa, sema.code.nullTerminatedString(extra.decl_name));
62686268 const decl_index = if (extra.namespace != .none) index_blk: {
62696269 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
62726272 const maybe_index = try sema.lookupInNamespace(block, operand_src, container_namespace, decl_name, false);
62736273 break :index_blk maybe_index orelse
......@@ -6632,7 +6632,7 @@ fn lookupIdentifier(sema: *Sema, block: *Block, src: LazySrcLoc, name: InternPoo
66326632 const mod = sema.mod;
66336633 var namespace = block.namespace;
66346634 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| {
66366636 return decl_index;
66376637 }
66386638 namespace = mod.namespacePtr(namespace).parent.unwrap() orelse break;
......@@ -6646,12 +6646,13 @@ fn lookupInNamespace(
66466646 sema: *Sema,
66476647 block: *Block,
66486648 src: LazySrcLoc,
6649 namespace_index: InternPool.NamespaceIndex,
6649 opt_namespace_index: InternPool.OptionalNamespaceIndex,
66506650 ident_name: InternPool.NullTerminatedString,
66516651 observe_usingnamespace: bool,
66526652) CompileError!?InternPool.DeclIndex {
66536653 const mod = sema.mod;
66546654
6655 const namespace_index = opt_namespace_index.unwrap() orelse return null;
66556656 const namespace = mod.namespacePtr(namespace_index);
66566657 const namespace_decl = mod.declPtr(namespace.decl_index);
66576658 if (namespace_decl.analysis == .file_failure) {
......@@ -6696,7 +6697,7 @@ fn lookupInNamespace(
66966697 }
66976698 try sema.ensureDeclAnalyzed(sub_usingnamespace_decl_index);
66986699 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;
67006701 try checked_namespaces.put(gpa, sub_ns, src_file == sub_usingnamespace_decl.getFileScope(mod));
67016702 }
67026703 }
......@@ -13700,8 +13701,7 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1370013701 } });
1370113702 }
1370213703
13703 const namespace = container_type.getNamespaceIndex(mod).unwrap() orelse
13704 return .bool_false;
13704 const namespace = container_type.getNamespaceIndex(mod);
1370513705 if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl_index| {
1370613706 const decl = mod.declPtr(decl_index);
1370713707 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
1753517535 const fn_info_decl_index = (try sema.namespaceLookup(
1753617536 block,
1753717537 src,
17538 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17538 type_info_ty.getNamespaceIndex(mod),
1753917539 try ip.getOrPutString(gpa, "Fn"),
1754017540 )).?;
1754117541 try sema.ensureDeclAnalyzed(fn_info_decl_index);
......@@ -17545,7 +17545,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1754517545 const param_info_decl_index = (try sema.namespaceLookup(
1754617546 block,
1754717547 src,
17548 fn_info_ty.getNamespaceIndex(mod).unwrap().?,
17548 fn_info_ty.getNamespaceIndex(mod),
1754917549 try ip.getOrPutString(gpa, "Param"),
1755017550 )).?;
1755117551 try sema.ensureDeclAnalyzed(param_info_decl_index);
......@@ -17647,7 +17647,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1764717647 const int_info_decl_index = (try sema.namespaceLookup(
1764817648 block,
1764917649 src,
17650 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17650 type_info_ty.getNamespaceIndex(mod),
1765117651 try ip.getOrPutString(gpa, "Int"),
1765217652 )).?;
1765317653 try sema.ensureDeclAnalyzed(int_info_decl_index);
......@@ -17675,7 +17675,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1767517675 const float_info_decl_index = (try sema.namespaceLookup(
1767617676 block,
1767717677 src,
17678 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17678 type_info_ty.getNamespaceIndex(mod),
1767917679 try ip.getOrPutString(gpa, "Float"),
1768017680 )).?;
1768117681 try sema.ensureDeclAnalyzed(float_info_decl_index);
......@@ -17707,7 +17707,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1770717707 const decl_index = (try sema.namespaceLookup(
1770817708 block,
1770917709 src,
17710 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod).unwrap().?,
17710 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod),
1771117711 try ip.getOrPutString(gpa, "Pointer"),
1771217712 )).?;
1771317713 try sema.ensureDeclAnalyzed(decl_index);
......@@ -17718,7 +17718,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1771817718 const decl_index = (try sema.namespaceLookup(
1771917719 block,
1772017720 src,
17721 pointer_ty.getNamespaceIndex(mod).unwrap().?,
17721 pointer_ty.getNamespaceIndex(mod),
1772217722 try ip.getOrPutString(gpa, "Size"),
1772317723 )).?;
1772417724 try sema.ensureDeclAnalyzed(decl_index);
......@@ -17761,7 +17761,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1776117761 const array_field_ty_decl_index = (try sema.namespaceLookup(
1776217762 block,
1776317763 src,
17764 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17764 type_info_ty.getNamespaceIndex(mod),
1776517765 try ip.getOrPutString(gpa, "Array"),
1776617766 )).?;
1776717767 try sema.ensureDeclAnalyzed(array_field_ty_decl_index);
......@@ -17792,7 +17792,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1779217792 const vector_field_ty_decl_index = (try sema.namespaceLookup(
1779317793 block,
1779417794 src,
17795 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17795 type_info_ty.getNamespaceIndex(mod),
1779617796 try ip.getOrPutString(gpa, "Vector"),
1779717797 )).?;
1779817798 try sema.ensureDeclAnalyzed(vector_field_ty_decl_index);
......@@ -17821,7 +17821,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1782117821 const optional_field_ty_decl_index = (try sema.namespaceLookup(
1782217822 block,
1782317823 src,
17824 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17824 type_info_ty.getNamespaceIndex(mod),
1782517825 try ip.getOrPutString(gpa, "Optional"),
1782617826 )).?;
1782717827 try sema.ensureDeclAnalyzed(optional_field_ty_decl_index);
......@@ -17848,7 +17848,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1784817848 const set_field_ty_decl_index = (try sema.namespaceLookup(
1784917849 block,
1785017850 src,
17851 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17851 type_info_ty.getNamespaceIndex(mod),
1785217852 try ip.getOrPutString(gpa, "Error"),
1785317853 )).?;
1785417854 try sema.ensureDeclAnalyzed(set_field_ty_decl_index);
......@@ -17953,7 +17953,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1795317953 const error_union_field_ty_decl_index = (try sema.namespaceLookup(
1795417954 block,
1795517955 src,
17956 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17956 type_info_ty.getNamespaceIndex(mod),
1795717957 try ip.getOrPutString(gpa, "ErrorUnion"),
1795817958 )).?;
1795917959 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
1798317983 const enum_field_ty_decl_index = (try sema.namespaceLookup(
1798417984 block,
1798517985 src,
17986 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17986 type_info_ty.getNamespaceIndex(mod),
1798717987 try ip.getOrPutString(gpa, "EnumField"),
1798817988 )).?;
1798917989 try sema.ensureDeclAnalyzed(enum_field_ty_decl_index);
......@@ -18074,7 +18074,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1807418074 const type_enum_ty_decl_index = (try sema.namespaceLookup(
1807518075 block,
1807618076 src,
18077 type_info_ty.getNamespaceIndex(mod).unwrap().?,
18077 type_info_ty.getNamespaceIndex(mod),
1807818078 try ip.getOrPutString(gpa, "Enum"),
1807918079 )).?;
1808018080 try sema.ensureDeclAnalyzed(type_enum_ty_decl_index);
......@@ -18106,7 +18106,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1810618106 const type_union_ty_decl_index = (try sema.namespaceLookup(
1810718107 block,
1810818108 src,
18109 type_info_ty.getNamespaceIndex(mod).unwrap().?,
18109 type_info_ty.getNamespaceIndex(mod),
1811018110 try ip.getOrPutString(gpa, "Union"),
1811118111 )).?;
1811218112 try sema.ensureDeclAnalyzed(type_union_ty_decl_index);
......@@ -18118,7 +18118,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1811818118 const union_field_ty_decl_index = (try sema.namespaceLookup(
1811918119 block,
1812018120 src,
18121 type_info_ty.getNamespaceIndex(mod).unwrap().?,
18121 type_info_ty.getNamespaceIndex(mod),
1812218122 try ip.getOrPutString(gpa, "UnionField"),
1812318123 )).?;
1812418124 try sema.ensureDeclAnalyzed(union_field_ty_decl_index);
......@@ -18220,7 +18220,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1822018220 const decl_index = (try sema.namespaceLookup(
1822118221 block,
1822218222 src,
18223 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod).unwrap().?,
18223 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod),
1822418224 try ip.getOrPutString(gpa, "ContainerLayout"),
1822518225 )).?;
1822618226 try sema.ensureDeclAnalyzed(decl_index);
......@@ -18253,7 +18253,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1825318253 const type_struct_ty_decl_index = (try sema.namespaceLookup(
1825418254 block,
1825518255 src,
18256 type_info_ty.getNamespaceIndex(mod).unwrap().?,
18256 type_info_ty.getNamespaceIndex(mod),
1825718257 try ip.getOrPutString(gpa, "Struct"),
1825818258 )).?;
1825918259 try sema.ensureDeclAnalyzed(type_struct_ty_decl_index);
......@@ -18265,7 +18265,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1826518265 const struct_field_ty_decl_index = (try sema.namespaceLookup(
1826618266 block,
1826718267 src,
18268 type_info_ty.getNamespaceIndex(mod).unwrap().?,
18268 type_info_ty.getNamespaceIndex(mod),
1826918269 try ip.getOrPutString(gpa, "StructField"),
1827018270 )).?;
1827118271 try sema.ensureDeclAnalyzed(struct_field_ty_decl_index);
......@@ -18450,7 +18450,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1845018450 const decl_index = (try sema.namespaceLookup(
1845118451 block,
1845218452 src,
18453 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod).unwrap().?,
18453 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod),
1845418454 try ip.getOrPutString(gpa, "ContainerLayout"),
1845518455 )).?;
1845618456 try sema.ensureDeclAnalyzed(decl_index);
......@@ -18486,7 +18486,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1848618486 const type_opaque_ty_decl_index = (try sema.namespaceLookup(
1848718487 block,
1848818488 src,
18489 type_info_ty.getNamespaceIndex(mod).unwrap().?,
18489 type_info_ty.getNamespaceIndex(mod),
1849018490 try ip.getOrPutString(gpa, "Opaque"),
1849118491 )).?;
1849218492 try sema.ensureDeclAnalyzed(type_opaque_ty_decl_index);
......@@ -18529,7 +18529,7 @@ fn typeInfoDecls(
1852918529 const declaration_ty_decl_index = (try sema.namespaceLookup(
1853018530 block,
1853118531 src,
18532 type_info_ty.getNamespaceIndex(mod).unwrap().?,
18532 type_info_ty.getNamespaceIndex(mod),
1853318533 try mod.intern_pool.getOrPutString(gpa, "Declaration"),
1853418534 )).?;
1853518535 try sema.ensureDeclAnalyzed(declaration_ty_decl_index);
......@@ -18544,10 +18544,7 @@ fn typeInfoDecls(
1854418544 var seen_namespaces = std.AutoHashMap(*Namespace, void).init(gpa);
1854518545 defer seen_namespaces.deinit();
1854618546
18547 if (opt_namespace.unwrap()) |namespace_index| {
18548 const namespace = mod.namespacePtr(namespace_index);
18549 try sema.typeInfoNamespaceDecls(block, namespace, declaration_ty, &decl_vals, &seen_namespaces);
18550 }
18547 try sema.typeInfoNamespaceDecls(block, opt_namespace, declaration_ty, &decl_vals, &seen_namespaces);
1855118548
1855218549 const array_decl_ty = try mod.arrayType(.{
1855318550 .len = decl_vals.items.len,
......@@ -18580,23 +18577,27 @@ fn typeInfoDecls(
1858018577fn typeInfoNamespaceDecls(
1858118578 sema: *Sema,
1858218579 block: *Block,
18583 namespace: *Namespace,
18580 opt_namespace_index: InternPool.OptionalNamespaceIndex,
1858418581 declaration_ty: Type,
1858518582 decl_vals: *std.ArrayList(InternPool.Index),
1858618583 seen_namespaces: *std.AutoHashMap(*Namespace, void),
1858718584) !void {
1858818585 const mod = sema.mod;
1858918586 const ip = &mod.intern_pool;
18587
18588 const namespace_index = opt_namespace_index.unwrap() orelse return;
18589 const namespace = mod.namespacePtr(namespace_index);
18590
1859018591 const gop = try seen_namespaces.getOrPut(namespace);
1859118592 if (gop.found_existing) return;
18593
1859218594 const decls = namespace.decls.keys();
1859318595 for (decls) |decl_index| {
1859418596 const decl = mod.declPtr(decl_index);
1859518597 if (decl.kind == .@"usingnamespace") {
1859618598 if (decl.analysis == .in_progress) continue;
1859718599 try mod.ensureDeclAnalyzed(decl_index);
18598 const new_ns = decl.val.toType().getNamespace(mod).?;
18599 try sema.typeInfoNamespaceDecls(block, new_ns, declaration_ty, decl_vals, seen_namespaces);
18600 try sema.typeInfoNamespaceDecls(block, decl.val.toType().getNamespaceIndex(mod), declaration_ty, decl_vals, seen_namespaces);
1860018601 continue;
1860118602 }
1860218603 if (decl.kind != .named or !decl.is_pub) continue;
......@@ -26585,7 +26586,7 @@ fn preparePanicId(sema: *Sema, block: *Block, panic_id: Module.PanicId) !InternP
2658526586 const msg_decl_index = (sema.namespaceLookup(
2658626587 block,
2658726588 .unneeded,
26588 panic_messages_ty.getNamespaceIndex(mod).unwrap().?,
26589 panic_messages_ty.getNamespaceIndex(mod),
2658926590 try mod.intern_pool.getOrPutString(gpa, @tagName(panic_id)),
2659026591 ) catch |err| switch (err) {
2659126592 error.AnalysisFail, error.NeededSourceLocation => @panic("std.builtin.panic_messages is corrupt"),
......@@ -27004,10 +27005,8 @@ fn fieldVal(
2700427005 } })));
2700527006 },
2700627007 .Union => {
27007 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
27008 if (try sema.namespaceLookupVal(block, src, namespace, field_name)) |inst| {
27009 return inst;
27010 }
27008 if (try sema.namespaceLookupVal(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27009 return inst;
2701127010 }
2701227011 try sema.resolveTypeFields(child_type);
2701327012 if (child_type.unionTagType(mod)) |enum_ty| {
......@@ -27019,10 +27018,8 @@ fn fieldVal(
2701927018 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
2702027019 },
2702127020 .Enum => {
27022 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
27023 if (try sema.namespaceLookupVal(block, src, namespace, field_name)) |inst| {
27024 return inst;
27025 }
27021 if (try sema.namespaceLookupVal(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27022 return inst;
2702627023 }
2702727024 const field_index_usize = child_type.enumFieldIndex(field_name, mod) orelse
2702827025 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
......@@ -27031,10 +27028,8 @@ fn fieldVal(
2703127028 return Air.internedToRef(enum_val.toIntern());
2703227029 },
2703327030 .Struct, .Opaque => {
27034 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
27035 if (try sema.namespaceLookupVal(block, src, namespace, field_name)) |inst| {
27036 return inst;
27037 }
27031 if (try sema.namespaceLookupVal(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27032 return inst;
2703827033 }
2703927034 return sema.failWithBadMemberAccess(block, child_type, src, field_name);
2704027035 },
......@@ -27230,10 +27225,8 @@ fn fieldPtr(
2723027225 } }));
2723127226 },
2723227227 .Union => {
27233 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
27234 if (try sema.namespaceLookupRef(block, src, namespace, field_name)) |inst| {
27235 return inst;
27236 }
27228 if (try sema.namespaceLookupRef(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27229 return inst;
2723727230 }
2723827231 try sema.resolveTypeFields(child_type);
2723927232 if (child_type.unionTagType(mod)) |enum_ty| {
......@@ -27246,10 +27239,8 @@ fn fieldPtr(
2724627239 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
2724727240 },
2724827241 .Enum => {
27249 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
27250 if (try sema.namespaceLookupRef(block, src, namespace, field_name)) |inst| {
27251 return inst;
27252 }
27242 if (try sema.namespaceLookupRef(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27243 return inst;
2725327244 }
2725427245 const field_index = child_type.enumFieldIndex(field_name, mod) orelse {
2725527246 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
......@@ -27259,10 +27250,8 @@ fn fieldPtr(
2725927250 return anonDeclRef(sema, idx_val.toIntern());
2726027251 },
2726127252 .Struct, .Opaque => {
27262 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
27263 if (try sema.namespaceLookupRef(block, src, namespace, field_name)) |inst| {
27264 return inst;
27265 }
27253 if (try sema.namespaceLookupRef(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27254 return inst;
2726627255 }
2726727256 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
2726827257 },
......@@ -27375,73 +27364,68 @@ fn fieldCallBind(
2737527364 }
2737627365
2737727366 // 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)) {
27379 .Struct, .Opaque, .Union, .Enum => found_decl: {
27380 if (concrete_ty.getNamespaceIndex(mod).unwrap()) |namespace| {
27381 if (try sema.namespaceLookup(block, src, namespace, field_name)) |decl_idx| {
27382 try sema.addReferencedBy(block, src, decl_idx);
27383 const decl_val = try sema.analyzeDeclVal(block, src, decl_idx);
27384 const decl_type = sema.typeOf(decl_val);
27385 if (mod.typeToFunc(decl_type)) |func_type| f: {
27386 if (func_type.param_types.len == 0) break :f;
27387
27388 const first_param_type = Type.fromInterned(func_type.param_types.get(ip)[0]);
27389 // zig fmt: off
27390 if (first_param_type.isGenericPoison() or (
27391 first_param_type.zigTypeTag(mod) == .Pointer and
27392 (first_param_type.ptrSize(mod) == .One or
27393 first_param_type.ptrSize(mod) == .C) and
27394 first_param_type.childType(mod).eql(concrete_ty, mod)))
27395 {
27396 // zig fmt: on
27397 // 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 we
27399 // can safely assume it can be a pointer.
27400 // TODO: bound fn calls on rvalues should probably
27401 // generate a by-value argument somehow.
27402 return .{ .method = .{
27403 .func_inst = decl_val,
27404 .arg0_inst = object_ptr,
27405 } };
27406 } else if (first_param_type.eql(concrete_ty, mod)) {
27407 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
27408 return .{ .method = .{
27409 .func_inst = decl_val,
27410 .arg0_inst = deref,
27411 } };
27412 } else if (first_param_type.zigTypeTag(mod) == .Optional) {
27413 const child = first_param_type.optionalChild(mod);
27414 if (child.eql(concrete_ty, mod)) {
27415 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
27416 return .{ .method = .{
27417 .func_inst = decl_val,
27418 .arg0_inst = deref,
27419 } };
27420 } else if (child.zigTypeTag(mod) == .Pointer and
27421 child.ptrSize(mod) == .One and
27422 child.childType(mod).eql(concrete_ty, mod))
27423 {
27424 return .{ .method = .{
27425 .func_inst = decl_val,
27426 .arg0_inst = object_ptr,
27427 } };
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;
27367 const found_decl = found_decl: {
27368 const namespace = concrete_ty.getNamespace(mod) orelse
27369 break :found_decl null;
27370 const decl_idx = (try sema.namespaceLookup(block, src, namespace, field_name)) orelse
27371 break :found_decl null;
27372
27373 try sema.addReferencedBy(block, src, decl_idx);
27374 const decl_val = try sema.analyzeDeclVal(block, src, decl_idx);
27375 const decl_type = sema.typeOf(decl_val);
27376 if (mod.typeToFunc(decl_type)) |func_type| f: {
27377 if (func_type.param_types.len == 0) break :f;
27378
27379 const first_param_type = Type.fromInterned(func_type.param_types.get(ip)[0]);
27380 if (first_param_type.isGenericPoison() or
27381 (first_param_type.zigTypeTag(mod) == .Pointer and
27382 (first_param_type.ptrSize(mod) == .One or
27383 first_param_type.ptrSize(mod) == .C) and
27384 first_param_type.childType(mod).eql(concrete_ty, mod)))
27385 {
27386 // Note that if the param type is generic poison, we know that it must
27387 // specifically be `anytype` since it's the first parameter, meaning we
27388 // can safely assume it can be a pointer.
27389 // TODO: bound fn calls on rvalues should probably
27390 // generate a by-value argument somehow.
27391 return .{ .method = .{
27392 .func_inst = decl_val,
27393 .arg0_inst = object_ptr,
27394 } };
27395 } else if (first_param_type.eql(concrete_ty, mod)) {
27396 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
27397 return .{ .method = .{
27398 .func_inst = decl_val,
27399 .arg0_inst = deref,
27400 } };
27401 } else if (first_param_type.zigTypeTag(mod) == .Optional) {
27402 const child = first_param_type.optionalChild(mod);
27403 if (child.eql(concrete_ty, mod)) {
27404 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
27405 return .{ .method = .{
27406 .func_inst = decl_val,
27407 .arg0_inst = deref,
27408 } };
27409 } else if (child.zigTypeTag(mod) == .Pointer and
27410 child.ptrSize(mod) == .One and
27411 child.childType(mod).eql(concrete_ty, mod))
27412 {
27413 return .{ .method = .{
27414 .func_inst = decl_val,
27415 .arg0_inst = object_ptr,
27416 } };
2744027417 }
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 } };
2744127426 }
27442 break :found_decl null;
27443 },
27444 else => null,
27427 }
27428 break :found_decl decl_idx;
2744527429 };
2744627430
2744727431 const msg = msg: {
......@@ -27507,12 +27491,12 @@ fn namespaceLookup(
2750727491 sema: *Sema,
2750827492 block: *Block,
2750927493 src: LazySrcLoc,
27510 namespace: InternPool.NamespaceIndex,
27494 opt_namespace: InternPool.OptionalNamespaceIndex,
2751127495 decl_name: InternPool.NullTerminatedString,
2751227496) CompileError!?InternPool.DeclIndex {
2751327497 const mod = sema.mod;
2751427498 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| {
2751627500 const decl = mod.declPtr(decl_index);
2751727501 if (!decl.is_pub and decl.getFileScope(mod) != block.getFileScope(mod)) {
2751827502 const msg = msg: {
......@@ -27534,10 +27518,10 @@ fn namespaceLookupRef(
2753427518 sema: *Sema,
2753527519 block: *Block,
2753627520 src: LazySrcLoc,
27537 namespace: InternPool.NamespaceIndex,
27521 opt_namespace: InternPool.OptionalNamespaceIndex,
2753827522 decl_name: InternPool.NullTerminatedString,
2753927523) 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;
2754127525 try sema.addReferencedBy(block, src, decl);
2754227526 return try sema.analyzeDeclRef(decl);
2754327527}
......@@ -27546,10 +27530,10 @@ fn namespaceLookupVal(
2754627530 sema: *Sema,
2754727531 block: *Block,
2754827532 src: LazySrcLoc,
27549 namespace: InternPool.NamespaceIndex,
27533 opt_namespace: InternPool.OptionalNamespaceIndex,
2755027534 decl_name: InternPool.NullTerminatedString,
2755127535) 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;
2755327537 return try sema.analyzeDeclVal(block, src, decl);
2755427538}
2755527539
......@@ -37602,7 +37586,7 @@ fn getBuiltinDecl(sema: *Sema, block: *Block, name: []const u8) CompileError!Int
3760237586 const opt_builtin_inst = (try sema.namespaceLookupRef(
3760337587 block,
3760437588 src,
37605 mod.declPtr(std_file.root_decl.unwrap().?).src_namespace,
37589 mod.declPtr(std_file.root_decl.unwrap().?).src_namespace.toOptional(),
3760637590 try ip.getOrPutString(gpa, "builtin"),
3760737591 )) orelse @panic("lib/std.zig is corrupt and missing 'builtin'");
3760837592 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
3761337597 const decl_index = (try sema.namespaceLookup(
3761437598 block,
3761537599 src,
37616 builtin_ty.getNamespaceIndex(mod).unwrap().?,
37600 builtin_ty.getNamespaceIndex(mod),
3761737601 try ip.getOrPutString(gpa, name),
3761837602 )) orelse std.debug.panic("lib/std/builtin.zig is corrupt and missing '{s}'", .{name});
3761937603 return decl_index;
src/codegen/llvm.zig+1-1
......@@ -2852,7 +2852,7 @@ pub const Object = struct {
28522852 const stack_trace_str = try mod.intern_pool.getOrPutString(mod.gpa, "StackTrace");
28532853 // buffer is only used for int_type, `builtin` is a struct.
28542854 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)).?;
28562856 const stack_trace_decl_index = builtin_namespace.decls.getKeyAdapted(stack_trace_str, Module.DeclAdapter{ .zcu = mod }).?;
28572857 const stack_trace_decl = mod.declPtr(stack_trace_decl_index);
28582858
src/type.zig+26-8
......@@ -2845,22 +2845,40 @@ pub const Type = struct {
28452845 };
28462846 }
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
28482853 /// Returns null if the type has no namespace.
2849 pub fn getNamespaceIndex(ty: Type, mod: *Module) InternPool.OptionalNamespaceIndex {
2850 const ip = &mod.intern_pool;
2854 pub fn getNamespace(ty: Type, zcu: *Zcu) ?InternPool.OptionalNamespaceIndex {
2855 const ip = &zcu.intern_pool;
28512856 return switch (ip.indexToKey(ty.toIntern())) {
28522857 .opaque_type => ip.loadOpaqueType(ty.toIntern()).namespace,
28532858 .struct_type => ip.loadStructType(ty.toIntern()).namespace,
28542859 .union_type => ip.loadUnionType(ty.toIntern()).namespace,
28552860 .enum_type => ip.loadEnumType(ty.toIntern()).namespace,
28562861
2857 else => .none,
2858 };
2859 }
2862 .anon_struct_type => .none,
2863 .simple_type => |s| switch (s) {
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.
2862 pub fn getNamespace(ty: Type, mod: *Module) ?*Module.Namespace {
2863 return if (getNamespaceIndex(ty, mod).unwrap()) |i| mod.namespacePtr(i) else null;
2880 else => null,
2881 };
28642882 }
28652883
28662884 // Works for vectors and vectors of integers.
test/behavior/usingnamespace.zig+9
......@@ -97,3 +97,12 @@ test "container member access usingnamespace decls" {
9797 var foo = Bar{};
9898 foo.two();
9999}
100
101usingnamespace opaque {};
102
103usingnamespace @Type(.{ .Struct = .{
104 .layout = .auto,
105 .fields = &.{},
106 .decls = &.{},
107 .is_tuple = false,
108} });