Skip to content

Commit b443a10

Browse files
authored
Rollup merge of #73382 - Aaron1011:fix/self-receiver-candidates, r=petrochenkov
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`)
2 parents 687f929 + 8956a7f commit b443a10

File tree

3 files changed

+10
-29
lines changed

3 files changed

+10
-29
lines changed

src/librustc_typeck/check/expr.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -888,10 +888,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
888888
rcvr,
889889
probe::ProbeScope::AllTraits,
890890
) {
891-
err.span_label(
892-
pick.item.ident.span,
893-
&format!("the method is available for `{}` here", new_rcvr_t),
894-
);
891+
debug!("try_alt_rcvr: pick candidate {:?}", pick);
892+
// Make sure the method is defined for the *actual* receiver:
893+
// we don't want to treat `Box<Self>` as a receiver if
894+
// it only works because of an autoderef to `&self`
895+
if pick.autoderefs == 0 {
896+
err.span_label(
897+
pick.item.ident.span,
898+
&format!("the method is available for `{}` here", new_rcvr_t),
899+
);
900+
}
895901
}
896902
}
897903
};

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:

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)