Skip to content
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

Closed
thestinger opened this issue Jun 15, 2013 · 5 comments
Closed

RFC: remove syntactic sugar for @mut borrows #7140

thestinger opened this issue Jun 15, 2013 · 5 comments

Comments

@thestinger
Copy link
Contributor

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.

let x = @mut 5;
do x.borrow |r| {
    ...
}

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.

@emberian
Copy link
Member

+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.

@pcwalton
Copy link
Contributor

+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 mutate() as part of an object with a destructor.

@nikomatsakis
Copy link
Contributor

If we move away from @mut and over to Mut, I guess this happens by default

@brson
Copy link
Contributor

brson commented Oct 10, 2013

cc #9796

@thestinger
Copy link
Contributor Author

This is completed.

flip1995 pushed a commit to flip1995/rust that referenced this issue May 6, 2021
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
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

No branches or pull requests

5 participants