Skip to content

Commit 0dba6c1

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 87191fa commit 0dba6c1

File tree

1 file changed

+121
-33
lines changed

1 file changed

+121
-33
lines changed

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

+121-33
Original file line numberDiff line numberDiff line change
@@ -1942,9 +1942,127 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
19421942
other: bool,
19431943
param_env: ty::ParamEnv<'tcx>,
19441944
) -> bool {
1945-
// If we have a single implementation, try to unify it with the trait ref
1946-
// that failed. This should uncover a better hint for what *is* implemented.
1945+
let alternative_candidates = |def_id: DefId| {
1946+
let mut impl_candidates: Vec<_> = self
1947+
.tcx
1948+
.all_impls(def_id)
1949+
// Ignore automatically derived impls and `!Trait` impls.
1950+
.filter_map(|def_id| self.tcx.impl_trait_header(def_id))
1951+
.filter_map(|header| {
1952+
(header.polarity != ty::ImplPolarity::Negative
1953+
|| self.tcx.is_automatically_derived(def_id))
1954+
.then(|| header.trait_ref.instantiate_identity())
1955+
})
1956+
.filter(|trait_ref| {
1957+
let self_ty = trait_ref.self_ty();
1958+
// Avoid mentioning type parameters.
1959+
if let ty::Param(_) = self_ty.kind() {
1960+
false
1961+
}
1962+
// Avoid mentioning types that are private to another crate
1963+
else if let ty::Adt(def, _) = self_ty.peel_refs().kind() {
1964+
// FIXME(compiler-errors): This could be generalized, both to
1965+
// be more granular, and probably look past other `#[fundamental]`
1966+
// types, too.
1967+
self.tcx.visibility(def.did()).is_accessible_from(body_def_id, self.tcx)
1968+
} else {
1969+
true
1970+
}
1971+
})
1972+
.collect();
1973+
1974+
impl_candidates.sort_by_key(|tr| tr.to_string());
1975+
impl_candidates.dedup();
1976+
impl_candidates
1977+
};
1978+
1979+
// We'll check for the case where the reason for the mismatch is that the trait comes from
1980+
// one crate version and the type comes from another crate version, even though they both
1981+
// are from the same crate.
1982+
let trait_def_id = trait_ref.def_id();
1983+
if let ty::Adt(def, _) = trait_ref.self_ty().skip_binder().peel_refs().kind()
1984+
&& let found_type = def.did()
1985+
&& trait_def_id.krate != found_type.krate
1986+
&& self.tcx.crate_name(trait_def_id.krate) == self.tcx.crate_name(found_type.krate)
1987+
{
1988+
let name = self.tcx.crate_name(trait_def_id.krate);
1989+
let spans: Vec<_> = [trait_def_id, found_type]
1990+
.into_iter()
1991+
.filter_map(|def_id| self.tcx.extern_crate(def_id))
1992+
.map(|data| {
1993+
let dependency = if data.dependency_of == LOCAL_CRATE {
1994+
"direct dependency of the current crate".to_string()
1995+
} else {
1996+
let dep = self.tcx.crate_name(data.dependency_of);
1997+
format!("dependency of crate `{dep}`")
1998+
};
1999+
(
2000+
data.span,
2001+
format!("one version of crate `{name}` is used here, as a {dependency}"),
2002+
)
2003+
})
2004+
.collect();
2005+
let mut span: MultiSpan = spans.iter().map(|(sp, _)| *sp).collect::<Vec<Span>>().into();
2006+
for (sp, label) in spans.into_iter() {
2007+
span.push_span_label(sp, label);
2008+
}
2009+
err.highlighted_span_help(
2010+
span,
2011+
vec![
2012+
StringPart::normal("you have ".to_string()),
2013+
StringPart::highlighted("multiple different versions".to_string()),
2014+
StringPart::normal(" of crate `".to_string()),
2015+
StringPart::highlighted(format!("{name}")),
2016+
StringPart::normal("` in your dependency graph".to_string()),
2017+
],
2018+
);
2019+
let candidates = if impl_candidates.is_empty() {
2020+
alternative_candidates(trait_def_id)
2021+
} else {
2022+
impl_candidates.into_iter().map(|cand| cand.trait_ref).collect()
2023+
};
2024+
if let Some((sp_candidate, sp_found)) = candidates.iter().find_map(|trait_ref| {
2025+
if let ty::Adt(def, _) = trait_ref.self_ty().peel_refs().kind()
2026+
&& let candidate_def_id = def.did()
2027+
&& let Some(name) = self.tcx.opt_item_name(candidate_def_id)
2028+
&& let Some(found) = self.tcx.opt_item_name(found_type)
2029+
&& name == found
2030+
&& candidate_def_id.krate != found_type.krate
2031+
&& self.tcx.crate_name(candidate_def_id.krate)
2032+
== self.tcx.crate_name(found_type.krate)
2033+
{
2034+
// A candidate was found of an item with the same name, from two separate
2035+
// versions of the same crate, let's clarify.
2036+
Some((self.tcx.def_span(candidate_def_id), self.tcx.def_span(found_type)))
2037+
} else {
2038+
None
2039+
}
2040+
}) {
2041+
let mut span: MultiSpan = vec![sp_candidate, sp_found].into();
2042+
span.push_span_label(self.tcx.def_span(trait_def_id), "this is the required trait");
2043+
span.push_span_label(sp_candidate, "this type implements the required trait");
2044+
span.push_span_label(
2045+
sp_found,
2046+
"this type doesn't implement the required trait",
2047+
);
2048+
err.highlighted_span_note(
2049+
span,
2050+
vec![
2051+
StringPart::normal(
2052+
"two types coming from two different versions of the same crate are \
2053+
different types "
2054+
.to_string(),
2055+
),
2056+
StringPart::highlighted("even if they look the same".to_string()),
2057+
],
2058+
);
2059+
}
2060+
return true;
2061+
}
2062+
19472063
if let [single] = &impl_candidates {
2064+
// If we have a single implementation, try to unify it with the trait ref
2065+
// that failed. This should uncover a better hint for what *is* implemented.
19482066
if self.probe(|_| {
19492067
let ocx = ObligationCtxt::new(self);
19502068

@@ -2099,37 +2217,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
20992217
// Mentioning implementers of `Copy`, `Debug` and friends is not useful.
21002218
return false;
21012219
}
2102-
let mut impl_candidates: Vec<_> = self
2103-
.tcx
2104-
.all_impls(def_id)
2105-
// Ignore automatically derived impls and `!Trait` impls.
2106-
.filter_map(|def_id| self.tcx.impl_trait_header(def_id))
2107-
.filter_map(|header| {
2108-
(header.polarity != ty::ImplPolarity::Negative
2109-
|| self.tcx.is_automatically_derived(def_id))
2110-
.then(|| header.trait_ref.instantiate_identity())
2111-
})
2112-
.filter(|trait_ref| {
2113-
let self_ty = trait_ref.self_ty();
2114-
// Avoid mentioning type parameters.
2115-
if let ty::Param(_) = self_ty.kind() {
2116-
false
2117-
}
2118-
// Avoid mentioning types that are private to another crate
2119-
else if let ty::Adt(def, _) = self_ty.peel_refs().kind() {
2120-
// FIXME(compiler-errors): This could be generalized, both to
2121-
// be more granular, and probably look past other `#[fundamental]`
2122-
// types, too.
2123-
self.tcx.visibility(def.did()).is_accessible_from(body_def_id, self.tcx)
2124-
} else {
2125-
true
2126-
}
2127-
})
2128-
.collect();
2129-
2130-
impl_candidates.sort_by_key(|tr| tr.to_string());
2131-
impl_candidates.dedup();
2132-
return report(impl_candidates, err);
2220+
return report(alternative_candidates(def_id), err);
21332221
}
21342222

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

0 commit comments

Comments
 (0)