Skip to content

librustc: Fix up mutability in method autoderefs if incorrect, and #17501

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

Merged
merged 1 commit into from
Oct 1, 2014

Conversation

pcwalton
Copy link
Contributor

prefer Deref over DerefMut in all other circumstances.

Because the compiler now prefers Deref, this can break code that
looked like:

let mut foo = bar.borrow_mut();
(*foo).call_something_that_requires_mutable_self();

Replace this code with:

let mut foo = bar.baz();
(&mut *foo).call_something_that_requires_mutable_self();

Closes #12825.

[breaking-change]

r? @nikomatsakis

@rust-highfive
Copy link
Contributor

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!

@eddyb
Copy link
Member

eddyb commented Sep 24, 2014

Can't explicit derefs also be fixed up? Are there issues with that?
I was expecting the early choice to never affect user-visible semantics, maybe only the error messages.

@nikomatsakis
Copy link
Contributor

On Wed, Sep 24, 2014 at 01:35:19AM -0700, Eduard Burtescu wrote:

Can't explicit derefs also be fixed up?

I was expecting to fixup explicit derefs as well.

@nikomatsakis
Copy link
Contributor

@pcwalton based on a read through, this patch seemed pretty small -- mostly unintended side-effects -- so it seems like it's worth trying to fix this the full way, which would mean basically patching up the tables also for the receiver expression (I had envisioned walking the receiver a second time and flipping traits from Deref to DerefMut).

@pcwalton
Copy link
Contributor Author

@nikomatsakis Recursively too?

@pcwalton pcwalton force-pushed the improve-method-lookup-autoderef branch from 2c03f15 to f936419 Compare September 26, 2014 07:37
@pcwalton
Copy link
Contributor Author

re-r? @nikomatsakis

Some(MethodCall::expr(self_expr.id)),
Some(self_expr),
self_ty,
PreferMutLvalue));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop should also consider recurse on field accesses and indexing, I think, and consider autoderefs on those expressions we pass through. This is why expressions like guard.access.pending.remove() require an explicit &mut (which I think they should not).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted the loop above in this way.

@pcwalton pcwalton force-pushed the improve-method-lookup-autoderef branch from f936419 to 20e89f7 Compare September 27, 2014 05:03
@pcwalton
Copy link
Contributor Author

re-r? @nikomatsakis

@ghost ghost mentioned this pull request Sep 27, 2014
@pcwalton pcwalton force-pushed the improve-method-lookup-autoderef branch from 20e89f7 to afa9303 Compare September 30, 2014 16:02
@pcwalton
Copy link
Contributor Author

re-r? @nikomatsakis

@pcwalton pcwalton force-pushed the improve-method-lookup-autoderef branch from afa9303 to f800d86 Compare September 30, 2014 16:09
@nikomatsakis
Copy link
Contributor

This basically looks good... the only thing that seems to be missing are advanced tests covering scenarios like x.f.get(), (*x).get() and so on. (I know that such things do occur in the code base).

r+ modulo tests.

prefer `Deref` over `DerefMut` in all other circumstances.

Closes rust-lang#12825.
@pcwalton pcwalton force-pushed the improve-method-lookup-autoderef branch from f800d86 to 496cc4c Compare September 30, 2014 21:38
@pcwalton
Copy link
Contributor Author

pcwalton commented Oct 1, 2014

@bors: retry

bors added a commit that referenced this pull request Oct 1, 2014
… r=nikomatsakis

prefer `Deref` over `DerefMut` in all other circumstances.

Because the compiler now prefers `Deref`, this can break code that
looked like:

    let mut foo = bar.borrow_mut();
    (*foo).call_something_that_requires_mutable_self();

Replace this code with:

    let mut foo = bar.baz();
    (&mut *foo).call_something_that_requires_mutable_self();

Closes #12825.

[breaking-change]

r? @nikomatsakis
@bors bors closed this Oct 1, 2014
@bors bors merged commit 496cc4c into rust-lang:master Oct 1, 2014
lnicola pushed a commit to lnicola/rust that referenced this pull request Jul 11, 2024
Allow non org members to assign area labels
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

Successfully merging this pull request may close these issues.

Improve method lookup auto-deref behavior (subissue of trait reform)
5 participants