Skip to content
/ rust Public
forked from rust-lang/rust

Commit

Permalink
Tweak diagnostic output
Browse files Browse the repository at this point in the history
```
error[E0277]: the trait bound `dep_2_reexport::Type: Trait` is not satisfied because the trait comes from a different crate version
 --> multiple-dep-versions.rs:7:18
  |
7 |     do_something(Type);
  |                  ^^^^ the trait `Trait` is not implemented for `dep_2_reexport::Type`
  |
note: there are multiple different versions of crate `dependency` in the dependency graph
 --> /home/gh-estebank/rust/build/x86_64-unknown-linux-gnu/test/run-make/crate-loading/rmake_out/multiple-dep-versions-1.rs:4:1
  |
3 | pub struct Type(pub i32);
  | --------------- this type implements the required trait
4 | pub trait Trait {
  | ^^^^^^^^^^^^^^^ this is the required trait
  |
 ::: multiple-dep-versions.rs:1:1
  |
1 | extern crate dep_2_reexport;
  | ---------------------------- one version of crate `dependency` is used here, as a dependency of crate `foo`
2 | extern crate dependency;
  | ------------------------ one version of crate `dependency` is used here, as a direct dependency of the current crate
  |
 ::: /home/gh-estebank/rust/build/x86_64-unknown-linux-gnu/test/run-make/crate-loading/rmake_out/multiple-dep-versions-2.rs:3:1
  |
3 | pub struct Type;
  | --------------- this type doesn't implement the required trait
4 | pub trait Trait {
  | --------------- 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
note: required by a bound in `do_something`
  --> /home/gh-estebank/rust/build/x86_64-unknown-linux-gnu/test/run-make/crate-loading/rmake_out/multiple-dep-versions-1.rs:12:24
   |
12 | pub fn do_something<X: Trait>(_: X) {}
   |                        ^^^^^ required by this bound in `do_something`
```
  • Loading branch information
estebank committed Nov 7, 2024
1 parent 35bde07 commit 6fbf444
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1723,7 +1723,14 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
} else {
None
};
let spans: Vec<_> = [trait_def_id, other_trait_def_id]
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| {
Expand All @@ -1740,25 +1747,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
),
)
})
.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![
StringPart::normal("there are ".to_string()),
StringPart::highlighted("multiple different versions".to_string()),
StringPart::normal(" of crate `".to_string()),
StringPart::highlighted(format!("{crate_name}")),
StringPart::normal("` in the dependency graph".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()
};
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");
if let Some(found_type) = found_type {
span.push_span_label(
self.tcx.def_span(found_type),
Expand Down Expand Up @@ -1786,14 +1777,25 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
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!("{crate_name}")),
StringPart::normal("` in the dependency graph\n".to_string()),
]);
err.highlighted_note(vec![
StringPart::normal(
"two types coming from two different versions of the same crate are \
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()),
]);
return true;
}

Expand Down
66 changes: 27 additions & 39 deletions tests/run-make/crate-loading/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,45 @@ fn main() {
.extern_("dependency", rust_lib_name("dependency"))
.extern_("dep_2_reexport", rust_lib_name("foo"))
.run_fail()
.assert_stderr_contains(
r#"error[E0277]: the trait bound `dep_2_reexport::Type: Trait` is not satisfied
.assert_stderr_contains(r#"error[E0277]: the trait bound `dep_2_reexport::Type: Trait` is not satisfied
--> multiple-dep-versions.rs:7:18
|
7 | do_something(Type);
| ------------ ^^^^ the trait `Trait` is not implemented for `dep_2_reexport::Type`
| |
| required by a bound introduced by this call
|
help: there are multiple different versions of crate `dependency` in the dependency graph
--> multiple-dep-versions.rs:1:1
|
1 | extern crate dep_2_reexport;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one version of crate `dependency` is used here, as a dependency of crate `foo`
2 | extern crate dependency;
| ^^^^^^^^^^^^^^^^^^^^^^^^ one version of crate `dependency` is used here, as a direct dependency of the current crate"#,
)
.assert_stderr_contains(
r#"
note: there are multiple different versions of crate `dependency` in the dependency graph"#)
.assert_stderr_contains(r#"
3 | pub struct Type(pub i32);
| --------------- this type implements the required trait
4 | pub trait Trait {
| ^^^^^^^^^^^^^^^ this is the required trait"#,
)
.assert_stderr_contains(
r#"
| ^^^^^^^^^^^^^^^ this is the required trait
"#)
.assert_stderr_contains(r#"
1 | extern crate dep_2_reexport;
| ---------------------------- one version of crate `dependency` is used here, as a dependency of crate `foo`
2 | extern crate dependency;
| ------------------------ one version of crate `dependency` is used here, as a direct dependency of the current crate"#)
.assert_stderr_contains(r#"
3 | pub struct Type;
| --------------- this type doesn't implement the required trait
4 | pub trait Trait {
| --------------- this is the found trait
= help: you can use `cargo tree` to explore your dependency tree"#,
)
.assert_stderr_contains(
r#"
error[E0599]: no method named `foo` found for struct `dep_2_reexport::Type` in the current scope
= 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"#)
.assert_stderr_contains(r#"note: required by a bound in `do_something`"#)
.assert_stderr_contains(r#"
12 | pub fn do_something<X: Trait>(_: X) {}
| ^^^^^ required by this bound in `do_something`"#)
.assert_stderr_contains(r#"error[E0599]: no method named `foo` found for struct `dep_2_reexport::Type` in the current scope
--> multiple-dep-versions.rs:8:10
|
8 | Type.foo();
| ^^^ method not found in `Type`
|
note: there are multiple different versions of crate `dependency` in the dependency graph"#,
)
.assert_stderr_contains(
r#"
note: there are multiple different versions of crate `dependency` in the dependency graph"#)
.assert_stderr_contains(r#"
4 | pub trait Trait {
| ^^^^^^^^^^^^^^^ this is the trait that is needed
5 | fn foo(&self);
Expand All @@ -70,25 +65,19 @@ note: there are multiple different versions of crate `dependency` in the depende
::: multiple-dep-versions.rs:4:18
|
4 | use dependency::{Trait, do_something};
| ----- `Trait` imported here doesn't correspond to the right version of crate `dependency`"#,
)
.assert_stderr_contains(
r#"
| ----- `Trait` imported here doesn't correspond to the right version of crate `dependency`"#)
.assert_stderr_contains(r#"
4 | pub trait Trait {
| --------------- this is the trait that was imported"#,
)
.assert_stderr_contains(
r#"
| --------------- this is the trait that was imported"#)
.assert_stderr_contains(r#"
error[E0599]: no function or associated item named `bar` found for struct `dep_2_reexport::Type` in the current scope
--> multiple-dep-versions.rs:9:11
|
9 | Type::bar();
| ^^^ function or associated item not found in `Type`
|
note: there are multiple different versions of crate `dependency` in the dependency graph"#,
)
.assert_stderr_contains(
r#"
note: there are multiple different versions of crate `dependency` in the dependency graph"#)
.assert_stderr_contains(r#"
4 | pub trait Trait {
| ^^^^^^^^^^^^^^^ this is the trait that is needed
5 | fn foo(&self);
Expand All @@ -98,8 +87,7 @@ note: there are multiple different versions of crate `dependency` in the depende
::: multiple-dep-versions.rs:4:18
|
4 | use dependency::{Trait, do_something};
| ----- `Trait` imported here doesn't correspond to the right version of crate `dependency`"#,
)
| ----- `Trait` imported here doesn't correspond to the right version of crate `dependency`"#)
.assert_stderr_contains(
r#"
6 | pub struct OtherType;
Expand Down

0 comments on commit 6fbf444

Please sign in to comment.