Skip to content

A generator's mut hold lends its yielded views, and the held place is not read by name in the loop - #3630

Merged
ehartford merged 2 commits into
mainfrom
gen-view-reborrow
Nov 26, 2026
Merged

ehartford merged 2 commits into
mainfrom
gen-view-reborrow

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Repairs a regression of #3627 (mine) and closes the gap beside it. No issue was open for either.

The regression. #3627 made the place a generator takes mut exclusively held while its loop runs. That also refused reads through the views the generator yields:

gen fn toks(mut b: Bag) -> Tok: …      // yields views of b
for t in toks(mut b):
    match t: …                         // error: cannot borrow: already mutably borrowed

A view derived from one the generator handed out (the loop's element, a binding a pattern takes from it) is the generator's own lend. check_borrow_create and check_borrow_create_direct now let such a borrow pass a generator loop's exclusive entry; a borrow that names the place itself is still refused.

The gap. Only passing the held place to a function was refused. A field read or a method call on it in the body was accepted, because the read-side check waited for a last use of the generator, which nothing names again. check_read_against_views now refuses it for the whole loop: "cannot read b while walk(…) holds it mut", with a help line.

Tests. behav_generator_mut_hold_lends_its_views; err_generator_mut_hold_named_in_body; err_generator_mut_hold_read_by_name. All 137 generator tests pass.

Evidence. Battery GREEN on 741f687; :release-checks green on the same commit.

@ehartford
ehartford enabled auto-merge (squash) November 26, 2026 10:16
@ehartford
ehartford merged commit 0fbee2d into main Nov 26, 2026
1 check passed
@ehartford
ehartford deleted the gen-view-reborrow branch November 26, 2026 10:16
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.

1 participant