Skip to content

early return in trait detection for non-trait item #142191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3494,7 +3494,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
continue;
}
trait_in_other_version_found = self
.detect_and_explain_multiple_crate_versions(
.detect_and_explain_multiple_crate_versions_of_trait_item(
err,
pick.item.def_id,
rcvr.hir_id,
Expand Down Expand Up @@ -3701,12 +3701,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// same crate.

let rcvr_ty = self.node_ty_opt(ty.hir_id);
trait_in_other_version_found = self.detect_and_explain_multiple_crate_versions(
err,
assoc.def_id,
ty.hir_id,
rcvr_ty,
);
trait_in_other_version_found = self
.detect_and_explain_multiple_crate_versions_of_trait_item(
err,
assoc.def_id,
ty.hir_id,
rcvr_ty,
);
}
if !trait_in_other_version_found
&& self.suggest_valid_traits(err, item_name, valid_out_of_scope_traits, true)
Expand Down Expand Up @@ -4098,7 +4099,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

fn detect_and_explain_multiple_crate_versions(
fn detect_and_explain_multiple_crate_versions_of_trait_item(
&self,
err: &mut Diag<'_>,
item_def_id: DefId,
Expand All @@ -4111,6 +4112,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return false;
}
let trait_def_id = self.tcx.parent(item_def_id);
if !self.tcx.is_trait(trait_def_id) {
return false;
}
let krate = self.tcx.crate_name(trait_def_id.krate);
let name = self.tcx.item_name(trait_def_id);
let candidates: Vec<_> = traits
Expand Down
10 changes: 0 additions & 10 deletions tests/crashes/135863.rs

This file was deleted.

12 changes: 12 additions & 0 deletions tests/ui/suggestions/double-reference-ty-in-self-ty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// issue#135863

struct A;

impl A {
fn len(self: &&A) {}
}

fn main() {
A.len();
//~^ ERROR: no method named `len` found for struct `A` in the current scope
}
19 changes: 19 additions & 0 deletions tests/ui/suggestions/double-reference-ty-in-self-ty.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0599]: no method named `len` found for struct `A` in the current scope
--> $DIR/double-reference-ty-in-self-ty.rs:10:7
|
LL | struct A;
| -------- method `len` not found for this struct
...
LL | fn len(self: &&A) {}
| --- the method is available for `&A` here
...
LL | A.len();
| ^^^ method not found in `A`
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `len`, perhaps you need to implement it:
candidate #1: `ExactSizeIterator`

error: aborting due to 1 previous error

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