Skip to content

Commit 8574f37

Browse files
committed
Do not call extern_crate on current trait on crate mismatch errors
When we encounter an error caused by traits/types of different versions of the same crate, filter out the current crate when collecting spans to add to the context so we don't call `extern_crate` on the `DefId` of the current crate, which is meaningless and ICEs. Produced output with this filter: ``` error[E0277]: the trait bound `foo::Struct: Trait` is not satisfied --> y.rs:13:19 | 13 | check_trait::<foo::Struct>(); | ^^^^^^^^^^^ the trait `Trait` is not implemented for `foo::Struct` | note: there are multiple different versions of crate `foo` in the dependency graph --> y.rs:7:1 | 4 | extern crate foo; | ----------------- one version of crate `foo` is used here, as a direct dependency of the current crate 5 | 6 | pub struct Struct; | ----------------- this type implements the required trait 7 | pub trait Trait {} | ^^^^^^^^^^^^^^^ this is the required trait | ::: x.rs:4:1 | 4 | pub struct Struct; | ----------------- this type doesn't implement the required trait 5 | 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 `check_trait` --> y.rs:10:19 | 10 | fn check_trait<T: Trait>() {} | ^^^^^ required by this bound in `check_trait` ``` Fix #133563.
1 parent 9b4d7c6 commit 8574f37

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
17311731
span.push_span_label(self.tcx.def_span(trait_def_id), "this is the required trait");
17321732
for (sp, label) in [trait_def_id, other_trait_def_id]
17331733
.iter()
1734+
// The current crate-version might depend on another version of the same crate
1735+
// (Think "semver-trick"). Do not call `extern_crate` in that case for the local
1736+
// crate as that doesn't make sense and ICEs (#133563).
1737+
.filter(|def_id| !def_id.is_local())
17341738
.filter_map(|def_id| self.tcx.extern_crate(def_id.krate))
17351739
.map(|data| {
17361740
let dependency = if data.dependency_of == LOCAL_CRATE {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![crate_type = "lib"]
2+
#![crate_name = "foo"]
3+
4+
extern crate foo;
5+
6+
pub struct Struct;
7+
pub trait Trait {}
8+
impl Trait for Struct {}
9+
10+
fn check_trait<T: Trait>() {}
11+
12+
fn ice() {
13+
check_trait::<foo::Struct>();
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![crate_type = "lib"]
2+
#![crate_name = "foo"]
3+
4+
pub struct Struct;
5+
pub trait Trait {}
6+
impl Trait for Struct {}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//@ only-linux
2+
//@ ignore-wasm32
3+
//@ ignore-wasm64
4+
// ignore-tidy-linelength
5+
6+
// Verify that if the current crate depends on a different version of the same crate, *and* types
7+
// and traits of the different versions are mixed, we produce diagnostic output and not an ICE.
8+
// #133563
9+
10+
use run_make_support::{rust_lib_name, rustc};
11+
12+
fn main() {
13+
rustc().input("foo-prev.rs").run();
14+
15+
rustc()
16+
.extra_filename("current")
17+
.metadata("current")
18+
.input("foo-current.rs")
19+
.extern_("foo", rust_lib_name("foo"))
20+
.run_fail()
21+
.assert_stderr_contains(r#"
22+
note: there are multiple different versions of crate `foo` in the dependency graph
23+
--> foo-current.rs:7:1
24+
|
25+
4 | extern crate foo;
26+
| ----------------- one version of crate `foo` is used here, as a direct dependency of the current crate
27+
5 |
28+
6 | pub struct Struct;
29+
| ----------------- this type implements the required trait
30+
7 | pub trait Trait {}
31+
| ^^^^^^^^^^^^^^^ this is the required trait"#);
32+
}

0 commit comments

Comments
 (0)