Skip to content

Spec-ruled fixes: Some(place) borrows for Option[&T], module globals are their own, unused import collisions are not errors - #1751

Merged
ehartford merged 10 commits into
gen-push-pullfrom
ruled-fixes
Sep 27, 2026
Merged

ehartford merged 10 commits into
gen-push-pullfrom
ruled-fixes

Conversation

@ehartford

@ehartford ehartford commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Stack layer 4. Fixes #1627 #1703 #1221. Pins #1500 (already fixed by #1601). Docs row for #1607 (/re/g.replace is sed's s///g; closed as specified). Each fix has a test shown failing on the base. Pending Eric: within the explicit-import tier, a named import (use m.X) beats a whole-module import (use m) for the name it selects; §18.2 does not order them (#993). Filed: #1743 #1744. Draft until the stack's one battery is green.

@ehartford
ehartford added this pull request to stack #1749 September 26, 2026 22:54
@ehartford ehartford changed the title ruled fixes Spec-ruled fixes: Some(place) borrows for Option[&T], module globals are their own, unused import collisions are not errors Sep 26, 2026
Eric Hartford added 10 commits September 27, 2026 00:36
…or `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.
…`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.
…ame 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".
…nly (§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.
…re/g.replace` (#1607: `g` is the regex spec's replace-all flag)
#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.
§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).
…d 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.
…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".
@ehartford

Copy link
Copy Markdown
Contributor Author

Stack battery GREEN on the stack top 5fc8160 (on main 2e8defa; host darwin-aarch64, pinned seed), run once for all six layers with with run tools/battery.w:

5fc8160a /Users/eric/.local/with-staging/battery-target
build rc=0 wall=409s
battery-checks rc=0 wall=1184s
GREEN

battery-checks = fixpoint, drop-audit, move-audit, every test target, user-programs-safe, test-green and last-green evidence. Merge the whole stack in order: gh stack merge 1761 --yes --squash.

@ehartford
ehartford marked this pull request as ready for review September 27, 2026 05:04
@ehartford
ehartford merged commit 682b77a into main Sep 27, 2026
@ehartford
ehartford deleted the ruled-fixes branch September 27, 2026 05:28
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.

Some(place) passed where Option[&T] is expected moves the place instead of borrowing it

1 participant