Skip to content

A view returned through a match or let-else pattern binding loses its origin: use-after-free accepted #2187

Closed

Description

@ehartford

Spec §21.1 rule 6; D22 (patterns are transparent to view origins).

type Thing { name: str }
enum Src ephemeral:
    One(&Thing)
    Two(&Thing)
fn at_match(s: &Src) -> &str:
    match s:
        .One(v) => &v.name
        .Two(v) => &v.name
fn outer() -> &str:
    let t = Thing { name: "alpha".to_owned() }
    let s: Src = .One(&t)
    at_match(&s)
print(outer())

Expected: returned view may outlive its origin at at_match(&s).
Actual: it compiles and prints freed data. explain:effect:at_match:s shows eff=[read]: no escape_view.

Two causes:

  1. A body whose tail is a view publishes its parameter escape effects only at body level, after the scope teardown, when a pattern binding origins are gone. This affects let-else too.
  2. The view-origin walkers (compute_expr_view_origin_mask, the storage mask, collect_expr_view_deps) do not walk match arms, so the facts frozen on each arm are never read.

Activity

  1. ehartford commented on Oct 5, 2026

    @ehartford
    ContributorAuthor

    Fixed on main by stack #2196 (merged as efb765c), battery green on a21e326; the regression test for it landed with the fix.

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