Skip to content

CCodegen: a global whose type cannot be recovered fails loudly instead of being declared int (#1429) - #1524

Merged
ehartford merged 1 commit into
fix-1413-range-flag-boolfrom
fix-1429-emit-c-global-type
Sep 23, 2026
Merged

ehartford merged 1 commit into
fix-1413-range-flag-boolfrom
fix-1429-emit-c-global-type

Conversation

@ehartford

@ehartford ehartford commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

global_decl_tid answered ty_i32 whenever it could not recover a global's
type: an extern var whose declared type did not resolve, a let whose
annotation did not resolve (it then took the initializer's type, which may
differ from the annotation), and an unannotated let whose initializer
Sema never typed. The emitted C then declared the global int — an extern
of another C type read the wrong bytes, a let stored its value truncated
(AGENTS.md No Silent Fallbacks). Until #1377 the root module's frozen
resolution reached the extern branch for any global whose type the root
cannot name; #1377 resolves in the declaring module, which removed that
route but left the fallback.

Now each case calls fail with the global's name and kind, the type's
spelling and the module it was resolved from, like the sizeof failure
since #1377, and returns 0, which both callers already skip.

No program reaches these branches on this tree, so no fixture reaches the
failure; the fix is that it can no longer be silent. Probed with --emit-c:
extern vars of arrays, pointers to arrays, tuples, fn types, Option,
Result and Vec, and unannotated lets of arrays, tuples, Option, str,
float, bool, suffixed ints and shifts all resolve. Emitting the whole
compiler (src/main.w) as C passes every global of every module through
the new check: emit_global_var_defs runs before any body, and both the
base stage1 and this one then stop in a body on the loud "HashMap
traversal lowering is not implemented" limitation.

The same probe showed CCodegen.global_init_text zero-initializing every
global whose initializer is not a literal (let SHIFTED = 1 << 3 emits
static int32_t ... = 0): filed as #1484.

Closes #1429

Stack #1528, bottom to top: #1522 (#1411) → #1523 (#1413) → #1524 (#1429) → #1525 (#1414) → #1526 (#1372) → #1527 (#1417). Each layer holds only its own commit.

Verification (iterate tier; no battery yet — draft until the seed-driven battery is green): stage1 and stage2 built from the stack top on darwin-aarch64 (SDKROOT CLT 15.4) and on native Windows 10 x64; fixtures below pass on both; check --validate-all swept over test/, examples/, lib/std (3188 files) with base stage2 (main f1e3bd2) vs the stack's stage2.

@ehartford

Copy link
Copy Markdown
Contributor Author

Stack #1528 battery (seed-driven, new seed, cold c_import cache, with :drop-audit/:move-audit) on main + #1522 → … → #1527 (head 33e6ba5; every PR head is a tested commit): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Merges cleanly into main. Merge the stack: gh stack merge 1527 --squash.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 16:45
…d of being declared int (#1429)

global_decl_tid answered `ty_i32` whenever it could not recover a global's
type: an `extern var` whose declared type did not resolve, a `let` whose
annotation did not resolve (it then took the initializer's type, which may
differ from the annotation), and an unannotated `let` whose initializer
Sema never typed. The emitted C then declared the global `int` — an extern
of another C type read the wrong bytes, a `let` stored its value truncated
(AGENTS.md No Silent Fallbacks). Until #1377 the root module's frozen
resolution reached the extern branch for any global whose type the root
cannot name; #1377 resolves in the declaring module, which removed that
route but left the fallback.

Now each case calls `fail` with the global's name and kind, the type's
spelling and the module it was resolved from, like the sizeof failure
since #1377, and returns 0, which both callers already skip.

No program reaches these branches on this tree, so no fixture reaches the
failure; the fix is that it can no longer be silent. Probed with --emit-c:
extern vars of arrays, pointers to arrays, tuples, fn types, Option,
Result and Vec, and unannotated lets of arrays, tuples, Option, str,
float, bool, suffixed ints and shifts all resolve. Emitting the whole
compiler (src/main.w) as C passes every global of every module through
the new check: emit_global_var_defs runs before any body, and both the
base stage1 and this one then stop in a body on the loud "HashMap
traversal lowering is not implemented" limitation.

The same probe showed CCodegen.global_init_text zero-initializing every
global whose initializer is not a literal (`let SHIFTED = 1 << 3` emits
`static int32_t ... = 0`): filed as #1484.
@ehartford
ehartford force-pushed the fix-1429-emit-c-global-type branch from 603e7fd to b79b2cd Compare September 23, 2026 19:40
@ehartford
ehartford removed this pull request from stack #1528 September 23, 2026 19:43
@ehartford
ehartford added this pull request to stack #1563 September 23, 2026 19:43
@ehartford
ehartford merged commit a5abfe5 into main Sep 23, 2026
@ehartford
ehartford deleted the fix-1429-emit-c-global-type branch September 24, 2026 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CCodegen.global_decl_tid: an extern var whose type does not resolve is silently emitted as i32

1 participant