Auto merge of #111684 - ChayimFriedman2:unused-offset-of, r=WaffleLapkin

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.
This commit is contained in:
bors 2023-06-21 16:40:54 +00:00
commit 006a26c0b5
13 changed files with 333 additions and 135 deletions

View File

@ -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).+)})
}

View File

@ -1,44 +0,0 @@
- // MIR for `concrete` before ConstProp
+ // MIR for `concrete` after ConstProp
fn concrete() -> () {
let mut _0: ();
let _1: usize;
scope 1 {
debug x => _1;
let _2: usize;
scope 2 {
debug y => _2;
let _3: usize;
scope 3 {
debug z0 => _3;
let _4: usize;
scope 4 {
debug z1 => _4;
}
}
}
}
bb0: {
StorageLive(_1);
- _1 = OffsetOf(Alpha, [0]);
+ _1 = const 4_usize;
StorageLive(_2);
- _2 = OffsetOf(Alpha, [1]);
+ _2 = const 0_usize;
StorageLive(_3);
- _3 = OffsetOf(Alpha, [2, 0]);
+ _3 = const 2_usize;
StorageLive(_4);
- _4 = OffsetOf(Alpha, [2, 1]);
+ _4 = const 3_usize;
_0 = const ();
StorageDead(_4);
StorageDead(_3);
StorageDead(_2);
StorageDead(_1);
return;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -1,40 +0,0 @@
- // MIR for `generic` before ConstProp
+ // MIR for `generic` after ConstProp
fn generic() -> () {
let mut _0: ();
let _1: usize;
scope 1 {
debug gx => _1;
let _2: usize;
scope 2 {
debug gy => _2;
let _3: usize;
scope 3 {
debug dx => _3;
let _4: usize;
scope 4 {
debug dy => _4;
}
}
}
}
bb0: {
StorageLive(_1);
_1 = OffsetOf(Gamma<T>, [0]);
StorageLive(_2);
_2 = OffsetOf(Gamma<T>, [1]);
StorageLive(_3);
_3 = OffsetOf(Delta<T>, [1]);
StorageLive(_4);
_4 = OffsetOf(Delta<T>, [2]);
_0 = const ();
StorageDead(_4);
StorageDead(_3);
StorageDead(_2);
StorageDead(_1);
return;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -1,4 +1,5 @@
// unit-test: ConstProp
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
#![feature(offset_of)]

View File

@ -34,6 +34,20 @@ LL | offset_of!((u8, dyn Trait), 1);
= help: the trait `Sized` is not implemented for `dyn Trait`
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:44:5
|
LL | offset_of!(Delta<Alpha>, z);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: within `Alpha`, the trait `Sized` is not implemented for `[u8]`
note: required because it appears within the type `Alpha`
--> $DIR/offset-of-dst-field.rs:5:8
|
LL | struct Alpha {
| ^^^^^
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the size for values of type `Extern` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:45:5
|
@ -52,20 +66,6 @@ LL | offset_of!(Delta<dyn Trait>, z);
= help: the trait `Sized` is not implemented for `dyn Trait`
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:44:5
|
LL | offset_of!(Delta<Alpha>, z);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: within `Alpha`, the trait `Sized` is not implemented for `[u8]`
note: required because it appears within the type `Alpha`
--> $DIR/offset-of-dst-field.rs:5:8
|
LL | struct Alpha {
| ^^^^^
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the size for values of type `T` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:50:5
|

View File

@ -0,0 +1,9 @@
// check-pass
#![feature(offset_of)]
#![warn(unused)]
fn main() {
core::mem::offset_of!((String,), 0);
//~^ WARN unused return value of `must_use` that must be used
}

View File

@ -0,0 +1,16 @@
warning: unused return value of `must_use` that must be used
--> $DIR/offset-of-must-use.rs:7:5
|
LL | core::mem::offset_of!((String,), 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/offset-of-must-use.rs:4:9
|
LL | #![warn(unused)]
| ^^^^^^
= note: `#[warn(unused_must_use)]` implied by `#[warn(unused)]`
= note: this warning originates in the macro `core::mem::offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
warning: 1 warning emitted

View File

@ -2,9 +2,7 @@ error[E0308]: mismatched types
--> $DIR/offset-of-output-type.rs:12:17
|
LL | let _: u8 = offset_of!(S, v);
| -- ^^^^^^^^^^^^^^^^ expected `u8`, found `usize`
| |
| expected due to this
| ^^^^^^^^^^^^^^^^ expected `u8`, found `usize`
|
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
@ -12,9 +10,7 @@ error[E0308]: mismatched types
--> $DIR/offset-of-output-type.rs:13:18
|
LL | let _: u16 = offset_of!(S, v);
| --- ^^^^^^^^^^^^^^^^ expected `u16`, found `usize`
| |
| expected due to this
| ^^^^^^^^^^^^^^^^ expected `u16`, found `usize`
|
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
@ -22,9 +18,7 @@ error[E0308]: mismatched types
--> $DIR/offset-of-output-type.rs:14:18
|
LL | let _: u32 = offset_of!(S, v);
| --- ^^^^^^^^^^^^^^^^ expected `u32`, found `usize`
| |
| expected due to this
| ^^^^^^^^^^^^^^^^ expected `u32`, found `usize`
|
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
@ -32,9 +26,7 @@ error[E0308]: mismatched types
--> $DIR/offset-of-output-type.rs:15:18
|
LL | let _: u64 = offset_of!(S, v);
| --- ^^^^^^^^^^^^^^^^ expected `u64`, found `usize`
| |
| expected due to this
| ^^^^^^^^^^^^^^^^ expected `u64`, found `usize`
|
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
@ -42,9 +34,7 @@ error[E0308]: mismatched types
--> $DIR/offset-of-output-type.rs:16:20
|
LL | let _: isize = offset_of!(S, v);
| ----- ^^^^^^^^^^^^^^^^ expected `isize`, found `usize`
| |
| expected due to this
| ^^^^^^^^^^^^^^^^ expected `isize`, found `usize`
|
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)

View File

@ -90,10 +90,7 @@ error: expected identifier, found `)`
--> $DIR/offset-of-tuple.rs:14:5
|
LL | offset_of!((u8, u8), 1.);
| ^^^^^^^^^^^^^^^^^^^^^^^^
| |
| expected identifier
| in this macro invocation
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected identifier
|
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
@ -110,10 +107,7 @@ error: expected identifier, found `)`
--> $DIR/offset-of-tuple.rs:36:5
|
LL | offset_of!(ComplexTup, 0.0.1.);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| expected identifier
| in this macro invocation
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected identifier
|
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
@ -147,10 +141,7 @@ error: expected identifier, found `)`
--> $DIR/offset-of-tuple.rs:40:5
|
LL | offset_of!(ComplexTup, 0.0 .1.);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| expected identifier
| in this macro invocation
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected identifier
|
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
@ -158,10 +149,7 @@ error: expected identifier, found `)`
--> $DIR/offset-of-tuple.rs:41:5
|
LL | offset_of!(ComplexTup, 0.0 . 1.);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| expected identifier
| in this macro invocation
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected identifier
|
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
@ -169,10 +157,7 @@ error: expected identifier, found `)`
--> $DIR/offset-of-tuple.rs:42:5
|
LL | offset_of!(ComplexTup, 0.0. 1.);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| expected identifier
| in this macro invocation
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected identifier
|
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)