aboutsummaryrefslogtreecommitdiff
path: root/src/Sema.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2024-03-17 15:26:55 -0700
committerGitHub <noreply@github.com>2024-03-17 15:26:55 -0700
commit95cb93944060d04ec49e9d2e21ef911ad2b09ccd (patch)
tree5b9839319d7d3258613a7367685c9aab1b6113f3 /src/Sema.zig
parentc11b6adf13fe5c765ec480af5bad6338e6982a9d (diff)
parent436c72e89a6e402b6920ab03207b95d0ca709ee9 (diff)
downloadzig-95cb93944060d04ec49e9d2e21ef911ad2b09ccd.tar.gz
zig-95cb93944060d04ec49e9d2e21ef911ad2b09ccd.zip
Merge pull request #19333 from Vexu/fixes
Miscellaneous error fixes
Diffstat (limited to 'src/Sema.zig')
-rw-r--r--src/Sema.zig259
1 files changed, 122 insertions, 137 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index cf103e7230..be3fd3e4db 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -6267,7 +6267,7 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
const decl_name = try mod.intern_pool.getOrPutString(mod.gpa, sema.code.nullTerminatedString(extra.decl_name));
const decl_index = if (extra.namespace != .none) index_blk: {
const container_ty = try sema.resolveType(block, operand_src, extra.namespace);
- const container_namespace = container_ty.getNamespaceIndex(mod).unwrap().?;
+ const container_namespace = container_ty.getNamespaceIndex(mod);
const maybe_index = try sema.lookupInNamespace(block, operand_src, container_namespace, decl_name, false);
break :index_blk maybe_index orelse
@@ -6632,7 +6632,7 @@ fn lookupIdentifier(sema: *Sema, block: *Block, src: LazySrcLoc, name: InternPoo
const mod = sema.mod;
var namespace = block.namespace;
while (true) {
- if (try sema.lookupInNamespace(block, src, namespace, name, false)) |decl_index| {
+ if (try sema.lookupInNamespace(block, src, namespace.toOptional(), name, false)) |decl_index| {
return decl_index;
}
namespace = mod.namespacePtr(namespace).parent.unwrap() orelse break;
@@ -6646,12 +6646,13 @@ fn lookupInNamespace(
sema: *Sema,
block: *Block,
src: LazySrcLoc,
- namespace_index: InternPool.NamespaceIndex,
+ opt_namespace_index: InternPool.OptionalNamespaceIndex,
ident_name: InternPool.NullTerminatedString,
observe_usingnamespace: bool,
) CompileError!?InternPool.DeclIndex {
const mod = sema.mod;
+ const namespace_index = opt_namespace_index.unwrap() orelse return null;
const namespace = mod.namespacePtr(namespace_index);
const namespace_decl = mod.declPtr(namespace.decl_index);
if (namespace_decl.analysis == .file_failure) {
@@ -6696,7 +6697,7 @@ fn lookupInNamespace(
}
try sema.ensureDeclAnalyzed(sub_usingnamespace_decl_index);
const ns_ty = sub_usingnamespace_decl.val.toType();
- const sub_ns = ns_ty.getNamespace(mod).?;
+ const sub_ns = mod.namespacePtrUnwrap(ns_ty.getNamespaceIndex(mod)) orelse continue;
try checked_namespaces.put(gpa, sub_ns, src_file == sub_usingnamespace_decl.getFileScope(mod));
}
}
@@ -9917,7 +9918,7 @@ fn zirParam(
.is_comptime = comptime_syntax,
.name = param_name,
});
- sema.inst_map.putAssumeCapacityNoClobber(inst, .generic_poison);
+ sema.inst_map.putAssumeCapacity(inst, .generic_poison);
return;
},
else => |e| return e,
@@ -9934,7 +9935,7 @@ fn zirParam(
.is_comptime = comptime_syntax,
.name = param_name,
});
- sema.inst_map.putAssumeCapacityNoClobber(inst, .generic_poison);
+ sema.inst_map.putAssumeCapacity(inst, .generic_poison);
return;
},
else => |e| return e,
@@ -9949,7 +9950,7 @@ fn zirParam(
if (is_comptime) {
// If this is a comptime parameter we can add a constant generic_poison
// since this is also a generic parameter.
- sema.inst_map.putAssumeCapacityNoClobber(inst, .generic_poison);
+ sema.inst_map.putAssumeCapacity(inst, .generic_poison);
} else {
// Otherwise we need a dummy runtime instruction.
const result_index: Air.Inst.Index = @enumFromInt(sema.air_instructions.len);
@@ -9957,7 +9958,7 @@ fn zirParam(
.tag = .alloc,
.data = .{ .ty = param_ty },
});
- sema.inst_map.putAssumeCapacityNoClobber(inst, result_index.toRef());
+ sema.inst_map.putAssumeCapacity(inst, result_index.toRef());
}
}
@@ -13699,8 +13700,7 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
} });
}
- const namespace = container_type.getNamespaceIndex(mod).unwrap() orelse
- return .bool_false;
+ const namespace = container_type.getNamespaceIndex(mod);
if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl_index| {
const decl = mod.declPtr(decl_index);
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
const fn_info_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "Fn"),
)).?;
try sema.ensureDeclAnalyzed(fn_info_decl_index);
@@ -17544,7 +17544,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const param_info_decl_index = (try sema.namespaceLookup(
block,
src,
- fn_info_ty.getNamespaceIndex(mod).unwrap().?,
+ fn_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "Param"),
)).?;
try sema.ensureDeclAnalyzed(param_info_decl_index);
@@ -17644,7 +17644,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const int_info_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "Int"),
)).?;
try sema.ensureDeclAnalyzed(int_info_decl_index);
@@ -17672,7 +17672,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const float_info_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "Float"),
)).?;
try sema.ensureDeclAnalyzed(float_info_decl_index);
@@ -17704,7 +17704,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const decl_index = (try sema.namespaceLookup(
block,
src,
- (try sema.getBuiltinType("Type")).getNamespaceIndex(mod).unwrap().?,
+ (try sema.getBuiltinType("Type")).getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "Pointer"),
)).?;
try sema.ensureDeclAnalyzed(decl_index);
@@ -17715,7 +17715,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const decl_index = (try sema.namespaceLookup(
block,
src,
- pointer_ty.getNamespaceIndex(mod).unwrap().?,
+ pointer_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "Size"),
)).?;
try sema.ensureDeclAnalyzed(decl_index);
@@ -17758,7 +17758,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const array_field_ty_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "Array"),
)).?;
try sema.ensureDeclAnalyzed(array_field_ty_decl_index);
@@ -17789,7 +17789,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const vector_field_ty_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "Vector"),
)).?;
try sema.ensureDeclAnalyzed(vector_field_ty_decl_index);
@@ -17818,7 +17818,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const optional_field_ty_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "Optional"),
)).?;
try sema.ensureDeclAnalyzed(optional_field_ty_decl_index);
@@ -17845,7 +17845,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const set_field_ty_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "Error"),
)).?;
try sema.ensureDeclAnalyzed(set_field_ty_decl_index);
@@ -17950,7 +17950,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const error_union_field_ty_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "ErrorUnion"),
)).?;
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
const enum_field_ty_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "EnumField"),
)).?;
try sema.ensureDeclAnalyzed(enum_field_ty_decl_index);
@@ -18071,7 +18071,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const type_enum_ty_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "Enum"),
)).?;
try sema.ensureDeclAnalyzed(type_enum_ty_decl_index);
@@ -18103,7 +18103,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const type_union_ty_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "Union"),
)).?;
try sema.ensureDeclAnalyzed(type_union_ty_decl_index);
@@ -18115,7 +18115,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const union_field_ty_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "UnionField"),
)).?;
try sema.ensureDeclAnalyzed(union_field_ty_decl_index);
@@ -18217,7 +18217,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const decl_index = (try sema.namespaceLookup(
block,
src,
- (try sema.getBuiltinType("Type")).getNamespaceIndex(mod).unwrap().?,
+ (try sema.getBuiltinType("Type")).getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "ContainerLayout"),
)).?;
try sema.ensureDeclAnalyzed(decl_index);
@@ -18250,7 +18250,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const type_struct_ty_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "Struct"),
)).?;
try sema.ensureDeclAnalyzed(type_struct_ty_decl_index);
@@ -18262,7 +18262,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const struct_field_ty_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "StructField"),
)).?;
try sema.ensureDeclAnalyzed(struct_field_ty_decl_index);
@@ -18447,7 +18447,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const decl_index = (try sema.namespaceLookup(
block,
src,
- (try sema.getBuiltinType("Type")).getNamespaceIndex(mod).unwrap().?,
+ (try sema.getBuiltinType("Type")).getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "ContainerLayout"),
)).?;
try sema.ensureDeclAnalyzed(decl_index);
@@ -18483,7 +18483,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const type_opaque_ty_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, "Opaque"),
)).?;
try sema.ensureDeclAnalyzed(type_opaque_ty_decl_index);
@@ -18526,7 +18526,7 @@ fn typeInfoDecls(
const declaration_ty_decl_index = (try sema.namespaceLookup(
block,
src,
- type_info_ty.getNamespaceIndex(mod).unwrap().?,
+ type_info_ty.getNamespaceIndex(mod),
try mod.intern_pool.getOrPutString(gpa, "Declaration"),
)).?;
try sema.ensureDeclAnalyzed(declaration_ty_decl_index);
@@ -18541,10 +18541,7 @@ fn typeInfoDecls(
var seen_namespaces = std.AutoHashMap(*Namespace, void).init(gpa);
defer seen_namespaces.deinit();
- if (opt_namespace.unwrap()) |namespace_index| {
- const namespace = mod.namespacePtr(namespace_index);
- try sema.typeInfoNamespaceDecls(block, namespace, declaration_ty, &decl_vals, &seen_namespaces);
- }
+ try sema.typeInfoNamespaceDecls(block, opt_namespace, declaration_ty, &decl_vals, &seen_namespaces);
const array_decl_ty = try mod.arrayType(.{
.len = decl_vals.items.len,
@@ -18577,23 +18574,27 @@ fn typeInfoDecls(
fn typeInfoNamespaceDecls(
sema: *Sema,
block: *Block,
- namespace: *Namespace,
+ opt_namespace_index: InternPool.OptionalNamespaceIndex,
declaration_ty: Type,
decl_vals: *std.ArrayList(InternPool.Index),
seen_namespaces: *std.AutoHashMap(*Namespace, void),
) !void {
const mod = sema.mod;
const ip = &mod.intern_pool;
+
+ const namespace_index = opt_namespace_index.unwrap() orelse return;
+ const namespace = mod.namespacePtr(namespace_index);
+
const gop = try seen_namespaces.getOrPut(namespace);
if (gop.found_existing) return;
+
const decls = namespace.decls.keys();
for (decls) |decl_index| {
const decl = mod.declPtr(decl_index);
if (decl.kind == .@"usingnamespace") {
if (decl.analysis == .in_progress) continue;
try mod.ensureDeclAnalyzed(decl_index);
- const new_ns = decl.val.toType().getNamespace(mod).?;
- try sema.typeInfoNamespaceDecls(block, new_ns, declaration_ty, decl_vals, seen_namespaces);
+ try sema.typeInfoNamespaceDecls(block, decl.val.toType().getNamespaceIndex(mod), declaration_ty, decl_vals, seen_namespaces);
continue;
}
if (decl.kind != .named or !decl.is_pub) continue;
@@ -19744,7 +19745,8 @@ fn zirStructInitEmpty(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
const src = inst_data.src();
- const obj_ty = try sema.resolveType(block, src, inst_data.operand);
+ const ty_src: LazySrcLoc = .{ .node_offset_init_ty = inst_data.src_node };
+ const obj_ty = try sema.resolveType(block, ty_src, inst_data.operand);
const mod = sema.mod;
switch (obj_ty.zigTypeTag(mod)) {
@@ -26558,7 +26560,7 @@ fn preparePanicId(sema: *Sema, block: *Block, panic_id: Module.PanicId) !InternP
const msg_decl_index = (sema.namespaceLookup(
block,
.unneeded,
- panic_messages_ty.getNamespaceIndex(mod).unwrap().?,
+ panic_messages_ty.getNamespaceIndex(mod),
try mod.intern_pool.getOrPutString(gpa, @tagName(panic_id)),
) catch |err| switch (err) {
error.AnalysisFail, error.NeededSourceLocation => @panic("std.builtin.panic_messages is corrupt"),
@@ -26977,10 +26979,8 @@ fn fieldVal(
} })));
},
.Union => {
- if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
- if (try sema.namespaceLookupVal(block, src, namespace, field_name)) |inst| {
- return inst;
- }
+ if (try sema.namespaceLookupVal(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
+ return inst;
}
try sema.resolveTypeFields(child_type);
if (child_type.unionTagType(mod)) |enum_ty| {
@@ -26992,10 +26992,8 @@ fn fieldVal(
return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
},
.Enum => {
- if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
- if (try sema.namespaceLookupVal(block, src, namespace, field_name)) |inst| {
- return inst;
- }
+ if (try sema.namespaceLookupVal(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
+ return inst;
}
const field_index_usize = child_type.enumFieldIndex(field_name, mod) orelse
return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
@@ -27004,10 +27002,8 @@ fn fieldVal(
return Air.internedToRef(enum_val.toIntern());
},
.Struct, .Opaque => {
- if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
- if (try sema.namespaceLookupVal(block, src, namespace, field_name)) |inst| {
- return inst;
- }
+ if (try sema.namespaceLookupVal(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
+ return inst;
}
return sema.failWithBadMemberAccess(block, child_type, src, field_name);
},
@@ -27203,10 +27199,8 @@ fn fieldPtr(
} }));
},
.Union => {
- if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
- if (try sema.namespaceLookupRef(block, src, namespace, field_name)) |inst| {
- return inst;
- }
+ if (try sema.namespaceLookupRef(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
+ return inst;
}
try sema.resolveTypeFields(child_type);
if (child_type.unionTagType(mod)) |enum_ty| {
@@ -27219,10 +27213,8 @@ fn fieldPtr(
return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
},
.Enum => {
- if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
- if (try sema.namespaceLookupRef(block, src, namespace, field_name)) |inst| {
- return inst;
- }
+ if (try sema.namespaceLookupRef(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
+ return inst;
}
const field_index = child_type.enumFieldIndex(field_name, mod) orelse {
return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
@@ -27232,10 +27224,8 @@ fn fieldPtr(
return anonDeclRef(sema, idx_val.toIntern());
},
.Struct, .Opaque => {
- if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
- if (try sema.namespaceLookupRef(block, src, namespace, field_name)) |inst| {
- return inst;
- }
+ if (try sema.namespaceLookupRef(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| {
+ return inst;
}
return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
},
@@ -27348,73 +27338,68 @@ fn fieldCallBind(
}
// If we get here, we need to look for a decl in the struct type instead.
- const found_decl = switch (concrete_ty.zigTypeTag(mod)) {
- .Struct, .Opaque, .Union, .Enum => found_decl: {
- if (concrete_ty.getNamespaceIndex(mod).unwrap()) |namespace| {
- if (try sema.namespaceLookup(block, src, namespace, field_name)) |decl_idx| {
- try sema.addReferencedBy(block, src, decl_idx);
- const decl_val = try sema.analyzeDeclVal(block, src, decl_idx);
- const decl_type = sema.typeOf(decl_val);
- if (mod.typeToFunc(decl_type)) |func_type| f: {
- if (func_type.param_types.len == 0) break :f;
-
- const first_param_type = Type.fromInterned(func_type.param_types.get(ip)[0]);
- // zig fmt: off
- if (first_param_type.isGenericPoison() or (
- first_param_type.zigTypeTag(mod) == .Pointer and
- (first_param_type.ptrSize(mod) == .One or
- first_param_type.ptrSize(mod) == .C) and
- first_param_type.childType(mod).eql(concrete_ty, mod)))
- {
- // zig fmt: on
- // Note that if the param type is generic poison, we know that it must
- // specifically be `anytype` since it's the first parameter, meaning we
- // can safely assume it can be a pointer.
- // TODO: bound fn calls on rvalues should probably
- // generate a by-value argument somehow.
- return .{ .method = .{
- .func_inst = decl_val,
- .arg0_inst = object_ptr,
- } };
- } else if (first_param_type.eql(concrete_ty, mod)) {
- const deref = try sema.analyzeLoad(block, src, object_ptr, src);
- return .{ .method = .{
- .func_inst = decl_val,
- .arg0_inst = deref,
- } };
- } else if (first_param_type.zigTypeTag(mod) == .Optional) {
- const child = first_param_type.optionalChild(mod);
- if (child.eql(concrete_ty, mod)) {
- const deref = try sema.analyzeLoad(block, src, object_ptr, src);
- return .{ .method = .{
- .func_inst = decl_val,
- .arg0_inst = deref,
- } };
- } else if (child.zigTypeTag(mod) == .Pointer and
- child.ptrSize(mod) == .One and
- child.childType(mod).eql(concrete_ty, mod))
- {
- return .{ .method = .{
- .func_inst = decl_val,
- .arg0_inst = object_ptr,
- } };
- }
- } else if (first_param_type.zigTypeTag(mod) == .ErrorUnion and
- first_param_type.errorUnionPayload(mod).eql(concrete_ty, mod))
- {
- const deref = try sema.analyzeLoad(block, src, object_ptr, src);
- return .{ .method = .{
- .func_inst = decl_val,
- .arg0_inst = deref,
- } };
- }
- }
- break :found_decl decl_idx;
+ const found_decl = found_decl: {
+ const namespace = concrete_ty.getNamespace(mod) orelse
+ break :found_decl null;
+ const decl_idx = (try sema.namespaceLookup(block, src, namespace, field_name)) orelse
+ break :found_decl null;
+
+ try sema.addReferencedBy(block, src, decl_idx);
+ const decl_val = try sema.analyzeDeclVal(block, src, decl_idx);
+ const decl_type = sema.typeOf(decl_val);
+ if (mod.typeToFunc(decl_type)) |func_type| f: {
+ if (func_type.param_types.len == 0) break :f;
+
+ const first_param_type = Type.fromInterned(func_type.param_types.get(ip)[0]);
+ if (first_param_type.isGenericPoison() or
+ (first_param_type.zigTypeTag(mod) == .Pointer and
+ (first_param_type.ptrSize(mod) == .One or
+ first_param_type.ptrSize(mod) == .C) and
+ first_param_type.childType(mod).eql(concrete_ty, mod)))
+ {
+ // Note that if the param type is generic poison, we know that it must
+ // specifically be `anytype` since it's the first parameter, meaning we
+ // can safely assume it can be a pointer.
+ // TODO: bound fn calls on rvalues should probably
+ // generate a by-value argument somehow.
+ return .{ .method = .{
+ .func_inst = decl_val,
+ .arg0_inst = object_ptr,
+ } };
+ } else if (first_param_type.eql(concrete_ty, mod)) {
+ const deref = try sema.analyzeLoad(block, src, object_ptr, src);
+ return .{ .method = .{
+ .func_inst = decl_val,
+ .arg0_inst = deref,
+ } };
+ } else if (first_param_type.zigTypeTag(mod) == .Optional) {
+ const child = first_param_type.optionalChild(mod);
+ if (child.eql(concrete_ty, mod)) {
+ const deref = try sema.analyzeLoad(block, src, object_ptr, src);
+ return .{ .method = .{
+ .func_inst = decl_val,
+ .arg0_inst = deref,
+ } };
+ } else if (child.zigTypeTag(mod) == .Pointer and
+ child.ptrSize(mod) == .One and
+ child.childType(mod).eql(concrete_ty, mod))
+ {
+ return .{ .method = .{
+ .func_inst = decl_val,
+ .arg0_inst = object_ptr,
+ } };
}
+ } else if (first_param_type.zigTypeTag(mod) == .ErrorUnion and
+ first_param_type.errorUnionPayload(mod).eql(concrete_ty, mod))
+ {
+ const deref = try sema.analyzeLoad(block, src, object_ptr, src);
+ return .{ .method = .{
+ .func_inst = decl_val,
+ .arg0_inst = deref,
+ } };
}
- break :found_decl null;
- },
- else => null,
+ }
+ break :found_decl decl_idx;
};
const msg = msg: {
@@ -27480,12 +27465,12 @@ fn namespaceLookup(
sema: *Sema,
block: *Block,
src: LazySrcLoc,
- namespace: InternPool.NamespaceIndex,
+ opt_namespace: InternPool.OptionalNamespaceIndex,
decl_name: InternPool.NullTerminatedString,
) CompileError!?InternPool.DeclIndex {
const mod = sema.mod;
const gpa = sema.gpa;
- if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl_index| {
+ if (try sema.lookupInNamespace(block, src, opt_namespace, decl_name, true)) |decl_index| {
const decl = mod.declPtr(decl_index);
if (!decl.is_pub and decl.getFileScope(mod) != block.getFileScope(mod)) {
const msg = msg: {
@@ -27507,10 +27492,10 @@ fn namespaceLookupRef(
sema: *Sema,
block: *Block,
src: LazySrcLoc,
- namespace: InternPool.NamespaceIndex,
+ opt_namespace: InternPool.OptionalNamespaceIndex,
decl_name: InternPool.NullTerminatedString,
) CompileError!?Air.Inst.Ref {
- const decl = (try sema.namespaceLookup(block, src, namespace, decl_name)) orelse return null;
+ const decl = (try sema.namespaceLookup(block, src, opt_namespace, decl_name)) orelse return null;
try sema.addReferencedBy(block, src, decl);
return try sema.analyzeDeclRef(decl);
}
@@ -27519,10 +27504,10 @@ fn namespaceLookupVal(
sema: *Sema,
block: *Block,
src: LazySrcLoc,
- namespace: InternPool.NamespaceIndex,
+ opt_namespace: InternPool.OptionalNamespaceIndex,
decl_name: InternPool.NullTerminatedString,
) CompileError!?Air.Inst.Ref {
- const decl = (try sema.namespaceLookup(block, src, namespace, decl_name)) orelse return null;
+ const decl = (try sema.namespaceLookup(block, src, opt_namespace, decl_name)) orelse return null;
return try sema.analyzeDeclVal(block, src, decl);
}
@@ -37580,7 +37565,7 @@ fn getBuiltinDecl(sema: *Sema, block: *Block, name: []const u8) CompileError!Int
const opt_builtin_inst = (try sema.namespaceLookupRef(
block,
src,
- mod.declPtr(std_file.root_decl.unwrap().?).src_namespace,
+ mod.declPtr(std_file.root_decl.unwrap().?).src_namespace.toOptional(),
try ip.getOrPutString(gpa, "builtin"),
)) orelse @panic("lib/std.zig is corrupt and missing 'builtin'");
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
const decl_index = (try sema.namespaceLookup(
block,
src,
- builtin_ty.getNamespaceIndex(mod).unwrap().?,
+ builtin_ty.getNamespaceIndex(mod),
try ip.getOrPutString(gpa, name),
)) orelse std.debug.panic("lib/std/builtin.zig is corrupt and missing '{s}'", .{name});
return decl_index;