Skip to content

Commit 65c46f8

Browse files
Rollup merge of rust-lang#153574 - TaKO8Ki:self-referential-param-env-normalization-ice, r=lcnr
Avoid ICE when param-env normalization leaves unresolved inference variables Fixes rust-lang#153354 Because the impl is already ill-formed, those variables are not fully constrained, so zfully_resolve` fails. We previously treated that as an immediate compiler bug with `span_bug!`, which caused an ICE on invalid input.
2 parents 9e6b3c6 + d29c489 commit 65c46f8

6 files changed

Lines changed: 132 additions & 28 deletions

File tree

compiler/rustc_trait_selection/src/traits/mod.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -300,19 +300,14 @@ fn do_normalize_predicates<'tcx>(
300300
Ok(predicates) => Ok(predicates),
301301
Err(fixup_err) => {
302302
// If we encounter a fixup error, it means that some type
303-
// variable wound up unconstrained. I actually don't know
304-
// if this can happen, and I certainly don't expect it to
305-
// happen often, but if it did happen it probably
306-
// represents a legitimate failure due to some kind of
307-
// unconstrained variable.
308-
//
309-
// @lcnr: Let's still ICE here for now. I want a test case
310-
// for that.
311-
span_bug!(
303+
// variable wound up unconstrained. That can happen for
304+
// ill-formed impls, so we delay a bug here instead of
305+
// immediately ICEing and let type checking report the
306+
// actual user-facing errors.
307+
Err(tcx.dcx().span_delayed_bug(
312308
span,
313-
"inference variables in normalized parameter environment: {}",
314-
fixup_err
315-
);
309+
format!("inference variables in normalized parameter environment: {fixup_err}"),
310+
))
316311
}
317312
}
318313
}

tests/crashes/120033.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@ compile-flags: --crate-type=lib
2+
3+
// Regression test for <https://github.com/rust-lang/rust/issues/120033>
4+
5+
#![feature(rustc_attrs)]
6+
#![feature(non_lifetime_binders)]
7+
#![allow(incomplete_features)]
8+
#![rustc_no_implicit_bounds]
9+
10+
pub trait Foo<T> {
11+
type Bar<K>;
12+
}
13+
14+
pub struct Bar<T: ?AutoTrait> {} //~ ERROR cannot find trait `AutoTrait`
15+
16+
pub fn f<T1, T2>()
17+
where
18+
T1: for<T> Foo<usize, Bar = Bar<T>>, //~ ERROR missing generics for associated type `Foo::Bar`
19+
//~| ERROR missing generics for associated type `Foo::Bar`
20+
T2: for<L, T> Foo<usize, Bar<T> = T1::Bar<T>>,
21+
{
22+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0405]: cannot find trait `AutoTrait` in this scope
2+
--> $DIR/normalized-param-env-unconstrained-type-120033.rs:14:20
3+
|
4+
LL | pub struct Bar<T: ?AutoTrait> {}
5+
| ^^^^^^^^^ not found in this scope
6+
7+
error[E0107]: missing generics for associated type `Foo::Bar`
8+
--> $DIR/normalized-param-env-unconstrained-type-120033.rs:18:27
9+
|
10+
LL | T1: for<T> Foo<usize, Bar = Bar<T>>,
11+
| ^^^ expected 1 generic argument
12+
|
13+
note: associated type defined here, with 1 generic parameter: `K`
14+
--> $DIR/normalized-param-env-unconstrained-type-120033.rs:11:10
15+
|
16+
LL | type Bar<K>;
17+
| ^^^ -
18+
help: add missing generic argument
19+
|
20+
LL | T1: for<T> Foo<usize, Bar<K> = Bar<T>>,
21+
| +++
22+
23+
error[E0107]: missing generics for associated type `Foo::Bar`
24+
--> $DIR/normalized-param-env-unconstrained-type-120033.rs:18:27
25+
|
26+
LL | T1: for<T> Foo<usize, Bar = Bar<T>>,
27+
| ^^^ expected 1 generic argument
28+
|
29+
note: associated type defined here, with 1 generic parameter: `K`
30+
--> $DIR/normalized-param-env-unconstrained-type-120033.rs:11:10
31+
|
32+
LL | type Bar<K>;
33+
| ^^^ -
34+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
35+
help: add missing generic argument
36+
|
37+
LL | T1: for<T> Foo<usize, Bar<K> = Bar<T>>,
38+
| +++
39+
40+
error: aborting due to 3 previous errors
41+
42+
Some errors have detailed explanations: E0107, E0405.
43+
For more information about an error, try `rustc --explain E0107`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//~ ERROR overflow evaluating the requirement `Self: StreamingIterator<'_>` [E0275]
2+
// Regression test for <https://github.com/rust-lang/rust/issues/153354>.
3+
4+
trait StreamingIterator<'a> {
5+
type Item: 'a;
6+
}
7+
8+
impl<'b, I, T> StreamingIterator<'b> for I
9+
//~^ ERROR the type parameter `T` is not constrained by the impl trait, self type, or predicates [E0207]
10+
where
11+
I: IntoIterator,
12+
T: FnMut(Self::Item, I::Item),
13+
{
14+
type Item = T;
15+
//~^ ERROR overflow evaluating the requirement `I: IntoIterator` [E0275]
16+
}
17+
18+
fn main() {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
error[E0275]: overflow evaluating the requirement `Self: StreamingIterator<'_>`
2+
|
3+
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`self_referential_param_env_normalization`)
4+
note: required for `Self` to implement `StreamingIterator<'_>`
5+
--> $DIR/self-referential-param-env-normalization.rs:8:16
6+
|
7+
LL | impl<'b, I, T> StreamingIterator<'b> for I
8+
| ^^^^^^^^^^^^^^^^^^^^^ ^
9+
...
10+
LL | T: FnMut(Self::Item, I::Item),
11+
| -------------------------- unsatisfied trait bound introduced here
12+
= note: 127 redundant requirements hidden
13+
= note: required for `Self` to implement `StreamingIterator<'a>`
14+
15+
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
16+
--> $DIR/self-referential-param-env-normalization.rs:8:13
17+
|
18+
LL | impl<'b, I, T> StreamingIterator<'b> for I
19+
| ^ unconstrained type parameter
20+
21+
error[E0275]: overflow evaluating the requirement `I: IntoIterator`
22+
--> $DIR/self-referential-param-env-normalization.rs:14:17
23+
|
24+
LL | type Item = T;
25+
| ^
26+
|
27+
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`self_referential_param_env_normalization`)
28+
note: required for `I` to implement `StreamingIterator<'_>`
29+
--> $DIR/self-referential-param-env-normalization.rs:8:16
30+
|
31+
LL | impl<'b, I, T> StreamingIterator<'b> for I
32+
| ^^^^^^^^^^^^^^^^^^^^^ ^
33+
...
34+
LL | T: FnMut(Self::Item, I::Item),
35+
| -------------------------- unsatisfied trait bound introduced here
36+
= note: 127 redundant requirements hidden
37+
= note: required for `I` to implement `StreamingIterator<'b>`
38+
39+
error: aborting due to 3 previous errors
40+
41+
Some errors have detailed explanations: E0207, E0275.
42+
For more information about an error, try `rustc --explain E0207`.

0 commit comments

Comments
 (0)