Skip to content

Commit c78e7fa

Browse files
committed
Do not ICE on non-ADT rcvr type when looking for crate version collision
When looking for multiple versions of the same crate, do not blindly construct the receiver type. Follow up to rust-lang#128786. Fix rust-lang#129205.
1 parent 4d5b3b1 commit c78e7fa

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -3498,7 +3498,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
34983498
err,
34993499
pick.item.def_id,
35003500
rcvr.hir_id,
3501-
*rcvr_ty,
3501+
Some(*rcvr_ty),
35023502
);
35033503
if pick.autoderefs == 0 && !trait_in_other_version_found {
35043504
err.span_label(
@@ -3689,8 +3689,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
36893689
if let SelfSource::QPath(ty) = source
36903690
&& !valid_out_of_scope_traits.is_empty()
36913691
&& let hir::TyKind::Path(path) = ty.kind
3692-
&& let hir::QPath::Resolved(_, path) = path
3693-
&& let Some(def_id) = path.res.opt_def_id()
3692+
&& let hir::QPath::Resolved(..) = path
36943693
&& let Some(assoc) = self
36953694
.tcx
36963695
.associated_items(valid_out_of_scope_traits[0])
@@ -3700,7 +3699,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
37003699
// See if the `Type::function(val)` where `function` wasn't found corresponds to a
37013700
// `Trait` that is imported directly, but `Type` came from a different version of the
37023701
// same crate.
3703-
let rcvr_ty = self.tcx.type_of(def_id).instantiate_identity();
3702+
3703+
let rcvr_ty = self.node_ty_opt(ty.hir_id);
37043704
trait_in_other_version_found = self.detect_and_explain_multiple_crate_versions(
37053705
err,
37063706
assoc.def_id,
@@ -4080,7 +4080,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
40804080
err: &mut Diag<'_>,
40814081
item_def_id: DefId,
40824082
hir_id: hir::HirId,
4083-
rcvr_ty: Ty<'_>,
4083+
rcvr_ty: Option<Ty<'_>>,
40844084
) -> bool {
40854085
let hir_id = self.tcx.parent_hir_id(hir_id);
40864086
let Some(traits) = self.tcx.in_scope_traits(hir_id) else { return false };
@@ -4110,8 +4110,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
41104110
let mut multi_span: MultiSpan = trait_span.into();
41114111
multi_span.push_span_label(trait_span, format!("this is the trait that is needed"));
41124112
let descr = self.tcx.associated_item(item_def_id).descr();
4113+
let rcvr_ty =
4114+
rcvr_ty.map(|t| format!("`{t}`")).unwrap_or_else(|| "the receiver".to_string());
41134115
multi_span
4114-
.push_span_label(item_span, format!("the {descr} is available for `{rcvr_ty}` here"));
4116+
.push_span_label(item_span, format!("the {descr} is available for {rcvr_ty} here"));
41154117
for (def_id, import_def_id) in candidates {
41164118
if let Some(import_def_id) = import_def_id {
41174119
multi_span.push_span_label(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Regression test for https://github.com/rust-lang/rust/issues/129205
2+
fn x<T: Copy>() {
3+
T::try_from(); //~ ERROR E0599
4+
}
5+
6+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0599]: no function or associated item named `try_from` found for type parameter `T` in the current scope
2+
--> $DIR/missing-method-on-type-parameter.rs:3:8
3+
|
4+
LL | fn x<T: Copy>() {
5+
| - function or associated item `try_from` not found for this type parameter
6+
LL | T::try_from();
7+
| ^^^^^^^^ function or associated item not found in `T`
8+
|
9+
= help: items from traits can only be used if the trait is in scope
10+
help: there is an associated function `from` with a similar name
11+
--> $SRC_DIR/core/src/convert/mod.rs:LL:COL
12+
help: trait `TryFrom` which provides `try_from` is implemented but not in scope; perhaps you want to import it
13+
|
14+
LL + use std::convert::TryFrom;
15+
|
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)