Skip to content

Modeled C stage 8: presentation retargeted (methods on the resource type; of/rename; fail-closed grouping) - #1601

Merged
ehartford merged 3 commits into
modeled-c-stage7from
modeled-c-stage8
Sep 24, 2026
Merged

ehartford merged 3 commits into
modeled-c-stage7from
modeled-c-stage8

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Stacked on stage 7 (#1597, modeled-c-stage7, rebased base 8976cbf). Draft until the seed-driven battery is green and posted (D56): stage 8 is its own batch (plan § Batching).

Commits: the mechanism (presentation on the resource type, of/rename, ambiguity, fail-closed assignment, in-place lends, the fixture migration) and the fixture flip. The mechanism is one commit: the name resolution (facade_render_present), the of narrowing (facade_method_host) and the assignment/ambiguity verification share one surface table and were not separable into independently :dev-green states without re-deriving intermediate code. Each commit is :dev-green.

Implements the plan's stage 8 paragraph (docs/modeled-c-implementation-plan.md) under the ruling docs/Ruling-modeled-C-ownership-effects-conventions-and-foreign-lifetimes.md §14, §53-§55, with §65/§66 respected: presentation changes spellings only; nothing here grants ownership, lending, destruction or success from a name; the SQLite facade is untouched.

Ruling sections implemented

  • §54 method presentation, §16.2b.11. Every operation a resource's rendering exposes — from/init constructors, destroys methods, lend methods — is spelled on the resource type by §16.2a's convention: the C name less the representation's snake-case prefix (db_new on Database wraps *mut db → Database.new; db_count → d.count(); db_shutdown → d.shutdown(); sqlite3_prepare_v2 on *mut sqlite3 → prepare_v2). No matching prefix keeps the C name (InflateStream.inflateInit, i.inflate(4)). Both the spelled and the unaliased representation name are candidates (z_streamp → z_stream).
  • §54 db.prepare(...). A producer whose first parameter receives another resource is presented as a method of that resource too, beside its constructor: d.prepare(sql) delegates to Statement.prepare(d, sql) (one body). Sema puts the constructor's declared dependency summary and its call effects (view invalidation, §38) on the receiver method as well.
  • §55 overrides. rename replaces the presented name; of assigns the operation. A rename is explicit and never yields to the convention.
  • §55 ambiguity omits the sugar. Two operations of one resource shortening to one name, a shortened name equal to another operation's imported or renamed name, or drop (the resource's Drop) → each keeps its imported name, and Sema warns naming the candidates and the rename that settles it (behav_c_facade_presentation_ambiguous, _ambiguous_warns pins the text). Two explicit spellings of one name are an error (err_c_facade_rename_collides). The compiler never picks.
  • §14, §53 multi-resource representations fail closed. An fn item whose first parameter receives a representation several resources wrap must state of naming one of them, or it is presented on none: an error naming the candidates with the of fix-it (err_c_facade_unassigned_multi_resource); of naming a resource the parameter does not receive is an error (err_c_facade_of_not_received). fn inflate / of InflateStream renders on that stream alone (behav_c_facade_presentation_in_place); every Sema net (borrowed returns, text views, call effects) goes through the same of-aware host.
  • Lend methods on in-place resources. Stage 4b rendered lends only on pointer resources; the z_stream test needs i.inflate(4) on a pinned resource, so a lend now hands C the pinned cell's address (D54), or &raw mut self.repr as a mut fn for a movable one through *mut.
  • Flip. behav_c_import_auto_method_baseline and _auto_constructor are what an app developer writes: a facade, c.incr(), Count.make(7), no unsafe. The without-facade variants keep unsafe as err_c_import_auto_method_without_facade_is_raw and err_c_import_auto_constructor_without_facade_is_raw.

Fixtures

Added: behav_c_facade_presentation_methods, behav_c_facade_presentation_in_place, behav_c_facade_presentation_ambiguous, behav_c_facade_presentation_ambiguous_warns; err_c_facade_unassigned_multi_resource, err_c_facade_of_not_received, err_c_facade_rename_collides, err_c_facade_presented_replaces_imported_name, err_c_import_auto_method_without_facade_is_raw, err_c_import_auto_constructor_without_facade_is_raw.
Flipped: behav_c_import_auto_method_baseline, behav_c_import_auto_constructor.
Migrated (spellings only, by a With script over resource R wraps T prefixes): 41 existing facade fixtures — Database.db_new → Database.new, s.st_step() → s.step(), Statement.st_new(d, …) → Statement.new(d, …); two expect-check-fail lines name the presented method (close_v2 on BorrowedDatabase, count on FailedDatabase).

Derived decisions

  1. Presentation applies only to operations the facade already exposes. §54 presents sqlite3_prepare_v2 as db.prepare "when a recognizable naming/receiver pattern suggests it", but §16.2b.5 says a facade-exposed operation is a lend by the facade's assertion, and §9 forbids "a destroying operation callable as a lend". An undescribed sqlite3_* function is therefore not lifted onto the resource by its name; it stays raw (unchanged err_c_facade_raw_lend_on_resource_stays_raw). Wrong here at worst removes capability.
  2. The prefix is the representation's struct name (§16.2a), not the resource's With name. Database wraps *mut sqlite3 shortens sqlite3_*; database_* would not be a C convention. Both the facade's spelling and the unaliased name are tried; the shortest remainder (longest prefix) wins, as §16.2a's ambiguity rule says.
  3. A shortened name that is a With keyword is silently not taken (it could not be spelled as a method); drop is taken as a candidate so the clash with the resource's Drop is reported, not silent (ruling §8: notes are normative tooling behavior).
  4. Ambiguity is a warning, not an error. The program is valid and every operation is reachable under its imported name (§55: "may simply be omitted while the underlying modeled foreign operation remains available"); the warning carries the candidates and the fix-it. Two explicit spellings of one name are an error — the facade said two things.
  5. One spelling per operation on the resource. The presented name replaces the imported one on the resource (d.db_count() is unknown; db_count(p) stays the raw surface) — §55's "remains available under its imported name" is the foreign operation, not a second method (err_c_facade_presented_replaces_imported_name).
  6. *const T receives a *mut T handle. counter_get(const Counter *) on Count wraps *mut Counter is a lend (C's own qualification conversion, ruling §61's reading for destroyers); Sema and the renderer make the same classification (facade_param_matches_repr / facade_render_const_of). Excluded for C-string representations, where const char * is a lent str (§16.3c), never an owned-text handle.
  7. A receiver method for a producer is rendered only when a borrow can hand C the parameter (the stage 6 presentability rule); an in-place init's constructor gets no receiver method (its self is the storage, not a parent).
  8. An fn item describing a resource's own clause operation (from, init, preinit, drop, destroys) adds facts to that operation and is not also a lend method beside its constructor; rename on such an item renames the constructor/destroyer.

Verification

./out/bootstrap/bin/with-stage1 check src/main.w green; with build :dev green on the rebased branch; analyze <fixture> audit:all ok on every new pointer-resource fixture. The two new in-place fixtures report the one pre-existing violation the seed already reports on stage 4b's behav_c_facade_resource_in_place_* fixtures (#1600). Sweep of every behav_c_facade_*, behav_c_import_*, behav_cstr_*, err_c_facade_*, err_c_import_*, err_cstr_* fixture under stage1: all pass except (a) behav_c_facade_libc_system_headers / behav_c_import_overlay_memchr — the memchr buf_in wrapper under a :dev stage1, the class the stage 7 PR body already records as failing on the stage 6 baseline (passes under the release binary); (b) the pre_d_build_runner fixtures, which need out/stage/out/release — their programs (pending_producer_warns, the new _ambiguous_warns) were checked directly under stage1: warning-free, and the two pinned warnings present. Not run here: :fixpoint, :test, :drop-audit, :move-audit (the battery).

Left unimplemented / found

Eric Hartford added 2 commits September 23, 2026 22:57
…ed methods and constructors, `of`/`rename`, ambiguity omits the sugar with a note, multi-resource representations fail closed (ruling §14, §53-§55; spec §16.2b.11)

Every operation a resource's rendering exposes is spelled on the resource
type by §16.2a's naming convention: the C name less the representation's
snake-case prefix (`db_new` on `Database wraps *mut db` is `Database.new`,
`db_count` is `d.count()`, `db_shutdown` is `d.shutdown()`), or the fn
item's `rename`. A producer whose first parameter receives another resource
is presented as a method of that resource too — `d.prepare(sql)` beside
`Statement.prepare(d, sql)` — delegating to the constructor; Sema puts the
constructor's dependency summary and call effects on both. Lend methods now
render on in-place resources (the pinned cell's address, `mut fn` for a
movable one through `*mut`), and a `*const T` parameter receives a `*mut T`
handle (C's own qualification conversion), so `counter_get(const Counter*)`
is a lend.

Ambiguity never guesses (§55): two operations of one resource shortening to
one name, or a shortened name that is another operation's imported name, or
`drop` (the resource's Drop), keep their imported names, and Sema warns
naming the candidates and the `rename` that settles it; two explicit
spellings of one name are an error. A shortened name that is a With keyword
is not taken.

Multi-resource representations fail closed (§14): an fn item whose first
parameter receives a representation several resources wrap must state `of`
naming one of them, or it is presented on none — an error naming the
candidates with the `of` fix-it; `of` naming a resource the parameter does
not receive is an error. `facade_method_host` narrows every Sema net by
`of`, so `fn inflate / of InflateStream` renders on that stream alone.

The existing fixtures are migrated to the presented spellings
(`Database.new`, `s.step()`, `Statement.prepare(d, …)`). Fixtures added:
behav_c_facade_presentation_methods, _in_place, _ambiguous,
_ambiguous_warns; err_c_facade_unassigned_multi_resource,
err_c_facade_of_not_received, err_c_facade_rename_collides,
err_c_facade_presented_replaces_imported_name.
…hat an app developer writes — no `unsafe` under a facade (`c.incr()`, `Count.make(7)`); the without-facade variants keep `unsafe` as err_c_import_*_without_facade_is_raw

The plan's stage 8 flip (docs/modeled-c-implementation-plan.md): under a
facade the auto-method surface lands on the resource type by the same
convention, and the resource is the safe surface of the representation, so
the program needs no `unsafe`. Without a facade the compiler cannot prove
the call, and the raw auto-methods and factory constructor stay raw; the two
compile_errors fixtures pin that.
@ehartford
ehartford added this pull request to stack #1602 September 24, 2026 03:01
… names too (`Database.new`, `Statement.prepare`, `s.step()`, `Texture.load`); two "constructors keep the C name" comments say what is now true

The migration script ran over test/behavior and test/compile_errors only;
native-phase-tests and debug-alloc-tests were red on the same class.
Verified under stage1: the four test/phase/c_facade_*.w fixtures pass and
tools/debug_drop.w reports the six test/debug_alloc/da_c_facade_*.w
fixtures PASS.
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery green — head 891f060 on the stage 7 + 8 tree (stage 7 @ 8976cbf on main 52d60d0), pinned seed, cold c_import cache, audits included:

build        rc=0
:fixpoint    rc=0
:drop-audit  rc=0
:move-audit  rc=0
:test        rc=0   (no red targets; the first run's phase/debug_alloc spelling misses are fixed in 891f0601)
:test-green  rc=0
:last-green  rc=0

Ready to merge after #1597: gh stack merge 1601 --squash lands both.

@ehartford
ehartford marked this pull request as ready for review September 24, 2026 03:44
@ehartford
ehartford merged commit d783366 into main Sep 24, 2026
@ehartford
ehartford deleted the modeled-c-stage8 branch September 24, 2026 16:57
ehartford pushed a commit that referenced this pull request Sep 27, 2026
…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.
ehartford pushed a commit that referenced this pull request Sep 27, 2026
…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.
ehartford pushed a commit that referenced this pull request Sep 27, 2026
…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.
ehartford pushed a commit that referenced this pull request Sep 27, 2026
…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.
ehartford pushed a commit that referenced this pull request Sep 27, 2026
…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.
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