Auto merge of #128849 - estebank:issue-89143, r=jackh726
Tweak detection of multiple crate versions to be more encompassing 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 because the trait comes from a different crate version --> src/main.rs:5:17 | 5 | cnb_runtime(CustomErrorHandler {}); | ^^^^^^^^^^^^^^^^^^^^^ the trait `ErrorHandler` is not implemented for `CustomErrorHandler` | note: there are multiple different versions of crate `c` in the dependency graph --> /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 | ::: 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 | ::: /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 = note: two types coming from two different versions of the same crate are different types even if they look the same = help: you can use `cargo tree` to explore your dependency tree ``` Fix #89143.
This commit is contained in:
commit
5b20c45999
5 changed files with 137 additions and 102 deletions
|
@ -1707,15 +1707,31 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
|
|||
// one crate version and the type comes from another crate version, even though they both
|
||||
// are from the same crate.
|
||||
let trait_def_id = trait_ref.def_id();
|
||||
if let ty::Adt(def, _) = trait_ref.self_ty().skip_binder().peel_refs().kind()
|
||||
&& let found_type = def.did()
|
||||
&& trait_def_id.krate != found_type.krate
|
||||
&& self.tcx.crate_name(trait_def_id.krate) == self.tcx.crate_name(found_type.krate)
|
||||
{
|
||||
let name = self.tcx.crate_name(trait_def_id.krate);
|
||||
let spans: Vec<_> = [trait_def_id, found_type]
|
||||
.into_iter()
|
||||
.filter(|def_id| def_id.krate != LOCAL_CRATE)
|
||||
let trait_name = self.tcx.item_name(trait_def_id);
|
||||
let crate_name = self.tcx.crate_name(trait_def_id.krate);
|
||||
if let Some(other_trait_def_id) = self.tcx.all_traits().find(|def_id| {
|
||||
trait_name == self.tcx.item_name(trait_def_id)
|
||||
&& trait_def_id.krate != def_id.krate
|
||||
&& crate_name == self.tcx.crate_name(def_id.krate)
|
||||
}) {
|
||||
// We've found two different traits with the same name, same crate name, but
|
||||
// different crate `DefId`. We highlight the traits.
|
||||
|
||||
let found_type =
|
||||
if let ty::Adt(def, _) = trait_ref.self_ty().skip_binder().peel_refs().kind() {
|
||||
Some(def.did())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let candidates = if impl_candidates.is_empty() {
|
||||
alternative_candidates(trait_def_id)
|
||||
} else {
|
||||
impl_candidates.into_iter().map(|cand| cand.trait_ref).collect()
|
||||
};
|
||||
let mut span: MultiSpan = self.tcx.def_span(trait_def_id).into();
|
||||
span.push_span_label(self.tcx.def_span(trait_def_id), "this is the required trait");
|
||||
for (sp, label) in [trait_def_id, other_trait_def_id]
|
||||
.iter()
|
||||
.filter_map(|def_id| self.tcx.extern_crate(def_id.krate))
|
||||
.map(|data| {
|
||||
let dependency = if data.dependency_of == LOCAL_CRATE {
|
||||
|
@ -1726,57 +1742,86 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
|
|||
};
|
||||
(
|
||||
data.span,
|
||||
format!("one version of crate `{name}` is used here, as a {dependency}"),
|
||||
format!(
|
||||
"one version of crate `{crate_name}` is used here, as a {dependency}"
|
||||
),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let mut span: MultiSpan = spans.iter().map(|(sp, _)| *sp).collect::<Vec<Span>>().into();
|
||||
for (sp, label) in spans.into_iter() {
|
||||
{
|
||||
span.push_span_label(sp, label);
|
||||
}
|
||||
err.highlighted_span_help(span, vec![
|
||||
let mut points_at_type = false;
|
||||
if let Some(found_type) = found_type {
|
||||
span.push_span_label(
|
||||
self.tcx.def_span(found_type),
|
||||
"this type doesn't implement the required trait",
|
||||
);
|
||||
for trait_ref in candidates {
|
||||
if let ty::Adt(def, _) = trait_ref.self_ty().peel_refs().kind()
|
||||
&& let candidate_def_id = def.did()
|
||||
&& let Some(name) = self.tcx.opt_item_name(candidate_def_id)
|
||||
&& let Some(found) = self.tcx.opt_item_name(found_type)
|
||||
&& name == found
|
||||
&& candidate_def_id.krate != found_type.krate
|
||||
&& self.tcx.crate_name(candidate_def_id.krate)
|
||||
== self.tcx.crate_name(found_type.krate)
|
||||
{
|
||||
// A candidate was found of an item with the same name, from two separate
|
||||
// versions of the same crate, let's clarify.
|
||||
let candidate_span = self.tcx.def_span(candidate_def_id);
|
||||
span.push_span_label(
|
||||
candidate_span,
|
||||
"this type implements the required trait",
|
||||
);
|
||||
points_at_type = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
span.push_span_label(self.tcx.def_span(other_trait_def_id), "this is the found trait");
|
||||
err.highlighted_span_note(span, vec![
|
||||
StringPart::normal("there are ".to_string()),
|
||||
StringPart::highlighted("multiple different versions".to_string()),
|
||||
StringPart::normal(" of crate `".to_string()),
|
||||
StringPart::highlighted(format!("{name}")),
|
||||
StringPart::normal("` in the dependency graph".to_string()),
|
||||
StringPart::highlighted(format!("{crate_name}")),
|
||||
StringPart::normal("` in the dependency graph\n".to_string()),
|
||||
]);
|
||||
let candidates = if impl_candidates.is_empty() {
|
||||
alternative_candidates(trait_def_id)
|
||||
} else {
|
||||
impl_candidates.into_iter().map(|cand| cand.trait_ref).collect()
|
||||
};
|
||||
if let Some((sp_candidate, sp_found)) = candidates.iter().find_map(|trait_ref| {
|
||||
if let ty::Adt(def, _) = trait_ref.self_ty().peel_refs().kind()
|
||||
&& let candidate_def_id = def.did()
|
||||
&& let Some(name) = self.tcx.opt_item_name(candidate_def_id)
|
||||
&& let Some(found) = self.tcx.opt_item_name(found_type)
|
||||
&& name == found
|
||||
&& candidate_def_id.krate != found_type.krate
|
||||
&& self.tcx.crate_name(candidate_def_id.krate)
|
||||
== self.tcx.crate_name(found_type.krate)
|
||||
{
|
||||
// A candidate was found of an item with the same name, from two separate
|
||||
// versions of the same crate, let's clarify.
|
||||
Some((self.tcx.def_span(candidate_def_id), self.tcx.def_span(found_type)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}) {
|
||||
let mut span: MultiSpan = vec![sp_candidate, sp_found].into();
|
||||
span.push_span_label(self.tcx.def_span(trait_def_id), "this is the required trait");
|
||||
span.push_span_label(sp_candidate, "this type implements the required trait");
|
||||
span.push_span_label(sp_found, "this type doesn't implement the required trait");
|
||||
err.highlighted_span_note(span, vec![
|
||||
if points_at_type {
|
||||
// We only clarify that the same type from different crate versions are not the
|
||||
// same when we *find* the same type coming from different crate versions, otherwise
|
||||
// it could be that it was a type provided by a different crate than the one that
|
||||
// provides the trait, and mentioning this adds verbosity without clarification.
|
||||
err.highlighted_note(vec![
|
||||
StringPart::normal(
|
||||
"two types coming from two different versions of the same crate are \
|
||||
different types "
|
||||
different types "
|
||||
.to_string(),
|
||||
),
|
||||
StringPart::highlighted("even if they look the same".to_string()),
|
||||
]);
|
||||
}
|
||||
err.help("you can use `cargo tree` to explore your dependency tree");
|
||||
err.highlighted_help(vec![
|
||||
StringPart::normal("you can use `".to_string()),
|
||||
StringPart::highlighted("cargo tree".to_string()),
|
||||
StringPart::normal("` to explore your dependency tree".to_string()),
|
||||
]);
|
||||
|
||||
// FIXME: this is a giant hack for the benefit of this specific diagnostic. Because
|
||||
// we're so nested in method calls before the error gets emitted, bubbling a single bit
|
||||
// flag informing the top level caller to stop adding extra detail to the diagnostic,
|
||||
// would actually be harder to follow. So we do something naughty here: we consume the
|
||||
// diagnostic, emit it and leave in its place a "delayed bug" that will continue being
|
||||
// modified but won't actually be printed to end users. This *is not ideal*, but allows
|
||||
// us to reduce the verbosity of an error that is already quite verbose and increase its
|
||||
// specificity. Below we modify the main message as well, in a way that *could* break if
|
||||
// the implementation of Diagnostics change significantly, but that would be caught with
|
||||
// a make test failure when this diagnostic is tested.
|
||||
err.primary_message(format!(
|
||||
"{} because the trait comes from a different crate version",
|
||||
err.messages[0].0.as_str().unwrap(),
|
||||
));
|
||||
let diag = err.clone();
|
||||
err.downgrade_to_delayed_bug();
|
||||
self.tcx.dcx().emit_diagnostic(diag);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue