Repository navigation
A view into a statement temporary cannot be bound or returned (#962) - #1240
Merged
Merged
Conversation
`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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #962.
let path = line.split("\t")[1](and.get(1)) bound a view into theVecthatsplit()returned; theVecdied 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 awith -ntwo-line snippet that seemed to ignore itsif(#1238, closed as a duplicate; the wrapper was correct).Fix (Sema).
record_view_producer_originsmarks a view whose receiver is not a place and owns storage (PK_NotPlaceandtype_needs_drop); transparent carriers propagate the mark. Binding such a view withlet, or returning it —check_returned_view_originsfor a&Ttail or return statement,check_returned_ephemeral_value_originsfor other ephemeral values; lldb on the repro showed a-> &strtail 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.