Skip to content

Commit af1d5ee

Browse files
authored
Rollup merge of rust-lang#136179 - oli-obk:push-vxvyttorquxw, r=BoxyUwU
Allow transmuting generic pattern types to and from their base Pattern types always have the same size as their base type, so we can just ignore the pattern and look at the base type for figuring out whether transmuting is possible.
2 parents 8cb535b + a639e27 commit af1d5ee

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

Diff for: compiler/rustc_middle/src/ty/layout.rs

+3
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,9 @@ impl<'tcx> SizeSkeleton<'tcx> {
504504
}
505505
}
506506

507+
// Pattern types are always the same size as their base.
508+
ty::Pat(base, _) => SizeSkeleton::compute(base, tcx, typing_env),
509+
507510
_ => Err(err),
508511
}
509512
}

Diff for: tests/ui/type/pattern_types/transmute.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![feature(pattern_types)]
2+
#![feature(pattern_type_macro)]
3+
4+
use std::pat::pattern_type;
5+
6+
// ok
7+
fn create<const S: u32, const E: u32>(x: u32) -> pattern_type!(u32 is S..=E) {
8+
unsafe { std::mem::transmute(x) }
9+
}
10+
11+
// ok
12+
fn unwrap<const S: u32, const E: u32>(x: pattern_type!(u32 is S..=E)) -> u32 {
13+
unsafe { std::mem::transmute(x) }
14+
}
15+
16+
// bad, only when S != u32::MIN or E != u32::MAX will this ok
17+
fn non_base_ty_transmute<const S: u32, const E: u32>(
18+
x: Option<pattern_type!(u32 is S..=E)>,
19+
) -> u32 {
20+
unsafe { std::mem::transmute(x) }
21+
//~^ ERROR types of different sizes
22+
}
23+
24+
// bad, only when S = u32::MIN and E = u32::MAX will this ok
25+
fn wrapped_transmute<const S: u32, const E: u32>(
26+
x: Option<pattern_type!(u32 is S..=E)>,
27+
) -> Option<u32> {
28+
unsafe { std::mem::transmute(x) }
29+
//~^ ERROR types of different sizes
30+
}
31+
32+
fn main() {}

Diff for: tests/ui/type/pattern_types/transmute.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
2+
--> $DIR/transmute.rs:20:14
3+
|
4+
LL | unsafe { std::mem::transmute(x) }
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: source type: `Option<(u32) is S..=E>` (size can vary because of u32)
8+
= note: target type: `u32` (32 bits)
9+
10+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
11+
--> $DIR/transmute.rs:28:14
12+
|
13+
LL | unsafe { std::mem::transmute(x) }
14+
| ^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: source type: `Option<(u32) is S..=E>` (size can vary because of u32)
17+
= note: target type: `Option<u32>` (64 bits)
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0512`.

0 commit comments

Comments
 (0)