Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8384,10 +8384,7 @@ fn builtinCall(
local_val.used = ident_token;
_ = try gz.addPlNode(.export_value, node, Zir.Inst.ExportValue{
.operand = local_val.inst,
// TODO: the result location here should be `.{ .coerced_ty = .export_options_type }`, but
// that currently hits assertions in Sema due to type resolution issues.
// See #16603
.options = try comptimeExpr(gz, scope, .{ .rl = .none }, params[1]),
.options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .export_options_type } }, params[1]),
});
return rvalue(gz, ri, .void_value, node);
}
Expand All @@ -8402,10 +8399,7 @@ fn builtinCall(
const loaded = try gz.addUnNode(.load, local_ptr.ptr, node);
_ = try gz.addPlNode(.export_value, node, Zir.Inst.ExportValue{
.operand = loaded,
// TODO: the result location here should be `.{ .coerced_ty = .export_options_type }`, but
// that currently hits assertions in Sema due to type resolution issues.
// See #16603
.options = try comptimeExpr(gz, scope, .{ .rl = .none }, params[1]),
.options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .export_options_type } }, params[1]),
});
return rvalue(gz, ri, .void_value, node);
}
Expand Down Expand Up @@ -8439,10 +8433,7 @@ fn builtinCall(
},
else => return astgen.failNode(params[0], "symbol to export must identify a declaration", .{}),
}
// TODO: the result location here should be `.{ .coerced_ty = .export_options_type }`, but
// that currently hits assertions in Sema due to type resolution issues.
// See #16603
const options = try comptimeExpr(gz, scope, .{ .rl = .none }, params[1]);
const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .export_options_type } }, params[1]);
_ = try gz.addPlNode(.@"export", node, Zir.Inst.Export{
.namespace = namespace,
.decl_name = decl_name,
Expand All @@ -8452,10 +8443,7 @@ fn builtinCall(
},
.@"extern" => {
const type_inst = try typeExpr(gz, scope, params[0]);
// TODO: the result location here should be `.{ .coerced_ty = .extern_options_type }`, but
// that currently hits assertions in Sema due to type resolution issues.
// See #16603
const options = try comptimeExpr(gz, scope, .{ .rl = .none }, params[1]);
const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .extern_options_type } }, params[1]);
const result = try gz.addExtendedPayload(.builtin_extern, Zir.Inst.BinNode{
.node = gz.nodeIndexToRelative(node),
.lhs = type_inst,
Expand Down Expand Up @@ -8559,10 +8547,7 @@ fn builtinCall(
// zig fmt: on

.Type => {
// TODO: the result location here should be `.{ .coerced_ty = .type_info_type }`, but
// that currently hits assertions in Sema due to type resolution issues.
// See #16603
const operand = try expr(gz, scope, .{ .rl = .none }, params[0]);
const operand = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .type_info_type } }, params[0]);

const gpa = gz.astgen.gpa;

Expand Down Expand Up @@ -8834,10 +8819,7 @@ fn builtinCall(
},
.prefetch => {
const ptr = try expr(gz, scope, .{ .rl = .none }, params[0]);
// TODO: the result location here should be `.{ .coerced_ty = .preftech_options_type }`, but
// that currently hits assertions in Sema due to type resolution issues.
// See #16603
const options = try comptimeExpr(gz, scope, .{ .rl = .none }, params[1]);
const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .prefetch_options_type } }, params[1]);
_ = try gz.addExtendedPayload(.prefetch, Zir.Inst.BinNode{
.node = gz.nodeIndexToRelative(node),
.lhs = ptr,
Expand Down
30 changes: 30 additions & 0 deletions src/InternPool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7176,3 +7176,33 @@ fn unwrapCoercedFunc(ip: *const InternPool, i: Index) Index {
else => unreachable,
};
}

/// Having resolved a builtin type to a real struct/union/enum (which is now at `resolverd_index`),
/// make `want_index` refer to this type instead. This invalidates `resolved_index`, so must be
/// called only when it is guaranteed that no reference to `resolved_index` exists.
pub fn resolveBuiltinType(ip: *InternPool, want_index: Index, resolved_index: Index) void {
assert(@intFromEnum(want_index) >= @intFromEnum(Index.first_type));
assert(@intFromEnum(want_index) <= @intFromEnum(Index.last_type));

// Make sure the type isn't already resolved!
assert(ip.indexToKey(want_index) == .simple_type);

// Make sure it's the same kind of type
assert((ip.zigTypeTagOrPoison(want_index) catch unreachable) ==
(ip.zigTypeTagOrPoison(resolved_index) catch unreachable));

// Copy the data
const item = ip.items.get(@intFromEnum(resolved_index));
ip.items.set(@intFromEnum(want_index), item);

if (std.debug.runtime_safety) {
// Make the value unreachable - this is a weird value which will make (incorrect) existing
// references easier to spot
ip.items.set(@intFromEnum(resolved_index), .{
.tag = .simple_value,
.data = @intFromEnum(SimpleValue.@"unreachable"),
});
} else {
// TODO: add the index to a free-list for reuse
}
}
55 changes: 51 additions & 4 deletions src/Module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -770,9 +770,8 @@ pub const Decl = struct {

/// Gets the namespace that this Decl creates by being a struct, union,
/// enum, or opaque.
/// Only returns it if the Decl is the owner.
pub fn getOwnedInnerNamespaceIndex(decl: Decl, mod: *Module) Namespace.OptionalIndex {
if (!decl.owns_tv) return .none;
pub fn getInnerNamespaceIndex(decl: Decl, mod: *Module) Namespace.OptionalIndex {
if (!decl.has_tv) return .none;
return switch (decl.val.ip_index) {
.empty_struct_type => .none,
.none => .none,
Expand All @@ -786,11 +785,22 @@ pub const Decl = struct {
};
}

/// Same as `getInnerNamespaceIndex` but additionally obtains the pointer.
/// Like `getInnerNamespaceIndex`, but only returns it if the Decl is the owner.
pub fn getOwnedInnerNamespaceIndex(decl: Decl, mod: *Module) Namespace.OptionalIndex {
if (!decl.owns_tv) return .none;
return decl.getInnerNamespaceIndex(mod);
}

/// Same as `getOwnedInnerNamespaceIndex` but additionally obtains the pointer.
pub fn getOwnedInnerNamespace(decl: Decl, mod: *Module) ?*Namespace {
return mod.namespacePtrUnwrap(decl.getOwnedInnerNamespaceIndex(mod));
}

/// Same as `getInnerNamespaceIndex` but additionally obtains the pointer.
pub fn getInnerNamespace(decl: Decl, mod: *Module) ?*Namespace {
return mod.namespacePtrUnwrap(decl.getInnerNamespaceIndex(mod));
}

pub fn dump(decl: *Decl) void {
const loc = std.zig.findLineColumn(decl.scope.source.bytes, decl.src);
std.debug.print("{s}:{d}:{d} name={d} status={s}", .{
Expand Down Expand Up @@ -4283,6 +4293,40 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
const zir = decl.getFileScope(mod).zir;
const zir_datas = zir.instructions.items(.data);

// TODO: figure out how this works under incremental changes to builtin.zig!
const builtin_type_target_index: InternPool.Index = blk: {
const std_mod = mod.main_pkg.table.get("std").?;
if (decl.getFileScope(mod).pkg != std_mod) break :blk .none;
// We're in the std module.
const std_file = (try mod.importPkg(std_mod)).file;
const std_decl = mod.declPtr(std_file.root_decl.unwrap().?);
const std_namespace = std_decl.getInnerNamespace(mod).?;
const builtin_str = try mod.intern_pool.getOrPutString(gpa, "builtin");
const builtin_decl = mod.declPtr(std_namespace.decls.getKeyAdapted(builtin_str, DeclAdapter{ .mod = mod }) orelse break :blk .none);
const builtin_namespace = builtin_decl.getInnerNamespaceIndex(mod).unwrap() orelse break :blk .none;
if (decl.src_namespace != builtin_namespace) break :blk .none;
// We're in builtin.zig. This could be a builtin we need to add to a specific InternPool index.
const decl_name = mod.intern_pool.stringToSlice(decl.name);
for ([_]struct { []const u8, InternPool.Index }{
.{ "AtomicOrder", .atomic_order_type },
.{ "AtomicRmwOp", .atomic_rmw_op_type },
.{ "CallingConvention", .calling_convention_type },
.{ "AddressSpace", .address_space_type },
.{ "FloatMode", .float_mode_type },
.{ "ReduceOp", .reduce_op_type },
.{ "CallModifier", .call_modifier_type },
.{ "PrefetchOptions", .prefetch_options_type },
.{ "ExportOptions", .export_options_type },
.{ "ExternOptions", .extern_options_type },
.{ "Type", .type_info_type },
}) |pair| {
if (std.mem.eql(u8, decl_name, pair[0])) {
break :blk pair[1];
}
}
break :blk .none;
};

decl.analysis = .in_progress;

var analysis_arena = std.heap.ArenaAllocator.init(gpa);
Expand All @@ -4304,6 +4348,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
.fn_ret_ty_ies = null,
.owner_func_index = .none,
.comptime_mutable_decls = &comptime_mutable_decls,
.builtin_type_target_index = builtin_type_target_index,
};
defer sema.deinit();

Expand Down Expand Up @@ -4340,6 +4385,8 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
const extra = zir.extraData(Zir.Inst.Block, inst_data.payload_index);
const body = zir.extra[extra.end..][0..extra.data.body_len];
const result_ref = (try sema.analyzeBodyBreak(&block_scope, body)).?.operand;
// We'll do some other bits with the Sema. Clear the type target index just in case they analyze any type.
sema.builtin_type_target_index = .none;
try wip_captures.finalize();
for (comptime_mutable_decls.items) |ct_decl_index| {
const ct_decl = mod.declPtr(ct_decl_index);
Expand Down
Loading