-
Notifications
You must be signed in to change notification settings - Fork 13k
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
RFC: remove syntactic sugar for @mut borrows #7140
Comments
+1, very much agree. Avoiding dynamic failure is one of the reasons I am attracted to Rust, and it should definitely be explicitly opt-in. It will make code much easier to reason about, I think. |
+1 in general, but I actually think that we don't need a closure here: we can possibly just return the reference you get from |
If we move away from @mut and over to Mut, I guess this happens by default |
cc #9796 |
This is completed. |
fix ice when checking rustc libstd ``` thread 'rustc' panicked at 'index out of bounds: the len is 0 but the index is 0', src/tools/clippy/clippy_lints/src/matches.rs:1595:53 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ``` I don't have a minimised testcase because I don't have time to reduce libstd down to a few lines right now. --- changelog: fix index out of bounds access when checking rustc libstd
Using
@mut
won't lead to dynamic failures until it ends up borrowed as&
or&mut
. I think this has proven to be a very confusing feature, and explaining it is a regular activity in #rust. It's very hard to reason about the failure cases until you actually run into one, especially if there are a lot of shallow copies which is the use case for managed pointers. It's usually a better idea to pass it by-value for the sake of robustness.Resorting to
@mut
should already be a last resort and dynamic freezes should be a very obvious opt-in feature, with appropriately named methods + docstrings.The current behaviour would also be incredibly hard to reproduce in a library type meant to be used in the same way like
RcMut
. We would need a very special trait able to run code at the end of the scope without an object + destructor involved.In the future, this will allow us to add back the sugared borrows to
@mut
for the subset of cases that we can prove are safe without dynamic freezes. By putting the dynamic failure strategy in a library, we leave open the possibility of better solutions as the language evolves.I know doing this would be a painful change, but it can be done gradually with a lint check set to deny the old implicit dynamic borrows by default.
The text was updated successfully, but these errors were encountered: