Skip to content

Commit ca79086

Browse files
committed
Fix rust-lang#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.
1 parent 6c90ac8 commit ca79086

File tree

7 files changed

+28
-26
lines changed

7 files changed

+28
-26
lines changed

compiler/rustc_lint/src/unused.rs

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
176176
| hir::BinOpKind::Shr => Some("bitwise operation"),
177177
},
178178
hir::ExprKind::AddrOf(..) => Some("borrow"),
179+
hir::ExprKind::OffsetOf(..) => Some("`offset_of` call"),
179180
hir::ExprKind::Unary(..) => Some("unary operation"),
180181
_ => None,
181182
};

library/core/src/mem/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1340,8 +1340,8 @@ impl<T> SizedTypeProperties for T {}
13401340
/// assert_eq!(mem::offset_of!(Option<&u8>, Some.0), 0);
13411341
/// ```
13421342
#[stable(feature = "offset_of", since = "1.77.0")]
1343-
#[allow_internal_unstable(builtin_syntax, hint_must_use)]
1343+
#[allow_internal_unstable(builtin_syntax)]
13441344
pub macro offset_of($Container:ty, $($fields:expr)+ $(,)?) {
13451345
// The `{}` is for better error messages
1346-
crate::hint::must_use({builtin # offset_of($Container, $($fields)+)})
1346+
{builtin # offset_of($Container, $($fields)+)}
13471347
}

tests/ui/offset-of/offset-of-dst-field.stderr

+14-14
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,6 @@ LL | offset_of!((u8, dyn Trait), 1);
3434
= help: the trait `Sized` is not implemented for `dyn Trait`
3535
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
3636

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

55+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
56+
--> $DIR/offset-of-dst-field.rs:44:5
57+
|
58+
LL | offset_of!(Delta<Alpha>, z);
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
60+
|
61+
= help: within `Alpha`, the trait `Sized` is not implemented for `[u8]`, which is required by `Alpha: Sized`
62+
note: required because it appears within the type `Alpha`
63+
--> $DIR/offset-of-dst-field.rs:5:8
64+
|
65+
LL | struct Alpha {
66+
| ^^^^^
67+
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
68+
6969
error[E0277]: the size for values of type `T` cannot be known at compilation time
7070
--> $DIR/offset-of-dst-field.rs:50:5
7171
|

tests/ui/offset-of/offset-of-must-use.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
fn main() {
66
core::mem::offset_of!((String,), 0);
7-
//~^ WARN unused return value of `must_use` that must be used
7+
//~^ WARN unused `offset_of` call that must be used
88
}

tests/ui/offset-of/offset-of-must-use.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
warning: unused return value of `must_use` that must be used
1+
warning: unused `offset_of` call that must be used
22
--> $DIR/offset-of-must-use.rs:6:5
33
|
44
LL | core::mem::offset_of!((String,), 0);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `offset_of` call produces a value
66
|
77
note: the lint level is defined here
88
--> $DIR/offset-of-must-use.rs:3:9

tests/ui/offset-of/offset-of-self.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ impl S {
2323
fn offs_in_c() -> usize {
2424
offset_of!(C<Self>, w)
2525
}
26-
fn offs_in_c_colon() -> usize {
27-
offset_of!(C::<Self>, w)
26+
// Put offset_of in a slice - test #124478.
27+
fn offs_in_c_colon() -> &'static [usize] {
28+
&[offset_of!(C::<Self>, w)]
2829
}
2930
}
3031

tests/ui/offset-of/offset-of-self.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | offset_of!(Self, Self::v);
55
| ^^^^^^^
66

77
error[E0412]: cannot find type `S` in module `self`
8-
--> $DIR/offset-of-self.rs:34:26
8+
--> $DIR/offset-of-self.rs:35:26
99
|
1010
LL | offset_of!(self::S, v);
1111
| ^ not found in `self`
@@ -21,7 +21,7 @@ LL + offset_of!(S, v);
2121
|
2222

2323
error[E0411]: cannot find type `Self` in this scope
24-
--> $DIR/offset-of-self.rs:51:16
24+
--> $DIR/offset-of-self.rs:52:16
2525
|
2626
LL | fn main() {
2727
| ---- `Self` not allowed in a function
@@ -38,21 +38,21 @@ LL | offset_of!(S, Self);
3838
= note: available fields are: `v`, `w`
3939

4040
error[E0616]: field `v` of struct `T` is private
41-
--> $DIR/offset-of-self.rs:40:30
41+
--> $DIR/offset-of-self.rs:41:30
4242
|
4343
LL | offset_of!(Self, v)
4444
| ^ private field
4545

4646
error[E0609]: no field `self` on type `S`
47-
--> $DIR/offset-of-self.rs:53:19
47+
--> $DIR/offset-of-self.rs:54:19
4848
|
4949
LL | offset_of!(S, self);
5050
| ^^^^
5151
|
5252
= note: available fields are: `v`, `w`
5353

5454
error[E0609]: no field `self` on type `u8`
55-
--> $DIR/offset-of-self.rs:54:21
55+
--> $DIR/offset-of-self.rs:55:21
5656
|
5757
LL | offset_of!(S, v.self);
5858
| ^^^^

0 commit comments

Comments
 (0)