Skip to content

Commit

Permalink
Fix #124478 - offset_of! returns a temporary
Browse files Browse the repository at this point in the history
This was due to the must_use() call. Adding HIR's OffsetOf to the must_use
checking within the compiler avoids this issue.
  • Loading branch information
GKFX committed Apr 28, 2024
1 parent 6c90ac8 commit ca79086
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)+)}
}
28 changes: 14 additions & 14 deletions tests/ui/offset-of/offset-of-dst-field.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand All @@ -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
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/offset-of/offset-of-must-use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions tests/ui/offset-of/offset-of-must-use.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/offset-of/offset-of-self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
}
}

Expand Down
10 changes: 5 additions & 5 deletions tests/ui/offset-of/offset-of-self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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
Expand All @@ -38,21 +38,21 @@ 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);
| ^^^^
|
= 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);
| ^^^^
Expand Down

0 comments on commit ca79086

Please sign in to comment.