Skip to content

Commit 592844c

Browse files
Warn on unused offset_of!() result
1 parent d7dcadc commit 592844c

13 files changed

+333
-135
lines changed

library/core/src/mem/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,8 @@ impl<T> SizedTypeProperties for T {}
13171317
/// assert_eq!(mem::offset_of!(NestedA, b.0), 0);
13181318
/// ```
13191319
#[unstable(feature = "offset_of", issue = "106655")]
1320-
#[allow_internal_unstable(builtin_syntax)]
1320+
#[allow_internal_unstable(builtin_syntax, hint_must_use)]
13211321
pub macro offset_of($Container:ty, $($fields:tt).+ $(,)?) {
1322-
builtin # offset_of($Container, $($fields).+)
1322+
// The `{}` is for better error messages
1323+
crate::hint::must_use({builtin # offset_of($Container, $($fields).+)})
13231324
}

tests/mir-opt/const_prop/offset_of.concrete.ConstProp.diff

-44
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
- // MIR for `concrete` before ConstProp
2+
+ // MIR for `concrete` after ConstProp
3+
4+
fn concrete() -> () {
5+
let mut _0: ();
6+
let _1: usize;
7+
let mut _2: usize;
8+
let mut _4: usize;
9+
let mut _6: usize;
10+
let mut _8: usize;
11+
scope 1 {
12+
debug x => _1;
13+
let _3: usize;
14+
scope 2 {
15+
debug y => _3;
16+
let _5: usize;
17+
scope 3 {
18+
debug z0 => _5;
19+
let _7: usize;
20+
scope 4 {
21+
debug z1 => _7;
22+
}
23+
}
24+
}
25+
}
26+
27+
bb0: {
28+
StorageLive(_1);
29+
StorageLive(_2);
30+
- _2 = OffsetOf(Alpha, [0]);
31+
+ _2 = const 4_usize;
32+
_1 = must_use::<usize>(move _2) -> [return: bb1, unwind unreachable];
33+
}
34+
35+
bb1: {
36+
StorageDead(_2);
37+
StorageLive(_3);
38+
StorageLive(_4);
39+
- _4 = OffsetOf(Alpha, [1]);
40+
+ _4 = const 0_usize;
41+
_3 = must_use::<usize>(move _4) -> [return: bb2, unwind unreachable];
42+
}
43+
44+
bb2: {
45+
StorageDead(_4);
46+
StorageLive(_5);
47+
StorageLive(_6);
48+
- _6 = OffsetOf(Alpha, [2, 0]);
49+
+ _6 = const 2_usize;
50+
_5 = must_use::<usize>(move _6) -> [return: bb3, unwind unreachable];
51+
}
52+
53+
bb3: {
54+
StorageDead(_6);
55+
StorageLive(_7);
56+
StorageLive(_8);
57+
- _8 = OffsetOf(Alpha, [2, 1]);
58+
+ _8 = const 3_usize;
59+
_7 = must_use::<usize>(move _8) -> [return: bb4, unwind unreachable];
60+
}
61+
62+
bb4: {
63+
StorageDead(_8);
64+
_0 = const ();
65+
StorageDead(_7);
66+
StorageDead(_5);
67+
StorageDead(_3);
68+
StorageDead(_1);
69+
return;
70+
}
71+
}
72+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
- // MIR for `concrete` before ConstProp
2+
+ // MIR for `concrete` after ConstProp
3+
4+
fn concrete() -> () {
5+
let mut _0: ();
6+
let _1: usize;
7+
let mut _2: usize;
8+
let mut _4: usize;
9+
let mut _6: usize;
10+
let mut _8: usize;
11+
scope 1 {
12+
debug x => _1;
13+
let _3: usize;
14+
scope 2 {
15+
debug y => _3;
16+
let _5: usize;
17+
scope 3 {
18+
debug z0 => _5;
19+
let _7: usize;
20+
scope 4 {
21+
debug z1 => _7;
22+
}
23+
}
24+
}
25+
}
26+
27+
bb0: {
28+
StorageLive(_1);
29+
StorageLive(_2);
30+
- _2 = OffsetOf(Alpha, [0]);
31+
+ _2 = const 4_usize;
32+
_1 = must_use::<usize>(move _2) -> bb1;
33+
}
34+
35+
bb1: {
36+
StorageDead(_2);
37+
StorageLive(_3);
38+
StorageLive(_4);
39+
- _4 = OffsetOf(Alpha, [1]);
40+
+ _4 = const 0_usize;
41+
_3 = must_use::<usize>(move _4) -> bb2;
42+
}
43+
44+
bb2: {
45+
StorageDead(_4);
46+
StorageLive(_5);
47+
StorageLive(_6);
48+
- _6 = OffsetOf(Alpha, [2, 0]);
49+
+ _6 = const 2_usize;
50+
_5 = must_use::<usize>(move _6) -> bb3;
51+
}
52+
53+
bb3: {
54+
StorageDead(_6);
55+
StorageLive(_7);
56+
StorageLive(_8);
57+
- _8 = OffsetOf(Alpha, [2, 1]);
58+
+ _8 = const 3_usize;
59+
_7 = must_use::<usize>(move _8) -> bb4;
60+
}
61+
62+
bb4: {
63+
StorageDead(_8);
64+
_0 = const ();
65+
StorageDead(_7);
66+
StorageDead(_5);
67+
StorageDead(_3);
68+
StorageDead(_1);
69+
return;
70+
}
71+
}
72+

tests/mir-opt/const_prop/offset_of.generic.ConstProp.diff

-40
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
- // MIR for `generic` before ConstProp
2+
+ // MIR for `generic` after ConstProp
3+
4+
fn generic() -> () {
5+
let mut _0: ();
6+
let _1: usize;
7+
let mut _2: usize;
8+
let mut _4: usize;
9+
let mut _6: usize;
10+
let mut _8: usize;
11+
scope 1 {
12+
debug gx => _1;
13+
let _3: usize;
14+
scope 2 {
15+
debug gy => _3;
16+
let _5: usize;
17+
scope 3 {
18+
debug dx => _5;
19+
let _7: usize;
20+
scope 4 {
21+
debug dy => _7;
22+
}
23+
}
24+
}
25+
}
26+
27+
bb0: {
28+
StorageLive(_1);
29+
StorageLive(_2);
30+
_2 = OffsetOf(Gamma<T>, [0]);
31+
_1 = must_use::<usize>(move _2) -> [return: bb1, unwind unreachable];
32+
}
33+
34+
bb1: {
35+
StorageDead(_2);
36+
StorageLive(_3);
37+
StorageLive(_4);
38+
_4 = OffsetOf(Gamma<T>, [1]);
39+
_3 = must_use::<usize>(move _4) -> [return: bb2, unwind unreachable];
40+
}
41+
42+
bb2: {
43+
StorageDead(_4);
44+
StorageLive(_5);
45+
StorageLive(_6);
46+
_6 = OffsetOf(Delta<T>, [1]);
47+
_5 = must_use::<usize>(move _6) -> [return: bb3, unwind unreachable];
48+
}
49+
50+
bb3: {
51+
StorageDead(_6);
52+
StorageLive(_7);
53+
StorageLive(_8);
54+
_8 = OffsetOf(Delta<T>, [2]);
55+
_7 = must_use::<usize>(move _8) -> [return: bb4, unwind unreachable];
56+
}
57+
58+
bb4: {
59+
StorageDead(_8);
60+
_0 = const ();
61+
StorageDead(_7);
62+
StorageDead(_5);
63+
StorageDead(_3);
64+
StorageDead(_1);
65+
return;
66+
}
67+
}
68+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
- // MIR for `generic` before ConstProp
2+
+ // MIR for `generic` after ConstProp
3+
4+
fn generic() -> () {
5+
let mut _0: ();
6+
let _1: usize;
7+
let mut _2: usize;
8+
let mut _4: usize;
9+
let mut _6: usize;
10+
let mut _8: usize;
11+
scope 1 {
12+
debug gx => _1;
13+
let _3: usize;
14+
scope 2 {
15+
debug gy => _3;
16+
let _5: usize;
17+
scope 3 {
18+
debug dx => _5;
19+
let _7: usize;
20+
scope 4 {
21+
debug dy => _7;
22+
}
23+
}
24+
}
25+
}
26+
27+
bb0: {
28+
StorageLive(_1);
29+
StorageLive(_2);
30+
_2 = OffsetOf(Gamma<T>, [0]);
31+
_1 = must_use::<usize>(move _2) -> bb1;
32+
}
33+
34+
bb1: {
35+
StorageDead(_2);
36+
StorageLive(_3);
37+
StorageLive(_4);
38+
_4 = OffsetOf(Gamma<T>, [1]);
39+
_3 = must_use::<usize>(move _4) -> bb2;
40+
}
41+
42+
bb2: {
43+
StorageDead(_4);
44+
StorageLive(_5);
45+
StorageLive(_6);
46+
_6 = OffsetOf(Delta<T>, [1]);
47+
_5 = must_use::<usize>(move _6) -> bb3;
48+
}
49+
50+
bb3: {
51+
StorageDead(_6);
52+
StorageLive(_7);
53+
StorageLive(_8);
54+
_8 = OffsetOf(Delta<T>, [2]);
55+
_7 = must_use::<usize>(move _8) -> bb4;
56+
}
57+
58+
bb4: {
59+
StorageDead(_8);
60+
_0 = const ();
61+
StorageDead(_7);
62+
StorageDead(_5);
63+
StorageDead(_3);
64+
StorageDead(_1);
65+
return;
66+
}
67+
}
68+

tests/mir-opt/const_prop/offset_of.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// unit-test: ConstProp
2+
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
23

34
#![feature(offset_of)]
45

0 commit comments

Comments
 (0)