Skip to content

Commit

Permalink
Do not ICE when trying to get layout of an infer type
Browse files Browse the repository at this point in the history
or other types whose layout cannot be computed such
as bound, coroutine witness etc.
  • Loading branch information
gurry committed Jul 16, 2024
1 parent 594702e commit 48ae9e6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_ty_utils/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,8 @@ fn layout_of_uncached<'tcx>(
}

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

ty::Placeholder(..) | ty::Param(_) => {
Expand Down
11 changes: 0 additions & 11 deletions tests/crashes/126942.rs

This file was deleted.

22 changes: 22 additions & 0 deletions tests/ui/layout/ice-layout-of-invalid-type-126942.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Regression test for #126942

// Tests that we do not ICE when an attempt
// is made to compute the layout of a type
// that normalizes to an infer type

struct Thing;

pub trait Every {
type Assoc;
}
impl<T: ?Sized> Every for Thing {
//~^ ERROR the type parameter `T` is not constrained by the impl trait, self type, or predicates
type Assoc = T;
}

// The type of this static normalizes to `Infer`
// thanks to the `?Sized` constraint in the impl above
static I: <Thing as Every>::Assoc = 3;
//~^ ERROR type annotations needed

fn main() {}
19 changes: 19 additions & 0 deletions tests/ui/layout/ice-layout-of-invalid-type-126942.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
--> $DIR/ice-layout-of-invalid-type-126942.rs:12:6
|
LL | impl<T: ?Sized> Every for Thing {
| ^ unconstrained type parameter

error[E0283]: type annotations needed
--> $DIR/ice-layout-of-invalid-type-126942.rs:19:11
|
LL | static I: <Thing as Every>::Assoc = 3;
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
|
= note: cannot satisfy `_: Sync`
= note: shared static variables must have a type that implements `Sync`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0207, E0283.
For more information about an error, try `rustc --explain E0207`.

0 comments on commit 48ae9e6

Please sign in to comment.