Skip to content

Spec §18.2 (D70): the last import wins; every import is also a namespace - #1752

Merged
ehartford merged 2 commits into
mainfrom
import-order-spec
Sep 26, 2026
Merged

ehartford merged 2 commits into
mainfrom
import-order-spec

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Eric's rulings on #1221 (2026-09-26):

  • "which comes last shadows the others" — among explicit imports, the last one wins; no ambiguity error.
  • "there still should be some way to get to the shadowed one, through namespaces" — every import is also a namespace: a module by its last path segment (math.PI) or full path (std.math.PI).
  • "default namespace for that, would be the name of the .h (minus the .h)" — a c_import's namespace is its header's file name (raylib.PI; <SDL3/SDL.h> is SDL).
  • use m as n renames a namespace, needed only when two would share a name.

Docs only (§18.2 + D70); spec-inventory-check green. Merging this is the blessing of the words. The compiler is NON-COMPLIANT until it implements namespaces (no qualified value access exists today); last-import-wins is being implemented on the stack (#1751).

Eric Hartford added 2 commits September 26, 2026 19:41
…last path segment, a c_import by its header's name without .h; `use m as n` renames
@ehartford
ehartford merged commit 5466e60 into main Sep 26, 2026
@ehartford
ehartford deleted the import-order-spec branch September 26, 2026 23:46
ehartford pushed a commit that referenced this pull request Sep 27, 2026
§18.2 (D70, #1752): a name a later import shadows stays reachable through
its import's namespace — a module's last path segment (`math.PI`) or its
full path (`std.math.PI`), a header's file name without `.h`
(`raylib.PI`), or the `as` name (`use m as n`, `use c_import(...) as n`).
None of these compiled before (`undefined variable`, `unknown qualified
extension method 'math.sqrt'`, `use … as` a parse error).

- Parser: `as NAME` after a `use` path or a `use c_import(...)`, kept in
  AstPool.use_alias_map.
- Resolve: each import carries its namespace name and its module's dotted
  path (ResolvedImport.namespace / module_text); inline C text has no
  namespace. Frontend.init_module_graph hands every namespace, c_imports
  included, to Sema (ns_modules/ns_names/ns_fulls/ns_targets/ns_offsets).
- Sema (D65: it owns the resolution): rewrite_namespace_access resolves
  `ns.member` — in check_expr for a value and at the top of check_call for
  a callee, before the method / qualified-extension reading — to the
  declaration that import provides: the module's own declaration, by the
  per-module identity #1703's displacement gives it; a c_import's value by
  the offset of its `use c_import` (decl_is_c_import). The node becomes that
  declaration's ident (Ast.bind_namespace_ident), marked so the short name is
  never re-resolved by import precedence; MIR and codegen read an ordinary
  ident. A namespace name is tier 3: a binding, visible type or fn of that
  name keeps it (a local `math` shadows the namespace). std.math's
  transcendental builtins (§17.6a) are reachable as `math.sqrt(x)`.
- Errors: two imports sharing a namespace name, at the use, naming both
  and offering `as`; a member the import does not provide; a private
  member. A callee the module does not declare stays a qualified extension
  call (`slug.tag(t)`). A namespaced type that another visible declaration
  shadows is refused loudly (types keep short names; #1757).
ehartford pushed a commit that referenced this pull request Sep 27, 2026
§18.2 (D70, #1752): a name a later import shadows stays reachable through
its import's namespace — a module's last path segment (`math.PI`) or its
full path (`std.math.PI`), a header's file name without `.h`
(`raylib.PI`), or the `as` name (`use m as n`, `use c_import(...) as n`).
None of these compiled before (`undefined variable`, `unknown qualified
extension method 'math.sqrt'`, `use … as` a parse error).

- Parser: `as NAME` after a `use` path or a `use c_import(...)`, kept in
  AstPool.use_alias_map.
- Resolve: each import carries its namespace name and its module's dotted
  path (ResolvedImport.namespace / module_text); inline C text has no
  namespace. Frontend.init_module_graph hands every namespace, c_imports
  included, to Sema (ns_modules/ns_names/ns_fulls/ns_targets/ns_offsets).
- Sema (D65: it owns the resolution): rewrite_namespace_access resolves
  `ns.member` — in check_expr for a value and at the top of check_call for
  a callee, before the method / qualified-extension reading — to the
  declaration that import provides: the module's own declaration, by the
  per-module identity #1703's displacement gives it; a c_import's value by
  the offset of its `use c_import` (decl_is_c_import). The node becomes that
  declaration's ident (Ast.bind_namespace_ident), marked so the short name is
  never re-resolved by import precedence; MIR and codegen read an ordinary
  ident. A namespace name is tier 3: a binding, visible type or fn of that
  name keeps it (a local `math` shadows the namespace). std.math's
  transcendental builtins (§17.6a) are reachable as `math.sqrt(x)`.
- Errors: two imports sharing a namespace name, at the use, naming both
  and offering `as`; a member the import does not provide; a private
  member. A callee the module does not declare stays a qualified extension
  call (`slug.tag(t)`). A namespaced type that another visible declaration
  shadows is refused loudly (types keep short names; #1757).
ehartford pushed a commit that referenced this pull request Sep 27, 2026
§18.2 (D70, #1752): a name a later import shadows stays reachable through
its import's namespace — a module's last path segment (`math.PI`) or its
full path (`std.math.PI`), a header's file name without `.h`
(`raylib.PI`), or the `as` name (`use m as n`, `use c_import(...) as n`).
None of these compiled before (`undefined variable`, `unknown qualified
extension method 'math.sqrt'`, `use … as` a parse error).

- Parser: `as NAME` after a `use` path or a `use c_import(...)`, kept in
  AstPool.use_alias_map.
- Resolve: each import carries its namespace name and its module's dotted
  path (ResolvedImport.namespace / module_text); inline C text has no
  namespace. Frontend.init_module_graph hands every namespace, c_imports
  included, to Sema (ns_modules/ns_names/ns_fulls/ns_targets/ns_offsets).
- Sema (D65: it owns the resolution): rewrite_namespace_access resolves
  `ns.member` — in check_expr for a value and at the top of check_call for
  a callee, before the method / qualified-extension reading — to the
  declaration that import provides: the module's own declaration, by the
  per-module identity #1703's displacement gives it; a c_import's value by
  the offset of its `use c_import` (decl_is_c_import). The node becomes that
  declaration's ident (Ast.bind_namespace_ident), marked so the short name is
  never re-resolved by import precedence; MIR and codegen read an ordinary
  ident. A namespace name is tier 3: a binding, visible type or fn of that
  name keeps it (a local `math` shadows the namespace). std.math's
  transcendental builtins (§17.6a) are reachable as `math.sqrt(x)`.
- Errors: two imports sharing a namespace name, at the use, naming both
  and offering `as`; a member the import does not provide; a private
  member. A callee the module does not declare stays a qualified extension
  call (`slug.tag(t)`). A namespaced type that another visible declaration
  shadows is refused loudly (types keep short names; #1757).
ehartford pushed a commit that referenced this pull request Sep 27, 2026
§18.2 (D70, #1752): a name a later import shadows stays reachable through
its import's namespace — a module's last path segment (`math.PI`) or its
full path (`std.math.PI`), a header's file name without `.h`
(`raylib.PI`), or the `as` name (`use m as n`, `use c_import(...) as n`).
None of these compiled before (`undefined variable`, `unknown qualified
extension method 'math.sqrt'`, `use … as` a parse error).

- Parser: `as NAME` after a `use` path or a `use c_import(...)`, kept in
  AstPool.use_alias_map.
- Resolve: each import carries its namespace name and its module's dotted
  path (ResolvedImport.namespace / module_text); inline C text has no
  namespace. Frontend.init_module_graph hands every namespace, c_imports
  included, to Sema (ns_modules/ns_names/ns_fulls/ns_targets/ns_offsets).
- Sema (D65: it owns the resolution): rewrite_namespace_access resolves
  `ns.member` — in check_expr for a value and at the top of check_call for
  a callee, before the method / qualified-extension reading — to the
  declaration that import provides: the module's own declaration, by the
  per-module identity #1703's displacement gives it; a c_import's value by
  the offset of its `use c_import` (decl_is_c_import). The node becomes that
  declaration's ident (Ast.bind_namespace_ident), marked so the short name is
  never re-resolved by import precedence; MIR and codegen read an ordinary
  ident. A namespace name is tier 3: a binding, visible type or fn of that
  name keeps it (a local `math` shadows the namespace). std.math's
  transcendental builtins (§17.6a) are reachable as `math.sqrt(x)`.
- Errors: two imports sharing a namespace name, at the use, naming both
  and offering `as`; a member the import does not provide; a private
  member. A callee the module does not declare stays a qualified extension
  call (`slug.tag(t)`). A namespaced type that another visible declaration
  shadows is refused loudly (types keep short names; #1757).
ehartford pushed a commit that referenced this pull request Sep 27, 2026
§18.2 (D70, #1752): a name a later import shadows stays reachable through
its import's namespace — a module's last path segment (`math.PI`) or its
full path (`std.math.PI`), a header's file name without `.h`
(`raylib.PI`), or the `as` name (`use m as n`, `use c_import(...) as n`).
None of these compiled before (`undefined variable`, `unknown qualified
extension method 'math.sqrt'`, `use … as` a parse error).

- Parser: `as NAME` after a `use` path or a `use c_import(...)`, kept in
  AstPool.use_alias_map.
- Resolve: each import carries its namespace name and its module's dotted
  path (ResolvedImport.namespace / module_text); inline C text has no
  namespace. Frontend.init_module_graph hands every namespace, c_imports
  included, to Sema (ns_modules/ns_names/ns_fulls/ns_targets/ns_offsets).
- Sema (D65: it owns the resolution): rewrite_namespace_access resolves
  `ns.member` — in check_expr for a value and at the top of check_call for
  a callee, before the method / qualified-extension reading — to the
  declaration that import provides: the module's own declaration, by the
  per-module identity #1703's displacement gives it; a c_import's value by
  the offset of its `use c_import` (decl_is_c_import). The node becomes that
  declaration's ident (Ast.bind_namespace_ident), marked so the short name is
  never re-resolved by import precedence; MIR and codegen read an ordinary
  ident. A namespace name is tier 3: a binding, visible type or fn of that
  name keeps it (a local `math` shadows the namespace). std.math's
  transcendental builtins (§17.6a) are reachable as `math.sqrt(x)`.
- Errors: two imports sharing a namespace name, at the use, naming both
  and offering `as`; a member the import does not provide; a private
  member. A callee the module does not declare stays a qualified extension
  call (`slug.tag(t)`). A namespaced type that another visible declaration
  shadows is refused loudly (types keep short names; #1757).
ehartford pushed a commit that referenced this pull request Sep 27, 2026
§18.2 (D70, #1752): a name a later import shadows stays reachable through
its import's namespace — a module's last path segment (`math.PI`) or its
full path (`std.math.PI`), a header's file name without `.h`
(`raylib.PI`), or the `as` name (`use m as n`, `use c_import(...) as n`).
None of these compiled before (`undefined variable`, `unknown qualified
extension method 'math.sqrt'`, `use … as` a parse error).

- Parser: `as NAME` after a `use` path or a `use c_import(...)`, kept in
  AstPool.use_alias_map.
- Resolve: each import carries its namespace name and its module's dotted
  path (ResolvedImport.namespace / module_text); inline C text has no
  namespace. Frontend.init_module_graph hands every namespace, c_imports
  included, to Sema (ns_modules/ns_names/ns_fulls/ns_targets/ns_offsets).
- Sema (D65: it owns the resolution): rewrite_namespace_access resolves
  `ns.member` — in check_expr for a value and at the top of check_call for
  a callee, before the method / qualified-extension reading — to the
  declaration that import provides: the module's own declaration, by the
  per-module identity #1703's displacement gives it; a c_import's value by
  the offset of its `use c_import` (decl_is_c_import). The node becomes that
  declaration's ident (Ast.bind_namespace_ident), marked so the short name is
  never re-resolved by import precedence; MIR and codegen read an ordinary
  ident. A namespace name is tier 3: a binding, visible type or fn of that
  name keeps it (a local `math` shadows the namespace). std.math's
  transcendental builtins (§17.6a) are reachable as `math.sqrt(x)`.
- Errors: two imports sharing a namespace name, at the use, naming both
  and offering `as`; a member the import does not provide; a private
  member. A callee the module does not declare stays a qualified extension
  call (`slug.tag(t)`). A namespaced type that another visible declaration
  shadows is refused loudly (types keep short names; #1757).
ehartford added a commit that referenced this pull request Sep 27, 2026
…are their own, unused import collisions are not errors (#1751)

* Sema/MIR: a place against a `&T` enum payload borrows — `Some(ctx)` for `Option[&Ctx]` observes `ctx` (§3.8, D22)

Fixes #1627

- Sema: check_call's no-signature argument loop marked every variant
  constructor argument moved, whatever the payload type; the static
  `Option[&Ctx].Some(x)` path did the same. payload_arg_auto_refs asks the
  payload demand (can_auto_ref_arg): a `&T` payload borrows the place
  (check_borrow_create) and records the node in auto_ref_payload_args, which
  collect_expr_view_deps reads as `&place` — so the Option carries the view's
  origin and a write to `ctx` while `z` is live is refused.
- Sema: `.Some(x)` (check_variant_shorthand_args) marked nothing at all, so
  `.Some(ctx)` into an owned `Option[Ctx]` left `ctx` usable over its
  moved-out bytes (#764 class); it now consumes or borrows by the same rule.
- MIR: the call-form and static constructors lowered the argument plainly —
  `move ctx` into the `&Ctx` slot, blanking `ctx` (analyze audit:all:
  "use-after-kill"). lower_payload_arg applies the #933 adjustment chain
  (auto-ref, contextual copy, auto-deref) for all three spellings.
- Validator: validate-all said ok over that aggregate. The typed validator
  now rejects an enum payload that is a value where the payload is a
  reference to it (mir_validate_aggregate_missing_borrow), pinned by a
  hand-built MIR test.

* Frontend/Sema: a module's top-level values are its own — a colliding `let`/`const`/`global` of another module is displaced, not "shadowing is not allowed" (§18.2, §18.3)

Fixes #1703

Every module's `let`, `var`, `const` and `global` shared one flat Sema
table keyed by name, so register_top_level_global_decl reported "shadowing
is not allowed" for the second declaration of a name in any module: a
program's `const PACKAGE` beside the source-compiled std.re's `pub let
PACKAGE` (stage1 only — the release binary reads std.re as bundle-interface
storage, which already had its own table), two modules' private `SCALE`, a
program's `let PI` beside `use std.math`.

The fix is #1350's for fns, applied to values: displace_colliding_globals
(run once every source, interface and c_import declaration is in the pool)
keeps the short name for the root module's declaration, then the first
other owner, and gives each other owner's declaration its module-qualified
identity `name$in$<module>`. Sema records it (record_displaced_fn, now
also for module values) and resolve_displaced_global_ident binds each
reference: a lexical binding; the current module's own declaration (a
c_import's value is an import, not the importer's declaration); the flat
declaration when visible; else a visible displaced one. The ident is
rewritten, so MIR, comptime and codegen read the declaration Sema chose.

The repro still failed on a stage1 built from this tree before the change
(`shadowing is not allowed for 'PACKAGE' (a global of module
lib/std/re/defs.w)`), so #1708 had not closed it.

* Sema: two imports that provide one name are an error only where the name is used; a named import provides only what it selects (§18.2)

Fixes #1221

`use c_import("raylib.h")` beside `use std.math` (or even `use
std.math.TAU`) was "shadowing is not allowed for 'PI'" at the header's
declaration, in a program that never names PI. With module values
displaced per module (#1703) the declarations coexist; this change decides
the uses by §18.2 precedence (resolve_displaced_global_ident):

- tier 2: the current module's own declaration;
- tier 3: the explicit imports that provide the name — a c_import provides
  its values to the module that imports it, `use m` provides m's public
  names, and `use m.X` / `use m.{X, Y}` provides the names it selects and
  nothing else (Resolve records them: ResolvedImport.selected, carried to
  Sema as module_import_selected); a named import beats a whole-module one;
- tier 5: the one declaration the std fallback reaches.

Two providers at the tier that decides a use is an error at that use —
"'PI' is ambiguous: `std.math` and this module's c_import both provide it",
a label at each declaration, and the spelling that picks one ("a named
import picks one: `use std.math.PI`"). A same-module redeclaration keeps
"shadowing is not allowed".

* tests: pin that a producer's `lend` fn item renders its constructor only (§16.2b.4, ruling §9)

Fixes #1500

The bug does not reproduce on this tree: Modeled C stage 8 (d783366,
#1601) made facade_render_lend_item refuse an fn item that names a
resource operation (facade_render_is_resource_op: a `from`, `init`,
`preinit`, `drop` or `destroys` of any resource), so `fn db_prepare /
lend` beside `from db_prepare(out param 2)` adds facts to the constructor
and renders no lend method. The installed compiler (main) and this
branch's base both render only `Statement.prepare(d, sql, tail)` and the
receiver form `Database.prepare(sql, tail)`. Nothing pinned it: this test
calls the parent with an out slot and expects the one `prepare` Database
has to refuse it.

* docs: the one-liner table shows sed's global regex substitution as `/re/g.replace` (#1607: `g` is the regex spec's replace-all flag)

* Sema: the later import shadows the earlier (§18.2, Eric's ruling; Fixes #1221)

Fixes #1221
Fixes #993

Eric's ruling on explicit imports that provide one name: "which comes last
shadows the others." There is no ambiguity error among explicit imports and
no named-vs-whole-module ordering; a `use c_import(...)` is an import and
takes its place in the order. Tiers are unchanged.

- Values (resolve_displaced_global_ident): lexical, then the current
  module's own declaration, then last_import_provider — of the candidates an
  explicit import of the current module provides, the one whose import sits
  at the highest offset — then the std fallback, where D29's ambiguity for two
  std candidates stays (emit_ambiguous_fallback_use). The use-site ambiguity
  error of the previous commit is gone.
- Import order: Resolve's span_start of each `use` is carried to Sema as
  module_import_offsets; a c_import declaration's decl_is_c_import is now
  1 + the offset of its importing `use c_import` (was 1). A named import
  still provides only what it selects; the prelude edge is tier 4.
- Fns (#993) share the mechanism: resolve_displaced_fn_ident asks
  last_import_provider after the current module's own fn. The flat merge
  had ordered modules by dependency, so `use c` then `use a` (c importing a)
  called c's helper whatever the program wrote.

Tests: both orders of a header `PI 3` and `use std.math` (3.14159 / 3),
a module's own declaration beating the header, same-module redeclaration
still an error, and #993's two orders; err_1221_ambiguous_import_use is
removed with the error it pinned.

* Sema: every import is also a namespace (§18.2, D70)

§18.2 (D70, #1752): a name a later import shadows stays reachable through
its import's namespace — a module's last path segment (`math.PI`) or its
full path (`std.math.PI`), a header's file name without `.h`
(`raylib.PI`), or the `as` name (`use m as n`, `use c_import(...) as n`).
None of these compiled before (`undefined variable`, `unknown qualified
extension method 'math.sqrt'`, `use … as` a parse error).

- Parser: `as NAME` after a `use` path or a `use c_import(...)`, kept in
  AstPool.use_alias_map.
- Resolve: each import carries its namespace name and its module's dotted
  path (ResolvedImport.namespace / module_text); inline C text has no
  namespace. Frontend.init_module_graph hands every namespace, c_imports
  included, to Sema (ns_modules/ns_names/ns_fulls/ns_targets/ns_offsets).
- Sema (D65: it owns the resolution): rewrite_namespace_access resolves
  `ns.member` — in check_expr for a value and at the top of check_call for
  a callee, before the method / qualified-extension reading — to the
  declaration that import provides: the module's own declaration, by the
  per-module identity #1703's displacement gives it; a c_import's value by
  the offset of its `use c_import` (decl_is_c_import). The node becomes that
  declaration's ident (Ast.bind_namespace_ident), marked so the short name is
  never re-resolved by import precedence; MIR and codegen read an ordinary
  ident. A namespace name is tier 3: a binding, visible type or fn of that
  name keeps it (a local `math` shadows the namespace). std.math's
  transcendental builtins (§17.6a) are reachable as `math.sqrt(x)`.
- Errors: two imports sharing a namespace name, at the use, naming both
  and offering `as`; a member the import does not provide; a private
  member. A callee the module does not declare stays a qualified extension
  call (`slug.tag(t)`). A namespaced type that another visible declaration
  shadows is refused loudly (types keep short names; #1757).

* MIR: a constructor payload borrows where Sema recorded the borrow, and re-derives no other adjustment (#1627 follow-up)

The #1627 fix made the call-form (`Some(x)`) and static (`Option[&T].Some(x)`)
constructors lower every payload through lower_payload_arg — the
shorthand's whole type-driven call-argument chain (auto-ref, contextual
copy, auto-deref). On a generic specialization that chain reached
MirBuilder.lower_auto_deref_call_arg, whose type walk asks
Sema.type_implements_trait_frozen about a generic instance no Sema pass had
preregistered, and the frozen phase check aborted
test/spec/spec_ss10_8_error_trait.w (std.result's ContextError path):
"BUG: type_implements_trait_frozen generic blanket path needs preregistered
answer" (lldb: lower_concrete_specialization → lower_expr's NK_CALL
constructor arm → lower_payload_arg → lower_auto_deref_call_arg).

The cause is MIR deciding what Sema already decided (D65). Sema records
every payload it borrows (auto_ref_payload_args, payload_arg_auto_refs);
lower_constructor_payload_arg now borrows exactly those nodes — a recorded
borrow MIR cannot realize is a phase bug — and lowers every other payload
as before (`lower_expr`). The shorthand `.V(x)` path keeps its own #933
chain, unchanged from before #1627.

* Sema: a nullable facade callback's `Some(...)` userdata is borrowed, a temporary as a place (#1627/#1618)

A nullable callback's userdata parameter is `Option[&U]`, but
Sema.facade_prepare_callback_call checks the userdata argument with no
expected type (its `U` is bound from that argument), so `Some(Ctx { .. })`
checked as `Option[Ctx]`: check_call's variant loop took the payload as
owned (payload type `T`), payload_arg_auto_refs never saw a `&T` demand,
and nothing recorded the borrow. MIR then retyped the constructor to the
parameter's `Option[&Ctx]` and, since f272a5d borrows only where Sema
recorded it, stored the temporary's value in the `&Ctx` slot; the typed
validator refused it (behav_sqlite_facade_end_to_end.w:54, "enum payload 0
is a value where the variant's payload is a reference to it"). Before
f272a5d MIR borrowed by type on its own; main never drops the temporary
(debug-alloc: leak count=2) and reports `Some(ctx)` of a place as a move.

facade_prepare_callback_call now marks the userdata constructor it checks
(Sema.facade_userdata_ctor) when the callback is nullable, and
payload_arg_auto_refs borrows an owned payload of that constructor as it
borrows one against a `&T` payload: the borrow is recorded
(auto_ref_payload_args), a place stays live (check_borrow_create, no
move), and MIR materializes a temporary and borrows it, dropped once at
the end of the statement.

behav_c_facade_callback_userdata_temporary.w pins a temporary (dropped
once, after its call's line), the static `Option[&Ctx].Some(..)` spelling
and a place used again after the call; on f272a5d's stage1 the temporary
case is the validator error above and the place case "use of moved value".

* MirValidationTests: the #1627 test fn needs no explicit Unit return (unit-return-review)

---------

Co-authored-by: Eric Hartford <[email protected]>
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.

1 participant