fix(ir): resolve forward alias in top-level global annotations (issue 0070)
Issue 0069's resolveForwardIdentifierAliases fixpoint runs at the END of scanDecls, but top-level var_decl globals and typed module constants had their annotations resolved via resolveType(ta) inside the SAME scan loop, before the fixpoint. So a forward identifier alias (`A :: B; B :: s32;`) used as a global's type (`g : A = 7;`) was still absent from type_alias_map: resolveType fabricated an empty-struct stub, and the global got a type mismatching its initializer at LLVM verification (the typed-const path `K : A : 42;` silently mistyped the constant instead). Split scanDecls into two passes: pass 1 registers function/type/alias facts, then resolveForwardIdentifierAliases converges the aliases, then pass 2 registers var_decl globals (registerTopLevelGlobal) and typed module constants (registerTypedModuleConst) against the converged alias map. Globals/typed-consts can't be named in a type position, so deferring them past type/alias registration is order-safe; the untyped module-const branch (no annotation to resolve) stays in pass 1. One incidental IR snapshot reorder (examples/1309: user globals now emit after foreign-class globals — semantically identical, program still exits 0). Regression: examples/0133-types-forward-alias-global.sx (forward-alias global + typed const). Gate: zig build, zig build test, run_examples.sh -> 354/0.
This commit is contained in:
135
src/ir/lower.zig
135
src/ir/lower.zig
@@ -1153,16 +1153,13 @@ pub const Lowering = struct {
|
||||
}
|
||||
// comptime_expr handled in Pass 2
|
||||
|
||||
// Simple value constants with type annotation (e.g. AF_INET :s32: 2)
|
||||
if (cd.type_annotation) |ta| {
|
||||
switch (cd.value.data) {
|
||||
.int_literal, .float_literal, .bool_literal, .string_literal, .undef_literal, .null_literal => {
|
||||
const ty = self.resolveType(ta);
|
||||
self.program_index.module_const_map.put(cd.name, .{ .value = cd.value, .ty = ty }) catch {};
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
} else {
|
||||
// Typed value constants (`AF_INET :s32: 2`) are registered in
|
||||
// pass 2 below — after the forward-alias fixpoint — so a
|
||||
// forward identifier alias in the annotation resolves to its
|
||||
// target instead of a fabricated stub (issue 0070). Untyped
|
||||
// literal constants carry no annotation to resolve, so they
|
||||
// stay here (their type comes from the literal / inference).
|
||||
if (cd.type_annotation == null) {
|
||||
// Untyped literal constants (e.g. UI_VERT_SRC :: #string GLSL...GLSL;)
|
||||
switch (cd.value.data) {
|
||||
.string_literal => self.program_index.module_const_map.put(cd.name, .{ .value = cd.value, .ty = .string }) catch {},
|
||||
@@ -1210,50 +1207,88 @@ pub const Lowering = struct {
|
||||
.ufcs_alias => |ua| {
|
||||
self.program_index.ufcs_alias_map.put(ua.name, ua.target) catch {};
|
||||
},
|
||||
.var_decl => |vd| {
|
||||
// Top-level mutable global (e.g., `context : Context = ---;`)
|
||||
// Use self.resolveType so type aliases like `Handle :: u32;` resolve
|
||||
// to their target type (not a synthetic empty struct). When the
|
||||
// user omitted the annotation, infer from the initializer
|
||||
// expression; foreign globals with no annotation are diagnosed
|
||||
// because their type can't be inferred without an initializer.
|
||||
const var_ty: TypeId = if (vd.type_annotation) |ta|
|
||||
self.resolveType(ta)
|
||||
else if (vd.value) |val|
|
||||
self.inferExprType(val)
|
||||
else blk: {
|
||||
if (self.diagnostics) |d|
|
||||
d.addFmt(.err, null, "top-level var '{s}' has no type annotation and no initializer to infer from", .{vd.name});
|
||||
break :blk .void;
|
||||
};
|
||||
// Foreign globals reference a symbol defined in libSystem etc.
|
||||
// (`_NSConcreteStackBlock : *void #foreign;`). The C symbol
|
||||
// name is the optional override or the sx name itself.
|
||||
const sym_name = vd.foreign_name orelse vd.name;
|
||||
const name_id = self.module.types.internString(sym_name);
|
||||
const init_val: ?inst_mod.ConstantValue = if (vd.is_foreign) null else if (vd.value) |v| switch (v.data) {
|
||||
.undef_literal => .zeroinit,
|
||||
.int_literal => |il| .{ .int = il.value },
|
||||
.bool_literal => |bl| .{ .boolean = bl.value },
|
||||
.float_literal => |fl| .{ .float = fl.value },
|
||||
.string_literal => |sl| .{ .string = self.module.types.internString(sl.raw) },
|
||||
.array_literal => |al| self.constArrayLiteral(al.elements),
|
||||
.struct_literal => |sl| self.constStructLiteral(&sl, var_ty),
|
||||
else => null,
|
||||
} else null;
|
||||
const gid = self.module.addGlobal(.{
|
||||
.name = name_id,
|
||||
.ty = var_ty,
|
||||
.init_val = init_val,
|
||||
.is_const = false,
|
||||
.is_extern = vd.is_foreign,
|
||||
});
|
||||
self.program_index.global_names.put(vd.name, .{ .id = gid, .ty = var_ty }) catch {};
|
||||
},
|
||||
// Top-level globals are registered in a second pass (below),
|
||||
// after the forward-alias fixpoint, so a forward identifier
|
||||
// alias used as a global's type annotation resolves (issue 0070).
|
||||
.var_decl => {},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
self.resolveForwardIdentifierAliases(decls);
|
||||
// Pass 2: registrations that resolve a top-level type annotation run
|
||||
// after the alias fixpoint, so a forward identifier alias used as the
|
||||
// annotation resolves to its target (issue 0070).
|
||||
for (decls) |decl| {
|
||||
self.setCurrentSourceFile(decl.source_file);
|
||||
switch (decl.data) {
|
||||
.var_decl => self.registerTopLevelGlobal(&decl.data.var_decl),
|
||||
.const_decl => |cd| self.registerTypedModuleConst(&cd),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Register a typed module-level value constant (`AF_INET :s32: 2`). Run in
|
||||
/// scanDecls pass 2 (after `resolveForwardIdentifierAliases`) so a forward
|
||||
/// identifier alias in the annotation (`A :: B; B :: s32; K : A : 42;`)
|
||||
/// resolves to its target rather than a fabricated empty-struct stub, which
|
||||
/// would otherwise mistype the constant (issue 0070).
|
||||
fn registerTypedModuleConst(self: *Lowering, cd: *const ast.ConstDecl) void {
|
||||
const ta = cd.type_annotation orelse return;
|
||||
switch (cd.value.data) {
|
||||
.int_literal, .float_literal, .bool_literal, .string_literal, .undef_literal, .null_literal => {
|
||||
const ty = self.resolveType(ta);
|
||||
self.program_index.module_const_map.put(cd.name, .{ .value = cd.value, .ty = ty }) catch {};
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
/// Register a top-level mutable global (e.g., `context : Context = ---;`).
|
||||
/// Run AFTER `resolveForwardIdentifierAliases` so a forward identifier alias
|
||||
/// in the type annotation (`A :: B; B :: s32; g : A = 7;`) resolves to its
|
||||
/// target instead of a fabricated empty-struct stub, which would otherwise
|
||||
/// give the global a type that mismatches its initializer at LLVM
|
||||
/// verification (issue 0070). Globals can't be named in a type position, so
|
||||
/// deferring them past type/alias registration introduces no ordering hazard.
|
||||
fn registerTopLevelGlobal(self: *Lowering, vd: *const ast.VarDecl) void {
|
||||
// Use self.resolveType so type aliases like `Handle :: u32;` resolve
|
||||
// to their target type (not a synthetic empty struct). When the
|
||||
// user omitted the annotation, infer from the initializer
|
||||
// expression; foreign globals with no annotation are diagnosed
|
||||
// because their type can't be inferred without an initializer.
|
||||
const var_ty: TypeId = if (vd.type_annotation) |ta|
|
||||
self.resolveType(ta)
|
||||
else if (vd.value) |val|
|
||||
self.inferExprType(val)
|
||||
else blk: {
|
||||
if (self.diagnostics) |d|
|
||||
d.addFmt(.err, null, "top-level var '{s}' has no type annotation and no initializer to infer from", .{vd.name});
|
||||
break :blk .void;
|
||||
};
|
||||
// Foreign globals reference a symbol defined in libSystem etc.
|
||||
// (`_NSConcreteStackBlock : *void #foreign;`). The C symbol
|
||||
// name is the optional override or the sx name itself.
|
||||
const sym_name = vd.foreign_name orelse vd.name;
|
||||
const name_id = self.module.types.internString(sym_name);
|
||||
const init_val: ?inst_mod.ConstantValue = if (vd.is_foreign) null else if (vd.value) |v| switch (v.data) {
|
||||
.undef_literal => .zeroinit,
|
||||
.int_literal => |il| .{ .int = il.value },
|
||||
.bool_literal => |bl| .{ .boolean = bl.value },
|
||||
.float_literal => |fl| .{ .float = fl.value },
|
||||
.string_literal => |sl| .{ .string = self.module.types.internString(sl.raw) },
|
||||
.array_literal => |al| self.constArrayLiteral(al.elements),
|
||||
.struct_literal => |sl| self.constStructLiteral(&sl, var_ty),
|
||||
else => null,
|
||||
} else null;
|
||||
const gid = self.module.addGlobal(.{
|
||||
.name = name_id,
|
||||
.ty = var_ty,
|
||||
.init_val = init_val,
|
||||
.is_const = false,
|
||||
.is_extern = vd.is_foreign,
|
||||
});
|
||||
self.program_index.global_names.put(vd.name, .{ .id = gid, .ty = var_ty }) catch {};
|
||||
}
|
||||
|
||||
/// Resolve identifier-RHS type aliases whose target is declared LATER in the
|
||||
|
||||
Reference in New Issue
Block a user