Skip to content

Commit db3275c

Browse files
committed
Safe Transmute: Add alignment tests
1 parent 94ad084 commit db3275c

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// check-fail
2+
#![feature(transmutability)]
3+
4+
mod assert {
5+
use std::mem::{Assume, BikeshedIntrinsicFrom};
6+
pub struct Context;
7+
8+
pub fn is_maybe_transmutable<Src, Dst>()
9+
where
10+
Dst: BikeshedIntrinsicFrom<Src, Context, {
11+
Assume {
12+
alignment: false,
13+
lifetimes: true,
14+
safety: true,
15+
validity: true,
16+
}
17+
}>
18+
{}
19+
}
20+
21+
fn main() {
22+
assert::is_maybe_transmutable::<&'static [u8; 0], &'static [u16; 0]>(); //~ ERROR `&[u8; 0]` cannot be safely transmuted into `&[u16; 0]`
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0277]: `&[u8; 0]` cannot be safely transmuted into `&[u16; 0]` in the defining scope of `assert::Context`
2+
--> $DIR/align-fail.rs:22:55
3+
|
4+
LL | ...tatic [u8; 0], &'static [u16; 0]>();
5+
| ^^^^^^^^^^^^^^^^^ The alignment of `&[u8; 0]` should be stricter than that of `&[u16; 0]`, but it is not
6+
|
7+
note: required by a bound in `is_maybe_transmutable`
8+
--> $DIR/align-fail.rs:10:14
9+
|
10+
LL | pub fn is_maybe_transmutable<Src, Dst>()
11+
| --------------------- required by a bound in this function
12+
LL | where
13+
LL | Dst: BikeshedIntrinsicFrom<Src, Context, {
14+
| ______________^
15+
LL | | Assume {
16+
LL | | alignment: false,
17+
LL | | lifetimes: true,
18+
... |
19+
LL | | }
20+
LL | | }>
21+
| |__________^ required by this bound in `is_maybe_transmutable`
22+
help: consider removing the leading `&`-reference
23+
|
24+
LL - assert::is_maybe_transmutable::<&'static [u8; 0], &'static [u16; 0]>();
25+
LL + assert::is_maybe_transmutable::<&'static [u8; 0], [u16; 0]>();
26+
|
27+
28+
error: aborting due to previous error
29+
30+
For more information about this error, try `rustc --explain E0277`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// check-pass
2+
#![feature(transmutability)]
3+
4+
mod assert {
5+
use std::mem::{Assume, BikeshedIntrinsicFrom};
6+
pub struct Context;
7+
8+
pub fn is_maybe_transmutable<Src, Dst>()
9+
where
10+
Dst: BikeshedIntrinsicFrom<Src, Context, {
11+
Assume {
12+
alignment: false,
13+
lifetimes: false,
14+
safety: true,
15+
validity: false,
16+
}
17+
}>
18+
{}
19+
}
20+
21+
fn main() {
22+
assert::is_maybe_transmutable::<&'static [u16; 0], &'static [u8; 0]>();
23+
}

0 commit comments

Comments
 (0)