Skip to content

A gen fn's mut parameter is the caller's place, held by the generator alone while its loop runs (closes #3624) - #3627

Merged
ehartford merged 2 commits into
mainfrom
gen-mut-param
Nov 26, 2026
Merged

ehartford merged 2 commits into
mainfrom
gen-mut-param

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Closes #3624.

The bug. gen fn walk(mut b: Box) wrote to a copy: after for x in walk(mut b) the caller's b was unchanged, with no diagnostic. D172 (§3.8a): a mut p: T parameter is the caller's place.

Root cause. The generator value stores its arguments (D69). It held a mut parameter by value, where it already held a borrowed receiver as a view of the caller's place (generator_borrows_receiver).

The fix.

  • generator_param_is_view: a borrowed receiver or a mut parameter is held as a view; the gen fn and its producer take it by place. The state type, the constructor's MIR and the driver's MIR read one per-function mask (generator_fn_view_params).
  • The place is the generator's alone while its loop runs: its loop borrow is exclusive, so the body may neither write nor read it. Before, the body's access was accepted (the same closure-reads-a-mut-place case an ordinary mut call refuses, §3.2). This applies to a mut fn generator's receiver too.

Tests. behav_3624_generator_mut_param_is_the_callers_place (printed left 2 before, left 0 now); err_3624_generator_holds_its_mut_param (a write in the body); err_3624_generator_mut_param_is_not_read (a read). All 130 existing generator behaviour tests pass.

Evidence. Battery GREEN on 108b23b (rc=0, 1092 s); :release-checks green on the same commit.

@ehartford
ehartford enabled auto-merge (squash) November 26, 2026 06:41
@ehartford
ehartford merged commit a4cf71f into main Nov 26, 2026
1 check passed
@ehartford
ehartford deleted the gen-mut-param branch November 26, 2026 06:41
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.

A generator's mut parameter does not write through to the caller's value

1 participant