Debian Codegraph @

gcc/testsuite/rust/compile/const-issue1440.rs

raw
1 // { dg-additional-options "-w" }
2 #![feature(intrinsics)]
3
4 #[lang = "sized"]
5 pub trait Sized {}
6
7 mod intrinsics {
8 extern "rust-intrinsic" {
9 pub fn wrapping_add<T>(a: T, b: T) -> T;
10 pub fn rotate_left<T>(a: T, b: T) -> T;
11 pub fn rotate_right<T>(a: T, b: T) -> T;
12 pub fn offset<T>(ptr: *const T, count: isize) -> *const T;
13 }
14 }
15
16 mod mem {
17 extern "rust-intrinsic" {
18 #[rustc_const_stable(feature = "const_transmute", since = "1.46.0")]
19 pub fn transmute<T, U>(_: T) -> U;
20 pub fn size_of<T>() -> usize;
21 }
22 }
23
24 macro_rules! impl_uint {
25 ($($ty:ident = $lang:literal),*) => {
26 $(
27 impl $ty {
28 pub fn wrapping_add(self, rhs: Self) -> Self {
29 // intrinsics::wrapping_add(self, rhs)
30 self + rhs
31 }
32
33 pub fn rotate_left(self, n: u32) -> Self {
34 unsafe {
35 intrinsics::rotate_left(self, n as Self)
36 }
37 }
38
39 pub fn rotate_right(self, n: u32) -> Self {
40 unsafe {
41 intrinsics::rotate_right(self, n as Self)
42 }
43 }
44
45 pub fn to_le(self) -> Self {
46 {
47 self
48 }
49 }
50
51 pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
52 // { dg-error "only functions marked as .const. are allowed to be called from constant contexts" "" { target *-*-* } .-1 }
53 Self::from_le(Self::from_ne_bytes(bytes))
54 }
55
56 pub const fn from_le(x: Self) -> Self {
57 #[cfg(target_endian = "little")]
58 {
59 x
60 }
61 }
62
63 pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
64 // { dg-error "only functions marked as .const. are allowed to be called from constant contexts" "" { target *-*-* } .-1 }
65 unsafe { mem::transmute(bytes) }
66 }
67 }
68 )*
69 }
70 }
71
72 impl_uint!(
73 u8 = "u8",
74 u16 = "u16",
75 u32 = "u32",
76 u64 = "u64",
77 u128 = "u128",
78 usize = "usize"
79 );