mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-07 12:33:14 +00:00
Fix #124478 - offset_of! returns a temporary
This was due to the must_use() call. Adding HIR's OffsetOf to the must_use checking within the compiler avoids this issue.
This commit is contained in:
parent
6c90ac8d8f
commit
ca79086c87
@ -176,6 +176,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
|
||||
| hir::BinOpKind::Shr => Some("bitwise operation"),
|
||||
},
|
||||
hir::ExprKind::AddrOf(..) => Some("borrow"),
|
||||
hir::ExprKind::OffsetOf(..) => Some("`offset_of` call"),
|
||||
hir::ExprKind::Unary(..) => Some("unary operation"),
|
||||
_ => None,
|
||||
};
|
||||
|
@ -1340,8 +1340,8 @@ impl<T> SizedTypeProperties for T {}
|
||||
/// assert_eq!(mem::offset_of!(Option<&u8>, Some.0), 0);
|
||||
/// ```
|
||||
#[stable(feature = "offset_of", since = "1.77.0")]
|
||||
#[allow_internal_unstable(builtin_syntax, hint_must_use)]
|
||||
#[allow_internal_unstable(builtin_syntax)]
|
||||
pub macro offset_of($Container:ty, $($fields:expr)+ $(,)?) {
|
||||
// The `{}` is for better error messages
|
||||
crate::hint::must_use({builtin # offset_of($Container, $($fields)+)})
|
||||
{builtin # offset_of($Container, $($fields)+)}
|
||||
}
|
||||
|
@ -34,20 +34,6 @@ 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]`, which is required by `Alpha: Sized`
|
||||
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
|
||||
|
|
||||
@ -66,6 +52,20 @@ 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]`, which is required by `Alpha: Sized`
|
||||
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
|
||||
|
|
||||
|
@ -4,5 +4,5 @@
|
||||
|
||||
fn main() {
|
||||
core::mem::offset_of!((String,), 0);
|
||||
//~^ WARN unused return value of `must_use` that must be used
|
||||
//~^ WARN unused `offset_of` call that must be used
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
warning: unused return value of `must_use` that must be used
|
||||
warning: unused `offset_of` call that must be used
|
||||
--> $DIR/offset-of-must-use.rs:6:5
|
||||
|
|
||||
LL | core::mem::offset_of!((String,), 0);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `offset_of` call produces a value
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/offset-of-must-use.rs:3:9
|
||||
|
@ -23,8 +23,9 @@ impl S {
|
||||
fn offs_in_c() -> usize {
|
||||
offset_of!(C<Self>, w)
|
||||
}
|
||||
fn offs_in_c_colon() -> usize {
|
||||
offset_of!(C::<Self>, w)
|
||||
// Put offset_of in a slice - test #124478.
|
||||
fn offs_in_c_colon() -> &'static [usize] {
|
||||
&[offset_of!(C::<Self>, w)]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,7 @@ LL | offset_of!(Self, Self::v);
|
||||
| ^^^^^^^
|
||||
|
||||
error[E0412]: cannot find type `S` in module `self`
|
||||
--> $DIR/offset-of-self.rs:34:26
|
||||
--> $DIR/offset-of-self.rs:35:26
|
||||
|
|
||||
LL | offset_of!(self::S, v);
|
||||
| ^ not found in `self`
|
||||
@ -21,7 +21,7 @@ LL + offset_of!(S, v);
|
||||
|
|
||||
|
||||
error[E0411]: cannot find type `Self` in this scope
|
||||
--> $DIR/offset-of-self.rs:51:16
|
||||
--> $DIR/offset-of-self.rs:52:16
|
||||
|
|
||||
LL | fn main() {
|
||||
| ---- `Self` not allowed in a function
|
||||
@ -38,13 +38,13 @@ LL | offset_of!(S, Self);
|
||||
= note: available fields are: `v`, `w`
|
||||
|
||||
error[E0616]: field `v` of struct `T` is private
|
||||
--> $DIR/offset-of-self.rs:40:30
|
||||
--> $DIR/offset-of-self.rs:41:30
|
||||
|
|
||||
LL | offset_of!(Self, v)
|
||||
| ^ private field
|
||||
|
||||
error[E0609]: no field `self` on type `S`
|
||||
--> $DIR/offset-of-self.rs:53:19
|
||||
--> $DIR/offset-of-self.rs:54:19
|
||||
|
|
||||
LL | offset_of!(S, self);
|
||||
| ^^^^
|
||||
@ -52,7 +52,7 @@ LL | offset_of!(S, self);
|
||||
= note: available fields are: `v`, `w`
|
||||
|
||||
error[E0609]: no field `self` on type `u8`
|
||||
--> $DIR/offset-of-self.rs:54:21
|
||||
--> $DIR/offset-of-self.rs:55:21
|
||||
|
|
||||
LL | offset_of!(S, v.self);
|
||||
| ^^^^
|
||||
|
Loading…
Reference in New Issue
Block a user