about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2024-01-09 14:46:30 +0000
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2024-01-09 16:09:30 +0000
commit4f0869ea8903947d61b6db73178a4234a9959feb (patch)
tree849d8d86fb124bcc16c55e951dff8aa813a0d1d6
parentbe00c5a9b89161b7f45ba80340f709e8e41122f9 (diff)
downloadrust-4f0869ea8903947d61b6db73178a4234a9959feb.tar.gz
rust-4f0869ea8903947d61b6db73178a4234a9959feb.zip
Fix an ICE that occurs after an error has already been reported
-rw-r--r--compiler/rustc_transmute/src/layout/tree.rs1
-rw-r--r--tests/ui/transmutability/structs/repr/transmute_infinitely_recursive_type.rs26
-rw-r--r--tests/ui/transmutability/structs/repr/transmute_infinitely_recursive_type.stderr21
3 files changed, 48 insertions, 0 deletions
diff --git a/compiler/rustc_transmute/src/layout/tree.rs b/compiler/rustc_transmute/src/layout/tree.rs
index 49f24f66b24..86a077ee808 100644
--- a/compiler/rustc_transmute/src/layout/tree.rs
+++ b/compiler/rustc_transmute/src/layout/tree.rs
@@ -199,6 +199,7 @@ pub(crate) mod rustc {
             match err {
                 LayoutError::Unknown(..) | LayoutError::ReferencesError(..) => Self::UnknownLayout,
                 LayoutError::SizeOverflow(..) => Self::SizeOverflow,
+                LayoutError::Cycle(err) => Self::TypeError(*err),
                 err => unimplemented!("{:?}", err),
             }
         }
diff --git a/tests/ui/transmutability/structs/repr/transmute_infinitely_recursive_type.rs b/tests/ui/transmutability/structs/repr/transmute_infinitely_recursive_type.rs
new file mode 100644
index 00000000000..0be5b41c80b
--- /dev/null
+++ b/tests/ui/transmutability/structs/repr/transmute_infinitely_recursive_type.rs
@@ -0,0 +1,26 @@
+//~ ERROR: cycle detected
+//! Safe transmute did not handle cycle errors that could occur during
+//! layout computation. This test checks that we do not ICE in such
+//! situations (see #117491).
+#![crate_type = "lib"]
+#![feature(transmutability)]
+#![allow(dead_code, incomplete_features, non_camel_case_types)]
+
+mod assert {
+    use std::mem::{Assume, BikeshedIntrinsicFrom};
+    pub struct Context;
+
+    pub fn is_maybe_transmutable<Src, Dst>()
+    where
+        Dst: BikeshedIntrinsicFrom<Src, Context>,
+    {
+    }
+}
+
+fn should_pad_explicitly_packed_field() {
+    #[repr(C)]
+    struct ExplicitlyPadded(ExplicitlyPadded);
+    //~^ ERROR: recursive type
+
+    assert::is_maybe_transmutable::<ExplicitlyPadded, ()>();
+}
diff --git a/tests/ui/transmutability/structs/repr/transmute_infinitely_recursive_type.stderr b/tests/ui/transmutability/structs/repr/transmute_infinitely_recursive_type.stderr
new file mode 100644
index 00000000000..0dedd5aaf73
--- /dev/null
+++ b/tests/ui/transmutability/structs/repr/transmute_infinitely_recursive_type.stderr
@@ -0,0 +1,21 @@
+error[E0072]: recursive type `ExplicitlyPadded` has infinite size
+  --> $DIR/transmute_infinitely_recursive_type.rs:22:5
+   |
+LL |     struct ExplicitlyPadded(ExplicitlyPadded);
+   |     ^^^^^^^^^^^^^^^^^^^^^^^ ---------------- recursive without indirection
+   |
+help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle
+   |
+LL |     struct ExplicitlyPadded(Box<ExplicitlyPadded>);
+   |                             ++++                +
+
+error[E0391]: cycle detected when computing layout of `should_pad_explicitly_packed_field::ExplicitlyPadded`
+   |
+   = note: ...which immediately requires computing layout of `should_pad_explicitly_packed_field::ExplicitlyPadded` again
+   = note: cycle used when evaluating trait selection obligation `(): core::mem::transmutability::BikeshedIntrinsicFrom<should_pad_explicitly_packed_field::ExplicitlyPadded, assert::Context, core::mem::transmutability::Assume { alignment: false, lifetimes: false, safety: false, validity: false }>`
+   = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
+
+error: aborting due to 2 previous errors
+
+Some errors have detailed explanations: E0072, E0391.
+For more information about an error, try `rustc --explain E0072`.