Skip to content

Frontend/Sema: a fn another module shadows keeps its module-qualified identity; each module's own calls bind it (#1350) - #1364

Merged
ehartford merged 2 commits into
mainfrom
fix-1350-stage1-stdlib-visibility
Sep 23, 2026
Merged

ehartford merged 2 commits into
mainfrom
fix-1350-stage1-stdlib-visibility

Conversation

@ehartford

@ehartford ehartford commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1350

Root cause (defect 1)

src/compiler/Frontend.w, the flat import merge (frontend_fn_shadowed_in_tier, called at the prelude tier and the user-import tier): every fn decl whose short name a higher-precedence decl also declares was dropped, whichever module it belonged to. Sema's flat signature table then bound every module's bare reference to the survivor. lldb on the seed-built stage1 (isd.w, breakpoints on Sema.symbol_visible_from_current with $x1 == sym(is_digit) and on Sema.record_decl_visibility): record_decl_visibility ran for is_digit only for the root's node (is_pub=0), never for std.re.defs'; symbol_visible_from_current walked a one-record chain (path isd.w, private) → emit_private_symbol_error from check_call.

Consequences on every compiler, not only stage1:

§18.1: current-module declarations outrank imports and the prelude.

Fix

A shadowed fn of another module is displaced, not dropped: the frontend renames it name$in$<canonical module path> (frontend_fn_tier_verdict, frontend_displace_fn_decl); Sema records it under its short name (record_displaced_fn) and binds a bare reference in precedence order — lexical binding, the current module's own decl, the visible flat winner, a visible displaced decl — and rewrites the ident node so MIR/comptime/codegen read what Sema checked (resolve_displaced_fn_ident in check_ident, check_call, check_generic_pipeline_call). Diagnostics keep the short name (set_pretty_symbol). In module-object/bundle mode the link name drops the suffix (fn_abi_module_link_name) — the module prefix already qualifies it, so a bundle's .wi name still links. Same-module duplicates, externs, interface decls, c_export fns and embedded-runtime defs keep the drop. Interface decls (bundle .wi) are filed as #1362 with the §18.2 gate leak they carry.

Visible effects of the displacement: in the compiler itself one fn (ClangBridge.c_strlen, identical to LlvmBridge's) now binds its own copy; tommyds' per-module tommy_ctz_u32… copies each bind their own module's (were silently merged).

Defect 2 (stage1 vs stage2): by design, not a seed miscompile

stage1 links zero-length .wo blobs (build.w 425–431: "only bootstrap-lib — stage1 is linked before the tree's bundle exists — carries zero-length blobs"; Compilation.register_embedded_bundle_interfaces: "An unfilled slot (stage1) has nothing to register"; docs/wo_bundles.md "Bootstrap chain": stage1 is the compiler that builds the tree's bundles). With no interface registered, the prelude's use std.regex resolves std.re.* as source from lib/std/re, so every stage1 compile checks all of pcre2's bodies (and, outside the checkout, stage1 cannot find std.re.defs at all — the corpus is excluded from the embedded stdlib). stage2 registers the embedded .wi and never checks std.re bodies, so the same defect-1 misbinding stayed invisible there (and silent for std.string). No change to register_embedded_bundle_interfaces.

Evidence (iterate tier)

  • stage1 (seed-built) and stage2 from this branch: isd.w ok; new fixtures pass; both fixtures fail on the installed compiler (is_alnum(33) assertion; "is_digit is not visible").
  • with check src/main.w ok; :dev and :stage2 green.
  • stage2 over test/behavior (simple expect-stdout cases): see comment below.

Full battery not run (draft until it is).

Batch-battery reds (dd786a4 + #1355 #1357 #1364 #1370 #1371), fixed in 7c82e3f

1. err_no_std_use_std_sync_leak reported Order requires std at <embedded-std>/std/collections.w:330. collections.w's fence is now displaced (sync.w's fence takes the short name) and so collected. Sema.record_displaced_fn is a mut fn and bound let path = if self.current_module_path.len() > 0: self.current_module_path else: "". That if-arm moves the field out of the receiver without any diagnostic (#1395, a D32 miss; repro in the issue). collect_fn_decl then resolved fence's parameter types with current_module_path == "", so the --no-std tier gate treated std's own declaration as user code. Evidence: an instrumented print at require_std_tier_for_symbol showed cur= empty right after collect_declarations had set <embedded-std>/std/collections.w for that decl (the stage1 has no variable DWARF, so this was a temporary print, now removed). The same shape was already in record_named_type_with_pub (a mut fn) and in the embed_file check, so all three now clone the path. The test is unchanged, and stage1 and stage2 now report error: std.sync requires std at the user file. test/compile_errors sweep on stage2: 852 pass, 0 fail.

2. abi-hash-check: re-recorded, no WITH_ABI_VERSION bump. fn_abi_module_link_name gains one clause: a displaced <name>$in$<module> links as __with_mod_<hash>__<name>. No existing symbol's link name changes, because no source name can contain $. Evidence, bundles built by main's (dd786a4) stage1 vs this branch's:

  • pcre2, zlib and c_algorithms: identical symbol tables.
  • tommyds: 0 symbols removed or renamed; 84 added — per-module private helpers (tommy_ctz_u32, tommy_ilog2_u32, …) that the old merge collapsed into one.
  • all four .wi interfaces: byte-identical.

docs/with-abi.md §5 states the rule. docs/with-abi.sha256 is re-recorded. This PR touches FnAbi.w and needs its own battery.

stage2 behavior sweep after the fix: 894 pass, 0 fail.

@ehartford

Copy link
Copy Markdown
Contributor Author

Iterate-tier sweep: stage2 from a9b7fc3 over test/behavior (every expect-stdout fixture without args/env/only-on; exit code only): pass=890 fail=0 skipped=307. stage1 over the shadow/module/prelude/regex/generic/extension/private/zlib subset: pass=33 fail=0. Full battery not run; stays draft.

…eps its module-qualified identity; each module's own calls bind it (#1350)

The flat import merge (Frontend.w, prelude and user-import tiers) dropped
every fn decl whose short name a higher-precedence decl also declared,
whichever module either lived in. The flat signature table then bound
every module's bare reference to the survivor:

- a user `fn is_digit(ch: u8)` became the callee of std.string's
  is_alnum: `is_alnum(33)` returned true on every compiler (silent);
- std.re.defs' own `is_digit(c)` resolved to the user's private fn and
  failed "symbol 'is_digit' is not visible from this module" wherever
  std.re is compiled in-unit (stage1, which embeds no .wo bundles);
- two user modules each defining a private `f` broke the earlier one.

§18.1 precedence puts current-module declarations above imports and the
prelude. A shadowed fn of ANOTHER module is now displaced, not dropped:
the frontend renames it `name$in$<canonical module path>`
(frontend_fn_tier_verdict / frontend_displace_fn_decl); Sema records it
under its short name (record_displaced_fn) and binds a bare reference, in
precedence order — lexical binding, the current module's own decl, the
visible flat winner, a visible displaced decl — rewriting the ident node
so MIR/comptime/codegen read the declaration Sema checked
(resolve_displaced_fn_ident at check_ident, check_call and the generic
pipeline). Diagnostics keep the short name; a module-object/bundle link
name drops the suffix (its module prefix already qualifies it).
Same-module duplicates, externs, interface decls, c_export fns and
embedded runtime defs keep the drop. Interface decls are #1362.

Stage1 vs stage2: not a seed miscompile. stage1 links zero-length .wo
bundle blobs by design (build.w "only bootstrap-lib ... carries
zero-length blobs"; stage1 builds the tree's bundles), so it compiles
std.re from lib/std/re source for every program (prelude -> std.regex);
stage2 reads the embedded .wi and never checks std.re bodies.

Fixtures: behav_1350_user_fn_names_std_helpers (every defs.w helper name
defined by the user; regex and zlib still run; is_alnum stays std's),
behav_1350_module_fn_same_name (owner/importer modules: direct call, fn
value, pipeline, generic, same-name local, importer binding).
…he ABI hash for the displaced-fn link-name rule (#1350)

1. err_no_std_use_std_sync_leak regressed on #1364's first commit: the
   --no-std gate reported `Order requires std` at
   <embedded-std>/std/collections.w:330 (`pub fn fence(order: Order)`)
   before the user's `use std.sync`. collections.w's fence is now
   displaced (sync.w's fence takes the short name) and so collected;
   Sema.record_displaced_fn, a `mut fn`, bound
   `let path = if self.current_module_path.len() > 0: self.current_module_path else: ""`
   — an if-arm field move that vacates the receiver's field without a
   diagnostic (#1395, a D32 miss). collect_fn_decl then resolved fence's
   parameter types with current_module_path == "" (instrumented print at
   require_std_tier_for_symbol: cur= after collect_declarations set
   <embedded-std>/std/collections.w), so the tier gate treated std's own
   declaration as user code. The same shape sat in
   record_named_type_with_pub (also a `mut fn`) and the embed_file check;
   all three now clone the path.

2. abi-hash-check: fn_abi_module_link_name gained one clause — a
   displaced `<name>$in$<module>` links as `__with_mod_<hash>__<name>`.
   No existing symbol's link name changes (no source name contains `$`):
   the four bundles built by this branch's stage1 vs main's (dd786a4)
   have identical .wi and a symbol diff of 0 removals; pcre2, zlib and
   c_algorithms are identical, tommyds only ADDS 84 per-module private
   helpers (tommy_ctz_u32 & co.) the old merge collapsed into one. So no
   WITH_ABI_VERSION bump; §5 states the rule and the hashes re-record.
@ehartford
ehartford force-pushed the fix-1350-stage1-stdlib-visibility branch from a9b7fc3 to 7c82e3f Compare September 23, 2026 00:44
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery (seed-driven, alone as an ABI change, rebased on main dd786a4 → 7c82e3f): build ok, :fixpoint ok, :test ok (no reds — err_no_std_use_std_sync_leak and abi-hash-check green), :test-green ok, :last-green ok.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 01:54
@ehartford
ehartford merged commit 137c33f into main Sep 23, 2026
ehartford pushed a commit that referenced this pull request Sep 23, 2026
…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).
ehartford pushed a commit that referenced this pull request Sep 23, 2026
…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).
ehartford pushed a commit that referenced this pull request Sep 23, 2026
…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).
ehartford added a commit that referenced this pull request Sep 23, 2026
… fns keep their module identity (#1362) (#1377)

* Sema/Frontend: bundle internals need a `use` (§18.2); a bundle interface 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).

* Sema: engine corpora are never ambient, a facade import does not expose 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.

* CCodegen: frozen type resolution runs in the declaring module; a failed 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).

* build/selfhost.w: an emit-C or helper build failure reports the captured 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`.

---------

Co-authored-by: Eric Hartford <[email protected]>
@ehartford
ehartford deleted the fix-1350-stage1-stdlib-visibility 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.

Seed-built stage1: a user fn named is_digit hides std.re's is_digit ("not visible from this module"); stage2 from the same source accepts

1 participant