Skip to content

Commit 02fcec2

Browse files
authored
Rollup merge of #99795 - compiler-errors:delay-specialization-normalize-error, r=spastorino
Delay a bug when failed to normalize trait ref during specialization The error messages still kinda suck here but they don't ICE anymore... Fixes #45814 Fixes #43037 r? types
2 parents 9c18fdc + 16a3601 commit 02fcec2

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed

compiler/rustc_trait_selection/src/traits/specialize/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ pub(super) fn specializes(tcx: TyCtxt<'_>, (impl1_def_id, impl2_def_id): (DefId,
151151

152152
// Create an infcx, taking the predicates of impl1 as assumptions:
153153
tcx.infer_ctxt().enter(|infcx| {
154-
// Normalize the trait reference. The WF rules ought to ensure
155-
// that this always succeeds.
156154
let impl1_trait_ref = match traits::fully_normalize(
157155
&infcx,
158156
FulfillmentContext::new(),
@@ -161,8 +159,12 @@ pub(super) fn specializes(tcx: TyCtxt<'_>, (impl1_def_id, impl2_def_id): (DefId,
161159
impl1_trait_ref,
162160
) {
163161
Ok(impl1_trait_ref) => impl1_trait_ref,
164-
Err(err) => {
165-
bug!("failed to fully normalize {:?}: {:?}", impl1_trait_ref, err);
162+
Err(_errors) => {
163+
tcx.sess.delay_span_bug(
164+
tcx.def_span(impl1_def_id),
165+
format!("failed to fully normalize {impl1_trait_ref}"),
166+
);
167+
impl1_trait_ref
166168
}
167169
};
168170

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(specialization)]
2+
#![allow(incomplete_features)]
3+
4+
trait X {}
5+
trait Y: X {}
6+
trait Z {
7+
type Assoc: Y;
8+
}
9+
struct A<T>(T);
10+
11+
impl<T> Y for T where T: X {}
12+
impl<T: X> Z for A<T> {
13+
type Assoc = T;
14+
}
15+
16+
// this impl is invalid, but causes an ICE anyway
17+
impl<T> From<<A<T> as Z>::Assoc> for T {}
18+
//~^ ERROR type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
2+
--> $DIR/issue-43037.rs:17:6
3+
|
4+
LL | impl<T> From<<A<T> as Z>::Assoc> for T {}
5+
| ^ type parameter `T` must be used as the type parameter for some local type
6+
|
7+
= note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
8+
= note: only traits defined in the current crate can be implemented for a type parameter
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0210`.
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//~ ERROR overflow evaluating the requirement `T: Trait<_>`
2+
3+
#![feature(specialization)]
4+
#![allow(incomplete_features)]
5+
6+
pub trait Trait<T> {}
7+
8+
default impl<T, U> Trait<T> for U {}
9+
10+
impl<T> Trait<<T as Iterator>::Item> for T {}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0275]: overflow evaluating the requirement `T: Trait<_>`
2+
|
3+
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`issue_45814`)
4+
note: required because of the requirements on the impl of `Trait<_>` for `T`
5+
--> $DIR/issue-45814.rs:8:20
6+
|
7+
LL | default impl<T, U> Trait<T> for U {}
8+
| ^^^^^^^^ ^
9+
= note: 128 redundant requirements hidden
10+
= note: required because of the requirements on the impl of `Trait<_>` for `T`
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0275`.

0 commit comments

Comments
 (0)