Skip to content

Commit 35bde07

Browse files
committed
Tweak detection of multiple crate versions to be more ecompassing
Previously, we only emitted the additional context if the type was in the same crate as the trait that appeared multiple times in the dependency tree. Now, we look at all traits looking for two with the same name in different crates with the same crate number, and we are more flexible looking for the types involved. This will work even if the type that implements the wrong trait version is from a different crate entirely. ``` error[E0277]: the trait bound `CustomErrorHandler: ErrorHandler` is not satisfied --> src/main.rs:5:17 | 5 | cnb_runtime(CustomErrorHandler {}); | ----------- ^^^^^^^^^^^^^^^^^^^^^ the trait `ErrorHandler` is not implemented for `CustomErrorHandler` | | | required by a bound introduced by this call | help: you have multiple different versions of crate `c` in your dependency graph --> src/main.rs:1:5 | 1 | use b::CustomErrorHandler; | ^ one version of crate `c` is used here, as a dependency of crate `b` 2 | use c::cnb_runtime; | ^ one version of crate `c` is used here, as a direct dependency of the current crate note: two types coming from two different versions of the same crate are different types even if they look the same --> /home/gh-estebank/testcase-rustc-crate-version-mismatch/c-v0.2/src/lib.rs:1:1 | 1 | pub trait ErrorHandler {} | ^^^^^^^^^^^^^^^^^^^^^^ this is the required trait | ::: /home/gh-estebank/testcase-rustc-crate-version-mismatch/b/src/lib.rs:1:1 | 1 | pub struct CustomErrorHandler {} | ----------------------------- this type doesn't implement the required trait | ::: /home/gh-estebank/testcase-rustc-crate-version-mismatch/c-v0.1/src/lib.rs:1:1 | 1 | pub trait ErrorHandler {} | ---------------------- this is the found trait = help: you can use `cargo tree` to explore your dependency tree note: required by a bound in `cnb_runtime` --> /home/gh-estebank/testcase-rustc-crate-version-mismatch/c-v0.2/src/lib.rs:3:41 | 3 | pub fn cnb_runtime(_error_handler: impl ErrorHandler) {} | ^^^^^^^^^^^^ required by this bound in `cnb_runtime` ``` Fix #89143.
1 parent e8c698b commit 35bde07

File tree

4 files changed

+74
-44
lines changed

4 files changed

+74
-44
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

+56-39
Original file line numberDiff line numberDiff line change
@@ -1707,15 +1707,24 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
17071707
// one crate version and the type comes from another crate version, even though they both
17081708
// are from the same crate.
17091709
let trait_def_id = trait_ref.def_id();
1710-
if let ty::Adt(def, _) = trait_ref.self_ty().skip_binder().peel_refs().kind()
1711-
&& let found_type = def.did()
1712-
&& trait_def_id.krate != found_type.krate
1713-
&& self.tcx.crate_name(trait_def_id.krate) == self.tcx.crate_name(found_type.krate)
1714-
{
1715-
let name = self.tcx.crate_name(trait_def_id.krate);
1716-
let spans: Vec<_> = [trait_def_id, found_type]
1717-
.into_iter()
1718-
.filter(|def_id| def_id.krate != LOCAL_CRATE)
1710+
let trait_name = self.tcx.item_name(trait_def_id);
1711+
let crate_name = self.tcx.crate_name(trait_def_id.krate);
1712+
if let Some(other_trait_def_id) = self.tcx.all_traits().find(|def_id| {
1713+
trait_name == self.tcx.item_name(trait_def_id)
1714+
&& trait_def_id.krate != def_id.krate
1715+
&& crate_name == self.tcx.crate_name(def_id.krate)
1716+
}) {
1717+
// We've found two different traits with the same name, same crate name, but
1718+
// different crate `DefId`. We highlight the traits.
1719+
1720+
let found_type =
1721+
if let ty::Adt(def, _) = trait_ref.self_ty().skip_binder().peel_refs().kind() {
1722+
Some(def.did())
1723+
} else {
1724+
None
1725+
};
1726+
let spans: Vec<_> = [trait_def_id, other_trait_def_id]
1727+
.iter()
17191728
.filter_map(|def_id| self.tcx.extern_crate(def_id.krate))
17201729
.map(|data| {
17211730
let dependency = if data.dependency_of == LOCAL_CRATE {
@@ -1726,7 +1735,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
17261735
};
17271736
(
17281737
data.span,
1729-
format!("one version of crate `{name}` is used here, as a {dependency}"),
1738+
format!(
1739+
"one version of crate `{crate_name}` is used here, as a {dependency}"
1740+
),
17301741
)
17311742
})
17321743
.collect();
@@ -1738,44 +1749,50 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
17381749
StringPart::normal("there are ".to_string()),
17391750
StringPart::highlighted("multiple different versions".to_string()),
17401751
StringPart::normal(" of crate `".to_string()),
1741-
StringPart::highlighted(format!("{name}")),
1752+
StringPart::highlighted(format!("{crate_name}")),
17421753
StringPart::normal("` in the dependency graph".to_string()),
17431754
]);
17441755
let candidates = if impl_candidates.is_empty() {
17451756
alternative_candidates(trait_def_id)
17461757
} else {
17471758
impl_candidates.into_iter().map(|cand| cand.trait_ref).collect()
17481759
};
1749-
if let Some((sp_candidate, sp_found)) = candidates.iter().find_map(|trait_ref| {
1750-
if let ty::Adt(def, _) = trait_ref.self_ty().peel_refs().kind()
1751-
&& let candidate_def_id = def.did()
1752-
&& let Some(name) = self.tcx.opt_item_name(candidate_def_id)
1753-
&& let Some(found) = self.tcx.opt_item_name(found_type)
1754-
&& name == found
1755-
&& candidate_def_id.krate != found_type.krate
1756-
&& self.tcx.crate_name(candidate_def_id.krate)
1757-
== self.tcx.crate_name(found_type.krate)
1758-
{
1759-
// A candidate was found of an item with the same name, from two separate
1760-
// versions of the same crate, let's clarify.
1761-
Some((self.tcx.def_span(candidate_def_id), self.tcx.def_span(found_type)))
1762-
} else {
1763-
None
1760+
let mut span: MultiSpan = self.tcx.def_span(trait_def_id).into();
1761+
span.push_span_label(self.tcx.def_span(trait_def_id), "this is the required trait");
1762+
if let Some(found_type) = found_type {
1763+
span.push_span_label(
1764+
self.tcx.def_span(found_type),
1765+
"this type doesn't implement the required trait",
1766+
);
1767+
for trait_ref in candidates {
1768+
if let ty::Adt(def, _) = trait_ref.self_ty().peel_refs().kind()
1769+
&& let candidate_def_id = def.did()
1770+
&& let Some(name) = self.tcx.opt_item_name(candidate_def_id)
1771+
&& let Some(found) = self.tcx.opt_item_name(found_type)
1772+
&& name == found
1773+
&& candidate_def_id.krate != found_type.krate
1774+
&& self.tcx.crate_name(candidate_def_id.krate)
1775+
== self.tcx.crate_name(found_type.krate)
1776+
{
1777+
// A candidate was found of an item with the same name, from two separate
1778+
// versions of the same crate, let's clarify.
1779+
let candidate_span = self.tcx.def_span(candidate_def_id);
1780+
span.push_span_label(
1781+
candidate_span,
1782+
"this type implements the required trait",
1783+
);
1784+
}
17641785
}
1765-
}) {
1766-
let mut span: MultiSpan = vec![sp_candidate, sp_found].into();
1767-
span.push_span_label(self.tcx.def_span(trait_def_id), "this is the required trait");
1768-
span.push_span_label(sp_candidate, "this type implements the required trait");
1769-
span.push_span_label(sp_found, "this type doesn't implement the required trait");
1770-
err.highlighted_span_note(span, vec![
1771-
StringPart::normal(
1772-
"two types coming from two different versions of the same crate are \
1773-
different types "
1774-
.to_string(),
1775-
),
1776-
StringPart::highlighted("even if they look the same".to_string()),
1777-
]);
17781786
}
1787+
span.push_span_label(self.tcx.def_span(other_trait_def_id), "this is the found trait");
1788+
err.highlighted_span_note(span, vec![
1789+
StringPart::normal(
1790+
"two types coming from two different versions of the same crate are \
1791+
different types "
1792+
.to_string(),
1793+
),
1794+
StringPart::highlighted("even if they look the same".to_string()),
1795+
]);
17791796
err.help("you can use `cargo tree` to explore your dependency tree");
17801797
return true;
17811798
}

tests/run-make/crate-loading/multiple-dep-versions-3.rs

+5
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@
33

44
extern crate dependency;
55
pub use dependency::Type;
6+
pub struct OtherType;
7+
impl dependency::Trait for OtherType {
8+
fn foo(&self) {}
9+
fn bar() {}
10+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
extern crate dep_2_reexport;
22
extern crate dependency;
3-
use dep_2_reexport::Type;
3+
use dep_2_reexport::{OtherType, Type};
44
use dependency::{Trait, do_something};
55

66
fn main() {
77
do_something(Type);
88
Type.foo();
99
Type::bar();
10+
do_something(OtherType);
1011
}

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,17 @@ help: there are multiple different versions of crate `dependency` in the depende
3838
.assert_stderr_contains(
3939
r#"
4040
3 | pub struct Type(pub i32);
41-
| ^^^^^^^^^^^^^^^ this type implements the required trait
41+
| --------------- this type implements the required trait
4242
4 | pub trait Trait {
43-
| --------------- this is the required trait"#,
43+
| ^^^^^^^^^^^^^^^ this is the required trait"#,
4444
)
4545
.assert_stderr_contains(
4646
r#"
4747
3 | pub struct Type;
48-
| ^^^^^^^^^^^^^^^ this type doesn't implement the required trait"#,
48+
| --------------- this type doesn't implement the required trait
49+
4 | pub trait Trait {
50+
| --------------- this is the found trait
51+
= help: you can use `cargo tree` to explore your dependency tree"#,
4952
)
5053
.assert_stderr_contains(
5154
r#"
@@ -96,5 +99,9 @@ note: there are multiple different versions of crate `dependency` in the depende
9699
|
97100
4 | use dependency::{Trait, do_something};
98101
| ----- `Trait` imported here doesn't correspond to the right version of crate `dependency`"#,
99-
);
102+
)
103+
.assert_stderr_contains(
104+
r#"
105+
6 | pub struct OtherType;
106+
| -------------------- this type doesn't implement the required trait"#);
100107
}

0 commit comments

Comments
 (0)