From 48ae9e6f73e1f790c91f96348aa36bdd3cff6b7c Mon Sep 17 00:00:00 2001 From: Gurinder Singh Date: Tue, 16 Jul 2024 14:20:59 +0530 Subject: [PATCH] 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. --- compiler/rustc_ty_utils/src/layout.rs | 3 ++- tests/crashes/126942.rs | 11 ---------- .../ice-layout-of-invalid-type-126942.rs | 22 +++++++++++++++++++ .../ice-layout-of-invalid-type-126942.stderr | 19 ++++++++++++++++ 4 files changed, 43 insertions(+), 12 deletions(-) delete mode 100644 tests/crashes/126942.rs create mode 100644 tests/ui/layout/ice-layout-of-invalid-type-126942.rs create mode 100644 tests/ui/layout/ice-layout-of-invalid-type-126942.stderr diff --git a/compiler/rustc_ty_utils/src/layout.rs b/compiler/rustc_ty_utils/src/layout.rs index 6045abc50a9da..254341e8305be 100644 --- a/compiler/rustc_ty_utils/src/layout.rs +++ b/compiler/rustc_ty_utils/src/layout.rs @@ -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(_) => { diff --git a/tests/crashes/126942.rs b/tests/crashes/126942.rs deleted file mode 100644 index e4adc8fab287e..0000000000000 --- a/tests/crashes/126942.rs +++ /dev/null @@ -1,11 +0,0 @@ -//@ known-bug: rust-lang/rust#126942 -struct Thing; - -pub trait Every { - type Assoc; -} -impl Every for Thing { - type Assoc = T; -} - -static I: ::Assoc = 3; diff --git a/tests/ui/layout/ice-layout-of-invalid-type-126942.rs b/tests/ui/layout/ice-layout-of-invalid-type-126942.rs new file mode 100644 index 0000000000000..c59ed156747a0 --- /dev/null +++ b/tests/ui/layout/ice-layout-of-invalid-type-126942.rs @@ -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 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: ::Assoc = 3; +//~^ ERROR type annotations needed + +fn main() {} diff --git a/tests/ui/layout/ice-layout-of-invalid-type-126942.stderr b/tests/ui/layout/ice-layout-of-invalid-type-126942.stderr new file mode 100644 index 0000000000000..75dfe0883518c --- /dev/null +++ b/tests/ui/layout/ice-layout-of-invalid-type-126942.stderr @@ -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 Every for Thing { + | ^ unconstrained type parameter + +error[E0283]: type annotations needed + --> $DIR/ice-layout-of-invalid-type-126942.rs:19:11 + | +LL | static I: ::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`.