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:
agra
2026-06-02 17:20:31 +03:00
parent 49a383df6d
commit 932cdfa2ec
7 changed files with 225 additions and 52 deletions

View File

@@ -0,0 +1,22 @@
// Forward identifier type alias as a TOP-LEVEL annotation — a global var
// and a typed module constant whose annotation is a forward alias
// (`A :: B; B :: s32;`) resolve to the alias target, the same as the
// ordered form, instead of a fabricated stub.
// Regression (issue 0070): top-level global / typed-const annotations were
// resolved inside the scan loop BEFORE the forward-alias fixpoint ran, so
// `g : A` got a stub type that mismatched its initializer at LLVM
// verification. Global/const annotation resolution now runs in scan pass 2,
// after the fixpoint.
#import "modules/std.sx";
A :: B;
B :: s32;
g : A = 7;
K : A : 35;
main :: () -> s32 {
print("global g: {}\n", g);
print("const K: {}\n", K);
return g + K;
}

View File

@@ -0,0 +1 @@
42

View File

@@ -0,0 +1,2 @@
global g: 7
const K: 35

View File

@@ -1,9 +1,9 @@
@__SxFoo_state_ivar = internal global ptr null
@__SxFoo_class = internal global ptr null
@OS = internal global i64 0
@ARCH = internal global i64 0
@POINTER_SIZE = internal global i64 8
@__SxFoo_state_ivar = internal global ptr null
@__SxFoo_class = internal global ptr null
@__sx_default_context = internal global { { ptr, ptr, ptr }, ptr } { { ptr, ptr, ptr } { ptr null, ptr @__thunk_CAllocator_Allocator_alloc, ptr @__thunk_CAllocator_Allocator_dealloc }, ptr null }
@__sx_objc_cstr_dealloc = internal global [8 x i8] c"dealloc\00"
@str = private unnamed_addr constant [2 x i8] c"0\00", align 1