Skip to content

Commit 70e2b5b

Browse files
committed
Detect multiple crate versions on method not found
When a type comes indirectly from one crate version but the imported trait comes from a separate crate version, the called method won't be found. We now show additional context: ``` error[E0599]: no method named `foo` found for struct `dep_2_reexport::Type` in the current scope --> multiple-dep-versions.rs:8:10 | 8 | Type.foo(); | ^^^ method not found in `Type` | note: you have multiple different versions of crate `dependency` in your dependency graph --> multiple-dep-versions.rs:4:32 | 4 | use dependency::{do_something, Trait}; | ^^^^^ `dependency` imported here doesn't correspond to the right crate version | ::: ~/rust/build/x86_64-unknown-linux-gnu/test/run-make/crate-loading/rmake_out/multiple-dep-versions-1.rs:4:1 | 4 | pub trait Trait { | --------------- this is the trait that was imported | ::: ~/rust/build/x86_64-unknown-linux-gnu/test/run-make/crate-loading/rmake_out/multiple-dep-versions-2.rs:4:1 | 4 | pub trait Trait { | --------------- this is the trait that is needed 5 | fn foo(&self); | --- the method is available for `dep_2_reexport::Type` here ```
1 parent 034b73b commit 70e2b5b

File tree

6 files changed

+99
-14
lines changed

6 files changed

+99
-14
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+65-4
Original file line numberDiff line numberDiff line change
@@ -3449,6 +3449,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
34493449
trait_missing_method: bool,
34503450
) {
34513451
let mut alt_rcvr_sugg = false;
3452+
let mut suggest = true;
34523453
if let (SelfSource::MethodCall(rcvr), false) = (source, unsatisfied_bounds) {
34533454
debug!(
34543455
"suggest_traits_to_import: span={:?}, item_name={:?}, rcvr_ty={:?}, rcvr={:?}",
@@ -3492,10 +3493,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
34923493
let did = Some(pick.item.container_id(self.tcx));
34933494
let skip = skippable.contains(&did);
34943495
if pick.autoderefs == 0 && !skip {
3495-
err.span_label(
3496-
pick.item.ident(self.tcx).span,
3497-
format!("the method is available for `{rcvr_ty}` here"),
3496+
suggest = self.detect_and_explain_multiple_crate_versions(
3497+
err,
3498+
&pick.item,
3499+
rcvr.hir_id.owner,
3500+
*rcvr_ty,
34983501
);
3502+
if suggest {
3503+
err.span_label(
3504+
pick.item.ident(self.tcx).span,
3505+
format!("the method is available for `{rcvr_ty}` here"),
3506+
);
3507+
}
34993508
}
35003509
break;
35013510
}
@@ -3676,7 +3685,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
36763685
}
36773686
}
36783687
}
3679-
if self.suggest_valid_traits(err, item_name, valid_out_of_scope_traits, true) {
3688+
if suggest && self.suggest_valid_traits(err, item_name, valid_out_of_scope_traits, true) {
36803689
return;
36813690
}
36823691

@@ -4041,6 +4050,58 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
40414050
}
40424051
}
40434052

4053+
fn detect_and_explain_multiple_crate_versions(
4054+
&self,
4055+
err: &mut Diag<'_>,
4056+
item: &ty::AssocItem,
4057+
owner: hir::OwnerId,
4058+
rcvr_ty: Ty<'_>,
4059+
) -> bool {
4060+
let pick_name = self.tcx.crate_name(item.def_id.krate);
4061+
if let Some(map) = self.tcx.in_scope_traits_map(owner) {
4062+
for trait_candidate in map.to_sorted_stable_ord().into_iter().flat_map(|v| v.1.iter()) {
4063+
let name = self.tcx.crate_name(trait_candidate.def_id.krate);
4064+
if trait_candidate.def_id.krate != item.def_id.krate && name == pick_name {
4065+
let msg = format!(
4066+
"you have multiple different versions of crate `{name}` in your \
4067+
dependency graph",
4068+
);
4069+
let tdid = self.tcx.parent(item.def_id);
4070+
if self.tcx.item_name(trait_candidate.def_id) == self.tcx.item_name(tdid)
4071+
&& let Some(def_id) = trait_candidate.import_ids.get(0)
4072+
{
4073+
let span = self.tcx.def_span(*def_id);
4074+
let mut multi_span: MultiSpan = span.into();
4075+
multi_span.push_span_label(
4076+
span,
4077+
format!(
4078+
"`{name}` imported here doesn't correspond to the right crate \
4079+
version",
4080+
),
4081+
);
4082+
multi_span.push_span_label(
4083+
self.tcx.def_span(trait_candidate.def_id),
4084+
format!("this is the trait that was imported"),
4085+
);
4086+
multi_span.push_span_label(
4087+
self.tcx.def_span(tdid),
4088+
format!("this is the trait that is needed"),
4089+
);
4090+
multi_span.push_span_label(
4091+
item.ident(self.tcx).span,
4092+
format!("the method is available for `{rcvr_ty}` here"),
4093+
);
4094+
err.span_note(multi_span, msg);
4095+
return false;
4096+
} else {
4097+
err.note(msg);
4098+
}
4099+
}
4100+
}
4101+
}
4102+
true
4103+
}
4104+
40444105
/// issue #102320, for `unwrap_or` with closure as argument, suggest `unwrap_or_else`
40454106
/// FIXME: currently not working for suggesting `map_or_else`, see #102408
40464107
pub(crate) fn suggest_else_fn_with_closure(
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#![crate_name = "dependency"]
22
#![crate_type = "rlib"]
3-
pub struct Type;
4-
pub trait Trait {}
5-
impl Trait for Type {}
3+
pub struct Type(pub i32);
4+
pub trait Trait {
5+
fn foo(&self);
6+
}
7+
impl Trait for Type {
8+
fn foo(&self) {}
9+
}
610
pub fn do_something<X: Trait>(_: X) {}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#![crate_name = "dependency"]
22
#![crate_type = "rlib"]
3-
pub struct Type(pub i32);
4-
pub trait Trait {}
5-
impl Trait for Type {}
3+
pub struct Type;
4+
pub trait Trait {
5+
fn foo(&self);
6+
}
7+
impl Trait for Type {
8+
fn foo(&self) {}
9+
}
610
pub fn do_something<X: Trait>(_: X) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![crate_name = "foo"]
2+
#![crate_type = "rlib"]
3+
4+
extern crate dependency;
5+
pub use dependency::Type;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
extern crate dep_2_reexport;
22
extern crate dependency;
3-
use dep_2_reexport::do_something;
4-
use dependency::Type;
3+
use dep_2_reexport::Type;
4+
use dependency::{do_something, Trait};
55

66
fn main() {
77
do_something(Type);
8+
Type.foo();
89
}

tests/run-make/crate-loading/rmake.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ use run_make_support::{assert_contains, rust_lib_name, rustc};
88
fn main() {
99
rustc().input("multiple-dep-versions-1.rs").run();
1010
rustc().input("multiple-dep-versions-2.rs").extra_filename("2").metadata("2").run();
11+
rustc()
12+
.input("multiple-dep-versions-3.rs")
13+
.extern_("dependency", rust_lib_name("dependency2"))
14+
.run();
1115

1216
let out = rustc()
1317
.input("multiple-dep-versions.rs")
1418
.extern_("dependency", rust_lib_name("dependency"))
15-
.extern_("dep_2_reexport", rust_lib_name("dependency2"))
19+
.extern_("dep_2_reexport", rust_lib_name("foo"))
1620
.run_fail()
1721
.assert_stderr_contains(
1822
"you have multiple different versions of crate `dependency` in your dependency graph",
@@ -23,5 +27,11 @@ fn main() {
2327
)
2428
.assert_stderr_contains("this type doesn't implement the required trait")
2529
.assert_stderr_contains("this type implements the required trait")
26-
.assert_stderr_contains("this is the required trait");
30+
.assert_stderr_contains("this is the required trait")
31+
.assert_stderr_contains(
32+
"`dependency` imported here doesn't correspond to the right crate version",
33+
)
34+
.assert_stderr_contains("this is the trait that was imported")
35+
.assert_stderr_contains("this is the trait that is needed")
36+
.assert_stderr_contains("the method is available for `dep_2_reexport::Type` here");
2737
}

0 commit comments

Comments
 (0)