From 592844cf88b586a87cddf877d8f45e6ffe2a2910 Mon Sep 17 00:00:00 2001 From: Chayim Refael Friedman Date: Thu, 8 Jun 2023 15:11:31 +0300 Subject: [PATCH] Warn on unused offset_of!() result --- library/core/src/mem/mod.rs | 5 +- .../offset_of.concrete.ConstProp.diff | 44 ------------ ...set_of.concrete.ConstProp.panic-abort.diff | 72 +++++++++++++++++++ ...et_of.concrete.ConstProp.panic-unwind.diff | 72 +++++++++++++++++++ .../offset_of.generic.ConstProp.diff | 40 ----------- ...fset_of.generic.ConstProp.panic-abort.diff | 68 ++++++++++++++++++ ...set_of.generic.ConstProp.panic-unwind.diff | 68 ++++++++++++++++++ tests/mir-opt/const_prop/offset_of.rs | 1 + tests/ui/offset-of/offset-of-dst-field.stderr | 28 ++++---- tests/ui/offset-of/offset-of-must-use.rs | 9 +++ tests/ui/offset-of/offset-of-must-use.stderr | 16 +++++ .../ui/offset-of/offset-of-output-type.stderr | 20 ++---- tests/ui/offset-of/offset-of-tuple.stderr | 25 ++----- 13 files changed, 333 insertions(+), 135 deletions(-) delete mode 100644 tests/mir-opt/const_prop/offset_of.concrete.ConstProp.diff create mode 100644 tests/mir-opt/const_prop/offset_of.concrete.ConstProp.panic-abort.diff create mode 100644 tests/mir-opt/const_prop/offset_of.concrete.ConstProp.panic-unwind.diff delete mode 100644 tests/mir-opt/const_prop/offset_of.generic.ConstProp.diff create mode 100644 tests/mir-opt/const_prop/offset_of.generic.ConstProp.panic-abort.diff create mode 100644 tests/mir-opt/const_prop/offset_of.generic.ConstProp.panic-unwind.diff create mode 100644 tests/ui/offset-of/offset-of-must-use.rs create mode 100644 tests/ui/offset-of/offset-of-must-use.stderr diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs index 39c9a04eea9..2fff3f0efd7 100644 --- a/library/core/src/mem/mod.rs +++ b/library/core/src/mem/mod.rs @@ -1317,7 +1317,8 @@ impl 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).+)}) } diff --git a/tests/mir-opt/const_prop/offset_of.concrete.ConstProp.diff b/tests/mir-opt/const_prop/offset_of.concrete.ConstProp.diff deleted file mode 100644 index b510cecd9e0..00000000000 --- a/tests/mir-opt/const_prop/offset_of.concrete.ConstProp.diff +++ /dev/null @@ -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; - } - } - diff --git a/tests/mir-opt/const_prop/offset_of.concrete.ConstProp.panic-abort.diff b/tests/mir-opt/const_prop/offset_of.concrete.ConstProp.panic-abort.diff new file mode 100644 index 00000000000..046a79b4bfb --- /dev/null +++ b/tests/mir-opt/const_prop/offset_of.concrete.ConstProp.panic-abort.diff @@ -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::(move _2) -> [return: bb1, unwind unreachable]; + } + + bb1: { + StorageDead(_2); + StorageLive(_3); + StorageLive(_4); +- _4 = OffsetOf(Alpha, [1]); ++ _4 = const 0_usize; + _3 = must_use::(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::(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::(move _8) -> [return: bb4, unwind unreachable]; + } + + bb4: { + StorageDead(_8); + _0 = const (); + StorageDead(_7); + StorageDead(_5); + StorageDead(_3); + StorageDead(_1); + return; + } + } + diff --git a/tests/mir-opt/const_prop/offset_of.concrete.ConstProp.panic-unwind.diff b/tests/mir-opt/const_prop/offset_of.concrete.ConstProp.panic-unwind.diff new file mode 100644 index 00000000000..29cad611b54 --- /dev/null +++ b/tests/mir-opt/const_prop/offset_of.concrete.ConstProp.panic-unwind.diff @@ -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::(move _2) -> bb1; + } + + bb1: { + StorageDead(_2); + StorageLive(_3); + StorageLive(_4); +- _4 = OffsetOf(Alpha, [1]); ++ _4 = const 0_usize; + _3 = must_use::(move _4) -> bb2; + } + + bb2: { + StorageDead(_4); + StorageLive(_5); + StorageLive(_6); +- _6 = OffsetOf(Alpha, [2, 0]); ++ _6 = const 2_usize; + _5 = must_use::(move _6) -> bb3; + } + + bb3: { + StorageDead(_6); + StorageLive(_7); + StorageLive(_8); +- _8 = OffsetOf(Alpha, [2, 1]); ++ _8 = const 3_usize; + _7 = must_use::(move _8) -> bb4; + } + + bb4: { + StorageDead(_8); + _0 = const (); + StorageDead(_7); + StorageDead(_5); + StorageDead(_3); + StorageDead(_1); + return; + } + } + diff --git a/tests/mir-opt/const_prop/offset_of.generic.ConstProp.diff b/tests/mir-opt/const_prop/offset_of.generic.ConstProp.diff deleted file mode 100644 index 8e0adb42755..00000000000 --- a/tests/mir-opt/const_prop/offset_of.generic.ConstProp.diff +++ /dev/null @@ -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, [0]); - StorageLive(_2); - _2 = OffsetOf(Gamma, [1]); - StorageLive(_3); - _3 = OffsetOf(Delta, [1]); - StorageLive(_4); - _4 = OffsetOf(Delta, [2]); - _0 = const (); - StorageDead(_4); - StorageDead(_3); - StorageDead(_2); - StorageDead(_1); - return; - } - } - diff --git a/tests/mir-opt/const_prop/offset_of.generic.ConstProp.panic-abort.diff b/tests/mir-opt/const_prop/offset_of.generic.ConstProp.panic-abort.diff new file mode 100644 index 00000000000..7519331f6d7 --- /dev/null +++ b/tests/mir-opt/const_prop/offset_of.generic.ConstProp.panic-abort.diff @@ -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, [0]); + _1 = must_use::(move _2) -> [return: bb1, unwind unreachable]; + } + + bb1: { + StorageDead(_2); + StorageLive(_3); + StorageLive(_4); + _4 = OffsetOf(Gamma, [1]); + _3 = must_use::(move _4) -> [return: bb2, unwind unreachable]; + } + + bb2: { + StorageDead(_4); + StorageLive(_5); + StorageLive(_6); + _6 = OffsetOf(Delta, [1]); + _5 = must_use::(move _6) -> [return: bb3, unwind unreachable]; + } + + bb3: { + StorageDead(_6); + StorageLive(_7); + StorageLive(_8); + _8 = OffsetOf(Delta, [2]); + _7 = must_use::(move _8) -> [return: bb4, unwind unreachable]; + } + + bb4: { + StorageDead(_8); + _0 = const (); + StorageDead(_7); + StorageDead(_5); + StorageDead(_3); + StorageDead(_1); + return; + } + } + diff --git a/tests/mir-opt/const_prop/offset_of.generic.ConstProp.panic-unwind.diff b/tests/mir-opt/const_prop/offset_of.generic.ConstProp.panic-unwind.diff new file mode 100644 index 00000000000..2a58a1a5ceb --- /dev/null +++ b/tests/mir-opt/const_prop/offset_of.generic.ConstProp.panic-unwind.diff @@ -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, [0]); + _1 = must_use::(move _2) -> bb1; + } + + bb1: { + StorageDead(_2); + StorageLive(_3); + StorageLive(_4); + _4 = OffsetOf(Gamma, [1]); + _3 = must_use::(move _4) -> bb2; + } + + bb2: { + StorageDead(_4); + StorageLive(_5); + StorageLive(_6); + _6 = OffsetOf(Delta, [1]); + _5 = must_use::(move _6) -> bb3; + } + + bb3: { + StorageDead(_6); + StorageLive(_7); + StorageLive(_8); + _8 = OffsetOf(Delta, [2]); + _7 = must_use::(move _8) -> bb4; + } + + bb4: { + StorageDead(_8); + _0 = const (); + StorageDead(_7); + StorageDead(_5); + StorageDead(_3); + StorageDead(_1); + return; + } + } + diff --git a/tests/mir-opt/const_prop/offset_of.rs b/tests/mir-opt/const_prop/offset_of.rs index 4cdcd28eeb2..164db59572b 100644 --- a/tests/mir-opt/const_prop/offset_of.rs +++ b/tests/mir-opt/const_prop/offset_of.rs @@ -1,4 +1,5 @@ // unit-test: ConstProp +// EMIT_MIR_FOR_EACH_PANIC_STRATEGY #![feature(offset_of)] diff --git a/tests/ui/offset-of/offset-of-dst-field.stderr b/tests/ui/offset-of/offset-of-dst-field.stderr index 3f613d947e4..658678dc4ed 100644 --- a/tests/ui/offset-of/offset-of-dst-field.stderr +++ b/tests/ui/offset-of/offset-of-dst-field.stderr @@ -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, 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, 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, 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 | diff --git a/tests/ui/offset-of/offset-of-must-use.rs b/tests/ui/offset-of/offset-of-must-use.rs new file mode 100644 index 00000000000..e30145d7a2f --- /dev/null +++ b/tests/ui/offset-of/offset-of-must-use.rs @@ -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 +} diff --git a/tests/ui/offset-of/offset-of-must-use.stderr b/tests/ui/offset-of/offset-of-must-use.stderr new file mode 100644 index 00000000000..5fe387a7255 --- /dev/null +++ b/tests/ui/offset-of/offset-of-must-use.stderr @@ -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 + diff --git a/tests/ui/offset-of/offset-of-output-type.stderr b/tests/ui/offset-of/offset-of-output-type.stderr index 6f8c9475029..6133f3263ab 100644 --- a/tests/ui/offset-of/offset-of-output-type.stderr +++ b/tests/ui/offset-of/offset-of-output-type.stderr @@ -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) diff --git a/tests/ui/offset-of/offset-of-tuple.stderr b/tests/ui/offset-of/offset-of-tuple.stderr index 954515f80a6..ed952345806 100644 --- a/tests/ui/offset-of/offset-of-tuple.stderr +++ b/tests/ui/offset-of/offset-of-tuple.stderr @@ -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)