Skip to content

Commit 82d30ee

Browse files
committed
Add/extend tests for offset_of! for enums with uninhabited variants
1 parent 9608760 commit 82d30ee

File tree

3 files changed

+81
-7
lines changed

3 files changed

+81
-7
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//@ run-pass
2+
#![feature(offset_of_enum)]
3+
#![allow(unused)]
4+
5+
use std::mem::offset_of;
6+
7+
enum Never {}
8+
9+
#[repr(align(2))]
10+
struct AlignedNever(Never);
11+
12+
enum Alpha {
13+
One(u8),
14+
Two(u8),
15+
Three(u8, u8, Never),
16+
}
17+
18+
enum Beta {
19+
One(u8),
20+
Two(u8, Never),
21+
}
22+
23+
enum Gamma {
24+
One(u32),
25+
Two(u8, u8, u8, Never),
26+
}
27+
28+
enum Delta {
29+
One(u8, Never),
30+
Two(u8, u8, Never),
31+
}
32+
33+
fn main() {
34+
assert!(offset_of!(Alpha, One.0) <= size_of::<Alpha>() - size_of::<u8>());
35+
assert!(offset_of!(Alpha, Two.0) <= size_of::<Alpha>() - size_of::<u8>());
36+
assert!(offset_of!(Alpha, Three.0) <= size_of::<Alpha>() - size_of::<u8>());
37+
assert!(offset_of!(Alpha, Three.1) <= size_of::<Alpha>() - size_of::<u8>());
38+
assert!(offset_of!(Alpha, Three.2) <= size_of::<Alpha>() - size_of::<Never>());
39+
assert!(offset_of!(Alpha, Three.0) != offset_of!(Alpha, Three.1));
40+
41+
assert!(offset_of!(Beta, One.0) <= size_of::<Beta>() - size_of::<u8>());
42+
assert!(offset_of!(Beta, Two.0) <= size_of::<Beta>() - size_of::<u8>());
43+
assert!(offset_of!(Beta, Two.1) <= size_of::<Beta>() - size_of::<Never>());
44+
45+
assert!(offset_of!(Gamma, One.0) <= size_of::<Gamma>() - size_of::<u32>());
46+
assert!(offset_of!(Gamma, Two.0) <= size_of::<Gamma>() - size_of::<u8>());
47+
assert!(offset_of!(Gamma, Two.1) <= size_of::<Gamma>() - size_of::<u8>());
48+
assert!(offset_of!(Gamma, Two.2) <= size_of::<Gamma>() - size_of::<u8>());
49+
assert!(offset_of!(Gamma, Two.3) <= size_of::<Gamma>() - size_of::<Never>());
50+
assert!(offset_of!(Gamma, Two.0) != offset_of!(Gamma, Two.1));
51+
assert!(offset_of!(Gamma, Two.0) != offset_of!(Gamma, Two.2));
52+
assert!(offset_of!(Gamma, Two.1) != offset_of!(Gamma, Two.2));
53+
54+
assert!(offset_of!(Delta, One.0) <= size_of::<Delta>() - size_of::<u8>());
55+
assert!(offset_of!(Delta, One.1) <= size_of::<Delta>() - size_of::<Never>());
56+
assert!(offset_of!(Delta, Two.0) <= size_of::<Delta>() - size_of::<u8>());
57+
assert!(offset_of!(Delta, Two.0) <= size_of::<Delta>() - size_of::<u8>());
58+
assert!(offset_of!(Delta, Two.1) <= size_of::<Delta>() - size_of::<u8>());
59+
assert!(offset_of!(Delta, Two.2) <= size_of::<Delta>() - size_of::<Never>());
60+
assert!(offset_of!(Delta, Two.0) != offset_of!(Delta, Two.1));
61+
}

tests/ui/offset-of/offset-of-enum.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
use std::mem::offset_of;
44

5+
enum Never {}
6+
57
enum Alpha {
68
One(u8),
79
Two(u8),
10+
Three(u8, u8, Never),
811
}
912

1013
fn main() {
@@ -15,4 +18,8 @@ fn main() {
1518
offset_of!(Alpha, Two.foo); //~ ERROR no field named `foo` on enum variant `Alpha::Two`
1619
offset_of!(Alpha, NonExistent); //~ ERROR no variant named `NonExistent` found for enum `Alpha`
1720
offset_of!(Beta, One); //~ ERROR cannot find type `Beta` in this scope
21+
offset_of!(Alpha, Three.0);
22+
offset_of!(Alpha, Three.1);
23+
offset_of!(Alpha, Three.2);
24+
offset_of!(Alpha, Three.2.NonExistent); //~ ERROR no variant named `NonExistent` found for enum `Never`
1825
}
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0573]: expected type, found variant `Alpha::One`
2-
--> $DIR/offset-of-enum.rs:11:16
2+
--> $DIR/offset-of-enum.rs:14:16
33
|
44
LL | offset_of!(Alpha::One, 0);
55
| ^^^^^^^^^^
@@ -8,40 +8,46 @@ LL | offset_of!(Alpha::One, 0);
88
| help: try using the variant's enum: `Alpha`
99

1010
error[E0412]: cannot find type `Beta` in this scope
11-
--> $DIR/offset-of-enum.rs:17:16
11+
--> $DIR/offset-of-enum.rs:20:16
1212
|
1313
LL | offset_of!(Beta, One);
1414
| ^^^^ not found in this scope
1515

1616
error[E0795]: `One` is an enum variant; expected field at end of `offset_of`
17-
--> $DIR/offset-of-enum.rs:12:23
17+
--> $DIR/offset-of-enum.rs:15:23
1818
|
1919
LL | offset_of!(Alpha, One);
2020
| ^^^ enum variant
2121

2222
error[E0609]: no field named `1` on enum variant `Alpha::Two`
23-
--> $DIR/offset-of-enum.rs:14:23
23+
--> $DIR/offset-of-enum.rs:17:23
2424
|
2525
LL | offset_of!(Alpha, Two.1);
2626
| ^^^ - ...does not have this field
2727
| |
2828
| this enum variant...
2929

3030
error[E0609]: no field named `foo` on enum variant `Alpha::Two`
31-
--> $DIR/offset-of-enum.rs:15:23
31+
--> $DIR/offset-of-enum.rs:18:23
3232
|
3333
LL | offset_of!(Alpha, Two.foo);
3434
| ^^^ --- ...does not have this field
3535
| |
3636
| this enum variant...
3737

3838
error[E0599]: no variant named `NonExistent` found for enum `Alpha`
39-
--> $DIR/offset-of-enum.rs:16:23
39+
--> $DIR/offset-of-enum.rs:19:23
4040
|
4141
LL | offset_of!(Alpha, NonExistent);
4242
| ^^^^^^^^^^^ variant not found
4343

44-
error: aborting due to 6 previous errors
44+
error[E0599]: no variant named `NonExistent` found for enum `Never`
45+
--> $DIR/offset-of-enum.rs:24:31
46+
|
47+
LL | offset_of!(Alpha, Three.2.NonExistent);
48+
| ^^^^^^^^^^^ variant not found
49+
50+
error: aborting due to 7 previous errors
4551

4652
Some errors have detailed explanations: E0412, E0573, E0599, E0609, E0795.
4753
For more information about an error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)