Skip to content

Commit

Permalink
Make E0599's label more clear for field which is used like a method.
Browse files Browse the repository at this point in the history
fixes #127178
  • Loading branch information
surechen committed Jul 18, 2024
1 parent 52f3c71 commit 4f67d26
Show file tree
Hide file tree
Showing 66 changed files with 445 additions and 118 deletions.
15 changes: 11 additions & 4 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2277,7 +2277,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let call_expr = tcx.hir().expect_expr(tcx.parent_hir_id(expr.hir_id));

if let Some(span) = call_expr.span.trim_start(item_name.span) {
err.span_suggestion(
err.span_suggestion_verbose(
span,
"remove the arguments",
"",
Expand Down Expand Up @@ -3266,7 +3266,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.copied();

if explain {
err.help("items from traits can only be used if the trait is in scope");
err.help(
"items from traits can only be used if the trait is implemented and in scope",
);
}

let msg = format!(
Expand Down Expand Up @@ -3570,7 +3572,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
}
if self.suggest_valid_traits(err, item_name, valid_out_of_scope_traits, true) {
if self.suggest_valid_traits(
err,
item_name,
valid_out_of_scope_traits,
!trait_missing_method,
) {
return;
}

Expand Down Expand Up @@ -3680,7 +3687,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ => None,
};
if !trait_missing_method {
err.help(if param_type.is_some() {
err.span_help(span, if param_type.is_some() {
"items from traits can only be used if the type parameter is bounded by the trait"
} else {
"items from traits can only be used if the trait is implemented and in scope"
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/associated-consts/associated-const-no-item.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ error[E0599]: no associated item named `ID` found for type `i32` in the current
LL | const X: i32 = <i32>::ID;
| ^^ associated item not found in `i32`
|
= help: items from traits can only be used if the trait is implemented and in scope
help: items from traits can only be used if the trait is implemented and in scope
--> $DIR/associated-const-no-item.rs:5:23
|
LL | const X: i32 = <i32>::ID;
| ^^
note: `Foo` defines an item `ID`, perhaps you need to implement it
--> $DIR/associated-const-no-item.rs:1:1
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ LL | match fut.as_mut().poll(ctx) {
|
= note: the method is available for `Pin<&mut impl Future<Output = ()>>` here
|
= help: items from traits can only be used if the trait is in scope
= help: items from traits can only be used if the trait is implemented and in scope
help: trait `Future` which provides `poll` is implemented but not in scope; perhaps you want to import it
|
LL + use std::future::Future;
Expand Down
24 changes: 20 additions & 4 deletions tests/ui/auto-ref-slice-plus-ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ error[E0599]: no method named `test_mut` found for struct `Vec<{integer}>` in th
LL | a.test_mut();
| ^^^^^^^^
|
= help: items from traits can only be used if the trait is implemented and in scope
help: items from traits can only be used if the trait is implemented and in scope
--> $DIR/auto-ref-slice-plus-ref.rs:7:7
|
LL | a.test_mut();
| ^^^^^^^^
note: `MyIter` defines an item `test_mut`, perhaps you need to implement it
--> $DIR/auto-ref-slice-plus-ref.rs:14:1
|
Expand All @@ -19,7 +23,11 @@ error[E0599]: no method named `test` found for struct `Vec<{integer}>` in the cu
LL | a.test();
| ^^^^ method not found in `Vec<{integer}>`
|
= help: items from traits can only be used if the trait is implemented and in scope
help: items from traits can only be used if the trait is implemented and in scope
--> $DIR/auto-ref-slice-plus-ref.rs:8:7
|
LL | a.test();
| ^^^^
note: `MyIter` defines an item `test`, perhaps you need to implement it
--> $DIR/auto-ref-slice-plus-ref.rs:14:1
|
Expand All @@ -32,7 +40,11 @@ error[E0599]: no method named `test` found for array `[{integer}; 1]` in the cur
LL | ([1]).test();
| ^^^^ method not found in `[{integer}; 1]`
|
= help: items from traits can only be used if the trait is implemented and in scope
help: items from traits can only be used if the trait is implemented and in scope
--> $DIR/auto-ref-slice-plus-ref.rs:10:11
|
LL | ([1]).test();
| ^^^^
note: `MyIter` defines an item `test`, perhaps you need to implement it
--> $DIR/auto-ref-slice-plus-ref.rs:14:1
|
Expand All @@ -45,7 +57,11 @@ error[E0599]: no method named `test` found for reference `&[{integer}; 1]` in th
LL | (&[1]).test();
| ^^^^ method not found in `&[{integer}; 1]`
|
= help: items from traits can only be used if the trait is implemented and in scope
help: items from traits can only be used if the trait is implemented and in scope
--> $DIR/auto-ref-slice-plus-ref.rs:11:12
|
LL | (&[1]).test();
| ^^^^
note: `MyIter` defines an item `test`, perhaps you need to implement it
--> $DIR/auto-ref-slice-plus-ref.rs:14:1
|
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/box/unit/unique-object-noncopyable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ LL | let _z = y.clone();
which is required by `Box<dyn Foo>: Clone`
`dyn Foo: Clone`
which is required by `Box<dyn Foo>: Clone`
= help: items from traits can only be used if the trait is implemented and in scope
help: items from traits can only be used if the trait is implemented and in scope
--> $DIR/unique-object-noncopyable.rs:24:16
|
LL | let _z = y.clone();
| ^^^^^
= note: the following trait defines an item `clone`, perhaps you need to implement it:
candidate #1: `Clone`

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/coherence/coherence_inherent.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0599]: no method named `the_fn` found for reference `&TheStruct` in the c
LL | s.the_fn();
| ^^^^^^ method not found in `&TheStruct`
|
= help: items from traits can only be used if the trait is in scope
= help: items from traits can only be used if the trait is implemented and in scope
help: trait `TheTrait` which provides `the_fn` is implemented but not in scope; perhaps you want to import it
|
LL + use Lib::TheTrait;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/coherence/coherence_inherent_cc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0599]: no method named `the_fn` found for reference `&TheStruct` in the c
LL | s.the_fn();
| ^^^^^^ method not found in `&TheStruct`
|
= help: items from traits can only be used if the trait is in scope
= help: items from traits can only be used if the trait is implemented and in scope
help: trait `TheTrait` which provides `the_fn` is implemented but not in scope; perhaps you want to import it
|
LL + use coherence_inherent_cc_lib::TheTrait;
Expand Down
20 changes: 14 additions & 6 deletions tests/ui/confuse-field-and-method/issue-2392.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
| ------------- method `not_closure` not found for this struct
...
LL | o_closure.not_closure();
| ^^^^^^^^^^^-- help: remove the arguments
| |
| field, not a method
| ^^^^^^^^^^^ field, not a method
|
help: remove the arguments
|
LL - o_closure.not_closure();
LL + o_closure.not_closure;
|

error[E0599]: no method named `closure` found for struct `Obj` in the current scope
--> $DIR/issue-2392.rs:42:12
Expand Down Expand Up @@ -86,9 +90,13 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
| ------------- method `not_closure` not found for this struct
...
LL | w.wrap.not_closure();
| ^^^^^^^^^^^-- help: remove the arguments
| |
| field, not a method
| ^^^^^^^^^^^ field, not a method
|
help: remove the arguments
|
LL - w.wrap.not_closure();
LL + w.wrap.not_closure;
|

error[E0599]: no method named `closure` found for struct `Obj` in the current scope
--> $DIR/issue-2392.rs:58:24
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/copy-a-resource.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ LL | struct Foo {
LL | let _y = x.clone();
| ^^^^^ method not found in `Foo`
|
= help: items from traits can only be used if the trait is implemented and in scope
help: items from traits can only be used if the trait is implemented and in scope
--> $DIR/copy-a-resource.rs:18:16
|
LL | let _y = x.clone();
| ^^^^^
= note: the following trait defines an item `clone`, perhaps you need to implement it:
candidate #1: `Clone`

Expand Down
6 changes: 5 additions & 1 deletion tests/ui/functions-closures/fn-help-with-err.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ error[E0599]: no method named `bar` found for struct `Arc<{closure@$DIR/fn-help-
LL | arc2.bar();
| ^^^ method not found in `Arc<{closure@fn-help-with-err.rs:18:36}>`
|
= help: items from traits can only be used if the trait is implemented and in scope
help: items from traits can only be used if the trait is implemented and in scope
--> $DIR/fn-help-with-err.rs:19:10
|
LL | arc2.bar();
| ^^^
note: `Bar` defines an item `bar`, perhaps you need to implement it
--> $DIR/fn-help-with-err.rs:5:1
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn f(a: S) {
a.f();
//~^ ERROR the method `f` exists for struct `S`, but its trait bounds were not satisfied
//~| NOTE method cannot be called on `S` due to unsatisfied trait bounds
//~| HELP items from traits can only be used if the trait is implemented and in scope
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ LL | impl<T: X<Y<i32> = i32>> M for T {}
| ^^^^^^^^^^^^ - -
| |
| unsatisfied trait bound introduced here
= help: items from traits can only be used if the trait is implemented and in scope
help: items from traits can only be used if the trait is implemented and in scope
--> $DIR/method-unsatisfied-assoc-type-predicate.rs:26:7
|
LL | a.f();
| ^
note: `M` defines an item `f`, perhaps you need to implement it
--> $DIR/method-unsatisfied-assoc-type-predicate.rs:8:1
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ note: the following trait bounds were not satisfied:
|
LL | impl<T> StreamExt for T where for<'a> &'a mut T: Stream {}
| --------- - ^^^^^^ unsatisfied trait bound introduced here
= help: items from traits can only be used if the trait is implemented and in scope
help: items from traits can only be used if the trait is implemented and in scope
--> $DIR/hrtb-doesnt-borrow-self-1.rs:116:22
|
LL | let filter = map.filterx(|x: &_| true);
| ^^^^^^^
note: `StreamExt` defines an item `filterx`, perhaps you need to implement it
--> $DIR/hrtb-doesnt-borrow-self-1.rs:66:1
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ note: the following trait bounds were not satisfied:
|
LL | impl<T> StreamExt for T where for<'a> &'a mut T: Stream {}
| --------- - ^^^^^^ unsatisfied trait bound introduced here
= help: items from traits can only be used if the trait is implemented and in scope
help: items from traits can only be used if the trait is implemented and in scope
--> $DIR/hrtb-doesnt-borrow-self-2.rs:112:24
|
LL | let count = filter.countx();
| ^^^^^^
note: `StreamExt` defines an item `countx`, perhaps you need to implement it
--> $DIR/hrtb-doesnt-borrow-self-2.rs:66:1
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/hygiene/no_implicit_prelude.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ LL | fn f() { ::bar::m!(); }
LL | ().clone()
| ^^^^^
|
= help: items from traits can only be used if the trait is in scope
= help: items from traits can only be used if the trait is implemented and in scope
help: there is a method `clone_from` with a similar name, but with different arguments
--> $SRC_DIR/core/src/clone.rs:LL:COL
= note: this error originates in the macro `::bar::m` (in Nightly builds, run with -Z macro-backtrace for more info)
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/hygiene/trait_items.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ LL | fn f() { ::baz::m!(); }
LL | pub macro m() { ().f() }
| ^ method not found in `()`
|
= help: items from traits can only be used if the trait is in scope
= help: items from traits can only be used if the trait is implemented and in scope
= note: this error originates in the macro `::baz::m` (in Nightly builds, run with -Z macro-backtrace for more info)
help: trait `T` which provides `f` is implemented but not in scope; perhaps you want to import it
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ error[E0599]: no method named `my_debug` found for reference `&impl Debug` in th
LL | x.my_debug();
| ^^^^^^^^ method not found in `&impl Debug`
|
= help: items from traits can only be used if the trait is implemented and in scope
help: items from traits can only be used if the trait is implemented and in scope
--> $DIR/call_method_on_inherent_impl_on_rigid_type.rs:16:11
|
LL | x.my_debug();
| ^^^^^^^^
note: `MyDebug` defines an item `my_debug`, perhaps you need to implement it
--> $DIR/call_method_on_inherent_impl_on_rigid_type.rs:4:1
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ LL | fn my_debug(&self);
LL | x.my_debug();
| ^^^^^^^^ method not found in `impl Debug`
|
= help: items from traits can only be used if the trait is implemented and in scope
help: items from traits can only be used if the trait is implemented and in scope
--> $DIR/call_method_on_inherent_impl_ref.rs:20:11
|
LL | x.my_debug();
| ^^^^^^^^
note: `MyDebug` defines an item `my_debug`, perhaps you need to implement it
--> $DIR/call_method_on_inherent_impl_ref.rs:4:1
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | x.fmt(f);
|
= note: the method is available for `impl Debug` here
|
= help: items from traits can only be used if the trait is in scope
= help: items from traits can only be used if the trait is implemented and in scope
help: trait `Debug` which provides `fmt` is implemented but not in scope; perhaps you want to import it
|
LL + use std::fmt::Debug;
Expand All @@ -19,7 +19,7 @@ error[E0599]: no method named `fmt` found for mutable reference `&mut impl Debug
LL | x.fmt(f);
| ^^^ method not found in `&mut impl Debug`
|
= help: items from traits can only be used if the trait is in scope
= help: items from traits can only be used if the trait is implemented and in scope
help: the following traits which provide `fmt` are implemented but not in scope; perhaps you want to import one of them
|
LL + use std::fmt::Binary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ LL | struct Bar;
LL | f1.foo(1usize);
| ^^^ method not found in `Bar`
|
= help: items from traits can only be used if the trait is implemented and in scope
help: items from traits can only be used if the trait is implemented and in scope
--> $DIR/issue-21659-show-relevant-trait-impls-3.rs:20:8
|
LL | f1.foo(1usize);
| ^^^
note: `Foo` defines an item `foo`, perhaps you need to implement it
--> $DIR/issue-21659-show-relevant-trait-impls-3.rs:1:1
|
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/impl-trait/method-suggestion-no-duplication.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ LL | struct Foo;
LL | foo(|s| s.is_empty());
| ^^^^^^^^ method not found in `Foo`
|
= help: items from traits can only be used if the trait is implemented and in scope
help: items from traits can only be used if the trait is implemented and in scope
--> $DIR/method-suggestion-no-duplication.rs:7:15
|
LL | foo(|s| s.is_empty());
| ^^^^^^^^
= note: the following trait defines an item `is_empty`, perhaps you need to implement it:
candidate #1: `ExactSizeIterator`

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/impl-trait/no-method-suggested-traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ fn main() {

1u32.method();
//~^ ERROR no method named
//~|items from traits can only be used if the trait is in scope
//~|items from traits can only be used if the trait is implemented and in scope
std::rc::Rc::new(&mut Box::new(&1u32)).method();
//~^items from traits can only be used if the trait is in scope
//~^items from traits can only be used if the trait is implemented and in scope
//~| ERROR no method named `method` found for struct

'a'.method();
Expand Down
Loading

0 comments on commit 4f67d26

Please sign in to comment.