Rollup merge of #129250 - estebank:issue-129205, r=compiler-errors

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 #128786.
Fixes #129205
Fixes #129216
This commit is contained in:
Matthias Krüger 2024-08-27 00:42:00 +02:00 committed by GitHub
commit d4d4b6b4fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 23 deletions

View File

@ -3498,7 +3498,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err, err,
pick.item.def_id, pick.item.def_id,
rcvr.hir_id, rcvr.hir_id,
*rcvr_ty, Some(*rcvr_ty),
); );
if pick.autoderefs == 0 && !trait_in_other_version_found { if pick.autoderefs == 0 && !trait_in_other_version_found {
err.span_label( err.span_label(
@ -3689,8 +3689,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let SelfSource::QPath(ty) = source if let SelfSource::QPath(ty) = source
&& !valid_out_of_scope_traits.is_empty() && !valid_out_of_scope_traits.is_empty()
&& let hir::TyKind::Path(path) = ty.kind && let hir::TyKind::Path(path) = ty.kind
&& let hir::QPath::Resolved(_, path) = path && let hir::QPath::Resolved(..) = path
&& let Some(def_id) = path.res.opt_def_id()
&& let Some(assoc) = self && let Some(assoc) = self
.tcx .tcx
.associated_items(valid_out_of_scope_traits[0]) .associated_items(valid_out_of_scope_traits[0])
@ -3700,7 +3699,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// See if the `Type::function(val)` where `function` wasn't found corresponds to a // See if the `Type::function(val)` where `function` wasn't found corresponds to a
// `Trait` that is imported directly, but `Type` came from a different version of the // `Trait` that is imported directly, but `Type` came from a different version of the
// same crate. // same crate.
let rcvr_ty = self.tcx.type_of(def_id).instantiate_identity();
let rcvr_ty = self.node_ty_opt(ty.hir_id);
trait_in_other_version_found = self.detect_and_explain_multiple_crate_versions( trait_in_other_version_found = self.detect_and_explain_multiple_crate_versions(
err, err,
assoc.def_id, assoc.def_id,
@ -4080,7 +4080,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err: &mut Diag<'_>, err: &mut Diag<'_>,
item_def_id: DefId, item_def_id: DefId,
hir_id: hir::HirId, hir_id: hir::HirId,
rcvr_ty: Ty<'_>, rcvr_ty: Option<Ty<'_>>,
) -> bool { ) -> bool {
let hir_id = self.tcx.parent_hir_id(hir_id); let hir_id = self.tcx.parent_hir_id(hir_id);
let Some(traits) = self.tcx.in_scope_traits(hir_id) else { return false }; let Some(traits) = self.tcx.in_scope_traits(hir_id) else { return false };
@ -4110,8 +4110,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut multi_span: MultiSpan = trait_span.into(); let mut multi_span: MultiSpan = trait_span.into();
multi_span.push_span_label(trait_span, format!("this is the trait that is needed")); multi_span.push_span_label(trait_span, format!("this is the trait that is needed"));
let descr = self.tcx.associated_item(item_def_id).descr(); let descr = self.tcx.associated_item(item_def_id).descr();
let rcvr_ty =
rcvr_ty.map(|t| format!("`{t}`")).unwrap_or_else(|| "the receiver".to_string());
multi_span multi_span
.push_span_label(item_span, format!("the {descr} is available for `{rcvr_ty}` here")); .push_span_label(item_span, format!("the {descr} is available for {rcvr_ty} here"));
for (def_id, import_def_id) in candidates { for (def_id, import_def_id) in candidates {
if let Some(import_def_id) = import_def_id { if let Some(import_def_id) = import_def_id {
multi_span.push_span_label( multi_span.push_span_label(

View File

@ -1,5 +0,0 @@
//@ known-bug: rust-lang/rust#129205
fn x<T: Copy>() {
T::try_from();
}

View File

@ -1,12 +0,0 @@
//@ known-bug: rust-lang/rust#129216
//@ only-linux
trait Mirror {
type Assoc;
}
struct Foo;
fn main() {
<Foo as Mirror>::Assoc::new();
}

View File

@ -0,0 +1,6 @@
// Regression test for https://github.com/rust-lang/rust/issues/129205
fn x<T: Copy>() {
T::try_from(); //~ ERROR E0599
}
fn main() {}

View File

@ -0,0 +1,19 @@
error[E0599]: no function or associated item named `try_from` found for type parameter `T` in the current scope
--> $DIR/missing-method-on-type-parameter.rs:3:8
|
LL | fn x<T: Copy>() {
| - function or associated item `try_from` not found for this type parameter
LL | T::try_from();
| ^^^^^^^^ function or associated item not found in `T`
|
= help: items from traits can only be used if the trait is in scope
help: there is an associated function `from` with a similar name
--> $SRC_DIR/core/src/convert/mod.rs:LL:COL
help: trait `TryFrom` which provides `try_from` is implemented but not in scope; perhaps you want to import it
|
LL + use std::convert::TryFrom;
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0599`.