confusing error when trying to call a not-in-scope method that shares a name with a field
Open
Nobody has claimed this yet.
A-diagnostics
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
struct Struct { x: u8 }
mod m {
trait GetX {
fn x(&self) -> u8;
}
impl GetX for super::Struct {
fn x(&self) -> u8 {
self.x
}
}
}
pub fn show_x(st: Struct) {
println!("{}", st.x())
}
Current output
error[E0599]: no method named `x` found for struct `Struct` in the current scope
--> src/lib.rs:16:23
|
1 | struct Struct { x: u8 }
| ------------- method `x` not found for this struct
...
16 | println!("{}", st.x())
| ^ field, not a method
|
= help: items from traits can only be used if the trait is implemented and in scope
help: remove the arguments
|
16 - println!("{}", st.x())
16 + println!("{}", st.x)
|
help: trait `GetX` which provides `x` is implemented but not in scope; perhaps you want to import it
|
1 + use crate::m::GetX;
|
Desired output
error[E0599]: no method named `x` found for struct `Struct` in the current scope
--> src/lib.rs:16:23
|
1 | struct Struct { x: u8 }
| ------------- method `x` not found for this struct
...
16 | println!("{}", st.x())
| ^ field, method not in-scope
|
= help: items from traits can only be used if the trait is implemented and in scope
help: trait `GetX` which provides `x` is implemented but not in scope; perhaps you want to import it
|
1 + use crate::m::GetX;
|
help: remove the arguments
|
16 - println!("{}", st.x())
16 + println!("{}", st.x)
|
Rationale and extra context
No response
Other cases
there should also be a unit test to make sure the output makes since when the field is not available directly, but instead by `Deref` coercion. it is extremely confusing to see "field, not a method" when a type's documentation lists it as a method, but not a field.
Rust Version
rustc 1.81.0-nightly (8337ba918 2024-06-12)
binary: rustc
commit-hash: 8337ba9189de188e2ed417018af2bf17a57d51ac
commit-date: 2024-06-12
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7
Anything else?
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the Rust reproducer from the issue and compare the current diagnostic with the desired output. Trace the compiler diagnostic path for E0599 and add coverage for both a directly available field and a field reached through Deref coercion; done means the message distinguishes a field from a method that is not in scope.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100