-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"cannot assign to immutable field" could be confusing #35937
Comments
I got this error now:
Where |
@Zoxc My guess would be missing @nikomatsakis @jonathandturner We should print the first element of the lvalue that causes the whole lvalue to be immutable only, either an immutable variable, reference or overloaded deref/indexing. |
@eddyb I forgot I changed |
The error message would ideally contain "...because |
I was working with @caipre on a PR to do this at some point, but I think they lost steam. It was a bit trickier than I at first thought. In any case, there is an issue specific to this problem: #28419 |
Yeah, sorry about that. 😞 The issue turned out to be rather more difficult than I anticipated and I got caught up with some things where I wasn't able to put time into it any longer. If someone has a good idea of how to resolve that issue, I don't want to be a blocker. |
Given a file ```rust struct S { x: i32, } fn foo() { let s = S { x: 42 }; s.x += 1; } fn bar(s: S) { s.x += 1; } ``` Provide the following output: ```rust error: cannot assign to immutable field `s.x` --> $DIR/issue-35937.rs:16:5 | 5 | let s = S { x: 42 }; | - consider changing this to `mut s` 6 | s.x += 1; | ^^^^^^^^ cannot mutably borrow immutable field error: cannot assign to immutable field `s.x` --> $DIR/issue-35937.rs:20:5 | 8 | fn bar(s: S) { | - consider changing this to `mut s` 9 | s.x += 1; | ^^^^^^^^ cannot mutably borrow immutable field ``` Follow up to rust-lang#40445. Fix rust-lang#35937.
This converts all of borrowck's `mut` suggestions to a new `mc::ImmutabilityBlame` API instead of the current mix of various hacks. Fixes rust-lang#35937. Fixes rust-lang#40823.
borrowck: consolidate `mut` suggestions This converts all of borrowck's `mut` suggestions to a new `mc::ImmutabilityBlame` API instead of the current mix of various hacks. Fixes rust-lang#35937. Fixes rust-lang#40823. Fixes rust-lang#40859. cc @estebank r? @pnkfelix
IMO this was not fixed by #40767. The note that was added (suggesting a change to the struct binding) is helpful, but the error still says "immutable field" which implies that individual fields can be mutable. |
@durka I am inclined to agree. I'd like us to be less specific in many of these messages, since I think they imply things that are not true. |
Reword trying to operate in immutable fields The previous message ("cannot assign/mutably borrow immutable field") when trying to modify a field of an immutable binding gave the (incorrect) impression that fields can be mutable independently of their ADT's binding. Slightly reword the message to read "cannot assign/mutably borrow field of immutable binding". Re #35937.
Current output:
|
LGTM. |
It occurred to me that this might be confusing to newcomers because fields don't have mutability in Rust, instead we have inherited mutability. So maybe it should say "cannot assign to field of immutable struct" or "cannot modify field due to immutable binding" or something.
cf #18150
The text was updated successfully, but these errors were encountered: