Skip to content

Commit 8956a7f

Browse files
committed
Only display other method receiver candidates if they actually apply
Previously, we would suggest `Box<Self>` as a valid receiver, even if method resolution only succeeded due to an autoderef (e.g. to `&self`)
1 parent ff4a253 commit 8956a7f

File tree

3 files changed

+10
-29
lines changed

3 files changed

+10
-29
lines changed

Diff for: src/librustc_typeck/check/expr.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -913,10 +913,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
913913
rcvr,
914914
probe::ProbeScope::AllTraits,
915915
) {
916-
err.span_label(
917-
pick.item.ident.span,
918-
&format!("the method is available for `{}` here", new_rcvr_t),
919-
);
916+
debug!("try_alt_rcvr: pick candidate {:?}", pick);
917+
// Make sure the method is defined for the *actual* receiver:
918+
// we don't want to treat `Box<Self>` as a receiver if
919+
// it only works because of an autoderef to `&self`
920+
if pick.autoderefs == 0 {
921+
err.span_label(
922+
pick.item.ident.span,
923+
&format!("the method is available for `{}` here", new_rcvr_t),
924+
);
925+
}
920926
}
921927
}
922928
};

Diff for: src/test/ui/impl-trait/no-method-suggested-traits.stderr

-18
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@ LL | use foo::Bar;
4949
error[E0599]: no method named `method` found for struct `std::rc::Rc<&mut std::boxed::Box<&char>>` in the current scope
5050
--> $DIR/no-method-suggested-traits.rs:32:43
5151
|
52-
LL | fn method(&self) {}
53-
| ------
54-
| |
55-
| the method is available for `std::boxed::Box<std::rc::Rc<&mut std::boxed::Box<&char>>>` here
56-
| the method is available for `std::pin::Pin<std::rc::Rc<&mut std::boxed::Box<&char>>>` here
57-
| the method is available for `std::sync::Arc<std::rc::Rc<&mut std::boxed::Box<&char>>>` here
58-
| the method is available for `std::rc::Rc<std::rc::Rc<&mut std::boxed::Box<&char>>>` here
59-
...
6052
LL | std::rc::Rc::new(&mut Box::new(&'a')).method();
6153
| ^^^^^^ method not found in `std::rc::Rc<&mut std::boxed::Box<&char>>`
6254
|
@@ -83,16 +75,6 @@ error[E0599]: no method named `method` found for struct `std::rc::Rc<&mut std::b
8375
|
8476
LL | std::rc::Rc::new(&mut Box::new(&1i32)).method();
8577
| ^^^^^^ method not found in `std::rc::Rc<&mut std::boxed::Box<&i32>>`
86-
|
87-
::: $DIR/auxiliary/no_method_suggested_traits.rs:8:12
88-
|
89-
LL | fn method(&self) {}
90-
| ------
91-
| |
92-
| the method is available for `std::boxed::Box<std::rc::Rc<&mut std::boxed::Box<&i32>>>` here
93-
| the method is available for `std::pin::Pin<std::rc::Rc<&mut std::boxed::Box<&i32>>>` here
94-
| the method is available for `std::sync::Arc<std::rc::Rc<&mut std::boxed::Box<&i32>>>` here
95-
| the method is available for `std::rc::Rc<std::rc::Rc<&mut std::boxed::Box<&i32>>>` here
9678
|
9779
= help: items from traits can only be used if the trait is in scope
9880
help: the following trait is implemented but not in scope; perhaps add a `use` for it:

Diff for: src/test/ui/traits/trait-item-privacy.stderr

-7
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ error[E0599]: no method named `b` found for struct `S` in the current scope
2020
LL | struct S;
2121
| --------- method `b` not found for this
2222
...
23-
LL | fn b(&self) { }
24-
| -
25-
| |
26-
| the method is available for `std::boxed::Box<S>` here
27-
| the method is available for `std::sync::Arc<S>` here
28-
| the method is available for `std::rc::Rc<S>` here
29-
...
3023
LL | S.b();
3124
| ^ method not found in `S`
3225
|

0 commit comments

Comments
 (0)