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 Aug 14, 2024
1 parent 52f3c71 commit c4b6a59
Show file tree
Hide file tree
Showing 34 changed files with 110 additions and 51 deletions.
13 changes: 10 additions & 3 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
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
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
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() {}
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 @@ -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
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
12 changes: 6 additions & 6 deletions tests/ui/impl-trait/no-method-suggested-traits.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0599]: no method named `method` found for type `u32` in the current scope
LL | 1u32.method();
| ^^^^^^
|
= 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 `method` are implemented but not in scope; perhaps you want to import one of them
|
LL + use foo::Bar;
Expand All @@ -26,7 +26,7 @@ error[E0599]: no method named `method` found for struct `Rc<&mut Box<&u32>>` in
LL | std::rc::Rc::new(&mut Box::new(&1u32)).method();
| ^^^^^^
|
= 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 `method` are implemented but not in scope; perhaps you want to import one of them
|
LL + use foo::Bar;
Expand All @@ -51,7 +51,7 @@ LL | fn method(&self) {}
LL | 'a'.method();
| ^^^^^^
|
= 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 `Bar` which provides `method` is implemented but not in scope; perhaps you want to import it
|
LL + use foo::Bar;
Expand All @@ -67,7 +67,7 @@ error[E0599]: no method named `method` found for struct `Rc<&mut Box<&char>>` in
LL | std::rc::Rc::new(&mut Box::new(&'a')).method();
| ^^^^^^
|
= 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 `Bar` which provides `method` is implemented but not in scope; perhaps you want to import it
|
LL + use foo::Bar;
Expand All @@ -88,7 +88,7 @@ LL | 1i32.method();
LL | fn method(&self) {}
| ------ the method is available for `i32` 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 `PubPub` which provides `method` is implemented but not in scope; perhaps you want to import it
|
LL + use no_method_suggested_traits::foo::PubPub;
Expand All @@ -104,7 +104,7 @@ error[E0599]: no method named `method` found for struct `Rc<&mut Box<&i32>>` in
LL | std::rc::Rc::new(&mut Box::new(&1i32)).method();
| ^^^^^^
|
= 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 `PubPub` which provides `method` is implemented but not in scope; perhaps you want to import it
|
LL + use no_method_suggested_traits::foo::PubPub;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/overlapping_pub_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ fn main() {
use overlapping_pub_trait_source::S;
S.method();
//~^ ERROR no method named `method` found for struct `S` in the current scope [E0599]
//~| 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
}
2 changes: 1 addition & 1 deletion tests/ui/imports/overlapping_pub_trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LL | S.method();
LL | pub trait Tr { fn method(&self); }
| ------ the method is available for `S` 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 `Tr` which provides `method` is implemented but not in scope; perhaps you want to import it
|
LL + use overlapping_pub_trait_source::m::Tr;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/unnamed_pub_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ fn main() {
use unnamed_pub_trait_source::S;
S.method();
//~^ ERROR no method named `method` found for struct `S` in the current scope [E0599]
//~| 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
}
2 changes: 1 addition & 1 deletion tests/ui/imports/unnamed_pub_trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LL | S.method();
LL | pub trait Tr { fn method(&self); }
| ------ the method is available for `S` 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 `Tr` which provides `method` is implemented but not in scope; perhaps you want to import it
|
LL + use unnamed_pub_trait_source::prelude::*; // trait Tr
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-10465.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0599]: no method named `foo` found for reference `&B` in the current scop
LL | b.foo();
| ^^^ method not found in `&B`
|
= 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 `A` which provides `foo` is implemented but not in scope; perhaps you want to import it
|
LL + use a::A;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-39175.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0599]: no method named `exec` found for mutable reference `&mut Command`
LL | Command::new("echo").arg("hello").exec();
| ^^^^
|
= 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 `pre_exec` with a similar name, but with different arguments
--> $SRC_DIR/std/src/os/unix/process.rs:LL:COL
help: trait `CommandExt` which provides `exec` is implemented but not in scope; perhaps you want to import it
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/issues/issue-56175.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LL | reexported_trait::FooStruct.trait_method();
LL | fn trait_method(&self) {
| ------------ the method is available for `FooStruct` 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 `Trait` which provides `trait_method` is implemented but not in scope; perhaps you want to import it
|
LL + use reexported_trait::Trait;
Expand All @@ -30,7 +30,7 @@ LL | reexported_trait::FooStruct.trait_method_b();
LL | fn trait_method_b(&self) {
| -------------- the method is available for `FooStruct` 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 `TraitB` which provides `trait_method_b` is implemented but not in scope; perhaps you want to import it
|
LL + use reexported_trait::TraitBRename;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/methods/field-method-suggestion-using-return-ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ impl Wrapper<Option<i32>> {
fn inner_mut(&self) -> Option<&mut i32> {
self.as_mut()
//~^ ERROR no method named `as_mut` found for reference `&Wrapper<Option<i32>>` in the current scope
//~| HELP items from traits can only be used if the trait is implemented and in scope
//~| HELP one of the expressions' fields has a method of the same name
//~| HELP items from traits can only be used if
}

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

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/rust-2018/trait-import-suggestions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | fn foobar(&self) { }
LL | x.foobar();
| ^^^^^^
|
= 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 `Foobar` which provides `foobar` is implemented but not in scope; perhaps you want to import it
|
LL + use crate::foo::foobar::Foobar;
Expand All @@ -26,7 +26,7 @@ LL | fn bar(&self) { }
LL | x.bar();
| ^^^
|
= 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 `Bar` which provides `bar` is implemented but not in scope; perhaps you want to import it
|
LL + use crate::foo::Bar;
Expand All @@ -53,7 +53,7 @@ error[E0599]: no function or associated item named `from_str` found for type `u3
LL | let y = u32::from_str("33");
| ^^^^^^^^ function or associated item not found in `u32`
|
= 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 `FromStr` which provides `from_str` is implemented but not in scope; perhaps you want to import it
|
LL + use std::str::FromStr;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/rust-2018/uniform-paths/issue-87932.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | pub struct A {}
LL | A::deserialize();
| ^^^^^^^^^^^ function or associated item not found in `A`
|
= 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 `Deserialize` which provides `deserialize` is implemented but not in scope; perhaps you want to import it
|
LL + use <crate::A as issue_87932_a::Deserialize>::deserialize::_a::Deserialize;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/rust-2021/future-prelude-collision-shadow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0599]: no method named `try_into` found for type `u8` in the current scop
LL | let _: u32 = 3u8.try_into().unwrap();
| ^^^^^^^^
|
= 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: 'std::convert::TryInto' is included in the prelude starting in Edition 2021
help: the following traits which provide `try_into` are implemented but not in scope; perhaps you want to import one of them
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/shadowed/shadowed-trait-methods.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | pub trait T { fn f(&self) {} }
LL | ().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
help: trait `T` which provides `f` is implemented but not in scope; perhaps you want to import it
|
LL + use foo::T;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/suggestions/dont-wrap-ambiguous-receivers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ mod banana {
fn main() {
banana::Chaenomeles.pick()
//~^ ERROR no method named
//~| 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
}
2 changes: 1 addition & 1 deletion tests/ui/suggestions/dont-wrap-ambiguous-receivers.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | pub struct Chaenomeles;
LL | banana::Chaenomeles.pick()
| ^^^^ method not found in `Chaenomeles`
|
= 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 `pick` are implemented but not in scope; perhaps you want to import one of them
|
LL + use banana::Apple;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/suggestions/import-trait-for-method-call.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | h.finish()
|
= note: the method is available for `DefaultHasher` 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 `Hasher` which provides `finish` is implemented but not in scope; perhaps you want to import it
|
LL + use std::hash::Hasher;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/suggestions/suggest-tryinto-edition-change.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ LL | let _i: i16 = 0_i32.try_into().unwrap();
|
= note: the method is available for `i32` 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
= note: 'std::convert::TryInto' is included in the prelude starting in Edition 2021
help: trait `TryInto` which provides `try_into` is implemented but not in scope; perhaps you want to import it
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/suggestions/use-placement-typeck.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ LL | }
LL | pub struct S;
| ------------ method `abc` not found for this struct
|
= 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 `Foo` which provides `abc` is implemented but not in scope; perhaps you want to import it
|
LL + use m::Foo;
Expand Down
Loading

0 comments on commit c4b6a59

Please sign in to comment.