Skip to content

Commit 4868680

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 rust-lang#22750.
1 parent db6c05f commit 4868680

File tree

1 file changed

+124
-39
lines changed

1 file changed

+124
-39
lines changed

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

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

@@ -1798,43 +1919,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
17981919
// Mentioning implementers of `Copy`, `Debug` and friends is not useful.
17991920
return false;
18001921
}
1801-
let mut impl_candidates: Vec<_> = self
1802-
.tcx
1803-
.all_impls(def_id)
1804-
// ignore `do_not_recommend` items
1805-
.filter(|def_id| {
1806-
!self
1807-
.tcx
1808-
.has_attrs_with_path(*def_id, &[sym::diagnostic, sym::do_not_recommend])
1809-
})
1810-
// Ignore automatically derived impls and `!Trait` impls.
1811-
.filter_map(|def_id| self.tcx.impl_trait_header(def_id))
1812-
.filter_map(|header| {
1813-
(header.polarity != ty::ImplPolarity::Negative
1814-
|| self.tcx.is_automatically_derived(def_id))
1815-
.then(|| header.trait_ref.instantiate_identity())
1816-
})
1817-
.filter(|trait_ref| {
1818-
let self_ty = trait_ref.self_ty();
1819-
// Avoid mentioning type parameters.
1820-
if let ty::Param(_) = self_ty.kind() {
1821-
false
1822-
}
1823-
// Avoid mentioning types that are private to another crate
1824-
else if let ty::Adt(def, _) = self_ty.peel_refs().kind() {
1825-
// FIXME(compiler-errors): This could be generalized, both to
1826-
// be more granular, and probably look past other `#[fundamental]`
1827-
// types, too.
1828-
self.tcx.visibility(def.did()).is_accessible_from(body_def_id, self.tcx)
1829-
} else {
1830-
true
1831-
}
1832-
})
1833-
.collect();
1834-
1835-
impl_candidates.sort_by_key(|tr| tr.to_string());
1836-
impl_candidates.dedup();
1837-
return report(impl_candidates, err);
1922+
return report(alternative_candidates(def_id), err);
18381923
}
18391924

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

0 commit comments

Comments
 (0)