about summary refs log tree commit diff
path: root/tests/ui/transmutability
diff options
context:
space:
mode:
authorJack Wrenn <jack@wrenn.fyi>2025-06-04 15:58:01 +0000
committerJack Wrenn <jack@wrenn.fyi>2025-06-09 14:08:12 +0000
commite9eae28eee0a99c64e81f4fdf87d3c76241e56bd (patch)
tree51caaa23c92cceeff5c65e86248adf94f63a4762 /tests/ui/transmutability
parent7c10378e1fee5ddc6573b916aeb884ab10e0de17 (diff)
downloadrust-e9eae28eee0a99c64e81f4fdf87d3c76241e56bd.tar.gz
rust-e9eae28eee0a99c64e81f4fdf87d3c76241e56bd.zip
transmutability: shift abstraction boundary
Previously, `rustc_transmute`'s layout representations were genericized
over `R`, a reference. Now, it's instead genericized over
representations of type and region. This allows us to move reference
transmutability logic from `rustc_trait_selection` to
`rustc_transmutability` (and thus unit test it independently of the
compiler), and — in a follow-up PR — will make it possible to support
analyzing function pointer transmutability with minimal surgery.
Diffstat (limited to 'tests/ui/transmutability')
-rw-r--r--tests/ui/transmutability/references/reject_extension.stderr2
-rw-r--r--tests/ui/transmutability/references/unit-to-u8.stderr2
-rw-r--r--tests/ui/transmutability/references/unsafecell.rs12
-rw-r--r--tests/ui/transmutability/references/unsafecell.stderr32
4 files changed, 39 insertions, 9 deletions
diff --git a/tests/ui/transmutability/references/reject_extension.stderr b/tests/ui/transmutability/references/reject_extension.stderr
index 182106acf12..a5f67785094 100644
--- a/tests/ui/transmutability/references/reject_extension.stderr
+++ b/tests/ui/transmutability/references/reject_extension.stderr
@@ -2,7 +2,7 @@ error[E0277]: `&Packed<Two>` cannot be safely transmuted into `&Packed<Four>`
   --> $DIR/reject_extension.rs:48:37
    |
 LL |     assert::is_transmutable::<&Src, &Dst>();
-   |                                     ^^^^ the referent size of `&Packed<Two>` (2 bytes) is smaller than that of `&Packed<Four>` (4 bytes)
+   |                                     ^^^^ the size of `Packed<Two>` (2 bytes) is smaller than that of `Packed<Four>` (4 bytes)
    |
 note: required by a bound in `is_transmutable`
   --> $DIR/reject_extension.rs:13:14
diff --git a/tests/ui/transmutability/references/unit-to-u8.stderr b/tests/ui/transmutability/references/unit-to-u8.stderr
index bc9f286e097..35d1179f451 100644
--- a/tests/ui/transmutability/references/unit-to-u8.stderr
+++ b/tests/ui/transmutability/references/unit-to-u8.stderr
@@ -2,7 +2,7 @@ error[E0277]: `&Unit` cannot be safely transmuted into `&u8`
   --> $DIR/unit-to-u8.rs:22:52
    |
 LL |     assert::is_maybe_transmutable::<&'static Unit, &'static u8>();
-   |                                                    ^^^^^^^^^^^ the referent size of `&Unit` (0 bytes) is smaller than that of `&u8` (1 bytes)
+   |                                                    ^^^^^^^^^^^ the size of `Unit` (0 bytes) is smaller than that of `u8` (1 bytes)
    |
 note: required by a bound in `is_maybe_transmutable`
   --> $DIR/unit-to-u8.rs:9:14
diff --git a/tests/ui/transmutability/references/unsafecell.rs b/tests/ui/transmutability/references/unsafecell.rs
index 4001f139770..b0db4af45f5 100644
--- a/tests/ui/transmutability/references/unsafecell.rs
+++ b/tests/ui/transmutability/references/unsafecell.rs
@@ -38,10 +38,10 @@ fn mut_to_mut() {
 }
 
 fn mut_to_ref() {
-    // We don't care about `UnsafeCell` for transmutations in the form `&mut T
-    // -> &U`, because downgrading a `&mut T` to a `&U` deactivates `&mut T` for
-    // the lifetime of `&U`.
-    assert::is_maybe_transmutable::<&'static mut u8, &'static UnsafeCell<u8>>();
-    assert::is_maybe_transmutable::<&'static mut UnsafeCell<u8>, &'static u8>();
-    assert::is_maybe_transmutable::<&'static mut UnsafeCell<u8>, &'static UnsafeCell<u8>>();
+    // `&mut UnsafeCell` is irrelevant in the source.
+    assert::is_maybe_transmutable::<&'static mut UnsafeCell<bool>, &'static u8>();
+    // `&UnsafeCell` in forbidden in the destination, since the destination can be used to
+    // invalidate a shadowed source reference.
+    assert::is_maybe_transmutable::<&'static mut bool, &'static UnsafeCell<u8>>(); //~ ERROR: cannot be safely transmuted
+    assert::is_maybe_transmutable::<&'static mut UnsafeCell<bool>, &'static UnsafeCell<u8>>(); //~ ERROR: cannot be safely transmuted
 }
diff --git a/tests/ui/transmutability/references/unsafecell.stderr b/tests/ui/transmutability/references/unsafecell.stderr
index 6664d8a7d6f..02a0935e84e 100644
--- a/tests/ui/transmutability/references/unsafecell.stderr
+++ b/tests/ui/transmutability/references/unsafecell.stderr
@@ -28,6 +28,36 @@ LL |     where
 LL |         Dst: TransmuteFrom<Src, { Assume::SAFETY }>
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_maybe_transmutable`
 
-error: aborting due to 2 previous errors
+error[E0277]: `&mut bool` cannot be safely transmuted into `&UnsafeCell<u8>`
+  --> $DIR/unsafecell.rs:45:56
+   |
+LL |     assert::is_maybe_transmutable::<&'static mut bool, &'static UnsafeCell<u8>>();
+   |                                                        ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Freeze` is not implemented for `UnsafeCell<u8>`
+   |
+note: required by a bound in `is_maybe_transmutable`
+  --> $DIR/unsafecell.rs:12:14
+   |
+LL |     pub fn is_maybe_transmutable<Src, Dst>()
+   |            --------------------- required by a bound in this function
+LL |     where
+LL |         Dst: TransmuteFrom<Src, { Assume::SAFETY }>
+   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_maybe_transmutable`
+
+error[E0277]: `&mut UnsafeCell<bool>` cannot be safely transmuted into `&UnsafeCell<u8>`
+  --> $DIR/unsafecell.rs:46:68
+   |
+LL |     assert::is_maybe_transmutable::<&'static mut UnsafeCell<bool>, &'static UnsafeCell<u8>>();
+   |                                                                    ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Freeze` is not implemented for `UnsafeCell<u8>`
+   |
+note: required by a bound in `is_maybe_transmutable`
+  --> $DIR/unsafecell.rs:12:14
+   |
+LL |     pub fn is_maybe_transmutable<Src, Dst>()
+   |            --------------------- required by a bound in this function
+LL |     where
+LL |         Dst: TransmuteFrom<Src, { Assume::SAFETY }>
+   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_maybe_transmutable`
+
+error: aborting due to 4 previous errors
 
 For more information about this error, try `rustc --explain E0277`.