-
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
Provide more context on Fn
closure modifying binding
#133149
base: master
Are you sure you want to change the base?
Conversation
When modifying a binding from inside of an `Fn` closure, point at the binding definition and suggest using an `std::sync` type that would allow the code to compile. ``` error[E0594]: cannot assign to `counter`, as it is a captured variable in a `Fn` closure --> f703.rs:6:9 | 4 | let mut counter = 0; | ----------- `counter` declared here, outside the closure 5 | let x: Box<dyn Fn()> = Box::new(|| { | -- in this closure 6 | counter += 1; | ^^^^^^^^^^^^ cannot assign | = help: consider using `std::sync::atomic::AtomicI32` instead, which allows for multiple threads to access and modify the value ```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm somewhat worried that this will lead to users trying to mutate state through APIs that are designed to be immutable. Taking an impl Fn()
can have a lot of different intentions from an API designer perspective, and there's not much context we can glean by simply just seeing a mutate-within-Fn
error in borrowck. Mentioning threading and atomics unprompted seems a bit strange, even though it could fix some people's errors, and even if it does, it's possibly not the right solution compared to using a different API or approaching a problem differently.
I'd prefer if this help message were elaborated to mention that the function expects a Fn
and to tell the user pay attention to why this is the case, and then advising the user that if mutation is intended then it can be done concurrently through an atomic since it does not require exclusive access.
I fear that if we provide just the information in this help message, we're at risk of misleading users further by turning borrowck errors into logical or design errors that they cannot so easily debug or catch.
@rustbot author |
@compiler-errors what do you think if instead of specifying the type they can use we instead linked at the book section on shared state? I did not make it a structured suggestion to introduce a bit of friction to people blindly following this, but your point stands. |
When modifying a binding from inside of an
Fn
closure, point at the binding definition and suggest using anstd::sync
type that would allow the code to compile.This provides more context on where the binding being modified was declared, and more importantly, guides newcomers towards
std::sync
when encountering cases like these.When the requirement comes from an argument of a local function, we already tell the user that they might want to change from
Fn
toFnMut
:We might want to avoid the
help
in this case.Inspired by a part of Niko's keynote at RustNation UK 2024.