Skip to content

Commit 7e4937e

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 6de928d commit 7e4937e

File tree

1 file changed

+60
-42
lines changed

1 file changed

+60
-42
lines changed

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

+60-42
Original file line numberDiff line numberDiff line change
@@ -1660,14 +1660,24 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
16601660
// one crate version and the type comes from another crate version, even though they both
16611661
// are from the same crate.
16621662
let trait_def_id = trait_ref.def_id();
1663-
if let ty::Adt(def, _) = trait_ref.self_ty().skip_binder().peel_refs().kind()
1664-
&& let found_type = def.did()
1665-
&& trait_def_id.krate != found_type.krate
1666-
&& self.tcx.crate_name(trait_def_id.krate) == self.tcx.crate_name(found_type.krate)
1667-
{
1668-
let name = self.tcx.crate_name(trait_def_id.krate);
1669-
let spans: Vec<_> = [trait_def_id, found_type]
1670-
.into_iter()
1663+
let trait_name = self.tcx.item_name(trait_def_id);
1664+
let crate_name = self.tcx.crate_name(trait_def_id.krate);
1665+
if let Some(other_trait_def_id) = self.tcx.all_traits().find(|def_id| {
1666+
trait_name == self.tcx.item_name(trait_def_id)
1667+
&& trait_def_id.krate != def_id.krate
1668+
&& crate_name == self.tcx.crate_name(def_id.krate)
1669+
}) {
1670+
// We've found two different traits with the same name, same crate name, but
1671+
// different crate `DefId`. We highlight the traits.
1672+
1673+
let found_type =
1674+
if let ty::Adt(def, _) = trait_ref.self_ty().skip_binder().peel_refs().kind() {
1675+
Some(def.did())
1676+
} else {
1677+
None
1678+
};
1679+
let spans: Vec<_> = [trait_def_id, other_trait_def_id]
1680+
.iter()
16711681
.filter_map(|def_id| self.tcx.extern_crate(def_id.krate))
16721682
.map(|data| {
16731683
let dependency = if data.dependency_of == LOCAL_CRATE {
@@ -1678,7 +1688,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
16781688
};
16791689
(
16801690
data.span,
1681-
format!("one version of crate `{name}` is used here, as a {dependency}"),
1691+
format!(
1692+
"one version of crate `{crate_name}` is used here, as a {dependency}"
1693+
),
16821694
)
16831695
})
16841696
.collect();
@@ -1692,48 +1704,54 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
16921704
StringPart::normal("there are ".to_string()),
16931705
StringPart::highlighted("multiple different versions".to_string()),
16941706
StringPart::normal(" of crate `".to_string()),
1695-
StringPart::highlighted(format!("{name}")),
1696-
StringPart::normal("` the your dependency graph".to_string()),
1707+
StringPart::highlighted(format!("{crate_name}")),
1708+
StringPart::normal("` in the dependency graph".to_string()),
16971709
],
16981710
);
16991711
let candidates = if impl_candidates.is_empty() {
17001712
alternative_candidates(trait_def_id)
17011713
} else {
17021714
impl_candidates.into_iter().map(|cand| cand.trait_ref).collect()
17031715
};
1704-
if let Some((sp_candidate, sp_found)) = candidates.iter().find_map(|trait_ref| {
1705-
if let ty::Adt(def, _) = trait_ref.self_ty().peel_refs().kind()
1706-
&& let candidate_def_id = def.did()
1707-
&& let Some(name) = self.tcx.opt_item_name(candidate_def_id)
1708-
&& let Some(found) = self.tcx.opt_item_name(found_type)
1709-
&& name == found
1710-
&& candidate_def_id.krate != found_type.krate
1711-
&& self.tcx.crate_name(candidate_def_id.krate)
1712-
== self.tcx.crate_name(found_type.krate)
1713-
{
1714-
// A candidate was found of an item with the same name, from two separate
1715-
// versions of the same crate, let's clarify.
1716-
Some((self.tcx.def_span(candidate_def_id), self.tcx.def_span(found_type)))
1717-
} else {
1718-
None
1719-
}
1720-
}) {
1721-
let mut span: MultiSpan = vec![sp_candidate, sp_found].into();
1722-
span.push_span_label(self.tcx.def_span(trait_def_id), "this is the required trait");
1723-
span.push_span_label(sp_candidate, "this type implements the required trait");
1724-
span.push_span_label(sp_found, "this type doesn't implement the required trait");
1725-
err.highlighted_span_note(
1726-
span,
1727-
vec![
1728-
StringPart::normal(
1729-
"two types coming from two different versions of the same crate are \
1730-
different types "
1731-
.to_string(),
1732-
),
1733-
StringPart::highlighted("even if they look the same".to_string()),
1734-
],
1716+
let mut span: MultiSpan = self.tcx.def_span(trait_def_id).into();
1717+
span.push_span_label(self.tcx.def_span(trait_def_id), "this is the required trait");
1718+
if let Some(found_type) = found_type {
1719+
span.push_span_label(
1720+
self.tcx.def_span(found_type),
1721+
"this type doesn't implement the required trait",
17351722
);
1723+
for trait_ref in candidates {
1724+
if let ty::Adt(def, _) = trait_ref.self_ty().peel_refs().kind()
1725+
&& let candidate_def_id = def.did()
1726+
&& let Some(name) = self.tcx.opt_item_name(candidate_def_id)
1727+
&& let Some(found) = self.tcx.opt_item_name(found_type)
1728+
&& name == found
1729+
&& candidate_def_id.krate != found_type.krate
1730+
&& self.tcx.crate_name(candidate_def_id.krate)
1731+
== self.tcx.crate_name(found_type.krate)
1732+
{
1733+
// A candidate was found of an item with the same name, from two separate
1734+
// versions of the same crate, let's clarify.
1735+
let candidate_span = self.tcx.def_span(candidate_def_id);
1736+
span.push_span_label(
1737+
candidate_span,
1738+
"this type implements the required trait",
1739+
);
1740+
}
1741+
}
17361742
}
1743+
span.push_span_label(self.tcx.def_span(other_trait_def_id), "this is the found trait");
1744+
err.highlighted_span_note(
1745+
span,
1746+
vec![
1747+
StringPart::normal(
1748+
"two types coming from two different versions of the same crate are \
1749+
different types "
1750+
.to_string(),
1751+
),
1752+
StringPart::highlighted("even if they look the same".to_string()),
1753+
],
1754+
);
17371755
err.help("you can use `cargo tree` to explore your dependency tree");
17381756
return true;
17391757
}

0 commit comments

Comments
 (0)