-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Improve method lookup auto-deref behavior (subissue of trait reform) #12825
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
Comments
cc me |
Another test case: use std::cell::RefCell;
fn main() {
let a = RefCell::new((1i, 2i));
match *a.borrow_mut() {
(ref mut a, ref mut b) => { *a += *b; *b += *a; }
}
} This fail to compile, but if it's changed to |
I've been working, by the way, on a prototype of a new trait / method resolution algorithm that resolves issue (among many others). I hope to include it in a general RFC for #5527 very shortly. |
Same issue with Index/IndexMut, ftr. |
Nominating, if this is going to block vec switching to |
Assigning 1.0 milestone, P-backcompat-lang. |
I'm going to go ahead and assign this to @nikomatsakis. |
We need to change precedence so that inherent and trait impls are on equal footing instead of having inherent impls be first. |
prefer `Deref` over `DerefMut` in all other circumstances. Closes rust-lang#12825.
… 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
Is this really fixed? @alexcrichton’s example from above still fails to compile, and is incidentally very similar to the example in #15609, which also does not compile. |
|
I've confirmed that @pcwalton, would you prefer me to re-open this issue or create a new one? |
Reopening. |
`IndexMut` on mutable vectors. This fixes a bug whereby the mutability fixups for method behavior were not kicking in after autoderef failed to happen at any level. It also adds support for `Index` to the fixer-upper. Closes rust-lang#12825.
librustc: Improve method autoderef/deref/index behavior more, and enable IndexMut on mutable vectors. This fixes a bug whereby the mutability fixups for method behavior were not kicking in after autoderef failed to happen at any level. It also adds support for `Index` to the fixer-upper. Closes #12825. r? @pnkfelix
fix: Fix search for associated trait items being inconsistent
Previously to #12491, there were no real issues with the current method lookup implementation, which collects candidates and picks the first one in the auto-deref chain, without caring too much about mutability.
The problematic case #12491 introduces was
(*x).foo()
where*x
is an overloaded dereference, calling eitherDeref::deref
orDerefMut::deref_mut
.However, the method could take
&mut self
- which we can't know until we've picked one ofderef
orderef_mut
and continued to the lookup of.foo
.The choice I've made was to always try
deref_mut
(if not obviously immutable, e.g. x having a type of&T
instead of&mut T
orT
) first, and that will work forRefCell
'sRefMut
, which only had a mutable.get()
method previously.In #12610 it gets worse as the issue can happen at any auto-deref level, and here's a test case that fails to compile:
cc @nikomatsakis
The text was updated successfully, but these errors were encountered: