Skip to content

D22: a record view of a Copy record loses its origin at an unannotated let through ? #3019

Closed

Description

@ehartford

D22 (§3.4, §16.2b.6): a record view &T of a Copy record loses its origin at let r = d.root()?; a second call over it is pinned to the local.

Found while implementing D187 (#2965). With an imported record that is Copy (typedef struct tnode { int tag; } tnode;) and a facade fn tdoc_root returns borrow tnode from param 0:

fn either(d1: &Document, d2: &Document) -> Option[&tnode] from d1, d2:
    let r1 = d1.root()?
    let r2 = d2.root()?
    r1.pick(r2, 0)      // tnode_pick: returns borrow tnode from params 0, 1

is refused: "either declares its returned view from d1, but its body never returns a view derived from d1" (and d2), then "returned ephemeral value may outlive its origin r1". explain:origin:either:r2 records no binding origins for r2 at all. Giving the record a pointer field (struct tnode *first;, no longer Copy) makes the same program compile and print the right node, and explain:origin then shows r2: origins=[1] storage=[1] deps=[d2].

D22: "A &T stays a reference during inference and pattern projection, including when T: Copy; it materializes an independent T only once an owned-value demand is established." An unannotated let through ? establishes none, so the view must keep its origin. Expected: the Copy record behaves as the non-Copy one. Repro files: the two programs differ only in the record definition (attached shape above; the full fixture shape is test/behavior/behav_d187_borrow_from_two_params.w with struct tnode *first removed).

Activity

  1. ehartford commented on Nov 3, 2026

    @ehartford
    ContributorAuthor

    Binding-form matrix (stage1 of the D187 branch, explain:origin:either:r2): unannotated let r2 = d2.root()? and let r2 = d2.root().unwrap() record no origins for r2; let r2: &tnode = d2.root()? and let Some(r2) = d2.root() else: return None record origins=[1] storage=[1] deps=[d2]. So the loss is in the unannotated let of a &T (T: Copy) call result, not in ?.

  2. ehartford commented on Nov 3, 2026

    @ehartford
    ContributorAuthor

    Root cause: D144 (check_let_binding, SemaCheck.w ~17423): an unannotated binding of a &T of plain bits takes the value, exempting only a type that "still views after the copy" (type_carries_view). An imported record a facade hands out as a view is foreign memory whose identity is its address (every operation through the view hands C that address), so the exemption must cover it. Fix on the D187 branch: type_carries_view_from answers true for a record some facade item names in returns borrow T (facade_record_is_view_type). Fixture: test/behavior/behav_3019_record_view_of_plain_bits_stays_a_view.w.

  3. added a commit that references this issue on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions