Skip to content

Commit

Permalink
Allow transmuting generic pattern types to and from their base
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jan 29, 2025
1 parent 22f0741 commit a639e27
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 23 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/type/pattern_types/transmute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ use std::pat::pattern_type;
// ok
fn create<const S: u32, const E: u32>(x: u32) -> pattern_type!(u32 is S..=E) {
unsafe { std::mem::transmute(x) }
//~^ ERROR types of different sizes
}

// ok
fn unwrap<const S: u32, const E: u32>(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
Expand Down
24 changes: 3 additions & 21 deletions tests/ui/type/pattern_types/transmute.stderr
Original file line number Diff line number Diff line change
@@ -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) }
| ^^^^^^^^^^^^^^^^^^^
Expand All @@ -26,14 +8,14 @@ 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) }
| ^^^^^^^^^^^^^^^^^^^
|
= note: source type: `Option<(u32) is S..=E>` (size can vary because of u32)
= note: target type: `Option<u32>` (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`.

0 comments on commit a639e27

Please sign in to comment.