From 22f074129a27e229b30c69b8e8d600dee3593537 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 28 Jan 2025 11:59:29 +0000 Subject: [PATCH 1/2] Add tests for transmuting pattern types --- tests/ui/type/pattern_types/transmute.rs | 34 +++++++++++++++++ tests/ui/type/pattern_types/transmute.stderr | 39 ++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 tests/ui/type/pattern_types/transmute.rs create mode 100644 tests/ui/type/pattern_types/transmute.stderr diff --git a/tests/ui/type/pattern_types/transmute.rs b/tests/ui/type/pattern_types/transmute.rs new file mode 100644 index 0000000000000..54aab6e3cb248 --- /dev/null +++ b/tests/ui/type/pattern_types/transmute.rs @@ -0,0 +1,34 @@ +#![feature(pattern_types)] +#![feature(pattern_type_macro)] + +use std::pat::pattern_type; + +// ok +fn create(x: u32) -> pattern_type!(u32 is S..=E) { + unsafe { std::mem::transmute(x) } + //~^ ERROR types of different sizes +} + +// ok +fn unwrap(x: pattern_type!(u32 is S..=E)) -> u32 { + unsafe { std::mem::transmute(x) } + //~^ ERROR types of different sizes +} + +// bad, only when S != u32::MIN or E != u32::MAX will this ok +fn non_base_ty_transmute( + x: Option, +) -> u32 { + unsafe { std::mem::transmute(x) } + //~^ ERROR types of different sizes +} + +// bad, only when S = u32::MIN and E = u32::MAX will this ok +fn wrapped_transmute( + x: Option, +) -> Option { + unsafe { std::mem::transmute(x) } + //~^ ERROR types of different sizes +} + +fn main() {} diff --git a/tests/ui/type/pattern_types/transmute.stderr b/tests/ui/type/pattern_types/transmute.stderr new file mode 100644 index 0000000000000..f348740b10597 --- /dev/null +++ b/tests/ui/type/pattern_types/transmute.stderr @@ -0,0 +1,39 @@ +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> $DIR/transmute.rs:8:14 + | +LL | unsafe { std::mem::transmute(x) } + | ^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `u32` (32 bits) + = note: target type: `(u32) is S..=E` (size can vary because of u32) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> $DIR/transmute.rs:14:14 + | +LL | unsafe { std::mem::transmute(x) } + | ^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `(u32) is S..=E` (size can vary because of u32) + = note: target type: `u32` (32 bits) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> $DIR/transmute.rs:22:14 + | +LL | unsafe { std::mem::transmute(x) } + | ^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `Option<(u32) is S..=E>` (size can vary because of u32) + = note: target type: `u32` (32 bits) + +error[E0512]: cannot transmute between types of different sizes, or dependently-sized types + --> $DIR/transmute.rs:30:14 + | +LL | unsafe { std::mem::transmute(x) } + | ^^^^^^^^^^^^^^^^^^^ + | + = note: source type: `Option<(u32) is S..=E>` (size can vary because of u32) + = note: target type: `Option` (64 bits) + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0512`. From a639e276f3bfb0e0538bad78f3d68ebb7877aeaa Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 28 Jan 2025 11:08:22 +0000 Subject: [PATCH 2/2] Allow transmuting generic pattern types to and from their base --- compiler/rustc_middle/src/ty/layout.rs | 3 +++ tests/ui/type/pattern_types/transmute.rs | 2 -- tests/ui/type/pattern_types/transmute.stderr | 24 +++----------------- 3 files changed, 6 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index 0d41a1d7dbfba..b212992bd2dd7 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -504,6 +504,9 @@ impl<'tcx> SizeSkeleton<'tcx> { } } + // Pattern types are always the same size as their base. + ty::Pat(base, _) => SizeSkeleton::compute(base, tcx, typing_env), + _ => Err(err), } } diff --git a/tests/ui/type/pattern_types/transmute.rs b/tests/ui/type/pattern_types/transmute.rs index 54aab6e3cb248..cb76b2b938dc6 100644 --- a/tests/ui/type/pattern_types/transmute.rs +++ b/tests/ui/type/pattern_types/transmute.rs @@ -6,13 +6,11 @@ use std::pat::pattern_type; // ok fn create(x: u32) -> pattern_type!(u32 is S..=E) { unsafe { std::mem::transmute(x) } - //~^ ERROR types of different sizes } // ok fn unwrap(x: pattern_type!(u32 is S..=E)) -> u32 { unsafe { std::mem::transmute(x) } - //~^ ERROR types of different sizes } // bad, only when S != u32::MIN or E != u32::MAX will this ok diff --git a/tests/ui/type/pattern_types/transmute.stderr b/tests/ui/type/pattern_types/transmute.stderr index f348740b10597..578549b515c10 100644 --- a/tests/ui/type/pattern_types/transmute.stderr +++ b/tests/ui/type/pattern_types/transmute.stderr @@ -1,23 +1,5 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-sized types - --> $DIR/transmute.rs:8:14 - | -LL | unsafe { std::mem::transmute(x) } - | ^^^^^^^^^^^^^^^^^^^ - | - = note: source type: `u32` (32 bits) - = note: target type: `(u32) is S..=E` (size can vary because of u32) - -error[E0512]: cannot transmute between types of different sizes, or dependently-sized types - --> $DIR/transmute.rs:14:14 - | -LL | unsafe { std::mem::transmute(x) } - | ^^^^^^^^^^^^^^^^^^^ - | - = note: source type: `(u32) is S..=E` (size can vary because of u32) - = note: target type: `u32` (32 bits) - -error[E0512]: cannot transmute between types of different sizes, or dependently-sized types - --> $DIR/transmute.rs:22:14 + --> $DIR/transmute.rs:20:14 | LL | unsafe { std::mem::transmute(x) } | ^^^^^^^^^^^^^^^^^^^ @@ -26,7 +8,7 @@ LL | unsafe { std::mem::transmute(x) } = note: target type: `u32` (32 bits) error[E0512]: cannot transmute between types of different sizes, or dependently-sized types - --> $DIR/transmute.rs:30:14 + --> $DIR/transmute.rs:28:14 | LL | unsafe { std::mem::transmute(x) } | ^^^^^^^^^^^^^^^^^^^ @@ -34,6 +16,6 @@ LL | unsafe { std::mem::transmute(x) } = note: source type: `Option<(u32) is S..=E>` (size can vary because of u32) = note: target type: `Option` (64 bits) -error: aborting due to 4 previous errors +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0512`.