Skip to content

Commit 63d04de

Browse files
committed
add some more repr(C) enum size tests
1 parent 9e6ba08 commit 63d04de

File tree

5 files changed

+109
-3
lines changed

5 files changed

+109
-3
lines changed

tests/auxiliary/minicore.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,29 @@ macro_rules! stringify {
162162
};
163163
}
164164

165+
#[lang = "neg"]
166+
pub trait Neg {
167+
type Output;
168+
169+
fn neg(self) -> Self::Output;
170+
}
171+
172+
impl Neg for i32 {
173+
type Output = i32;
174+
175+
fn neg(self) -> i32 {
176+
loop {} // Dummy impl, not actually used
177+
}
178+
}
179+
180+
impl Neg for isize {
181+
type Output = isize;
182+
183+
fn neg(self) -> isize {
184+
loop {} // Dummy impl, not actually used
185+
}
186+
}
187+
165188
#[lang = "add"]
166189
pub trait Add<Rhs = Self> {
167190
type Output;

tests/ui/enum-discriminant/repr-c-size.linux32.stderr

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,18 @@ note: the lint level is defined here
1111
LL | #[warn(overflowing_literals)]
1212
| ^^^^^^^^^^^^^^^^^^^^
1313

14-
warning: 1 warning emitted
14+
warning: literal out of range for `isize`
15+
--> $DIR/repr-c-size.rs:43:9
16+
|
17+
LL | A = 4294967294, // u32::MAX - 1
18+
| ^^^^^^^^^^
19+
|
20+
= note: the literal `4294967294` does not fit into the type `isize` whose range is `-2147483648..=2147483647`
21+
note: the lint level is defined here
22+
--> $DIR/repr-c-size.rs:41:8
23+
|
24+
LL | #[warn(overflowing_literals)]
25+
| ^^^^^^^^^^^^^^^^^^^^
26+
27+
warning: 2 warnings emitted
1528

tests/ui/enum-discriminant/repr-c-size.msvc32.stderr

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,19 @@ note: the lint level is defined here
1212
LL | #[warn(overflowing_literals)]
1313
| ^^^^^^^^^^^^^^^^^^^^
1414

15-
warning: 1 warning emitted
15+
warning: literal out of range for `i32`
16+
--> $DIR/repr-c-size.rs:43:9
17+
|
18+
LL | A = 4294967294, // u32::MAX - 1
19+
| ^^^^^^^^^^
20+
|
21+
= note: the literal `4294967294` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
22+
= help: consider using the type `u32` instead
23+
note: the lint level is defined here
24+
--> $DIR/repr-c-size.rs:41:8
25+
|
26+
LL | #[warn(overflowing_literals)]
27+
| ^^^^^^^^^^^^^^^^^^^^
28+
29+
warning: 2 warnings emitted
1630

tests/ui/enum-discriminant/repr-c-size.msvc64.stderr

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,19 @@ note: the lint level is defined here
1212
LL | #[warn(overflowing_literals)]
1313
| ^^^^^^^^^^^^^^^^^^^^
1414

15-
warning: 1 warning emitted
15+
warning: literal out of range for `i32`
16+
--> $DIR/repr-c-size.rs:43:9
17+
|
18+
LL | A = 4294967294, // u32::MAX - 1
19+
| ^^^^^^^^^^
20+
|
21+
= note: the literal `4294967294` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
22+
= help: consider using the type `u32` instead
23+
note: the lint level is defined here
24+
--> $DIR/repr-c-size.rs:41:8
25+
|
26+
LL | #[warn(overflowing_literals)]
27+
| ^^^^^^^^^^^^^^^^^^^^
28+
29+
warning: 2 warnings emitted
1630

tests/ui/enum-discriminant/repr-c-size.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,46 @@ const _: () = if mem::size_of::<OverflowingEnum>() != 8 {
3434
unsafe { hint::unreachable() }
3535
};
3636

37+
// Each value fits in i32 or u32, but not all values fit into the same type.
38+
// C compiler demo: <https://godbolt.org/z/GPYafPhjK>
39+
// FIXME: This seems to do the wrong thing for 32bit Linux?
40+
#[repr(C)]
41+
#[warn(overflowing_literals)]
42+
enum OverflowingEnum2 {
43+
A = 4294967294, // u32::MAX - 1
44+
//[linux32,msvc32,msvc64]~^ WARN: literal out of range
45+
B = -1,
46+
}
47+
48+
#[cfg(not(linux64))]
49+
const _: () = if mem::size_of::<OverflowingEnum2>() != 4 {
50+
unsafe { hint::unreachable() }
51+
};
52+
#[cfg(linux64)]
53+
const _: () = if mem::size_of::<OverflowingEnum2>() != 8 {
54+
unsafe { hint::unreachable() }
55+
};
56+
57+
// Force i32 or u32, respectively.
58+
// C compiler demo: <https://godbolt.org/z/bGss855a4>
59+
#[repr(C)]
60+
enum I32Enum {
61+
A = 2147483647, // i32::MAX
62+
B = -2147483647,
63+
}
64+
const _: () = if mem::size_of::<I32Enum>() != 4 {
65+
unsafe { hint::unreachable() }
66+
};
67+
68+
// C compiler demo: <https://godbolt.org/z/MeE9YGj4n>
69+
#[repr(C)]
70+
#[allow(overflowing_literals)]
71+
enum U32Enum {
72+
A = 4294967295, // u32::MAX
73+
B = 0,
74+
}
75+
const _: () = if mem::size_of::<U32Enum>() != 4 {
76+
unsafe { hint::unreachable() }
77+
};
78+
3779
fn main() {}

0 commit comments

Comments
 (0)