Skip to content

Commit 48ae9e6

Browse files
committed
Do not ICE when trying to get layout of an infer type
or other types whose layout cannot be computed such as bound, coroutine witness etc.
1 parent 594702e commit 48ae9e6

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

compiler/rustc_ty_utils/src/layout.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,8 @@ fn layout_of_uncached<'tcx>(
671671
}
672672

673673
ty::Bound(..) | ty::CoroutineWitness(..) | ty::Infer(_) | ty::Error(_) => {
674-
bug!("Layout::compute: unexpected type `{}`", ty)
674+
tcx.dcx().delayed_bug(format!("Layout::compute: unexpected type `{}`", ty));
675+
return Err(error(cx, LayoutError::Unknown(ty)));
675676
}
676677

677678
ty::Placeholder(..) | ty::Param(_) => {

tests/crashes/126942.rs

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Regression test for #126942
2+
3+
// Tests that we do not ICE when an attempt
4+
// is made to compute the layout of a type
5+
// that normalizes to an infer type
6+
7+
struct Thing;
8+
9+
pub trait Every {
10+
type Assoc;
11+
}
12+
impl<T: ?Sized> Every for Thing {
13+
//~^ ERROR the type parameter `T` is not constrained by the impl trait, self type, or predicates
14+
type Assoc = T;
15+
}
16+
17+
// The type of this static normalizes to `Infer`
18+
// thanks to the `?Sized` constraint in the impl above
19+
static I: <Thing as Every>::Assoc = 3;
20+
//~^ ERROR type annotations needed
21+
22+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
2+
--> $DIR/ice-layout-of-invalid-type-126942.rs:12:6
3+
|
4+
LL | impl<T: ?Sized> Every for Thing {
5+
| ^ unconstrained type parameter
6+
7+
error[E0283]: type annotations needed
8+
--> $DIR/ice-layout-of-invalid-type-126942.rs:19:11
9+
|
10+
LL | static I: <Thing as Every>::Assoc = 3;
11+
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
12+
|
13+
= note: cannot satisfy `_: Sync`
14+
= note: shared static variables must have a type that implements `Sync`
15+
16+
error: aborting due to 2 previous errors
17+
18+
Some errors have detailed explanations: E0207, E0283.
19+
For more information about an error, try `rustc --explain E0207`.

0 commit comments

Comments
 (0)