Skip to content

Sema/Frontend: engine corpora never ambient (§18.2); bundle interface fns keep their module identity (#1362) - #1377

Merged
ehartford merged 4 commits into
fix-1430-enum-layout-orderfrom
fix-1362-bundle-internals-scope
Sep 23, 2026
Merged

ehartford merged 4 commits into
fix-1430-enum-layout-orderfrom
fix-1362-bundle-internals-scope

Conversation

@ehartford

@ehartford ehartford commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1362. Base: main (#1364 is merged). Commits: 1b87475 (first cut), 4df85ab (revision after review against §18.2), c9947a4 + 9766033 (emit-C battery reds, last section).

The spec (quoted exactly)

§18.1 Modules:

The module header is optional; a file without one is addressed by its path. One file is one module; directories group modules into hierarchical paths. use resolution searches, in order: the embedded standard library, paths relative to the importing module's directory, the project's lib/ roots, and the project root.

§18.2 Imports:

Prelude: The following are automatically imported into every module: Option.{Some, None}, Result.{Ok, Err}, Bool.{true, false}, primitive types, Unit, Vec[T], String / str, traits Eq, Ord, Hash, Debug, Display, Default, Drop, print, eprint, assert, assert_eq, assert_ne, require, check, panic, unreachable, todo, drop[T](val: T). (list abbreviated; full text in docs/with-specification.md §18.2)

Implicit standard-library availability. Every public declaration of the standard library is available by its unqualified name as the lowest-priority resolution tier. Fallback declarations retain canonical module identity — this tier is a lookup fallback, never injection into the module's declaration table.

Name precedence is deterministic, highest to lowest:

  1. Lexical bindings and generic parameters
  2. Current-module declarations
  3. Explicit imports and aliases
  4. The prelude
  5. Unique standard-library fallback

A user-controlled declaration is never shadowed, merged, or impl-captured by the fallback tier.

Fallback matching is exact — no fuzzy matching, ever. Two or more standard-library candidates for one name is a hard ambiguity error, with each candidate offered as an insertable-import fix-it; candidate ranking may order the suggestions but never resolves.

Engine packages are ordinary explicit dependencies and are never ambient; a public re-export from the standard library is deliberate promotion into the ambient vocabulary.

The rule this PR implements

  1. Engine corpora are never ambient. A migrated corpus that a .wo bundle provides (std.re, std.zl, std.tommyds, std.c_algorithms) is reached from user code only through the user's own use of one of its modules. A std module counts as engine when its directory holds a bundle.w root (Sema.record_engine_corpora). That is read from the interface registry, or from the source tree when the corpus compiles from source (stage1, bundle builds), so every stage classifies the same modules. Unimported u128_mul_would_overflow, check_match and c_int are errors, and the error names the use to add.
  2. A facade import does not expose its engine. module_visible_no_prelude enters an engine module only from the importing module itself or from another module of the same corpus. use std.zlib therefore does not bring in std.zl.defs. No std facade re-exports engine names, and the language has no pub use.
  3. Public std names do not become new errors. The §18.2 fallback tier is not built yet; that is Activate the std fallback tier: implicit standard-library availability (name-resolution D) #752 (D29 work item 3, after Campaign: canonical module-qualified identity for every declaration (name-resolution B) #751). This PR does not build it: tier 5 needs an index of every public std declaration, including modules the compilation never loaded, to decide uniqueness. D29 also makes the with update pinning migration a hard requirement that ships with the tier. Instead:
  4. Colliding interface functions are renamed (unchanged from 06ede15). On-demand interface declarations are merged after Frontend/Sema: a fn another module shadows keeps its module-qualified identity; each module's own calls bind it (#1350) #1364's rename step. A colliding one now gets its module-qualified name name$in$<module> (Zcu.displace_colliding_interface_fns). Sema's on-demand collection matches it by its short name, and its link name stays the bundle's. A user fn named like any bundle interface fn no longer breaks audit:all.

Closing the leak exposed a bug it had hidden: a generic declaration's type names were resolved in the module that asked for them. Proof from lldb on the new stage2, running check on SortedVec[i32], stopped on the new error:
emit_unknown_type_error ← resolve_type_expr ← resolve_generic_return_type_node ← type_layout_generic_struct_field_type ← eager_type_caches_pass.
SortedVec[T] { array: *mut _SortedArray } was resolved against the user's imports. A named type node now resolves in the module whose file holds it (resolve_type_expr_in_owner_module), the same way a generic fn body already does (check_generic_body).

Root cause of the leak

src/Sema.w, decl_visible_from_current_gated:

if current_is_std == 0 and target_is_std != 0 and self.module_in_prelude_closure(target_path) != 0:

Only the prelude modules and the modules they import directly were gated. std.re.defs is one step further (prelude → std.regex → std.re.defs), so no gate applied. The fallback visibility check (module_is_visible_from_current) then followed the prelude's own import edge and found it. The facade case (use std.zlib → std.zl.defs) went through the transitive walk in module_visible_no_prelude.

Enumeration against the revised rule

Sweep: with-stage2 check on every file, with #1364's stage2 (a9b7fc3) as "before" and this branch's stage2 as "after"; every error line diffed. Scope: src/main.w, lib/, rt/, tools/, test/, tests/, scripts/, runtime/, examples/, build.w, build/, build/release_uat_fixtures/, 3,116 files in all. Also checked: the raylib UAT in a with init + with get c.raylib project, and the 59 code blocks of docs/blog_posts/*.md. Then all 1,134 test/behavior programs were run on the new stage2. Non-zero exits were rerun serially on both compilers.

class where result
(a) compiler / stdlib / tests src, lib, rt, tools, test, tests, scripts, runtime, build.w, build/ no file newly fails. One file's messages changed: tests/cimport_scope/test_own_ci.w (legacy, run by no suite) fails before and after. Its error moves from "c_ulonglong is private to module c_user.w" to an engine c_uint gate error; the defect underneath is #1372. No use needed adding anywhere.
(b) examples examples/ none
(c) release UAT fixtures, blog all five build/release_uat_fixtures/*.w (raylib included), 59 blog code blocks none: same result before and after
  • Behavior run: 921 expect-stdout passes all match. The 81 non-zero exits fail identically on the base compiler (expected panics, check-fail fixtures, no-main fixtures).
  • Programs the test harnesses write as strings can't be checked as files. I scanned string literals in non-std files for the pcre2 interface's 2,174 public names. The only hits are migrator-output text that tests assert on, or programs that use their own migrated defs.

Fixtures

The four marked "fails on base" fail on #1364's stage2 and pass here.

  • test/compile_errors/err_1362_bundle_internal_unimported.w: u128_mul_would_overflow(1, 2) with no import is an error with add: use std.re.defs.u128_mul_would_overflow (fails on base).
  • test/compile_errors/err_1362_facade_import_no_engine_internals.w: use std.zlib then check_match(...) is an error with add: use std.zl.defs.check_match (fails on base).
  • test/behavior/behav_1362_std_helper_resolves_unimported.w: is_alnum with no import compiles and binds std.string's, next to a user fn is_digit(ch: u8). It passes on base too: it guards against this PR's own regression.
  • test/behavior/behav_1362_user_fns_named_like_bundle_interface_fns.w: a user fn for every non-generic engine defs.w helper, with regex, zlib and HashMap in use (fails on base with "declaration check_match has no finalized FnAbi signature").
  • test/behavior/behav_1362_bundle_interface_collision_audit.w: audit:all reports violations=0 on the previous fixture. A second program uses SortedVec and hash_index, so all four corpora take part; it runs, and its audit shows no FnAbi or codegen violation (fails on base).

The first cut's err_1362_std_helper_unimported is removed: it pinned the error that the revised rule withdraws.

Evidence: iterate tier only. with build :stage2 is green and check src/main.w is ok. The full battery has not run, so this PR stays a draft.

Open items

Battery red on emit-C (main + #1377 + #1420), fixed in c9947a4 / 9766033

The branch is rebased on origin/main (04e4934; #1364 is merged, so the base is main). The two #1362 commits are unchanged by the rebase (git range-diff: =).

Four targets failed, all emit-C: err_emit_c_dyn_trait_dispatch, err_emit_c_vec_get_disjoint, emit-c-smoke, and cli-selfhost-edge-tests (the build-options-emit-c case). None of them got as far as the construct it tests. Under --emit-c the prelude's std.regex compiles std.re in-unit, and emission stopped inside std.re first. With the diagnostic from this change:

emit-c could not resolve the type argument 'pcre2_real_code_8' of sizeof in 'find_minlength'
(resolved from module 'test/compile_errors/err_emit_c_dyn_trait_dispatch.w')

Evidence, driven by the pinned seed (WITH=$PWD/src/main src/main build :<target>):

target result
:native-compile-error-tests green, 889 files, both emit_c fixtures included
:emit-c-smoke green
:cli-selfhost-edge-tests green

The behavior suite on the rebased stage2 has no new failures against the previous run. The re-sweep of examples, the UAT fixtures (raylib in a project) and the blog blocks shows no new errors.

Filed during this work:

The full battery has not been re-run. This stays a draft until it is.

@ehartford
ehartford force-pushed the fix-1362-bundle-internals-scope branch from c4db9be to 24216b7 Compare September 23, 2026 00:04
@ehartford ehartford changed the title Sema/Frontend: bundle internals need a use (§18.2); a bundle interface fn keeps its module-qualified identity (#1362) Sema/Frontend: engine corpora never ambient (§18.2); bundle interface fns keep their module identity (#1362) Sep 23, 2026
@ehartford
ehartford changed the base branch from fix-1350-stage1-stdlib-visibility to main September 23, 2026 02:41
@ehartford
ehartford force-pushed the fix-1362-bundle-internals-scope branch from 24216b7 to 3071f27 Compare September 23, 2026 03:10
@ehartford
ehartford force-pushed the fix-1362-bundle-internals-scope branch from 3071f27 to 9766033 Compare September 23, 2026 04:44
ehartford pushed a commit that referenced this pull request Sep 23, 2026
…ed sizeof names its type argument (#1362)

The seed-driven battery of main + #1377 went red on four emit-C targets
(err_emit_c_dyn_trait_dispatch, err_emit_c_vec_get_disjoint, emit-c-smoke,
cli-selfhost-edge-tests build-options-emit-c). None of them reached the
construct it tests: under --emit-c the prelude's std.regex compiles std.re
in-unit, and emission stopped in std.re first:

  emit-c could not resolve the type argument 'pcre2_real_code_8' of sizeof
  in 'find_minlength' (resolved from module
  'test/compile_errors/err_emit_c_dyn_trait_dispatch.w')

(lldb on the stage2: CCodegen.fail <- emit_builtin_generic_call_term <-
emit_builtin_call_term <- emit_term <- emit_fn_body.) The refusals themselves
key on nothing #1377 renamed. CCodegen never pointed sema.current_module_path
at the function it emits, so resolve_type_level_arg_expr_frozen judged
visibility from the compilation root. Before #1377 the root reached
std.re.defs over the prelude edge by accident; engine corpora are no longer
ambient (§18.2). This is the #705 class the LLVM backend fixed in
Codegen.enter_decl.

- emit_fn_body sets sema.current_module_path to the function's declaring
  module (fn_symbol_source_path).
- global_decl_tid resolves a global's declared type in its declaring module
  (resolve_decl_type_frozen). Otherwise an extern var of an engine type
  failed to resolve and fell to the silent i32 fallback (filed #1429).
- The sizeof/alignof failure names the type argument, the function and the
  module it was resolved from. The old message named none, so the red could
  not be read without lldb.

native-compile-error-tests (889 files), emit-c-smoke and
cli-selfhost-edge-tests are green driven by the seed. emit-c-test is red
on a pre-#1377 compiler too, for another reason (#1434).
ehartford pushed a commit that referenced this pull request Sep 23, 2026
…red stderr instead of dying in ToolFs

run_emit_c_smoke_action and run_build_helper_programs_action reported a
failed subprocess by reading back its stderr capture with
`fs.read_text(<absolute capture path>)`. ToolFs accepts only
project-relative paths (tool_path_require_project_relative), so the report
itself exited with "ToolFs path escapes project root:
.../emit-c-smoke/emit-c-prelude-emit.stderr" and hid the compiler's error
(the #1377 battery). run_capture already returns the captured text, so the
report uses the result's `stderr`.
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery (seed-driven, new seed) on the PR head 9766033 (main + #1377, with the C-backend module fix and the loud emit-C failure report): build ok, :fixpoint ok, :test ok (no reds — err_emit_c_dyn_trait_dispatch, err_emit_c_vec_get_disjoint, emit-c-smoke, cli-selfhost-edge-tests all green), :test-green ok, :last-green ok.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 05:12
Eric Hartford added 4 commits September 23, 2026 04:43
…ace fn keeps its module-qualified identity beside a same-named fn (#1362)

1. The §18.2 prelude gate (Sema.decl_visible_from_current_gated) applied
   only when the target module was in the prelude closure — the prelude
   modules and the modules they name. std.re.defs sits one edge deeper
   (prelude -> std.regex -> std.re.defs), so its declarations skipped the
   gate and module_is_visible_from_current walked the synthetic prelude
   edge: `u128_mul_would_overflow(1, 2)`, `is_alnum(33)` and `c_int`
   compiled with no import. Every std target is gated now; a non-§18.2
   name resolves from user code only over an explicit import path
   (module_visible_no_prelude). std_gated_import_note offers the prelude
   closure's modules first, else any std module declaring the name
   (displaced declarations included), so the error names the `use`.

2. On-demand interface declarations (Zcu.merge_interface_sections_on_demand)
   are merged after the tier merge, so #1350's displacement never saw
   them: a colliding interface fn shared the flat symbol of a user or std
   fn (D39 interface_yields), and the codegen audit checked the interface
   signature against the other function ("finalized source type or LLVM
   parameter disagrees with FnAbi"); with several helpers codegen failed
   ("declaration check_match has no finalized FnAbi signature"). A
   colliding interface fn of another module is displaced to
   `name$in$<module>` (displace_colliding_interface_fns); Sema's lazy
   interface demand answers to its short name (interface_decl_is_named);
   the link name keeps the bundle's (fn_abi_module_link_name).

Sweep (stage2 check, before = #1364 stage2, after = this tree) over src/,
lib/, rt/, tools/, test/, tests/, scripts/, examples/, build.w, build/,
build/release_uat_fixtures/ (raylib via `with get c.raylib`) and the
docs/blog_posts code blocks: no file newly fails. Behavior: 1134 run,
no regressions. Two separate pre-existing bugs found: #1372, #1374.

Fixtures: err_1362_bundle_internal_unimported, err_1362_std_helper_unimported,
behav_1362_user_fns_named_like_bundle_interface_fns,
behav_1362_bundle_interface_collision_audit (audit:all).
…se them, and a public std name keeps resolving (§18.2, #1362)

Review of 06ede15 against §18.2 ("Every public declaration of the
standard library is available by its unqualified name as the
lowest-priority resolution tier"; "Engine packages are ordinary explicit
dependencies and are never ambient; a public re-export from the standard
library is deliberate promotion into the ambient vocabulary"): gating
every std module turned std.string's is_alnum, which programs called
unimported, into an error. The rule is now:

- Engine corpora (a std module whose directory holds a `bundle.w` root:
  std.re, std.zl, std.tommyds, std.c_algorithms; record_engine_corpora,
  read from the interface registry or the source tree, so stage1 and a
  bundle build classify them too) are never ambient: user code reaches
  one only through its own `use` of a module of that corpus.
  module_visible_no_prelude enters an engine module only from the start
  module or from its own corpus, so `use std.zlib` does not expose
  std.zl.defs (no facade re-exports; the language has no `pub use`).
- Non-engine std keeps the pre-#1362 rule: the D29 prelude gate on the
  prelude closure, nothing new.
- std_fallback_bridge_path: a name that resolved only because an engine
  twin was reachable over the prelude edge (is_alnum beside std.re.defs')
  resolves to the unique public non-engine std declaration — the one
  §18.2's tier 5 names. No program that compiled becomes an error; the
  full tier 5 is #752 (D29 work item 3, after #751).

Closing the leak exposed a context bug it had masked: a generic
declaration's type node was resolved in the requesting module
(type_layout_generic_struct_field_type -> resolve_generic_return_type_node
resolved SortedVec[T]'s `*mut _SortedArray` against the user's imports;
lldb: emit_unknown_type_error <- resolve_type_expr <-
resolve_generic_return_type_node <- type_layout_generic_struct_field_type
<- eager_type_caches_pass). A named type node now resolves in the module
whose file holds it (resolve_type_expr_in_owner_module), as a generic fn
body already does.

Fixtures: err_1362_std_helper_unimported replaced by
behav_1362_std_helper_resolves_unimported (compiles, binds std.string's);
new err_1362_facade_import_no_engine_internals (`use std.zlib` +
check_match). Sweep and behavior run: no file newly fails.
…ed sizeof names its type argument (#1362)

The seed-driven battery of main + #1377 went red on four emit-C targets
(err_emit_c_dyn_trait_dispatch, err_emit_c_vec_get_disjoint, emit-c-smoke,
cli-selfhost-edge-tests build-options-emit-c). None of them reached the
construct it tests: under --emit-c the prelude's std.regex compiles std.re
in-unit, and emission stopped in std.re first:

  emit-c could not resolve the type argument 'pcre2_real_code_8' of sizeof
  in 'find_minlength' (resolved from module
  'test/compile_errors/err_emit_c_dyn_trait_dispatch.w')

(lldb on the stage2: CCodegen.fail <- emit_builtin_generic_call_term <-
emit_builtin_call_term <- emit_term <- emit_fn_body.) The refusals themselves
key on nothing #1377 renamed. CCodegen never pointed sema.current_module_path
at the function it emits, so resolve_type_level_arg_expr_frozen judged
visibility from the compilation root. Before #1377 the root reached
std.re.defs over the prelude edge by accident; engine corpora are no longer
ambient (§18.2). This is the #705 class the LLVM backend fixed in
Codegen.enter_decl.

- emit_fn_body sets sema.current_module_path to the function's declaring
  module (fn_symbol_source_path).
- global_decl_tid resolves a global's declared type in its declaring module
  (resolve_decl_type_frozen). Otherwise an extern var of an engine type
  failed to resolve and fell to the silent i32 fallback (filed #1429).
- The sizeof/alignof failure names the type argument, the function and the
  module it was resolved from. The old message named none, so the red could
  not be read without lldb.

native-compile-error-tests (889 files), emit-c-smoke and
cli-selfhost-edge-tests are green driven by the seed. emit-c-test is red
on a pre-#1377 compiler too, for another reason (#1434).
…red stderr instead of dying in ToolFs

run_emit_c_smoke_action and run_build_helper_programs_action reported a
failed subprocess by reading back its stderr capture with
`fs.read_text(<absolute capture path>)`. ToolFs accepts only
project-relative paths (tool_path_require_project_relative), so the report
itself exited with "ToolFs path escapes project root:
.../emit-c-smoke/emit-c-prelude-emit.stderr" and hid the compiler's error
(the #1377 battery). run_capture already returns the captured text, so the
report uses the result's `stderr`.
@ehartford

Copy link
Copy Markdown
Contributor Author

Stack #1466 battery (seed-driven, new seed, cold c_import cache, with :drop-audit/:move-audit) on main 1c76062 + #1418 → #1419 → #1425 → #1428 → #1449 → #1377 → #1459 → #1460 → #1461 → #1462 (head cf36211, including the #1449/#1462 Codegen resolution and the fixed #1460): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Each branch holds exactly its tested commits on the layer below. Merge the whole stack: gh stack merge 1462 --squash.

@ehartford
ehartford merged commit cfcee17 into main Sep 23, 2026
ehartford pushed a commit that referenced this pull request Sep 23, 2026
…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 added a commit that referenced this pull request Sep 23, 2026
…d of being declared int (#1429) (#1524)

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.

Co-authored-by: Eric Hartford <[email protected]>
@ehartford
ehartford deleted the fix-1362-bundle-internals-scope branch September 24, 2026 16:54
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.

Bundle interface fns leak past the §18.2 prelude gate, and a same-named user fn shares their flat signature (audit:all fails)

1 participant