Skip to content

Suggest fully qualified path on method name collision#153662

Open
estebank wants to merge 4 commits intorust-lang:mainfrom
estebank:suggest-fully-qualified-path
Open

Suggest fully qualified path on method name collision#153662
estebank wants to merge 4 commits intorust-lang:mainfrom
estebank:suggest-fully-qualified-path

Conversation

@estebank
Copy link
Copy Markdown
Contributor

@estebank estebank commented Mar 10, 2026

Provide suggestion for using a fully qualified path when method names collide between traits and inherent impl.

error[E0061]: this method takes 0 arguments but 1 argument was supplied
  --> $DIR/shadowed-intrinsic-method.rs:20:7
   |
LL |     a.borrow(());
   |       ^^^^^^ -- unexpected argument of type `()`
   |
note: the `borrow` call is resolved to the method in `std::borrow::Borrow`, shadowing the method of the same name on the inherent impl for `A`
  --> $DIR/shadowed-intrinsic-method.rs:20:7
   |
LL | use std::borrow::Borrow;
   |     ------------------- `std::borrow::Borrow` imported here
...
LL |     a.borrow(());
   |       ^^^^^^ refers to `std::borrow::Borrow::borrow`
note: method defined here
  --> $SRC_DIR/core/src/borrow.rs:LL:COL
help: you might have meant to call the other method; you can use the fully-qualified path to call it explicitly
   |
LL -     a.borrow(());
LL +     A::borrow(&mut a, ());
   |
help: remove the extra argument
   |
LL -     a.borrow(());
LL +     a.borrow();
   |

Fix #54103.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 10, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 10, 2026

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 17 candidates

),
),
let fn_sig = self.tcx.fn_sig(m.def_id);
if fn_sig.skip_binder().inputs().skip_binder().len() != args.len() + 1 {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be running this logic 3 times: one for "all of the arguments apply", "the number of arguments match" and "only the name matches", in order. If any of the cases happens, break (as we get less and less confident the further away we get from things matching).

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 11, 2026
Detect inherent method behind deref being shadowed by trait method

```
error[E0277]: the trait bound `Rc<RefCell<S>>: Borrow<S>` is not satisfied
  --> $DIR/shadowed-intrinsic-method-deref.rs:16:22
   |
LL |     let sb : &S = &s.borrow();
   |                      ^^^^^^ the trait `Borrow<S>` is not implemented for `Rc<RefCell<S>>`
   |
help: the trait `Borrow<S>` is not implemented for `Rc<RefCell<S>>`
      but trait `Borrow<RefCell<S>>` is implemented for it
  --> $SRC_DIR/alloc/src/rc.rs:LL:COL
   = help: for that trait implementation, expected `RefCell<S>`, found `S`
   = note: there's an inherent method on `RefCell<S>` of the same name, which can be auto-dereferenced from `&RefCell<T>`
help: to access the inherent method on `RefCell<S>`, use the fully-qualified path
   |
LL -     let sb : &S = &s.borrow();
LL +     let sb : &S = &RefCell::borrow(&s);
   |
```

In the example above, method `borrow` is available both on `<RefCell<S> as Borrow<S>>` *and* on `RefCell<S>`. Adding the import `use std::borrow::Borrow;` causes `s.borrow()` to find the former instead of the latter. We now point out that the other exists, and provide a suggestion on how to call it.

Fix rust-lang#41906. CC rust-lang#153662.
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Mar 12, 2026
Detect inherent method behind deref being shadowed by trait method

```
error[E0277]: the trait bound `Rc<RefCell<S>>: Borrow<S>` is not satisfied
  --> $DIR/shadowed-intrinsic-method-deref.rs:16:22
   |
LL |     let sb : &S = &s.borrow();
   |                      ^^^^^^ the trait `Borrow<S>` is not implemented for `Rc<RefCell<S>>`
   |
help: the trait `Borrow<S>` is not implemented for `Rc<RefCell<S>>`
      but trait `Borrow<RefCell<S>>` is implemented for it
  --> $SRC_DIR/alloc/src/rc.rs:LL:COL
   = help: for that trait implementation, expected `RefCell<S>`, found `S`
   = note: there's an inherent method on `RefCell<S>` of the same name, which can be auto-dereferenced from `&RefCell<T>`
help: to access the inherent method on `RefCell<S>`, use the fully-qualified path
   |
LL -     let sb : &S = &s.borrow();
LL +     let sb : &S = &RefCell::borrow(&s);
   |
```

In the example above, method `borrow` is available both on `<RefCell<S> as Borrow<S>>` *and* on `RefCell<S>`. Adding the import `use std::borrow::Borrow;` causes `s.borrow()` to find the former instead of the latter. We now point out that the other exists, and provide a suggestion on how to call it.

Fix rust-lang/rust#41906. CC rust-lang/rust#153662.
Copy link
Copy Markdown
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidtwco
Copy link
Copy Markdown
Member

@bors r+ rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Mar 25, 2026

📌 Commit 67036af has been approved by davidtwco

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 25, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 25, 2026
…ath, r=davidtwco

Suggest fully qualified path on method name collision

Provide suggestion for using a fully qualified path when method names collide between traits and inherent impl.

```
error[E0061]: this method takes 0 arguments but 1 argument was supplied
  --> $DIR/shadowed-intrinsic-method.rs:20:7
   |
LL |     a.borrow(());
   |       ^^^^^^ -- unexpected argument of type `()`
   |
note: the `borrow` call is resolved to the method in `std::borrow::Borrow`, shadowing the method of the same name on the inherent impl for `A`
  --> $DIR/shadowed-intrinsic-method.rs:20:7
   |
LL | use std::borrow::Borrow;
   |     ------------------- `std::borrow::Borrow` imported here
...
LL |     a.borrow(());
   |       ^^^^^^ refers to `std::borrow::Borrow::borrow`
note: method defined here
  --> $SRC_DIR/core/src/borrow.rs:LL:COL
help: you might have meant to call the other method; you can use the fully-qualified path to call it explicitly
   |
LL -     a.borrow(());
LL +     A::borrow(&mut a, ());
   |
help: remove the extra argument
   |
LL -     a.borrow(());
LL +     a.borrow();
   |
```

Fix rust-lang#54103.
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

@bors r-
#154378 (comment)

@rust-bors rust-bors bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Mar 25, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Mar 25, 2026

This pull request was unapproved.

This PR was contained in a rollup (#154378), which was unapproved.

```
error[E0061]: this method takes 0 arguments but 1 argument was supplied
  --> $DIR/shadowed-intrinsic-method.rs:18:7
   |
LL |     a.borrow(());
   |       ^^^^^^ -- unexpected argument of type `()`
   |
note: the `borrow` call is resolved to the method in `std::borrow::Borrow`, shadowing the method of the same name on the inherent impl for `A`
  --> $DIR/shadowed-intrinsic-method.rs:18:7
   |
LL | use std::borrow::Borrow;
   |     ------------------- `std::borrow::Borrow` imported here
...
LL |     a.borrow(());
   |       ^^^^^^ refers to `std::borrow::Borrow::borrow`
note: method defined here
  --> $SRC_DIR/core/src/borrow.rs:LL:COL
help: you might have meant to call the other method; you can use the fully-qualified path to call it explicitly
   |
LL -     a.borrow(());
LL +     A::borrow(&mut a, ());
   |
help: remove the extra argument
   |
LL -     a.borrow(());
LL +     a.borrow();
   |
```
Handle correct gramar in the face of a single other option, or many.
@estebank estebank force-pushed the suggest-fully-qualified-path branch from 67036af to 0bd2852 Compare March 25, 2026 19:22
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 25, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@davidtwco
Copy link
Copy Markdown
Member

@bors r+

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Mar 26, 2026

📌 Commit 0bd2852 has been approved by davidtwco

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 26, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 26, 2026
…ath, r=davidtwco

Suggest fully qualified path on method name collision

Provide suggestion for using a fully qualified path when method names collide between traits and inherent impl.

```
error[E0061]: this method takes 0 arguments but 1 argument was supplied
  --> $DIR/shadowed-intrinsic-method.rs:20:7
   |
LL |     a.borrow(());
   |       ^^^^^^ -- unexpected argument of type `()`
   |
note: the `borrow` call is resolved to the method in `std::borrow::Borrow`, shadowing the method of the same name on the inherent impl for `A`
  --> $DIR/shadowed-intrinsic-method.rs:20:7
   |
LL | use std::borrow::Borrow;
   |     ------------------- `std::borrow::Borrow` imported here
...
LL |     a.borrow(());
   |       ^^^^^^ refers to `std::borrow::Borrow::borrow`
note: method defined here
  --> $SRC_DIR/core/src/borrow.rs:LL:COL
help: you might have meant to call the other method; you can use the fully-qualified path to call it explicitly
   |
LL -     a.borrow(());
LL +     A::borrow(&mut a, ());
   |
help: remove the extra argument
   |
LL -     a.borrow(());
LL +     a.borrow();
   |
```

Fix rust-lang#54103.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 26, 2026
…ath, r=davidtwco

Suggest fully qualified path on method name collision

Provide suggestion for using a fully qualified path when method names collide between traits and inherent impl.

```
error[E0061]: this method takes 0 arguments but 1 argument was supplied
  --> $DIR/shadowed-intrinsic-method.rs:20:7
   |
LL |     a.borrow(());
   |       ^^^^^^ -- unexpected argument of type `()`
   |
note: the `borrow` call is resolved to the method in `std::borrow::Borrow`, shadowing the method of the same name on the inherent impl for `A`
  --> $DIR/shadowed-intrinsic-method.rs:20:7
   |
LL | use std::borrow::Borrow;
   |     ------------------- `std::borrow::Borrow` imported here
...
LL |     a.borrow(());
   |       ^^^^^^ refers to `std::borrow::Borrow::borrow`
note: method defined here
  --> $SRC_DIR/core/src/borrow.rs:LL:COL
help: you might have meant to call the other method; you can use the fully-qualified path to call it explicitly
   |
LL -     a.borrow(());
LL +     A::borrow(&mut a, ());
   |
help: remove the extra argument
   |
LL -     a.borrow(());
LL +     a.borrow();
   |
```

Fix rust-lang#54103.
rust-bors bot pushed a commit that referenced this pull request Mar 27, 2026
Rollup of 7 pull requests

Successful merges:

 - #152457 (Pass -pg to linker when using -Zinstrument-mcount)
 - #154031 (Remove divergence check from check_expr_array)
 - #154418 (move many tests out of `ui/unsafe`)
 - #153662 (Suggest fully qualified path on method name collision)
 - #153675 (simd_add/sub/mul/neg: document overflow behavior)
 - #154110 (Change "error finalizing incremental compilation" text and emit it as a note, not a warning)
 - #154430 (Create GPU target notification group)
Zalathar added a commit to Zalathar/rust that referenced this pull request Mar 27, 2026
…ath, r=davidtwco

Suggest fully qualified path on method name collision

Provide suggestion for using a fully qualified path when method names collide between traits and inherent impl.

```
error[E0061]: this method takes 0 arguments but 1 argument was supplied
  --> $DIR/shadowed-intrinsic-method.rs:20:7
   |
LL |     a.borrow(());
   |       ^^^^^^ -- unexpected argument of type `()`
   |
note: the `borrow` call is resolved to the method in `std::borrow::Borrow`, shadowing the method of the same name on the inherent impl for `A`
  --> $DIR/shadowed-intrinsic-method.rs:20:7
   |
LL | use std::borrow::Borrow;
   |     ------------------- `std::borrow::Borrow` imported here
...
LL |     a.borrow(());
   |       ^^^^^^ refers to `std::borrow::Borrow::borrow`
note: method defined here
  --> $SRC_DIR/core/src/borrow.rs:LL:COL
help: you might have meant to call the other method; you can use the fully-qualified path to call it explicitly
   |
LL -     a.borrow(());
LL +     A::borrow(&mut a, ());
   |
help: remove the extra argument
   |
LL -     a.borrow(());
LL +     a.borrow();
   |
```

Fix rust-lang#54103.
rust-bors bot pushed a commit that referenced this pull request Mar 27, 2026
Rollup of 7 pull requests

Successful merges:

 - #152457 (Pass -pg to linker when using -Zinstrument-mcount)
 - #154031 (Remove divergence check from check_expr_array)
 - #154418 (move many tests out of `ui/unsafe`)
 - #154441 (bootstrap: force a CI LLVM stamp bump)
 - #153662 (Suggest fully qualified path on method name collision)
 - #153675 (simd_add/sub/mul/neg: document overflow behavior)
 - #154430 (Create GPU target notification group)
@Zalathar
Copy link
Copy Markdown
Member

@bors r-
@bors try jobs=test-various

@rust-bors rust-bors bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Mar 27, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Mar 27, 2026

This pull request was unapproved.

This PR was contained in a rollup (#154448), which was unapproved.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Mar 27, 2026
Suggest fully qualified path on method name collision


try-job: test-various
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Mar 27, 2026

💔 Test for 7709c37 failed: CI. Failed job:

@rust-log-analyzer
Copy link
Copy Markdown
Collaborator

The job test-various failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
Saved the actual stderr to `/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/mismatched_types/diagnostic-method-lookup-returns-sig-with-fewer-args/diagnostic-method-lookup-returns-sig-with-fewer-args.stderr`
diff of stderr:

16    |
17 LL |     target.get(10.0); // (used to crash here)
18    |            ^^^ refers to `Target::get`
-    = note: additionally, there is 1 other available method that isn't in scope
- help: you might have meant to call one of the other methods; you can use the fully-qualified path to call one of them explicitly
21    |
22 LL -     target.get(10.0); // (used to crash here)
23 LL +     <_ as std::slice::SliceIndex<_>>::get(target, 10.0); // (used to crash here)

24    |
25 LL -     target.get(10.0); // (used to crash here)
- LL +     <_ as object::read::elf::relocation::Relr>::get(&target, 10.0); // (used to crash here)
27    |
28 
29 error: aborting due to 1 previous error


The actual stderr differed from the expected stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args mismatched_types/diagnostic-method-lookup-returns-sig-with-fewer-args.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/mismatched_types/diagnostic-method-lookup-returns-sig-with-fewer-args.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=wasm32-wasip1" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/mismatched_types/diagnostic-method-lookup-returns-sig-with-fewer-args" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/wasm32-wasip1/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error[E0308]: mismatched types
##[error]  --> /checkout/tests/ui/mismatched_types/diagnostic-method-lookup-returns-sig-with-fewer-args.rs:4:16
   |
LL |     target.get(10.0); // (used to crash here)
   |            --- ^^^^ expected `i32`, found floating-point number
   |            |
   |            arguments to this method are incorrect
   |
note: method defined here
  --> /checkout/tests/ui/mismatched_types/diagnostic-method-lookup-returns-sig-with-fewer-args.rs:22:12
   |
LL |     pub fn get(&self, data: i32) {
   |            ^^^        ---------
note: the `get` call is resolved to the method in `Target`, shadowing the method of the same name on trait `RandomTrait`
  --> /checkout/tests/ui/mismatched_types/diagnostic-method-lookup-returns-sig-with-fewer-args.rs:4:12
   |
LL |     target.get(10.0); // (used to crash here)
   |            ^^^ refers to `Target::get`
help: you might have meant to call the other method; you can use the fully-qualified path to call it explicitly
   |
LL -     target.get(10.0); // (used to crash here)
LL +     <_ as std::slice::SliceIndex<_>>::get(target, 10.0); // (used to crash here)
   |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

You can never call your own fn borrow(&mut self, ...) method if you use std::borrow::Borrow;

6 participants