srctree

Andrew Kelley parent c11b6adf 436c72e8 95cb9394
Merge pull request #19333 from Vexu/fixes

Miscellaneous error fixes

inlinesplit
src/Module.zig added: 194, removed: 157, total 37
@@ -1897,8 +1897,11 @@ pub const SrcLoc = struct {
const parent_node = src_loc.declRelativeToNodeIndex(node_off);
 
var buf: [2]Ast.Node.Index = undefined;
const full = tree.fullArrayInit(&buf, parent_node).?;
return tree.nodeToSpan(full.ast.type_expr);
const type_expr = if (tree.fullArrayInit(&buf, parent_node)) |array_init|
array_init.ast.type_expr
else
tree.fullStructInit(&buf, parent_node).?.ast.type_expr;
return tree.nodeToSpan(type_expr);
},
.node_offset_store_ptr => |node_off| {
const tree = try src_loc.file_scope.getTree(gpa);
 
src/Sema.zig added: 194, removed: 157, total 37
@@ -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;
},
else => 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 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;
 
src/codegen/llvm.zig added: 194, removed: 157, total 37
@@ -2852,7 +2852,7 @@ pub const Object = struct {
const stack_trace_str = try mod.intern_pool.getOrPutString(mod.gpa, "StackTrace");
// buffer is only used for int_type, `builtin` is a struct.
const builtin_ty = mod.declPtr(builtin_decl).val.toType();
const builtin_namespace = builtin_ty.getNamespace(mod).?;
const builtin_namespace = mod.namespacePtrUnwrap(builtin_ty.getNamespaceIndex(mod)).?;
const stack_trace_decl_index = builtin_namespace.decls.getKeyAdapted(stack_trace_str, Module.DeclAdapter{ .zcu = mod }).?;
const stack_trace_decl = mod.declPtr(stack_trace_decl_index);
 
 
src/type.zig added: 194, removed: 157, total 37
@@ -254,7 +254,11 @@ pub const Type = struct {
.error_union_type => |error_union_type| {
try print(Type.fromInterned(error_union_type.error_set_type), writer, mod);
try writer.writeByte('!');
try print(Type.fromInterned(error_union_type.payload_type), writer, mod);
if (error_union_type.payload_type == .generic_poison_type) {
try writer.writeAll("anytype");
} else {
try print(Type.fromInterned(error_union_type.payload_type), writer, mod);
}
return;
},
.inferred_error_set_type => |func_index| {
@@ -2833,22 +2837,40 @@ pub const Type = struct {
};
}
 
/// Asserts that the type can have a namespace.
pub fn getNamespaceIndex(ty: Type, zcu: *Zcu) InternPool.OptionalNamespaceIndex {
return ty.getNamespace(zcu).?;
}
 
/// Returns null if the type has no namespace.
pub fn getNamespaceIndex(ty: Type, mod: *Module) InternPool.OptionalNamespaceIndex {
const ip = &mod.intern_pool;
pub fn getNamespace(ty: Type, zcu: *Zcu) ?InternPool.OptionalNamespaceIndex {
const ip = &zcu.intern_pool;
return switch (ip.indexToKey(ty.toIntern())) {
.opaque_type => ip.loadOpaqueType(ty.toIntern()).namespace,
.struct_type => ip.loadStructType(ty.toIntern()).namespace,
.union_type => ip.loadUnionType(ty.toIntern()).namespace,
.enum_type => ip.loadEnumType(ty.toIntern()).namespace,
 
else => .none,
};
}
.anon_struct_type => .none,
.simple_type => |s| switch (s) {
.anyopaque,
.atomic_order,
.atomic_rmw_op,
.calling_convention,
.address_space,
.float_mode,
.reduce_op,
.call_modifier,
.prefetch_options,
.export_options,
.extern_options,
.type_info,
=> .none,
else => null,
},
 
/// Returns null if the type has no namespace.
pub fn getNamespace(ty: Type, mod: *Module) ?*Module.Namespace {
return if (getNamespaceIndex(ty, mod).unwrap()) |i| mod.namespacePtr(i) else null;
else => null,
};
}
 
// Works for vectors and vectors of integers.
 
test/behavior/generics.zig added: 194, removed: 157, total 37
@@ -578,3 +578,9 @@ test "call generic function that uses capture from function declaration's scope"
const s = S.foo(123);
try expectEqual(123.0, s[0]);
}
 
comptime {
// The same function parameter instruction being analyzed multiple times
// should override the result of the previous analysis.
for (0..2) |_| _ = fn (void) void;
}
 
test/behavior/usingnamespace.zig added: 194, removed: 157, total 37
@@ -97,3 +97,12 @@ test "container member access usingnamespace decls" {
var foo = Bar{};
foo.two();
}
 
usingnamespace opaque {};
 
usingnamespace @Type(.{ .Struct = .{
.layout = .auto,
.fields = &.{},
.decls = &.{},
.is_tuple = false,
} });
 
filename was Deleted added: 194, removed: 157, total 37
@@ -0,0 +1,12 @@
pub export fn entry() void {
_ = my_func{u8} catch {};
}
pub export fn entry1() void {
_ = my_func{} catch {};
}
fn my_func(comptime T: type) !T {}
 
// error
//
// :2:9: error: expected type 'type', found 'fn (comptime type) @typeInfo(@typeInfo(@TypeOf(tmp.my_func)).Fn.return_type.?).ErrorUnion.error_set!anytype'
// :5:9: error: expected type 'type', found 'fn (comptime type) @typeInfo(@typeInfo(@TypeOf(tmp.my_func)).Fn.return_type.?).ErrorUnion.error_set!anytype'