rust-lang / rust-lang/rust

"one of the expressions' fields has a method of the same name"'s help suggests adding `0.` instead of `.0`

Open
#133,011 1 comment 0 reactions 0 assignees View on GitHub

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

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=899783497cf148043b6fe15a5af2efba

struct Outer<'a>(&'a str);

fn main() {
    let outer = Outer("hi");
    outer.contains("foo");
}
Current output
error[E0599]: no method named `contains` found for struct `Outer` in the current scope
 --> src/main.rs:5:11
  |
1 | struct Outer<'a>(&'a str);
  | ---------------- method `contains` not found for this struct
...
5 |     outer.contains("foo");
  |           ^^^^^^^^ method not found in `Outer<'_>`
  |
  = help: items from traits can only be used if the trait is implemented and in scope
  = note: the following trait defines an item `contains`, perhaps you need to implement it:
          candidate #1: `RangeBounds`
help: one of the expressions' fields has a method of the same name
  |
5 |     outer.0.contains("foo");
  |           ++

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Desired output
error[E0599]: no method named `contains` found for struct `Outer` in the current scope
 --> src/main.rs:5:11
  |
1 | struct Outer<'a>(&'a str);
  | ---------------- method `contains` not found for this struct
...
5 |     outer.contains("foo");
  |           ^^^^^^^^ method not found in `Outer<'_>`
  |
  = help: items from traits can only be used if the trait is implemented and in scope
  = note: the following trait defines an item `contains`, perhaps you need to implement it:
          candidate #1: `RangeBounds`
help: one of the expressions' fields has a method of the same name
  |
5 |     outer.0.contains("foo");
  |          ++

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Rationale and extra context

This is an incredibly small change but I feel like suggesting .0 rather than 0. is more intuitive, as it better represents accessing the inner field rather than 0., which feels like an invalid rust snippet.

In Rust analyzer, the suggestion is also a little more confusing:

no method named `contains` found for reference `&Purged<'_>` in the current scope
method not found in `&Purged<'_>`rustc Click for full compiler diagnostic
example.rs(487, 48): one of the expressions' fields has a method of the same name: `0.`

Ultimately it produces correct code but it's just not as straightforward.

Other cases

Rust Version

Build using the Nightly version: 1.84.0-nightly

(2024-11-12 f7273e0044ad8f35ad27)
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the E0599 diagnostic with the Rust playground example and start by tracing the compiler diagnostic that emits “one of the expressions' fields has a method of the same name.” Add or update a regression test for tuple-field method suggestions, then verify the rendered help uses .0 rather than 0..

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.