Skip to content

Commit c4b6a59

Browse files
committed
Make E0599's label more clear for field which is used like a method.
fixes #127178
1 parent 52f3c71 commit c4b6a59

34 files changed

+110
-51
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,7 +2277,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22772277
let call_expr = tcx.hir().expect_expr(tcx.parent_hir_id(expr.hir_id));
22782278

22792279
if let Some(span) = call_expr.span.trim_start(item_name.span) {
2280-
err.span_suggestion(
2280+
err.span_suggestion_verbose(
22812281
span,
22822282
"remove the arguments",
22832283
"",
@@ -3266,7 +3266,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32663266
.copied();
32673267

32683268
if explain {
3269-
err.help("items from traits can only be used if the trait is in scope");
3269+
err.help(
3270+
"items from traits can only be used if the trait is implemented and in scope",
3271+
);
32703272
}
32713273

32723274
let msg = format!(
@@ -3570,7 +3572,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
35703572
}
35713573
}
35723574
}
3573-
if self.suggest_valid_traits(err, item_name, valid_out_of_scope_traits, true) {
3575+
if self.suggest_valid_traits(
3576+
err,
3577+
item_name,
3578+
valid_out_of_scope_traits,
3579+
!trait_missing_method,
3580+
) {
35743581
return;
35753582
}
35763583

tests/ui/async-await/in-trait/dont-project-to-specializable-projection.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LL | match fut.as_mut().poll(ctx) {
2929
|
3030
= note: the method is available for `Pin<&mut impl Future<Output = ()>>` here
3131
|
32-
= help: items from traits can only be used if the trait is in scope
32+
= help: items from traits can only be used if the trait is implemented and in scope
3333
help: trait `Future` which provides `poll` is implemented but not in scope; perhaps you want to import it
3434
|
3535
LL + use std::future::Future;

tests/ui/coherence/coherence_inherent.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0599]: no method named `the_fn` found for reference `&TheStruct` in the c
44
LL | s.the_fn();
55
| ^^^^^^ method not found in `&TheStruct`
66
|
7-
= help: items from traits can only be used if the trait is in scope
7+
= help: items from traits can only be used if the trait is implemented and in scope
88
help: trait `TheTrait` which provides `the_fn` is implemented but not in scope; perhaps you want to import it
99
|
1010
LL + use Lib::TheTrait;

tests/ui/coherence/coherence_inherent_cc.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0599]: no method named `the_fn` found for reference `&TheStruct` in the c
44
LL | s.the_fn();
55
| ^^^^^^ method not found in `&TheStruct`
66
|
7-
= help: items from traits can only be used if the trait is in scope
7+
= help: items from traits can only be used if the trait is implemented and in scope
88
help: trait `TheTrait` which provides `the_fn` is implemented but not in scope; perhaps you want to import it
99
|
1010
LL + use coherence_inherent_cc_lib::TheTrait;

tests/ui/confuse-field-and-method/issue-2392.stderr

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
1919
| ------------- method `not_closure` not found for this struct
2020
...
2121
LL | o_closure.not_closure();
22-
| ^^^^^^^^^^^-- help: remove the arguments
23-
| |
24-
| field, not a method
22+
| ^^^^^^^^^^^ field, not a method
23+
|
24+
help: remove the arguments
25+
|
26+
LL - o_closure.not_closure();
27+
LL + o_closure.not_closure;
28+
|
2529

2630
error[E0599]: no method named `closure` found for struct `Obj` in the current scope
2731
--> $DIR/issue-2392.rs:42:12
@@ -86,9 +90,13 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
8690
| ------------- method `not_closure` not found for this struct
8791
...
8892
LL | w.wrap.not_closure();
89-
| ^^^^^^^^^^^-- help: remove the arguments
90-
| |
91-
| field, not a method
93+
| ^^^^^^^^^^^ field, not a method
94+
|
95+
help: remove the arguments
96+
|
97+
LL - w.wrap.not_closure();
98+
LL + w.wrap.not_closure;
99+
|
92100

93101
error[E0599]: no method named `closure` found for struct `Obj` in the current scope
94102
--> $DIR/issue-2392.rs:58:24

tests/ui/generic-associated-types/method-unsatisfied-assoc-type-predicate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn f(a: S) {
2626
a.f();
2727
//~^ ERROR the method `f` exists for struct `S`, but its trait bounds were not satisfied
2828
//~| NOTE method cannot be called on `S` due to unsatisfied trait bounds
29+
//~| HELP items from traits can only be used if the trait is implemented and in scope
2930
}
3031

3132
fn main() {}

tests/ui/hygiene/no_implicit_prelude.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ LL | fn f() { ::bar::m!(); }
2222
LL | ().clone()
2323
| ^^^^^
2424
|
25-
= help: items from traits can only be used if the trait is in scope
25+
= help: items from traits can only be used if the trait is implemented and in scope
2626
help: there is a method `clone_from` with a similar name, but with different arguments
2727
--> $SRC_DIR/core/src/clone.rs:LL:COL
2828
= note: this error originates in the macro `::bar::m` (in Nightly builds, run with -Z macro-backtrace for more info)

tests/ui/hygiene/trait_items.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | fn f() { ::baz::m!(); }
1010
LL | pub macro m() { ().f() }
1111
| ^ method not found in `()`
1212
|
13-
= help: items from traits can only be used if the trait is in scope
13+
= help: items from traits can only be used if the trait is implemented and in scope
1414
= note: this error originates in the macro `::baz::m` (in Nightly builds, run with -Z macro-backtrace for more info)
1515
help: trait `T` which provides `f` is implemented but not in scope; perhaps you want to import it
1616
|

tests/ui/impl-trait/call_method_without_import.no_import.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | x.fmt(f);
77
|
88
= note: the method is available for `impl Debug` here
99
|
10-
= help: items from traits can only be used if the trait is in scope
10+
= help: items from traits can only be used if the trait is implemented and in scope
1111
help: trait `Debug` which provides `fmt` is implemented but not in scope; perhaps you want to import it
1212
|
1313
LL + use std::fmt::Debug;
@@ -19,7 +19,7 @@ error[E0599]: no method named `fmt` found for mutable reference `&mut impl Debug
1919
LL | x.fmt(f);
2020
| ^^^ method not found in `&mut impl Debug`
2121
|
22-
= help: items from traits can only be used if the trait is in scope
22+
= help: items from traits can only be used if the trait is implemented and in scope
2323
help: the following traits which provide `fmt` are implemented but not in scope; perhaps you want to import one of them
2424
|
2525
LL + use std::fmt::Binary;

tests/ui/impl-trait/no-method-suggested-traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ fn main() {
2222

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

3030
'a'.method();

tests/ui/impl-trait/no-method-suggested-traits.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0599]: no method named `method` found for type `u32` in the current scope
44
LL | 1u32.method();
55
| ^^^^^^
66
|
7-
= help: items from traits can only be used if the trait is in scope
7+
= help: items from traits can only be used if the trait is implemented and in scope
88
help: the following traits which provide `method` are implemented but not in scope; perhaps you want to import one of them
99
|
1010
LL + use foo::Bar;
@@ -26,7 +26,7 @@ error[E0599]: no method named `method` found for struct `Rc<&mut Box<&u32>>` in
2626
LL | std::rc::Rc::new(&mut Box::new(&1u32)).method();
2727
| ^^^^^^
2828
|
29-
= help: items from traits can only be used if the trait is in scope
29+
= help: items from traits can only be used if the trait is implemented and in scope
3030
help: the following traits which provide `method` are implemented but not in scope; perhaps you want to import one of them
3131
|
3232
LL + use foo::Bar;
@@ -51,7 +51,7 @@ LL | fn method(&self) {}
5151
LL | 'a'.method();
5252
| ^^^^^^
5353
|
54-
= help: items from traits can only be used if the trait is in scope
54+
= help: items from traits can only be used if the trait is implemented and in scope
5555
help: trait `Bar` which provides `method` is implemented but not in scope; perhaps you want to import it
5656
|
5757
LL + use foo::Bar;
@@ -67,7 +67,7 @@ error[E0599]: no method named `method` found for struct `Rc<&mut Box<&char>>` in
6767
LL | std::rc::Rc::new(&mut Box::new(&'a')).method();
6868
| ^^^^^^
6969
|
70-
= help: items from traits can only be used if the trait is in scope
70+
= help: items from traits can only be used if the trait is implemented and in scope
7171
help: trait `Bar` which provides `method` is implemented but not in scope; perhaps you want to import it
7272
|
7373
LL + use foo::Bar;
@@ -88,7 +88,7 @@ LL | 1i32.method();
8888
LL | fn method(&self) {}
8989
| ------ the method is available for `i32` here
9090
|
91-
= help: items from traits can only be used if the trait is in scope
91+
= help: items from traits can only be used if the trait is implemented and in scope
9292
help: trait `PubPub` which provides `method` is implemented but not in scope; perhaps you want to import it
9393
|
9494
LL + use no_method_suggested_traits::foo::PubPub;
@@ -104,7 +104,7 @@ error[E0599]: no method named `method` found for struct `Rc<&mut Box<&i32>>` in
104104
LL | std::rc::Rc::new(&mut Box::new(&1i32)).method();
105105
| ^^^^^^
106106
|
107-
= help: items from traits can only be used if the trait is in scope
107+
= help: items from traits can only be used if the trait is implemented and in scope
108108
help: trait `PubPub` which provides `method` is implemented but not in scope; perhaps you want to import it
109109
|
110110
LL + use no_method_suggested_traits::foo::PubPub;

tests/ui/imports/overlapping_pub_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ fn main() {
1111
use overlapping_pub_trait_source::S;
1212
S.method();
1313
//~^ ERROR no method named `method` found for struct `S` in the current scope [E0599]
14-
//~| HELP items from traits can only be used if the trait is in scope
14+
//~| HELP items from traits can only be used if the trait is implemented and in scope
1515
}

tests/ui/imports/overlapping_pub_trait.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | S.method();
99
LL | pub trait Tr { fn method(&self); }
1010
| ------ the method is available for `S` here
1111
|
12-
= help: items from traits can only be used if the trait is in scope
12+
= help: items from traits can only be used if the trait is implemented and in scope
1313
help: trait `Tr` which provides `method` is implemented but not in scope; perhaps you want to import it
1414
|
1515
LL + use overlapping_pub_trait_source::m::Tr;

tests/ui/imports/unnamed_pub_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ fn main() {
1212
use unnamed_pub_trait_source::S;
1313
S.method();
1414
//~^ ERROR no method named `method` found for struct `S` in the current scope [E0599]
15-
//~| HELP items from traits can only be used if the trait is in scope
15+
//~| HELP items from traits can only be used if the trait is implemented and in scope
1616
}

tests/ui/imports/unnamed_pub_trait.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | S.method();
99
LL | pub trait Tr { fn method(&self); }
1010
| ------ the method is available for `S` here
1111
|
12-
= help: items from traits can only be used if the trait is in scope
12+
= help: items from traits can only be used if the trait is implemented and in scope
1313
help: trait `Tr` which provides `method` is implemented but not in scope; perhaps you want to import it
1414
|
1515
LL + use unnamed_pub_trait_source::prelude::*; // trait Tr

tests/ui/issues/issue-10465.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0599]: no method named `foo` found for reference `&B` in the current scop
44
LL | b.foo();
55
| ^^^ method not found in `&B`
66
|
7-
= help: items from traits can only be used if the trait is in scope
7+
= help: items from traits can only be used if the trait is implemented and in scope
88
help: trait `A` which provides `foo` is implemented but not in scope; perhaps you want to import it
99
|
1010
LL + use a::A;

tests/ui/issues/issue-39175.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0599]: no method named `exec` found for mutable reference `&mut Command`
44
LL | Command::new("echo").arg("hello").exec();
55
| ^^^^
66
|
7-
= help: items from traits can only be used if the trait is in scope
7+
= help: items from traits can only be used if the trait is implemented and in scope
88
help: there is a method `pre_exec` with a similar name, but with different arguments
99
--> $SRC_DIR/std/src/os/unix/process.rs:LL:COL
1010
help: trait `CommandExt` which provides `exec` is implemented but not in scope; perhaps you want to import it

tests/ui/issues/issue-56175.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | reexported_trait::FooStruct.trait_method();
99
LL | fn trait_method(&self) {
1010
| ------------ the method is available for `FooStruct` here
1111
|
12-
= help: items from traits can only be used if the trait is in scope
12+
= help: items from traits can only be used if the trait is implemented and in scope
1313
help: trait `Trait` which provides `trait_method` is implemented but not in scope; perhaps you want to import it
1414
|
1515
LL + use reexported_trait::Trait;
@@ -30,7 +30,7 @@ LL | reexported_trait::FooStruct.trait_method_b();
3030
LL | fn trait_method_b(&self) {
3131
| -------------- the method is available for `FooStruct` here
3232
|
33-
= help: items from traits can only be used if the trait is in scope
33+
= help: items from traits can only be used if the trait is implemented and in scope
3434
help: trait `TraitB` which provides `trait_method_b` is implemented but not in scope; perhaps you want to import it
3535
|
3636
LL + use reexported_trait::TraitBRename;

tests/ui/methods/field-method-suggestion-using-return-ty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ impl Wrapper<Option<i32>> {
44
fn inner_mut(&self) -> Option<&mut i32> {
55
self.as_mut()
66
//~^ ERROR no method named `as_mut` found for reference `&Wrapper<Option<i32>>` in the current scope
7+
//~| HELP items from traits can only be used if the trait is implemented and in scope
78
//~| HELP one of the expressions' fields has a method of the same name
8-
//~| HELP items from traits can only be used if
99
}
1010

1111
fn inner_mut_bad(&self) -> Option<&mut u32> {
1212
self.as_mut()
1313
//~^ ERROR no method named `as_mut` found for reference `&Wrapper<Option<i32>>` in the current scope
14-
//~| HELP items from traits can only be used if
14+
//~| HELP items from traits can only be used if the trait is implemented and in scope
1515
}
1616
}
1717

tests/ui/rust-2018/trait-import-suggestions.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | fn foobar(&self) { }
77
LL | x.foobar();
88
| ^^^^^^
99
|
10-
= help: items from traits can only be used if the trait is in scope
10+
= help: items from traits can only be used if the trait is implemented and in scope
1111
help: trait `Foobar` which provides `foobar` is implemented but not in scope; perhaps you want to import it
1212
|
1313
LL + use crate::foo::foobar::Foobar;
@@ -26,7 +26,7 @@ LL | fn bar(&self) { }
2626
LL | x.bar();
2727
| ^^^
2828
|
29-
= help: items from traits can only be used if the trait is in scope
29+
= help: items from traits can only be used if the trait is implemented and in scope
3030
help: trait `Bar` which provides `bar` is implemented but not in scope; perhaps you want to import it
3131
|
3232
LL + use crate::foo::Bar;
@@ -53,7 +53,7 @@ error[E0599]: no function or associated item named `from_str` found for type `u3
5353
LL | let y = u32::from_str("33");
5454
| ^^^^^^^^ function or associated item not found in `u32`
5555
|
56-
= help: items from traits can only be used if the trait is in scope
56+
= help: items from traits can only be used if the trait is implemented and in scope
5757
help: trait `FromStr` which provides `from_str` is implemented but not in scope; perhaps you want to import it
5858
|
5959
LL + use std::str::FromStr;

tests/ui/rust-2018/uniform-paths/issue-87932.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | pub struct A {}
77
LL | A::deserialize();
88
| ^^^^^^^^^^^ function or associated item not found in `A`
99
|
10-
= help: items from traits can only be used if the trait is in scope
10+
= help: items from traits can only be used if the trait is implemented and in scope
1111
help: trait `Deserialize` which provides `deserialize` is implemented but not in scope; perhaps you want to import it
1212
|
1313
LL + use <crate::A as issue_87932_a::Deserialize>::deserialize::_a::Deserialize;

tests/ui/rust-2021/future-prelude-collision-shadow.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0599]: no method named `try_into` found for type `u8` in the current scop
44
LL | let _: u32 = 3u8.try_into().unwrap();
55
| ^^^^^^^^
66
|
7-
= help: items from traits can only be used if the trait is in scope
7+
= help: items from traits can only be used if the trait is implemented and in scope
88
= note: 'std::convert::TryInto' is included in the prelude starting in Edition 2021
99
help: the following traits which provide `try_into` are implemented but not in scope; perhaps you want to import one of them
1010
|

tests/ui/shadowed/shadowed-trait-methods.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | pub trait T { fn f(&self) {} }
77
LL | ().f()
88
| ^ method not found in `()`
99
|
10-
= help: items from traits can only be used if the trait is in scope
10+
= help: items from traits can only be used if the trait is implemented and in scope
1111
help: trait `T` which provides `f` is implemented but not in scope; perhaps you want to import it
1212
|
1313
LL + use foo::T;

tests/ui/suggestions/dont-wrap-ambiguous-receivers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ mod banana {
1717
fn main() {
1818
banana::Chaenomeles.pick()
1919
//~^ ERROR no method named
20-
//~| HELP items from traits can only be used if the trait is in scope
20+
//~| HELP items from traits can only be used if the trait is implemented and in scope
2121
}

tests/ui/suggestions/dont-wrap-ambiguous-receivers.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | pub struct Chaenomeles;
77
LL | banana::Chaenomeles.pick()
88
| ^^^^ method not found in `Chaenomeles`
99
|
10-
= help: items from traits can only be used if the trait is in scope
10+
= help: items from traits can only be used if the trait is implemented and in scope
1111
help: the following traits which provide `pick` are implemented but not in scope; perhaps you want to import one of them
1212
|
1313
LL + use banana::Apple;

tests/ui/suggestions/import-trait-for-method-call.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | h.finish()
77
|
88
= note: the method is available for `DefaultHasher` here
99
|
10-
= help: items from traits can only be used if the trait is in scope
10+
= help: items from traits can only be used if the trait is implemented and in scope
1111
help: trait `Hasher` which provides `finish` is implemented but not in scope; perhaps you want to import it
1212
|
1313
LL + use std::hash::Hasher;

tests/ui/suggestions/suggest-tryinto-edition-change.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ LL | let _i: i16 = 0_i32.try_into().unwrap();
4747
|
4848
= note: the method is available for `i32` here
4949
|
50-
= help: items from traits can only be used if the trait is in scope
50+
= help: items from traits can only be used if the trait is implemented and in scope
5151
= note: 'std::convert::TryInto' is included in the prelude starting in Edition 2021
5252
help: trait `TryInto` which provides `try_into` is implemented but not in scope; perhaps you want to import it
5353
|

tests/ui/suggestions/use-placement-typeck.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | }
1010
LL | pub struct S;
1111
| ------------ method `abc` not found for this struct
1212
|
13-
= help: items from traits can only be used if the trait is in scope
13+
= help: items from traits can only be used if the trait is implemented and in scope
1414
help: trait `Foo` which provides `abc` is implemented but not in scope; perhaps you want to import it
1515
|
1616
LL + use m::Foo;

0 commit comments

Comments
 (0)