Skip to content

Commit

Permalink
Auto merge of #111684 - ChayimFriedman2:unused-offset-of, r=WaffleLapkin
Browse files Browse the repository at this point in the history
Warn on unused `offset_of!()` result

The usage of `core::hint::must_use()` means that we don't get a specialized message. I figured out that since there are plenty of other methods that just have `#[must_use]` with no message it'll be fine, but it is a bit unfortunate that the error mentions `must_use` and not `offset_of!`.

Fixes #111669.
  • Loading branch information
bors committed Jun 21, 2023
2 parents 536635c + 592844c commit 006a26c
Show file tree
Hide file tree
Showing 13 changed files with 333 additions and 135 deletions.
5 changes: 3 additions & 2 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,8 @@ impl<T> SizedTypeProperties for T {}
/// assert_eq!(mem::offset_of!(NestedA, b.0), 0);
/// ```
#[unstable(feature = "offset_of", issue = "106655")]
#[allow_internal_unstable(builtin_syntax)]
#[allow_internal_unstable(builtin_syntax, hint_must_use)]
pub macro offset_of($Container:ty, $($fields:tt).+ $(,)?) {
builtin # offset_of($Container, $($fields).+)
// The `{}` is for better error messages
crate::hint::must_use({builtin # offset_of($Container, $($fields).+)})
}
44 changes: 0 additions & 44 deletions tests/mir-opt/const_prop/offset_of.concrete.ConstProp.diff

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
- // MIR for `concrete` before ConstProp
+ // MIR for `concrete` after ConstProp

fn concrete() -> () {
let mut _0: ();
let _1: usize;
let mut _2: usize;
let mut _4: usize;
let mut _6: usize;
let mut _8: usize;
scope 1 {
debug x => _1;
let _3: usize;
scope 2 {
debug y => _3;
let _5: usize;
scope 3 {
debug z0 => _5;
let _7: usize;
scope 4 {
debug z1 => _7;
}
}
}
}

bb0: {
StorageLive(_1);
StorageLive(_2);
- _2 = OffsetOf(Alpha, [0]);
+ _2 = const 4_usize;
_1 = must_use::<usize>(move _2) -> [return: bb1, unwind unreachable];
}

bb1: {
StorageDead(_2);
StorageLive(_3);
StorageLive(_4);
- _4 = OffsetOf(Alpha, [1]);
+ _4 = const 0_usize;
_3 = must_use::<usize>(move _4) -> [return: bb2, unwind unreachable];
}

bb2: {
StorageDead(_4);
StorageLive(_5);
StorageLive(_6);
- _6 = OffsetOf(Alpha, [2, 0]);
+ _6 = const 2_usize;
_5 = must_use::<usize>(move _6) -> [return: bb3, unwind unreachable];
}

bb3: {
StorageDead(_6);
StorageLive(_7);
StorageLive(_8);
- _8 = OffsetOf(Alpha, [2, 1]);
+ _8 = const 3_usize;
_7 = must_use::<usize>(move _8) -> [return: bb4, unwind unreachable];
}

bb4: {
StorageDead(_8);
_0 = const ();
StorageDead(_7);
StorageDead(_5);
StorageDead(_3);
StorageDead(_1);
return;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
- // MIR for `concrete` before ConstProp
+ // MIR for `concrete` after ConstProp

fn concrete() -> () {
let mut _0: ();
let _1: usize;
let mut _2: usize;
let mut _4: usize;
let mut _6: usize;
let mut _8: usize;
scope 1 {
debug x => _1;
let _3: usize;
scope 2 {
debug y => _3;
let _5: usize;
scope 3 {
debug z0 => _5;
let _7: usize;
scope 4 {
debug z1 => _7;
}
}
}
}

bb0: {
StorageLive(_1);
StorageLive(_2);
- _2 = OffsetOf(Alpha, [0]);
+ _2 = const 4_usize;
_1 = must_use::<usize>(move _2) -> bb1;
}

bb1: {
StorageDead(_2);
StorageLive(_3);
StorageLive(_4);
- _4 = OffsetOf(Alpha, [1]);
+ _4 = const 0_usize;
_3 = must_use::<usize>(move _4) -> bb2;
}

bb2: {
StorageDead(_4);
StorageLive(_5);
StorageLive(_6);
- _6 = OffsetOf(Alpha, [2, 0]);
+ _6 = const 2_usize;
_5 = must_use::<usize>(move _6) -> bb3;
}

bb3: {
StorageDead(_6);
StorageLive(_7);
StorageLive(_8);
- _8 = OffsetOf(Alpha, [2, 1]);
+ _8 = const 3_usize;
_7 = must_use::<usize>(move _8) -> bb4;
}

bb4: {
StorageDead(_8);
_0 = const ();
StorageDead(_7);
StorageDead(_5);
StorageDead(_3);
StorageDead(_1);
return;
}
}

40 changes: 0 additions & 40 deletions tests/mir-opt/const_prop/offset_of.generic.ConstProp.diff

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
- // MIR for `generic` before ConstProp
+ // MIR for `generic` after ConstProp

fn generic() -> () {
let mut _0: ();
let _1: usize;
let mut _2: usize;
let mut _4: usize;
let mut _6: usize;
let mut _8: usize;
scope 1 {
debug gx => _1;
let _3: usize;
scope 2 {
debug gy => _3;
let _5: usize;
scope 3 {
debug dx => _5;
let _7: usize;
scope 4 {
debug dy => _7;
}
}
}
}

bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = OffsetOf(Gamma<T>, [0]);
_1 = must_use::<usize>(move _2) -> [return: bb1, unwind unreachable];
}

bb1: {
StorageDead(_2);
StorageLive(_3);
StorageLive(_4);
_4 = OffsetOf(Gamma<T>, [1]);
_3 = must_use::<usize>(move _4) -> [return: bb2, unwind unreachable];
}

bb2: {
StorageDead(_4);
StorageLive(_5);
StorageLive(_6);
_6 = OffsetOf(Delta<T>, [1]);
_5 = must_use::<usize>(move _6) -> [return: bb3, unwind unreachable];
}

bb3: {
StorageDead(_6);
StorageLive(_7);
StorageLive(_8);
_8 = OffsetOf(Delta<T>, [2]);
_7 = must_use::<usize>(move _8) -> [return: bb4, unwind unreachable];
}

bb4: {
StorageDead(_8);
_0 = const ();
StorageDead(_7);
StorageDead(_5);
StorageDead(_3);
StorageDead(_1);
return;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
- // MIR for `generic` before ConstProp
+ // MIR for `generic` after ConstProp

fn generic() -> () {
let mut _0: ();
let _1: usize;
let mut _2: usize;
let mut _4: usize;
let mut _6: usize;
let mut _8: usize;
scope 1 {
debug gx => _1;
let _3: usize;
scope 2 {
debug gy => _3;
let _5: usize;
scope 3 {
debug dx => _5;
let _7: usize;
scope 4 {
debug dy => _7;
}
}
}
}

bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = OffsetOf(Gamma<T>, [0]);
_1 = must_use::<usize>(move _2) -> bb1;
}

bb1: {
StorageDead(_2);
StorageLive(_3);
StorageLive(_4);
_4 = OffsetOf(Gamma<T>, [1]);
_3 = must_use::<usize>(move _4) -> bb2;
}

bb2: {
StorageDead(_4);
StorageLive(_5);
StorageLive(_6);
_6 = OffsetOf(Delta<T>, [1]);
_5 = must_use::<usize>(move _6) -> bb3;
}

bb3: {
StorageDead(_6);
StorageLive(_7);
StorageLive(_8);
_8 = OffsetOf(Delta<T>, [2]);
_7 = must_use::<usize>(move _8) -> bb4;
}

bb4: {
StorageDead(_8);
_0 = const ();
StorageDead(_7);
StorageDead(_5);
StorageDead(_3);
StorageDead(_1);
return;
}
}

1 change: 1 addition & 0 deletions tests/mir-opt/const_prop/offset_of.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// unit-test: ConstProp
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY

#![feature(offset_of)]

Expand Down
Loading

0 comments on commit 006a26c

Please sign in to comment.