Skip to content

Commit 3096887

Browse files
committed
On trait bound mismatch, detect multiple crate versions in dep tree
When encountering an E0277, if the type and the trait both come from a crate with the same name but different crate number, we explain that there are multiple crate versions in the dependency tree. If there's a type that fulfills the bound, and it has the same name as the passed in type and has the same crate name, we explain that the same type in two different versions of the same crate *are different*. ``` error[E0277]: the trait bound `Type: dependency::Trait` is not satisfied --> src/main.rs:4:18 | 4 | do_something(Type); | ------------ ^^^^ the trait `dependency::Trait` is not implemented for `Type` | | | required by a bound introduced by this call | help: you have multiple different versions of crate `dependency` in your dependency graph --> src/main.rs:1:5 | 1 | use bar::do_something; | ^^^ one version of crate `dependency` is used here, as a dependency of crate `bar` 2 | use dependency::Type; | ^^^^^^^^^^ one version of crate `dependency` 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/crate_versions/baz-2/src/lib.rs:1:1 | 1 | pub struct Type; | ^^^^^^^^^^^^^^^ this type doesn't implement the required trait | ::: /home/gh-estebank/crate_versions/baz/src/lib.rs:1:1 | 1 | pub struct Type; | ^^^^^^^^^^^^^^^ this type implements the required trait 2 | pub trait Trait {} | --------------- this is the required trait note: required by a bound in `bar::do_something` --> /home/gh-estebank/crate_versions/baz/src/lib.rs:4:24 | 4 | pub fn do_something<X: Trait>(_: X) {} | ^^^^^ required by this bound in `do_something` ``` Address #22750.
1 parent a6888b4 commit 3096887

File tree

1 file changed

+121
-33
lines changed

1 file changed

+121
-33
lines changed

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

+121-33
Original file line numberDiff line numberDiff line change
@@ -1624,9 +1624,127 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
16241624
other: bool,
16251625
param_env: ty::ParamEnv<'tcx>,
16261626
) -> bool {
1627-
// If we have a single implementation, try to unify it with the trait ref
1628-
// that failed. This should uncover a better hint for what *is* implemented.
1627+
let alternative_candidates = |def_id: DefId| {
1628+
let mut impl_candidates: Vec<_> = self
1629+
.tcx
1630+
.all_impls(def_id)
1631+
// Ignore automatically derived impls and `!Trait` impls.
1632+
.filter_map(|def_id| self.tcx.impl_trait_header(def_id))
1633+
.filter_map(|header| {
1634+
(header.polarity != ty::ImplPolarity::Negative
1635+
|| self.tcx.is_automatically_derived(def_id))
1636+
.then(|| header.trait_ref.instantiate_identity())
1637+
})
1638+
.filter(|trait_ref| {
1639+
let self_ty = trait_ref.self_ty();
1640+
// Avoid mentioning type parameters.
1641+
if let ty::Param(_) = self_ty.kind() {
1642+
false
1643+
}
1644+
// Avoid mentioning types that are private to another crate
1645+
else if let ty::Adt(def, _) = self_ty.peel_refs().kind() {
1646+
// FIXME(compiler-errors): This could be generalized, both to
1647+
// be more granular, and probably look past other `#[fundamental]`
1648+
// types, too.
1649+
self.tcx.visibility(def.did()).is_accessible_from(body_def_id, self.tcx)
1650+
} else {
1651+
true
1652+
}
1653+
})
1654+
.collect();
1655+
1656+
impl_candidates.sort_by_key(|tr| tr.to_string());
1657+
impl_candidates.dedup();
1658+
impl_candidates
1659+
};
1660+
1661+
// We'll check for the case where the reason for the mismatch is that the trait comes from
1662+
// one crate version and the type comes from another crate version, even though they both
1663+
// are from the same crate.
1664+
let trait_def_id = trait_ref.def_id();
1665+
if let ty::Adt(def, _) = trait_ref.self_ty().skip_binder().peel_refs().kind()
1666+
&& let found_type = def.did()
1667+
&& trait_def_id.krate != found_type.krate
1668+
&& self.tcx.crate_name(trait_def_id.krate) == self.tcx.crate_name(found_type.krate)
1669+
{
1670+
let name = self.tcx.crate_name(trait_def_id.krate);
1671+
let spans: Vec<_> = [trait_def_id, found_type]
1672+
.into_iter()
1673+
.filter_map(|def_id| self.tcx.extern_crate(def_id))
1674+
.map(|data| {
1675+
let dependency = if data.dependency_of == LOCAL_CRATE {
1676+
"direct dependency of the current crate".to_string()
1677+
} else {
1678+
let dep = self.tcx.crate_name(data.dependency_of);
1679+
format!("dependency of crate `{dep}`")
1680+
};
1681+
(
1682+
data.span,
1683+
format!("one version of crate `{name}` is used here, as a {dependency}"),
1684+
)
1685+
})
1686+
.collect();
1687+
let mut span: MultiSpan = spans.iter().map(|(sp, _)| *sp).collect::<Vec<Span>>().into();
1688+
for (sp, label) in spans.into_iter() {
1689+
span.push_span_label(sp, label);
1690+
}
1691+
err.highlighted_span_help(
1692+
span,
1693+
vec![
1694+
StringPart::normal("you have ".to_string()),
1695+
StringPart::highlighted("multiple different versions".to_string()),
1696+
StringPart::normal(" of crate `".to_string()),
1697+
StringPart::highlighted(format!("{name}")),
1698+
StringPart::normal("` in your dependency graph".to_string()),
1699+
],
1700+
);
1701+
let candidates = if impl_candidates.is_empty() {
1702+
alternative_candidates(trait_def_id)
1703+
} else {
1704+
impl_candidates.into_iter().map(|cand| cand.trait_ref).collect()
1705+
};
1706+
if let Some((sp_candidate, sp_found)) = candidates.iter().find_map(|trait_ref| {
1707+
if let ty::Adt(def, _) = trait_ref.self_ty().peel_refs().kind()
1708+
&& let candidate_def_id = def.did()
1709+
&& let Some(name) = self.tcx.opt_item_name(candidate_def_id)
1710+
&& let Some(found) = self.tcx.opt_item_name(found_type)
1711+
&& name == found
1712+
&& candidate_def_id.krate != found_type.krate
1713+
&& self.tcx.crate_name(candidate_def_id.krate)
1714+
== self.tcx.crate_name(found_type.krate)
1715+
{
1716+
// A candidate was found of an item with the same name, from two separate
1717+
// versions of the same crate, let's clarify.
1718+
Some((self.tcx.def_span(candidate_def_id), self.tcx.def_span(found_type)))
1719+
} else {
1720+
None
1721+
}
1722+
}) {
1723+
let mut span: MultiSpan = vec![sp_candidate, sp_found].into();
1724+
span.push_span_label(self.tcx.def_span(trait_def_id), "this is the required trait");
1725+
span.push_span_label(sp_candidate, "this type implements the required trait");
1726+
span.push_span_label(
1727+
sp_found,
1728+
"this type doesn't implement the required trait",
1729+
);
1730+
err.highlighted_span_note(
1731+
span,
1732+
vec![
1733+
StringPart::normal(
1734+
"two types coming from two different versions of the same crate are \
1735+
different types "
1736+
.to_string(),
1737+
),
1738+
StringPart::highlighted("even if they look the same".to_string()),
1739+
],
1740+
);
1741+
}
1742+
return true;
1743+
}
1744+
16291745
if let [single] = &impl_candidates {
1746+
// If we have a single implementation, try to unify it with the trait ref
1747+
// that failed. This should uncover a better hint for what *is* implemented.
16301748
if self.probe(|_| {
16311749
let ocx = ObligationCtxt::new(self);
16321750

@@ -1785,37 +1903,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
17851903
// Mentioning implementers of `Copy`, `Debug` and friends is not useful.
17861904
return false;
17871905
}
1788-
let mut impl_candidates: Vec<_> = self
1789-
.tcx
1790-
.all_impls(def_id)
1791-
// Ignore automatically derived impls and `!Trait` impls.
1792-
.filter_map(|def_id| self.tcx.impl_trait_header(def_id))
1793-
.filter_map(|header| {
1794-
(header.polarity != ty::ImplPolarity::Negative
1795-
|| self.tcx.is_automatically_derived(def_id))
1796-
.then(|| header.trait_ref.instantiate_identity())
1797-
})
1798-
.filter(|trait_ref| {
1799-
let self_ty = trait_ref.self_ty();
1800-
// Avoid mentioning type parameters.
1801-
if let ty::Param(_) = self_ty.kind() {
1802-
false
1803-
}
1804-
// Avoid mentioning types that are private to another crate
1805-
else if let ty::Adt(def, _) = self_ty.peel_refs().kind() {
1806-
// FIXME(compiler-errors): This could be generalized, both to
1807-
// be more granular, and probably look past other `#[fundamental]`
1808-
// types, too.
1809-
self.tcx.visibility(def.did()).is_accessible_from(body_def_id, self.tcx)
1810-
} else {
1811-
true
1812-
}
1813-
})
1814-
.collect();
1815-
1816-
impl_candidates.sort_by_key(|tr| tr.to_string());
1817-
impl_candidates.dedup();
1818-
return report(impl_candidates, err);
1906+
return report(alternative_candidates(def_id), err);
18191907
}
18201908

18211909
// Sort impl candidates so that ordering is consistent for UI tests.

0 commit comments

Comments
 (0)