Skip to content

RFC: remove syntactic sugar for @mut borrows #7140

@thestinger

Description

@thestinger
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.

Activity

emberian

emberian commented on Jun 22, 2013

@emberian
Contributor

+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

pcwalton commented on Jul 29, 2013

@pcwalton
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

nikomatsakis commented on Oct 10, 2013

@nikomatsakis
Contributor

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

brson

brson commented on Oct 10, 2013

@brson
Contributor
thestinger

thestinger commented on Jan 9, 2014

@thestinger
ContributorAuthor

This is completed.

added a commit that references this issue on May 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @brson@nikomatsakis@pcwalton@emberian@thestinger

        Issue actions

          RFC: remove syntactic sugar for @mut borrows · Issue #7140 · rust-lang/rust