Repository navigation
Sema: a field view of a borrowed collection element keeps its origin — &entry.value from for/.get(i) over a &Vec may be returned (#1297) - #1327
Merged
Conversation
added 2 commits
September 22, 2026 13:11
…oot is not the borrowed storage (#1297) `return Some(&entry.value)` with `let entry = entries.get(i)` over a borrowed parameter's collection (and `for (k, v) in m: return &v.n`) was rejected as "returned ephemeral value may outlive its origin 'entry'", while `Some(entries.get(i))` itself was accepted. §3.4 and §21.1 rule 10: projecting out of a value that contains views preserves the origin set; D22: patterns and projections are structural and never erase a borrow. Exact line (baseline stage1, lldb, `breakpoint set -n Sema.view_origin_is_stack_local` + `-n Sema.emit_error` on the repro `let e = v.get(i); return Some(&e.value)` with `v: &Vec[KV]`): Sema.view_origin_is_stack_local(x1 = 13306 /* sym `e` */) from Sema.check_returned_ephemeral_value_origins from Sema.check_return Sema.emit_error("returned ephemeral value may outlive its origin 'e'") from Sema.check_returned_ephemeral_value_origins (SemaCheck.w:10488) `e` reached the dep list from collect_expr_view_deps' UOP_REF branch (SemaCheck.w:10024), which pushed `place_root_sym(operand)` for every `&place`. `e` is `&KV`: `e.value` is a projection through the reference, so its storage is the pointee, whose origins `e` already carries (deps [v], parameter mask of v). Naming the root pinned the field view to the binding, and the binding is a stack local. `--trace-place`/`--explain-mir-origin`/`analyze explain:node` cannot reach this: the check fails before MIR exists. Fix: `ref_storage_root_sym(place)` — the binding whose OWN storage `&place` borrows: the place root, unless the place projects through a reference-typed root, in which case none (the root's collected deps and mask are the origins). A bare `&e` still borrows the reference's slot and stays rooted at `e`. Used at the UOP_REF dep collection and at the four direct `&place` root checks in check_returned_view_origins / check_yielded_view_origins. A view of a local's storage is rejected exactly as before (`&kv.value`, `&xs.get(0).value`, `&xs[0].value`; err_field_view_of_local_escape, err_field_view_of_local_element_escape).
… origins; a `&Vec`/`&HashMap` iterable yields views (#1297) `for entry in entries: return Some(&entry.value)` with `entries` a `&Vec[KV]` parameter or a match-bound payload of one was rejected as "returned ephemeral value may outlive its origin 'entry'". check_for put the plain loop binding in scope with no provenance at all (the pattern path already recorded it through record_pattern_view_bindings), so collect_expr_view_deps' NK_IDENT branch fell through to "a reference-typed local with no origins is its own origin" and view_origin_is_stack_local said yes: the binding, not the parameter, was named. D44: `for x in xs` is `xs.iter()`; the binding is a view produced from the iterated place. record_for_binding_view_origins records the binding the way record_view_producer_origins records the spelled call: the iterable's parameter mask and collected deps, else the iterated place's root (an owned local collection, so `&x.field` cannot escape and the diagnostic names `xs`). It registers no borrow of that root: a whole-root borrow of `self` would reject `for e in self.items: self.count += 1`, accepted today and idiomatic; the missing live-view check on the iterated collection is #1317. for_iterable_yields_views peeled the reference for arrays and slices but tested the Vec/HashMap shape on the unpeeled type, so a `&Vec[KV]` iterable never marked its binding view-bound although the binding is `&KV`; it now tests the peeled type, and the provenance recording is gated on the binding's own type (a reference is a view) rather than on that predicate. Matrix under the rebuilt stage1 (scratch matrix.w / method.w / pat.w): `for e in v`, `let e = v.get(i)`, `&v[i].value`, `&v.get(i).value`, the match-bound `entries` forms, `for (k, v) in m: &v.n`, and the `self` receiver forms all return their field view; `&kv.value`, `&xs.get(0).value`, `&xs[0].value`, `let e = xs.get(0); &e.value` and `for e in xs: return &e.value` over a local are rejected naming the local. behav_field_view_of_borrowed_collection_returns runs the accepted forms and checks the values; err_field_view_of_local_element_binding_escape pins the two binding forms over a local.
Contributor
Author
|
Battery on |
ehartford
added a commit
that referenced
this pull request
Sep 22, 2026
…mpiler defects (#1338) * examples: ephemerality-and-lowering stores the element directly, store(order[2], ...) (#1287 fixed in #1311) The typed-binding detour around #1287 (Atomic intrinsics passed an element view's address as the value) is gone with its comment. Package test output unchanged on a release compiler built from ae609cd. * examples: json-parser matches byte literals as patterns and returns &entry.value inline (#1295, #1297 fixed) Byte arms are Some(b'{') patterns, not guards (#1295, fixed in #1312); json_get_field returns Some(&entry.value) from a for loop over the entries, and the kv_value helper is gone (#1297, fixed in #1327). Workaround comments removed. Demo and package test output unchanged on a release compiler built from ae609cd. * examples: nebula's Status and Telemetry carry @[derive(Debug, Clone)] again (§11.8; #1288, #1289 fixed in #1311) The parking comment is removed. Demo and package test output unchanged on a release compiler built from ae609cd. * examples: service builds its change list and SET clause with the §7.2 builder (#1298 fixed in #1319) describe_changes and PgUserRepo.update spell with Vec.new() as mut ...: with a trailing if; the var desugaring and its comments are gone. Demo and package test output unchanged on a release compiler built from ae609cd (neither function is reached by the demo; the same shape was run separately under --debug-alloc: correct result, zero leaks). * examples: ecs cites D43 for DenseStorage.insert's -> Unit, not #1296 insert's tail if has an assignment arm (a value of type T) and a push arm (Unit): two meanings remain, so -> Unit is the spelling D43 requires, not a workaround. The compiler currently infers Unit without it because the assignment arm is a multi-statement block (#1337); the one-statement spellings of the same arm are rejected as D43 requires. --------- Co-authored-by: Eric Hartford <[email protected]>
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 #1297.
return Some(&entry.value)withentrybound byfor entry in entriesorlet entry = entries.get(i)over a borrowed parameter's collection was rejected ("returned ephemeral value may outlive its origin 'entry'") whileSome(entries.get(i))was accepted. §3.4 / §21.1 rule 10 / D22: projecting a field out of a view preserves the view's origin set.Two commits, one mechanism each:
&view.fieldkeeps the view's origin set.collect_expr_view_deps'UOP_REFbranch (and the four direct&placeroot checks incheck_returned_view_origins/check_yielded_view_origins) namedplace_root_sym(operand)as an origin of every&place. Withe: &KV,e.valueprojects through the reference: its storage is the pointee, whose originsealready carries (deps + parameter mask). Newref_storage_root_sym: the root's own storage is borrowed unless the place projects through a reference-typed root; a bare&estill roots ate. Exact line from lldb on the baseline stage1 (Sema.view_origin_is_stack_local(sym e)←check_returned_ephemeral_value_origins←check_return, thenemit_errorat SemaCheck.w:10488) is in the commit message;--trace-place/--explain-mir-origin/analyze explain:nodecannot reach this defect because the check fails before MIR exists.forbinding is a view of the iterated place.check_forgave the plain loop binding no provenance (the pattern path already did), so it became its own origin.record_for_binding_view_originsrecords it asrecord_view_producer_originsdoes for the spelled.iter()call (mask, deps, else the iterated place's root) without registering a whole-root borrow (which would reject the idiomaticfor e in self.items: self.count += 1).for_iterable_yields_viewsalso now peels the reference forVec/HashMapas it already did for slices, so a&Vec[KV]iterable's binding is view-bound.Matrix (rebuilt stage1): accepted —
for e in v,let e = v.get(i),&v[i].value,&v.get(i).value, the match-boundentriesforms,for (k, v) in m: &v.n, theself-receiver forms; rejected, naming the local —&kv.value,&xs.get(0).value,&xs[0].value,let e = xs.get(0); &e.value,for e in xs: return &e.value. Borrow behavior unchanged (let e = self.items.get(0); self.count += 1still rejected;for e in self.items: self.count += 1still accepted).Tests:
behav_field_view_of_borrowed_collection_returns(runs every accepted form and checks the values),err_field_view_of_local_escape,err_field_view_of_local_element_escape,err_field_view_of_local_element_binding_escape. Under stage1: the 4 new fixtures, the 60 existing view/origin/escape fixtures, the six D44 map debug-allocator cells (zero leaks), and 837/838test/compile_errorspass; the one failure (err_c_import_raw_call_requires_unsafe) fails identically on a baseline stage1 built fromorigin/main, as dovec_map_it/vec_map_basic/behav_d44_map_ownership(p7 runner) /examples/json-parserunder the bootstrap stage1.with check src/main.wpasses at each commit.Filed separately: #1317 —
for e in xs: xs.push(..)is accepted (no live-view borrow for the loop binding; a whole-root borrow would be a false positive onself, it needs the field path).Battery (
:fixpoint/:test) not run per the task; to be driven by the pinned seed before merge.