Skip to content

Commit 669bb03

Browse files
committed
add some more repr(C) enum size tests
1 parent d0153cf commit 669bb03

File tree

5 files changed

+106
-3
lines changed

5 files changed

+106
-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:40: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:38: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:40: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:38: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:40: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:38: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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,43 @@ const _: () = if mem::size_of::<OverflowingEnum>() != 8 {
3232
unsafe { hint::unreachable() }
3333
};
3434

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

0 commit comments

Comments
 (0)