Skip to content

Commit 7c1c566

Browse files
committed
early return in trait detection for non-trait item
1 parent 244bbfc commit 7c1c566

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3494,7 +3494,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
34943494
continue;
34953495
}
34963496
trait_in_other_version_found = self
3497-
.detect_and_explain_multiple_crate_versions(
3497+
.detect_and_explain_multiple_crate_versions_of_trait_item(
34983498
err,
34993499
pick.item.def_id,
35003500
rcvr.hir_id,
@@ -3701,12 +3701,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
37013701
// same crate.
37023702

37033703
let rcvr_ty = self.node_ty_opt(ty.hir_id);
3704-
trait_in_other_version_found = self.detect_and_explain_multiple_crate_versions(
3705-
err,
3706-
assoc.def_id,
3707-
ty.hir_id,
3708-
rcvr_ty,
3709-
);
3704+
trait_in_other_version_found = self
3705+
.detect_and_explain_multiple_crate_versions_of_trait_item(
3706+
err,
3707+
assoc.def_id,
3708+
ty.hir_id,
3709+
rcvr_ty,
3710+
);
37103711
}
37113712
if !trait_in_other_version_found
37123713
&& self.suggest_valid_traits(err, item_name, valid_out_of_scope_traits, true)
@@ -4098,7 +4099,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
40984099
}
40994100
}
41004101

4101-
fn detect_and_explain_multiple_crate_versions(
4102+
fn detect_and_explain_multiple_crate_versions_of_trait_item(
41024103
&self,
41034104
err: &mut Diag<'_>,
41044105
item_def_id: DefId,
@@ -4111,6 +4112,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
41114112
return false;
41124113
}
41134114
let trait_def_id = self.tcx.parent(item_def_id);
4115+
if !self.tcx.is_trait(trait_def_id) {
4116+
return false;
4117+
}
41144118
let krate = self.tcx.crate_name(trait_def_id.krate);
41154119
let name = self.tcx.item_name(trait_def_id);
41164120
let candidates: Vec<_> = traits

tests/crashes/135863.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// issue#135863
2+
3+
struct A;
4+
5+
impl A {
6+
fn len(self: &&A) {}
7+
}
8+
9+
fn main() {
10+
A.len();
11+
//~^ ERROR: no method named `len` found for struct `A` in the current scope
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0599]: no method named `len` found for struct `A` in the current scope
2+
--> $DIR/double-reference-ty-in-self-ty.rs:10:7
3+
|
4+
LL | struct A;
5+
| -------- method `len` not found for this struct
6+
...
7+
LL | fn len(self: &&A) {}
8+
| --- the method is available for `&A` here
9+
...
10+
LL | A.len();
11+
| ^^^ method not found in `A`
12+
|
13+
= help: items from traits can only be used if the trait is implemented and in scope
14+
= note: the following trait defines an item `len`, perhaps you need to implement it:
15+
candidate #1: `ExactSizeIterator`
16+
17+
error: aborting due to 1 previous error
18+
19+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)