Skip to content

A view into a statement temporary cannot be bound or returned (#962) - #1240

Merged
ehartford merged 1 commit into
mainfrom
reject-view-into-temporary
Sep 21, 2026
Merged

ehartford merged 1 commit into
mainfrom
reject-view-into-temporary

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #962.

let path = line.split("\t")[1] (and .get(1)) bound a view into the Vec that split() returned; the Vec died at the end of the statement and safe code read freed memory — empty text under the debug allocator, garbage comparisons otherwise (path != "docs" true, == "docs" false). It surfaced as a with -n two-line snippet that seemed to ignore its if (#1238, closed as a duplicate; the wrapper was correct).

Fix (Sema). record_view_producer_origins marks a view whose receiver is not a place and owns storage (PK_NotPlace and type_needs_drop); transparent carriers propagate the mark. Binding such a view with let, or returning it — check_returned_view_origins for a &T tail or return statement, check_returned_ephemeral_value_origins for other ephemeral values; lldb on the repro showed a -> &str tail takes only the former — is a compile error naming the temporary's type and both fixes (bind the collection first, or take an owned value). A use inside the statement is untouched: line.split("\t")[1] == "docs" still compiles.

A first cut required the temporary to borrow nothing, so a function whose receiver root was a parameter slipped through; a temporary is a temporary whatever it borrows.

Tests. err_view_into_temporary_{binding,get,return}.w, behav_view_into_temporary_used_in_statement.w. Sweep before the battery: 2315 files across tools/, test/*, lib/std — the only hits are the three new compile-error tests.

Also: the CLAUDE.md/AGENTS.md cheat-sheet's cut/awk rows now spell line.split("\t")[1] (Eric: [1], not .get(1); the two-spellings question is #1239).

Battery, seed-driven at HEAD, alone in its batch: build, :fixpoint, :test, :move-audit, :drop-audit, :test-green, :last-green all green.

`let path = line.split("\t")[1]` (and `.get(1)`) bound a view into the
Vec that split() returned; the Vec died at the end of the statement and safe
code read freed memory: empty text under the debug allocator, garbage
comparisons otherwise (`path != "docs"` true, `== "docs"` false, `ends_with`
false). It surfaced as a `with -n` two-line snippet that seemed to ignore its
`if`; the wrapper was correct (#1238 closed as a duplicate).

record_view_producer_origins marks a view whose receiver is not a place and
owns storage (PK_NotPlace and type_needs_drop) in expr_view_into_temporary;
transparent carriers propagate the mark. A `let` binding such a view
(record_view_binding_from_expr) or a return of one — through
check_returned_view_origins for a `&T` tail or return statement, and
check_returned_ephemeral_value_origins for other ephemeral values; lldb on the
repro showed the `-> &str` tail takes only the former — is a compile error
naming the temporary's type and the two fixes (bind the collection first, or
take an owned value). A use inside the statement is untouched:
`line.split("\t")[1] == "docs"` still compiles, and so does the cheat-sheet's
`print(line.split("\t")[1])`.

A first cut required the temporary to borrow nothing, so
`fn second(line: &str): line.split("\t")[1]` slipped through (the Vec carried
`line`'s parameter origin); a temporary is a temporary whatever it borrows.

Sweep before the battery: 2315 files across tools/, test/*, lib/std; the
only hits are the three new compile-error tests. The cheat-sheet's cut/awk
rows now spell `line.split("\t")[1]` (Eric: `[1]`, not `.get(1)`; #1239).
@ehartford
ehartford merged commit 96f7c8a into main Sep 21, 2026
5 checks passed
@ehartford
ehartford deleted the reject-view-into-temporary branch September 24, 2026 16:57
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.

Sema: a view into a temporary escapes its statement silently — let p = args().get(1) reads as "" instead of being rejected

1 participant