Skip to content

Commit 4df3e9b

Browse files
committed
Unrolled build for #153679 in rollup 164865
Rollup merge of #153679 - estebank:issue-113550, r=JonathanBrouwer When multiple traits are applicable for method, do not specialize main message to the first one that applied When calling a method that appears on `Iterator`, we often say "`Foo` is not an iterator", even if that method exists in other traits, which is quite confusing and many times nonsensical. Now, if we mention multiple traits we revert the main message back: ``` error[E0599]: no method named `take` found for struct `Foo` in the current scope --> $DIR/method-call-err-msg.rs:19:7 | LL | pub struct Foo; | -------------- method `take` not found for this struct because it doesn't satisfy `Foo: Iterator` ... LL | / y.zero() LL | | .take() | | -^^^^ `Foo` is not an iterator | |______| | | = note: the following trait bounds were not satisfied: `Foo: Iterator` which is required by `&mut Foo: Iterator` note: the trait `Iterator` must be implemented --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL = help: items from traits can only be used if the trait is implemented and in scope = note: the following traits define an item `take`, perhaps you need to implement one of them: candidate #1: `Iterator` candidate #2: `std::io::Read` ``` Fix #113550.
2 parents df87fa6 + 954ce3f commit 4df3e9b

4 files changed

Lines changed: 20 additions & 9 deletions

File tree

‎compiler/rustc_hir_typeck/src/method/suggest.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4548,6 +4548,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
45484548
name = item_name,
45494549
)
45504550
};
4551+
if candidates_len > 1 {
4552+
let rcvr_ty_kind = match rcvr_ty.peel_refs().kind() {
4553+
ty::Param(_) => "type parameter",
4554+
ty::Adt(def, _) => self.tcx.def_descr(def.did()),
4555+
_ => "type",
4556+
};
4557+
err.primary_message(format!(
4558+
"no method named `{item_name}` found for {rcvr_ty_kind} `{rcvr_ty}` in the \
4559+
current scope",
4560+
));
4561+
}
45514562
// Obtain the span for `param` and use it for a structured suggestion.
45524563
if let Some(param) = param_type {
45534564
let generics = self.tcx.generics_of(self.body_def_id.to_def_id());

‎tests/ui/methods/method-call-err-msg.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ impl Foo {
1010

1111
fn main() {
1212
let x = Foo;
13-
x.zero(0) //~ ERROR this method takes 0 arguments but 1 argument was supplied
14-
.one() //~ ERROR this method takes 1 argument but 0 arguments were supplied
15-
.two(0); //~ ERROR this method takes 2 arguments but 1 argument was supplied
13+
x.zero(0) //~ ERROR: this method takes 0 arguments but 1 argument was supplied
14+
.one() //~ ERROR: this method takes 1 argument but 0 arguments were supplied
15+
.two(0); //~ ERROR: this method takes 2 arguments but 1 argument was supplied
1616

1717
let y = Foo;
1818
y.zero()
19-
.take() //~ ERROR not an iterator
19+
.take() //~ ERROR: no method named `take` found for struct `Foo` in the current scope
2020
.one(0);
21-
y.three::<usize>(); //~ ERROR this method takes 3 arguments but 0 arguments were supplied
21+
y.three::<usize>(); //~ ERROR: this method takes 3 arguments but 0 arguments were supplied
2222
}

‎tests/ui/methods/method-call-err-msg.stderr‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ help: provide the argument
5252
LL | .two(0, /* isize */);
5353
| +++++++++++++
5454

55-
error[E0599]: `Foo` is not an iterator
55+
error[E0599]: no method named `take` found for struct `Foo` in the current scope
5656
--> $DIR/method-call-err-msg.rs:19:7
5757
|
5858
LL | pub struct Foo;

‎tests/ui/traits/method-on-unbounded-type-param.stderr‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | fn f<T: Iterator>(a: T, b: T) -> std::cmp::Ordering {
1414
LL | fn f<T: Ord>(a: T, b: T) -> std::cmp::Ordering {
1515
| +++++
1616

17-
error[E0599]: the method `cmp` exists for reference `&T`, but its trait bounds were not satisfied
17+
error[E0599]: no method named `cmp` found for type parameter `&T` in the current scope
1818
--> $DIR/method-on-unbounded-type-param.rs:5:10
1919
|
2020
LL | (&a).cmp(&b)
@@ -35,7 +35,7 @@ LL | fn g<T: Iterator>(a: T, b: T) -> std::cmp::Ordering {
3535
LL | fn g<T: Ord>(a: T, b: T) -> std::cmp::Ordering {
3636
| +++++
3737

38-
error[E0599]: the method `cmp` exists for reference `&T`, but its trait bounds were not satisfied
38+
error[E0599]: no method named `cmp` found for type parameter `&T` in the current scope
3939
--> $DIR/method-on-unbounded-type-param.rs:8:7
4040
|
4141
LL | a.cmp(&b)
@@ -56,7 +56,7 @@ LL | fn h<T: Iterator>(a: &T, b: T) -> std::cmp::Ordering {
5656
LL | fn h<T: Ord>(a: &T, b: T) -> std::cmp::Ordering {
5757
| +++++
5858

59-
error[E0599]: the method `cmp` exists for struct `Box<dyn T>`, but its trait bounds were not satisfied
59+
error[E0599]: no method named `cmp` found for struct `Box<dyn T>` in the current scope
6060
--> $DIR/method-on-unbounded-type-param.rs:14:7
6161
|
6262
LL | trait T {}

0 commit comments

Comments
 (0)