authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-03-17 15:26:55-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-03-17 15:26:55-07:00
log95cb93944060d04ec49e9d2e21ef911ad2b09ccd
tree5b9839319d7d3258613a7367685c9aab1b6113f3
parentc11b6adf13fe5c765ec480af5bad6338e6982a9d
parent436c72e89a6e402b6920ab03207b95d0ca709ee9
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #19333 from Vexu/fixes

Miscellaneous error fixes

7 files changed, 186 insertions(+), 149 deletions(-)

src/Module.zig+5-2
......@@ -1897,8 +1897,11 @@ pub const SrcLoc = struct {
18971897 const parent_node = src_loc.declRelativeToNodeIndex(node_off);
18981898
18991899 var buf: [2]Ast.Node.Index = undefined;
1900 const full = tree.fullArrayInit(&buf, parent_node).?;
1901 return tree.nodeToSpan(full.ast.type_expr);
1900 const type_expr = if (tree.fullArrayInit(&buf, parent_node)) |array_init|
1901 array_init.ast.type_expr
1902 else
1903 tree.fullStructInit(&buf, parent_node).?.ast.type_expr;
1904 return tree.nodeToSpan(type_expr);
19021905 },
19031906 .node_offset_store_ptr => |node_off| {
19041907 const tree = try src_loc.file_scope.getTree(gpa);
src/Sema.zig+122-137
......@@ -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 }
......@@ -9917,7 +9918,7 @@ fn zirParam(
99179918 .is_comptime = comptime_syntax,
99189919 .name = param_name,
99199920 });
9920 sema.inst_map.putAssumeCapacityNoClobber(inst, .generic_poison);
9921 sema.inst_map.putAssumeCapacity(inst, .generic_poison);
99219922 return;
99229923 },
99239924 else => |e| return e,
......@@ -9934,7 +9935,7 @@ fn zirParam(
99349935 .is_comptime = comptime_syntax,
99359936 .name = param_name,
99369937 });
9937 sema.inst_map.putAssumeCapacityNoClobber(inst, .generic_poison);
9938 sema.inst_map.putAssumeCapacity(inst, .generic_poison);
99389939 return;
99399940 },
99409941 else => |e| return e,
......@@ -9949,7 +9950,7 @@ fn zirParam(
99499950 if (is_comptime) {
99509951 // If this is a comptime parameter we can add a constant generic_poison
99519952 // since this is also a generic parameter.
9952 sema.inst_map.putAssumeCapacityNoClobber(inst, .generic_poison);
9953 sema.inst_map.putAssumeCapacity(inst, .generic_poison);
99539954 } else {
99549955 // Otherwise we need a dummy runtime instruction.
99559956 const result_index: Air.Inst.Index = @enumFromInt(sema.air_instructions.len);
......@@ -9957,7 +9958,7 @@ fn zirParam(
99579958 .tag = .alloc,
99589959 .data = .{ .ty = param_ty },
99599960 });
9960 sema.inst_map.putAssumeCapacityNoClobber(inst, result_index.toRef());
9961 sema.inst_map.putAssumeCapacity(inst, result_index.toRef());
99619962 }
99629963}
99639964
......@@ -13699,8 +13700,7 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1369913700 } });
1370013701 }
1370113702
13702 const namespace = container_type.getNamespaceIndex(mod).unwrap() orelse
13703 return .bool_false;
13703 const namespace = container_type.getNamespaceIndex(mod);
1370413704 if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl_index| {
1370513705 const decl = mod.declPtr(decl_index);
1370613706 if (decl.is_pub or decl.getFileScope(mod) == block.getFileScope(mod)) {
......@@ -17534,7 +17534,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1753417534 const fn_info_decl_index = (try sema.namespaceLookup(
1753517535 block,
1753617536 src,
17537 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17537 type_info_ty.getNamespaceIndex(mod),
1753817538 try ip.getOrPutString(gpa, "Fn"),
1753917539 )).?;
1754017540 try sema.ensureDeclAnalyzed(fn_info_decl_index);
......@@ -17544,7 +17544,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1754417544 const param_info_decl_index = (try sema.namespaceLookup(
1754517545 block,
1754617546 src,
17547 fn_info_ty.getNamespaceIndex(mod).unwrap().?,
17547 fn_info_ty.getNamespaceIndex(mod),
1754817548 try ip.getOrPutString(gpa, "Param"),
1754917549 )).?;
1755017550 try sema.ensureDeclAnalyzed(param_info_decl_index);
......@@ -17644,7 +17644,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1764417644 const int_info_decl_index = (try sema.namespaceLookup(
1764517645 block,
1764617646 src,
17647 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17647 type_info_ty.getNamespaceIndex(mod),
1764817648 try ip.getOrPutString(gpa, "Int"),
1764917649 )).?;
1765017650 try sema.ensureDeclAnalyzed(int_info_decl_index);
......@@ -17672,7 +17672,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1767217672 const float_info_decl_index = (try sema.namespaceLookup(
1767317673 block,
1767417674 src,
17675 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17675 type_info_ty.getNamespaceIndex(mod),
1767617676 try ip.getOrPutString(gpa, "Float"),
1767717677 )).?;
1767817678 try sema.ensureDeclAnalyzed(float_info_decl_index);
......@@ -17704,7 +17704,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1770417704 const decl_index = (try sema.namespaceLookup(
1770517705 block,
1770617706 src,
17707 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod).unwrap().?,
17707 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod),
1770817708 try ip.getOrPutString(gpa, "Pointer"),
1770917709 )).?;
1771017710 try sema.ensureDeclAnalyzed(decl_index);
......@@ -17715,7 +17715,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1771517715 const decl_index = (try sema.namespaceLookup(
1771617716 block,
1771717717 src,
17718 pointer_ty.getNamespaceIndex(mod).unwrap().?,
17718 pointer_ty.getNamespaceIndex(mod),
1771917719 try ip.getOrPutString(gpa, "Size"),
1772017720 )).?;
1772117721 try sema.ensureDeclAnalyzed(decl_index);
......@@ -17758,7 +17758,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1775817758 const array_field_ty_decl_index = (try sema.namespaceLookup(
1775917759 block,
1776017760 src,
17761 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17761 type_info_ty.getNamespaceIndex(mod),
1776217762 try ip.getOrPutString(gpa, "Array"),
1776317763 )).?;
1776417764 try sema.ensureDeclAnalyzed(array_field_ty_decl_index);
......@@ -17789,7 +17789,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1778917789 const vector_field_ty_decl_index = (try sema.namespaceLookup(
1779017790 block,
1779117791 src,
17792 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17792 type_info_ty.getNamespaceIndex(mod),
1779317793 try ip.getOrPutString(gpa, "Vector"),
1779417794 )).?;
1779517795 try sema.ensureDeclAnalyzed(vector_field_ty_decl_index);
......@@ -17818,7 +17818,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1781817818 const optional_field_ty_decl_index = (try sema.namespaceLookup(
1781917819 block,
1782017820 src,
17821 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17821 type_info_ty.getNamespaceIndex(mod),
1782217822 try ip.getOrPutString(gpa, "Optional"),
1782317823 )).?;
1782417824 try sema.ensureDeclAnalyzed(optional_field_ty_decl_index);
......@@ -17845,7 +17845,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1784517845 const set_field_ty_decl_index = (try sema.namespaceLookup(
1784617846 block,
1784717847 src,
17848 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17848 type_info_ty.getNamespaceIndex(mod),
1784917849 try ip.getOrPutString(gpa, "Error"),
1785017850 )).?;
1785117851 try sema.ensureDeclAnalyzed(set_field_ty_decl_index);
......@@ -17950,7 +17950,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1795017950 const error_union_field_ty_decl_index = (try sema.namespaceLookup(
1795117951 block,
1795217952 src,
17953 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17953 type_info_ty.getNamespaceIndex(mod),
1795417954 try ip.getOrPutString(gpa, "ErrorUnion"),
1795517955 )).?;
1795617956 try sema.ensureDeclAnalyzed(error_union_field_ty_decl_index);
......@@ -17980,7 +17980,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1798017980 const enum_field_ty_decl_index = (try sema.namespaceLookup(
1798117981 block,
1798217982 src,
17983 type_info_ty.getNamespaceIndex(mod).unwrap().?,
17983 type_info_ty.getNamespaceIndex(mod),
1798417984 try ip.getOrPutString(gpa, "EnumField"),
1798517985 )).?;
1798617986 try sema.ensureDeclAnalyzed(enum_field_ty_decl_index);
......@@ -18071,7 +18071,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1807118071 const type_enum_ty_decl_index = (try sema.namespaceLookup(
1807218072 block,
1807318073 src,
18074 type_info_ty.getNamespaceIndex(mod).unwrap().?,
18074 type_info_ty.getNamespaceIndex(mod),
1807518075 try ip.getOrPutString(gpa, "Enum"),
1807618076 )).?;
1807718077 try sema.ensureDeclAnalyzed(type_enum_ty_decl_index);
......@@ -18103,7 +18103,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1810318103 const type_union_ty_decl_index = (try sema.namespaceLookup(
1810418104 block,
1810518105 src,
18106 type_info_ty.getNamespaceIndex(mod).unwrap().?,
18106 type_info_ty.getNamespaceIndex(mod),
1810718107 try ip.getOrPutString(gpa, "Union"),
1810818108 )).?;
1810918109 try sema.ensureDeclAnalyzed(type_union_ty_decl_index);
......@@ -18115,7 +18115,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1811518115 const union_field_ty_decl_index = (try sema.namespaceLookup(
1811618116 block,
1811718117 src,
18118 type_info_ty.getNamespaceIndex(mod).unwrap().?,
18118 type_info_ty.getNamespaceIndex(mod),
1811918119 try ip.getOrPutString(gpa, "UnionField"),
1812018120 )).?;
1812118121 try sema.ensureDeclAnalyzed(union_field_ty_decl_index);
......@@ -18217,7 +18217,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1821718217 const decl_index = (try sema.namespaceLookup(
1821818218 block,
1821918219 src,
18220 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod).unwrap().?,
18220 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod),
1822118221 try ip.getOrPutString(gpa, "ContainerLayout"),
1822218222 )).?;
1822318223 try sema.ensureDeclAnalyzed(decl_index);
......@@ -18250,7 +18250,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1825018250 const type_struct_ty_decl_index = (try sema.namespaceLookup(
1825118251 block,
1825218252 src,
18253 type_info_ty.getNamespaceIndex(mod).unwrap().?,
18253 type_info_ty.getNamespaceIndex(mod),
1825418254 try ip.getOrPutString(gpa, "Struct"),
1825518255 )).?;
1825618256 try sema.ensureDeclAnalyzed(type_struct_ty_decl_index);
......@@ -18262,7 +18262,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1826218262 const struct_field_ty_decl_index = (try sema.namespaceLookup(
1826318263 block,
1826418264 src,
18265 type_info_ty.getNamespaceIndex(mod).unwrap().?,
18265 type_info_ty.getNamespaceIndex(mod),
1826618266 try ip.getOrPutString(gpa, "StructField"),
1826718267 )).?;
1826818268 try sema.ensureDeclAnalyzed(struct_field_ty_decl_index);
......@@ -18447,7 +18447,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1844718447 const decl_index = (try sema.namespaceLookup(
1844818448 block,
1844918449 src,
18450 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod).unwrap().?,
18450 (try sema.getBuiltinType("Type")).getNamespaceIndex(mod),
1845118451 try ip.getOrPutString(gpa, "ContainerLayout"),
1845218452 )).?;
1845318453 try sema.ensureDeclAnalyzed(decl_index);
......@@ -18483,7 +18483,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1848318483 const type_opaque_ty_decl_index = (try sema.namespaceLookup(
1848418484 block,
1848518485 src,
18486 type_info_ty.getNamespaceIndex(mod).unwrap().?,
18486 type_info_ty.getNamespaceIndex(mod),
1848718487 try ip.getOrPutString(gpa, "Opaque"),
1848818488 )).?;
1848918489 try sema.ensureDeclAnalyzed(type_opaque_ty_decl_index);
......@@ -18526,7 +18526,7 @@ fn typeInfoDecls(
1852618526 const declaration_ty_decl_index = (try sema.namespaceLookup(
1852718527 block,
1852818528 src,
18529 type_info_ty.getNamespaceIndex(mod).unwrap().?,
18529 type_info_ty.getNamespaceIndex(mod),
1853018530 try mod.intern_pool.getOrPutString(gpa, "Declaration"),
1853118531 )).?;
1853218532 try sema.ensureDeclAnalyzed(declaration_ty_decl_index);
......@@ -18541,10 +18541,7 @@ fn typeInfoDecls(
1854118541 var seen_namespaces = std.AutoHashMap(*Namespace, void).init(gpa);
1854218542 defer seen_namespaces.deinit();
1854318543
18544 if (opt_namespace.unwrap()) |namespace_index| {
18545 const namespace = mod.namespacePtr(namespace_index);
18546 try sema.typeInfoNamespaceDecls(block, namespace, declaration_ty, &decl_vals, &seen_namespaces);
18547 }
18544 try sema.typeInfoNamespaceDecls(block, opt_namespace, declaration_ty, &decl_vals, &seen_namespaces);
1854818545
1854918546 const array_decl_ty = try mod.arrayType(.{
1855018547 .len = decl_vals.items.len,
......@@ -18577,23 +18574,27 @@ fn typeInfoDecls(
1857718574fn typeInfoNamespaceDecls(
1857818575 sema: *Sema,
1857918576 block: *Block,
18580 namespace: *Namespace,
18577 opt_namespace_index: InternPool.OptionalNamespaceIndex,
1858118578 declaration_ty: Type,
1858218579 decl_vals: *std.ArrayList(InternPool.Index),
1858318580 seen_namespaces: *std.AutoHashMap(*Namespace, void),
1858418581) !void {
1858518582 const mod = sema.mod;
1858618583 const ip = &mod.intern_pool;
18584
18585 const namespace_index = opt_namespace_index.unwrap() orelse return;
18586 const namespace = mod.namespacePtr(namespace_index);
18587
1858718588 const gop = try seen_namespaces.getOrPut(namespace);
1858818589 if (gop.found_existing) return;
18590
1858918591 const decls = namespace.decls.keys();
1859018592 for (decls) |decl_index| {
1859118593 const decl = mod.declPtr(decl_index);
1859218594 if (decl.kind == .@"usingnamespace") {
1859318595 if (decl.analysis == .in_progress) continue;
1859418596 try mod.ensureDeclAnalyzed(decl_index);
18595 const new_ns = decl.val.toType().getNamespace(mod).?;
18596 try sema.typeInfoNamespaceDecls(block, new_ns, declaration_ty, decl_vals, seen_namespaces);
18597 try sema.typeInfoNamespaceDecls(block, decl.val.toType().getNamespaceIndex(mod), declaration_ty, decl_vals, seen_namespaces);
1859718598 continue;
1859818599 }
1859918600 if (decl.kind != .named or !decl.is_pub) continue;
......@@ -19744,7 +19745,8 @@ fn zirStructInitEmpty(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
1974419745
1974519746 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
1974619747 const src = inst_data.src();
19747 const obj_ty = try sema.resolveType(block, src, inst_data.operand);
19748 const ty_src: LazySrcLoc = .{ .node_offset_init_ty = inst_data.src_node };
19749 const obj_ty = try sema.resolveType(block, ty_src, inst_data.operand);
1974819750 const mod = sema.mod;
1974919751
1975019752 switch (obj_ty.zigTypeTag(mod)) {
......@@ -26558,7 +26560,7 @@ fn preparePanicId(sema: *Sema, block: *Block, panic_id: Module.PanicId) !InternP
2655826560 const msg_decl_index = (sema.namespaceLookup(
2655926561 block,
2656026562 .unneeded,
26561 panic_messages_ty.getNamespaceIndex(mod).unwrap().?,
26563 panic_messages_ty.getNamespaceIndex(mod),
2656226564 try mod.intern_pool.getOrPutString(gpa, @tagName(panic_id)),
2656326565 ) catch |err| switch (err) {
2656426566 error.AnalysisFail, error.NeededSourceLocation => @panic("std.builtin.panic_messages is corrupt"),
......@@ -26977,10 +26979,8 @@ fn fieldVal(
2697726979 } })));
2697826980 },
2697926981 .Union => {
26980 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
26981 if (try sema.namespaceLookupVal(block, src, namespace, field_name)) |inst| {
26982 return inst;
26983 }
26982 if (try sema.namespaceLookupVal(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
26983 return inst;
2698426984 }
2698526985 try sema.resolveTypeFields(child_type);
2698626986 if (child_type.unionTagType(mod)) |enum_ty| {
......@@ -26992,10 +26992,8 @@ fn fieldVal(
2699226992 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
2699326993 },
2699426994 .Enum => {
26995 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
26996 if (try sema.namespaceLookupVal(block, src, namespace, field_name)) |inst| {
26997 return inst;
26998 }
26995 if (try sema.namespaceLookupVal(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
26996 return inst;
2699926997 }
2700026998 const field_index_usize = child_type.enumFieldIndex(field_name, mod) orelse
2700126999 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
......@@ -27004,10 +27002,8 @@ fn fieldVal(
2700427002 return Air.internedToRef(enum_val.toIntern());
2700527003 },
2700627004 .Struct, .Opaque => {
27007 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
27008 if (try sema.namespaceLookupVal(block, src, namespace, field_name)) |inst| {
27009 return inst;
27010 }
27005 if (try sema.namespaceLookupVal(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27006 return inst;
2701127007 }
2701227008 return sema.failWithBadMemberAccess(block, child_type, src, field_name);
2701327009 },
......@@ -27203,10 +27199,8 @@ fn fieldPtr(
2720327199 } }));
2720427200 },
2720527201 .Union => {
27206 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
27207 if (try sema.namespaceLookupRef(block, src, namespace, field_name)) |inst| {
27208 return inst;
27209 }
27202 if (try sema.namespaceLookupRef(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27203 return inst;
2721027204 }
2721127205 try sema.resolveTypeFields(child_type);
2721227206 if (child_type.unionTagType(mod)) |enum_ty| {
......@@ -27219,10 +27213,8 @@ fn fieldPtr(
2721927213 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
2722027214 },
2722127215 .Enum => {
27222 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
27223 if (try sema.namespaceLookupRef(block, src, namespace, field_name)) |inst| {
27224 return inst;
27225 }
27216 if (try sema.namespaceLookupRef(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27217 return inst;
2722627218 }
2722727219 const field_index = child_type.enumFieldIndex(field_name, mod) orelse {
2722827220 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
......@@ -27232,10 +27224,8 @@ fn fieldPtr(
2723227224 return anonDeclRef(sema, idx_val.toIntern());
2723327225 },
2723427226 .Struct, .Opaque => {
27235 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
27236 if (try sema.namespaceLookupRef(block, src, namespace, field_name)) |inst| {
27237 return inst;
27238 }
27227 if (try sema.namespaceLookupRef(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
27228 return inst;
2723927229 }
2724027230 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
2724127231 },
......@@ -27348,73 +27338,68 @@ fn fieldCallBind(
2734827338 }
2734927339
2735027340 // If we get here, we need to look for a decl in the struct type instead.
27351 const found_decl = switch (concrete_ty.zigTypeTag(mod)) {
27352 .Struct, .Opaque, .Union, .Enum => found_decl: {
27353 if (concrete_ty.getNamespaceIndex(mod).unwrap()) |namespace| {
27354 if (try sema.namespaceLookup(block, src, namespace, field_name)) |decl_idx| {
27355 try sema.addReferencedBy(block, src, decl_idx);
27356 const decl_val = try sema.analyzeDeclVal(block, src, decl_idx);
27357 const decl_type = sema.typeOf(decl_val);
27358 if (mod.typeToFunc(decl_type)) |func_type| f: {
27359 if (func_type.param_types.len == 0) break :f;
27360
27361 const first_param_type = Type.fromInterned(func_type.param_types.get(ip)[0]);
27362 // zig fmt: off
27363 if (first_param_type.isGenericPoison() or (
27364 first_param_type.zigTypeTag(mod) == .Pointer and
27365 (first_param_type.ptrSize(mod) == .One or
27366 first_param_type.ptrSize(mod) == .C) and
27367 first_param_type.childType(mod).eql(concrete_ty, mod)))
27368 {
27369 // zig fmt: on
27370 // Note that if the param type is generic poison, we know that it must
27371 // specifically be `anytype` since it's the first parameter, meaning we
27372 // can safely assume it can be a pointer.
27373 // TODO: bound fn calls on rvalues should probably
27374 // generate a by-value argument somehow.
27375 return .{ .method = .{
27376 .func_inst = decl_val,
27377 .arg0_inst = object_ptr,
27378 } };
27379 } else if (first_param_type.eql(concrete_ty, mod)) {
27380 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
27381 return .{ .method = .{
27382 .func_inst = decl_val,
27383 .arg0_inst = deref,
27384 } };
27385 } else if (first_param_type.zigTypeTag(mod) == .Optional) {
27386 const child = first_param_type.optionalChild(mod);
27387 if (child.eql(concrete_ty, mod)) {
27388 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
27389 return .{ .method = .{
27390 .func_inst = decl_val,
27391 .arg0_inst = deref,
27392 } };
27393 } else if (child.zigTypeTag(mod) == .Pointer and
27394 child.ptrSize(mod) == .One and
27395 child.childType(mod).eql(concrete_ty, mod))
27396 {
27397 return .{ .method = .{
27398 .func_inst = decl_val,
27399 .arg0_inst = object_ptr,
27400 } };
27401 }
27402 } else if (first_param_type.zigTypeTag(mod) == .ErrorUnion and
27403 first_param_type.errorUnionPayload(mod).eql(concrete_ty, mod))
27404 {
27405 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
27406 return .{ .method = .{
27407 .func_inst = decl_val,
27408 .arg0_inst = deref,
27409 } };
27410 }
27411 }
27412 break :found_decl decl_idx;
27341 const found_decl = found_decl: {
27342 const namespace = concrete_ty.getNamespace(mod) orelse
27343 break :found_decl null;
27344 const decl_idx = (try sema.namespaceLookup(block, src, namespace, field_name)) orelse
27345 break :found_decl null;
27346
27347 try sema.addReferencedBy(block, src, decl_idx);
27348 const decl_val = try sema.analyzeDeclVal(block, src, decl_idx);
27349 const decl_type = sema.typeOf(decl_val);
27350 if (mod.typeToFunc(decl_type)) |func_type| f: {
27351 if (func_type.param_types.len == 0) break :f;
27352
27353 const first_param_type = Type.fromInterned(func_type.param_types.get(ip)[0]);
27354 if (first_param_type.isGenericPoison() or
27355 (first_param_type.zigTypeTag(mod) == .Pointer and
27356 (first_param_type.ptrSize(mod) == .One or
27357 first_param_type.ptrSize(mod) == .C) and
27358 first_param_type.childType(mod).eql(concrete_ty, mod)))
27359 {
27360 // Note that if the param type is generic poison, we know that it must
27361 // specifically be `anytype` since it's the first parameter, meaning we
27362 // can safely assume it can be a pointer.
27363 // TODO: bound fn calls on rvalues should probably
27364 // generate a by-value argument somehow.
27365 return .{ .method = .{
27366 .func_inst = decl_val,
27367 .arg0_inst = object_ptr,
27368 } };
27369 } else if (first_param_type.eql(concrete_ty, mod)) {
27370 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
27371 return .{ .method = .{
27372 .func_inst = decl_val,
27373 .arg0_inst = deref,
27374 } };
27375 } else if (first_param_type.zigTypeTag(mod) == .Optional) {
27376 const child = first_param_type.optionalChild(mod);
27377 if (child.eql(concrete_ty, mod)) {
27378 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
27379 return .{ .method = .{
27380 .func_inst = decl_val,
27381 .arg0_inst = deref,
27382 } };
27383 } else if (child.zigTypeTag(mod) == .Pointer and
27384 child.ptrSize(mod) == .One and
27385 child.childType(mod).eql(concrete_ty, mod))
27386 {
27387 return .{ .method = .{
27388 .func_inst = decl_val,
27389 .arg0_inst = object_ptr,
27390 } };
2741327391 }
27392 } else if (first_param_type.zigTypeTag(mod) == .ErrorUnion and
27393 first_param_type.errorUnionPayload(mod).eql(concrete_ty, mod))
27394 {
27395 const deref = try sema.analyzeLoad(block, src, object_ptr, src);
27396 return .{ .method = .{
27397 .func_inst = decl_val,
27398 .arg0_inst = deref,
27399 } };
2741427400 }
27415 break :found_decl null;
27416 },
27417 else => null,
27401 }
27402 break :found_decl decl_idx;
2741827403 };
2741927404
2742027405 const msg = msg: {
......@@ -27480,12 +27465,12 @@ fn namespaceLookup(
2748027465 sema: *Sema,
2748127466 block: *Block,
2748227467 src: LazySrcLoc,
27483 namespace: InternPool.NamespaceIndex,
27468 opt_namespace: InternPool.OptionalNamespaceIndex,
2748427469 decl_name: InternPool.NullTerminatedString,
2748527470) CompileError!?InternPool.DeclIndex {
2748627471 const mod = sema.mod;
2748727472 const gpa = sema.gpa;
27488 if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl_index| {
27473 if (try sema.lookupInNamespace(block, src, opt_namespace, decl_name, true)) |decl_index| {
2748927474 const decl = mod.declPtr(decl_index);
2749027475 if (!decl.is_pub and decl.getFileScope(mod) != block.getFileScope(mod)) {
2749127476 const msg = msg: {
......@@ -27507,10 +27492,10 @@ fn namespaceLookupRef(
2750727492 sema: *Sema,
2750827493 block: *Block,
2750927494 src: LazySrcLoc,
27510 namespace: InternPool.NamespaceIndex,
27495 opt_namespace: InternPool.OptionalNamespaceIndex,
2751127496 decl_name: InternPool.NullTerminatedString,
2751227497) CompileError!?Air.Inst.Ref {
27513 const decl = (try sema.namespaceLookup(block, src, namespace, decl_name)) orelse return null;
27498 const decl = (try sema.namespaceLookup(block, src, opt_namespace, decl_name)) orelse return null;
2751427499 try sema.addReferencedBy(block, src, decl);
2751527500 return try sema.analyzeDeclRef(decl);
2751627501}
......@@ -27519,10 +27504,10 @@ fn namespaceLookupVal(
2751927504 sema: *Sema,
2752027505 block: *Block,
2752127506 src: LazySrcLoc,
27522 namespace: InternPool.NamespaceIndex,
27507 opt_namespace: InternPool.OptionalNamespaceIndex,
2752327508 decl_name: InternPool.NullTerminatedString,
2752427509) CompileError!?Air.Inst.Ref {
27525 const decl = (try sema.namespaceLookup(block, src, namespace, decl_name)) orelse return null;
27510 const decl = (try sema.namespaceLookup(block, src, opt_namespace, decl_name)) orelse return null;
2752627511 return try sema.analyzeDeclVal(block, src, decl);
2752727512}
2752827513
......@@ -37580,7 +37565,7 @@ fn getBuiltinDecl(sema: *Sema, block: *Block, name: []const u8) CompileError!Int
3758037565 const opt_builtin_inst = (try sema.namespaceLookupRef(
3758137566 block,
3758237567 src,
37583 mod.declPtr(std_file.root_decl.unwrap().?).src_namespace,
37568 mod.declPtr(std_file.root_decl.unwrap().?).src_namespace.toOptional(),
3758437569 try ip.getOrPutString(gpa, "builtin"),
3758537570 )) orelse @panic("lib/std.zig is corrupt and missing 'builtin'");
3758637571 const builtin_inst = try sema.analyzeLoad(block, src, opt_builtin_inst, src);
......@@ -37591,7 +37576,7 @@ fn getBuiltinDecl(sema: *Sema, block: *Block, name: []const u8) CompileError!Int
3759137576 const decl_index = (try sema.namespaceLookup(
3759237577 block,
3759337578 src,
37594 builtin_ty.getNamespaceIndex(mod).unwrap().?,
37579 builtin_ty.getNamespaceIndex(mod),
3759537580 try ip.getOrPutString(gpa, name),
3759637581 )) orelse std.debug.panic("lib/std/builtin.zig is corrupt and missing '{s}'", .{name});
3759737582 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+31-9
......@@ -254,7 +254,11 @@ pub const Type = struct {
254254 .error_union_type => |error_union_type| {
255255 try print(Type.fromInterned(error_union_type.error_set_type), writer, mod);
256256 try writer.writeByte('!');
257 try print(Type.fromInterned(error_union_type.payload_type), writer, mod);
257 if (error_union_type.payload_type == .generic_poison_type) {
258 try writer.writeAll("anytype");
259 } else {
260 try print(Type.fromInterned(error_union_type.payload_type), writer, mod);
261 }
258262 return;
259263 },
260264 .inferred_error_set_type => |func_index| {
......@@ -2833,22 +2837,40 @@ pub const Type = struct {
28332837 };
28342838 }
28352839
2840 /// Asserts that the type can have a namespace.
2841 pub fn getNamespaceIndex(ty: Type, zcu: *Zcu) InternPool.OptionalNamespaceIndex {
2842 return ty.getNamespace(zcu).?;
2843 }
2844
28362845 /// Returns null if the type has no namespace.
2837 pub fn getNamespaceIndex(ty: Type, mod: *Module) InternPool.OptionalNamespaceIndex {
2838 const ip = &mod.intern_pool;
2846 pub fn getNamespace(ty: Type, zcu: *Zcu) ?InternPool.OptionalNamespaceIndex {
2847 const ip = &zcu.intern_pool;
28392848 return switch (ip.indexToKey(ty.toIntern())) {
28402849 .opaque_type => ip.loadOpaqueType(ty.toIntern()).namespace,
28412850 .struct_type => ip.loadStructType(ty.toIntern()).namespace,
28422851 .union_type => ip.loadUnionType(ty.toIntern()).namespace,
28432852 .enum_type => ip.loadEnumType(ty.toIntern()).namespace,
28442853
2845 else => .none,
2846 };
2847 }
2854 .anon_struct_type => .none,
2855 .simple_type => |s| switch (s) {
2856 .anyopaque,
2857 .atomic_order,
2858 .atomic_rmw_op,
2859 .calling_convention,
2860 .address_space,
2861 .float_mode,
2862 .reduce_op,
2863 .call_modifier,
2864 .prefetch_options,
2865 .export_options,
2866 .extern_options,
2867 .type_info,
2868 => .none,
2869 else => null,
2870 },
28482871
2849 /// Returns null if the type has no namespace.
2850 pub fn getNamespace(ty: Type, mod: *Module) ?*Module.Namespace {
2851 return if (getNamespaceIndex(ty, mod).unwrap()) |i| mod.namespacePtr(i) else null;
2872 else => null,
2873 };
28522874 }
28532875
28542876 // Works for vectors and vectors of integers.
test/behavior/generics.zig+6
......@@ -578,3 +578,9 @@ test "call generic function that uses capture from function declaration's scope"
578578 const s = S.foo(123);
579579 try expectEqual(123.0, s[0]);
580580}
581
582comptime {
583 // The same function parameter instruction being analyzed multiple times
584 // should override the result of the previous analysis.
585 for (0..2) |_| _ = fn (void) void;
586}
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} });
test/cases/compile_errors/array_init_generic_fn_with_inferred_error_set.zig created+12
......@@ -0,0 +1,12 @@
1pub export fn entry() void {
2 _ = my_func{u8} catch {};
3}
4pub export fn entry1() void {
5 _ = my_func{} catch {};
6}
7fn my_func(comptime T: type) !T {}
8
9// error
10//
11// :2:9: error: expected type 'type', found 'fn (comptime type) @typeInfo(@typeInfo(@TypeOf(tmp.my_func)).Fn.return_type.?).ErrorUnion.error_set!anytype'
12// :5:9: error: expected type 'type', found 'fn (comptime type) @typeInfo(@typeInfo(@TypeOf(tmp.my_func)).Fn.return_type.?).ErrorUnion.error_set!anytype'