Skip to content

Commit 8a568d9

Browse files
committed
Remove less relevant info from diagnostic
``` 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 ``` The approach to accomplish this is a HACK, and we'd want a better way to do this. I believe that moving E0277 to be a structured diagnostic would help in that regard.
1 parent 6fbf444 commit 8a568d9

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

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

+18
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,24 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
17961796
StringPart::highlighted("cargo tree".to_string()),
17971797
StringPart::normal("` to explore your dependency tree".to_string()),
17981798
]);
1799+
1800+
// FIXME: this is a giant hack for the benefit of this specific diagnostic. Because
1801+
// we're so nested in method calls before the error gets emitted, bubbling a single bit
1802+
// flag informing the top level caller to stop adding extra detail to the diagnostic,
1803+
// would actually be harder to follow. So we do something naughty here: we consume the
1804+
// diagnostic, emit it and leave in its place a "delayed bug" that will continue being
1805+
// modified but won't actually be printed to end users. This *is not ideal*, but allows
1806+
// us to reduce the verbosity of an error that is already quite verbose and increase its
1807+
// specificity. Below we modify the main message as well, in a way that *could* break if
1808+
// the implementation of Diagnostics change significantly, but that would be caught with
1809+
// a make test failure when this diagnostic is tested.
1810+
err.primary_message(format!(
1811+
"{} because the trait comes from a different crate version",
1812+
err.messages[0].0.as_str().unwrap(),
1813+
));
1814+
let diag = err.clone();
1815+
err.downgrade_to_delayed_bug();
1816+
self.tcx.dcx().emit_diagnostic(diag);
17991817
return true;
18001818
}
18011819

tests/run-make/crate-loading/rmake.rs

+20-26
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,31 @@ fn main() {
1818
.extern_("dependency", rust_lib_name("dependency"))
1919
.extern_("dep_2_reexport", rust_lib_name("foo"))
2020
.run_fail()
21-
.assert_stderr_contains(r#"error[E0277]: the trait bound `dep_2_reexport::Type: Trait` is not satisfied
22-
--> multiple-dep-versions.rs:7:18
23-
|
24-
7 | do_something(Type);
25-
| ------------ ^^^^ the trait `Trait` is not implemented for `dep_2_reexport::Type`
26-
| |
27-
| required by a bound introduced by this call
28-
|
21+
.assert_stderr_contains(r#"error[E0277]: the trait bound `dep_2_reexport::Type: Trait` is not satisfied because the trait comes from a different crate version
22+
--> multiple-dep-versions.rs:7:18
23+
|
24+
7 | do_something(Type);
25+
| ^^^^ the trait `Trait` is not implemented for `dep_2_reexport::Type`
26+
|
2927
note: there are multiple different versions of crate `dependency` in the dependency graph"#)
3028
.assert_stderr_contains(r#"
31-
3 | pub struct Type(pub i32);
32-
| --------------- this type implements the required trait
33-
4 | pub trait Trait {
34-
| ^^^^^^^^^^^^^^^ this is the required trait
29+
3 | pub struct Type(pub i32);
30+
| --------------- this type implements the required trait
31+
4 | pub trait Trait {
32+
| ^^^^^^^^^^^^^^^ this is the required trait
3533
"#)
3634
.assert_stderr_contains(r#"
37-
1 | extern crate dep_2_reexport;
38-
| ---------------------------- one version of crate `dependency` is used here, as a dependency of crate `foo`
39-
2 | extern crate dependency;
40-
| ------------------------ one version of crate `dependency` is used here, as a direct dependency of the current crate"#)
35+
1 | extern crate dep_2_reexport;
36+
| ---------------------------- one version of crate `dependency` is used here, as a dependency of crate `foo`
37+
2 | extern crate dependency;
38+
| ------------------------ one version of crate `dependency` is used here, as a direct dependency of the current crate"#)
4139
.assert_stderr_contains(r#"
42-
3 | pub struct Type;
43-
| --------------- this type doesn't implement the required trait
44-
4 | pub trait Trait {
45-
| --------------- this is the found trait
46-
= note: two types coming from two different versions of the same crate are different types even if they look the same
47-
= help: you can use `cargo tree` to explore your dependency tree"#)
48-
.assert_stderr_contains(r#"note: required by a bound in `do_something`"#)
49-
.assert_stderr_contains(r#"
50-
12 | pub fn do_something<X: Trait>(_: X) {}
51-
| ^^^^^ required by this bound in `do_something`"#)
40+
3 | pub struct Type;
41+
| --------------- this type doesn't implement the required trait
42+
4 | pub trait Trait {
43+
| --------------- this is the found trait
44+
= note: two types coming from two different versions of the same crate are different types even if they look the same
45+
= help: you can use `cargo tree` to explore your dependency tree"#)
5246
.assert_stderr_contains(r#"error[E0599]: no method named `foo` found for struct `dep_2_reexport::Type` in the current scope
5347
--> multiple-dep-versions.rs:8:10
5448
|

0 commit comments

Comments
 (0)