Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 09c619d

Browse files
committedJan 5, 2024
Stabilize simple offset_of
1 parent 5113ed2 commit 09c619d

30 files changed

+134
-49
lines changed
 

‎compiler/rustc_error_codes/src/error_codes/E0795.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Invalid argument for the `offset_of!` macro.
33
Erroneous code example:
44

55
```compile_fail,E0795
6-
#![feature(offset_of, offset_of_enum)]
6+
#![feature(offset_of_enum, offset_of_nested)]
77
88
let x = std::mem::offset_of!(Option<u8>, Some);
99
```
@@ -16,7 +16,7 @@ The offset of the contained `u8` in the `Option<u8>` can be found by specifying
1616
the field name `0`:
1717

1818
```
19-
#![feature(offset_of, offset_of_enum)]
19+
#![feature(offset_of_enum, offset_of_nested)]
2020
2121
let x: usize = std::mem::offset_of!(Option<u8>, Some.0);
2222
```

‎compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,8 @@ declare_features! (
550550
(unstable, object_safe_for_dispatch, "1.40.0", Some(43561)),
551551
/// Allows using enums in offset_of!
552552
(unstable, offset_of_enum, "1.75.0", Some(106655)),
553+
/// Allows using multiple nested field accesses in offset_of!
554+
(unstable, offset_of_nested, "CURRENT_RUSTC_VERSION", Some(106655)),
553555
/// Allows using `#[optimize(X)]`.
554556
(unstable, optimize_attribute, "1.34.0", Some(54882)),
555557
/// Allows macro attributes on expressions, statements and non-inline modules.

‎compiler/rustc_hir_typeck/src/expr.rs

+12
Original file line numberDiff line numberDiff line change
@@ -3252,6 +3252,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32523252
) -> Ty<'tcx> {
32533253
let container = self.to_ty(container).normalized;
32543254

3255+
if let Some(ident_2) = fields.get(1)
3256+
&& !self.tcx.features().offset_of_nested
3257+
{
3258+
rustc_session::parse::feature_err(
3259+
&self.tcx.sess.parse_sess,
3260+
sym::offset_of_nested,
3261+
ident_2.span,
3262+
"only a single ident or integer is stable as the field in offset_of",
3263+
)
3264+
.emit();
3265+
}
3266+
32553267
let mut field_indices = Vec::with_capacity(fields.len());
32563268
let mut current_container = container;
32573269
let mut fields = fields.into_iter();

‎compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,7 @@ symbols! {
11501150
offset,
11511151
offset_of,
11521152
offset_of_enum,
1153+
offset_of_nested,
11531154
ok_or_else,
11541155
omit_gdb_pretty_printer_section,
11551156
on,

‎library/core/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
//
112112
// Library features:
113113
// tidy-alphabetical-start
114+
#![cfg_attr(not(bootstrap), feature(offset_of_nested))]
114115
#![feature(char_indices_offset)]
115116
#![feature(const_align_of_val)]
116117
#![feature(const_align_of_val_raw)]
@@ -178,7 +179,6 @@
178179
#![feature(isqrt)]
179180
#![feature(maybe_uninit_uninit_array)]
180181
#![feature(non_null_convenience)]
181-
#![feature(offset_of)]
182182
#![feature(offset_of_enum)]
183183
#![feature(ptr_alignment_type)]
184184
#![feature(ptr_metadata)]

‎library/core/src/mem/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1303,11 +1303,12 @@ impl<T> SizedTypeProperties for T {}
13031303
/// Enum variants may be traversed as if they were fields. Variants themselves do
13041304
/// not have an offset.
13051305
///
1306+
/// However, on stable only a single field name is supported, which blocks the use of
1307+
/// enum support.
1308+
///
13061309
/// Visibility is respected - all types and fields must be visible to the call site:
13071310
///
13081311
/// ```
1309-
/// #![feature(offset_of)]
1310-
///
13111312
/// mod nested {
13121313
/// #[repr(C)]
13131314
/// pub struct Struct {
@@ -1330,8 +1331,6 @@ impl<T> SizedTypeProperties for T {}
13301331
/// not *identical*, e.g.:
13311332
///
13321333
/// ```
1333-
/// #![feature(offset_of)]
1334-
///
13351334
/// struct Wrapper<T, U>(T, U);
13361335
///
13371336
/// type A = Wrapper<u8, u8>;
@@ -1359,8 +1358,7 @@ impl<T> SizedTypeProperties for T {}
13591358
/// # Examples
13601359
///
13611360
/// ```
1362-
/// #![feature(offset_of)]
1363-
/// # #![feature(offset_of_enum)]
1361+
/// #![feature(offset_of_enum, offset_of_nested)]
13641362
///
13651363
/// use std::mem;
13661364
/// #[repr(C)]
@@ -1396,15 +1394,15 @@ impl<T> SizedTypeProperties for T {}
13961394
/// assert_eq!(mem::offset_of!(Option<&u8>, Some.0), 0);
13971395
/// ```
13981396
#[cfg(not(bootstrap))]
1399-
#[unstable(feature = "offset_of", issue = "106655")]
1397+
#[stable(feature = "offset_of", since = "CURRENT_RUSTC_VERSION")]
14001398
#[allow_internal_unstable(builtin_syntax, hint_must_use)]
14011399
pub macro offset_of($Container:ty, $($fields:expr)+ $(,)?) {
14021400
// The `{}` is for better error messages
14031401
crate::hint::must_use({builtin # offset_of($Container, $($fields)+)})
14041402
}
14051403

14061404
#[cfg(bootstrap)]
1407-
#[unstable(feature = "offset_of", issue = "106655")]
1405+
#[stable(feature = "offset_of", since = "CURRENT_RUSTC_VERSION")]
14081406
#[allow_internal_unstable(builtin_syntax, hint_must_use)]
14091407
#[allow(missing_docs)]
14101408
pub macro offset_of($Container:ty, $($fields:tt).+ $(,)?) {

‎library/core/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@
116116
#![feature(utf8_chunks)]
117117
#![feature(is_ascii_octdigit)]
118118
#![feature(get_many_mut)]
119-
#![feature(offset_of)]
120119
#![feature(iter_map_windows)]
121120
#![allow(internal_features)]
122121
#![deny(unsafe_op_in_unsafe_fn)]

‎library/std/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@
329329
#![feature(maybe_uninit_slice)]
330330
#![feature(maybe_uninit_uninit_array)]
331331
#![feature(maybe_uninit_write_slice)]
332-
#![feature(offset_of)]
333332
#![feature(panic_can_unwind)]
334333
#![feature(panic_info_message)]
335334
#![feature(panic_internals)]

‎tests/ui/feature-gates/feature-gate-offset-of-enum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(offset_of)]
1+
#![feature(offset_of_nested)]
22

33
use std::mem::offset_of;
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![feature(offset_of_enum)]
2+
3+
use std::mem::offset_of;
4+
5+
struct S {
6+
a: u8,
7+
b: (u8, u8),
8+
c: T,
9+
}
10+
11+
struct T {
12+
t: &'static str,
13+
}
14+
15+
enum Alpha {
16+
One(u8),
17+
Two(u8),
18+
}
19+
20+
fn main() {
21+
offset_of!(Alpha, Two.0); //~ ERROR only a single ident or integer is stable as the field in offset_of
22+
offset_of!(S, a);
23+
offset_of!((u8, S), 1);
24+
offset_of!((u32, (S, T)), 1.1); //~ ERROR only a single ident or integer is stable as the field in offset_of
25+
offset_of!(S, b.0); //~ ERROR only a single ident or integer is stable as the field in offset_of
26+
offset_of!((S, ()), 0.c); //~ ERROR only a single ident or integer is stable as the field in offset_of
27+
offset_of!(S, c.t); //~ ERROR only a single ident or integer is stable as the field in offset_of
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
error[E0658]: only a single ident or integer is stable as the field in offset_of
2+
--> $DIR/feature-gate-offset-of-nested.rs:21:27
3+
|
4+
LL | offset_of!(Alpha, Two.0);
5+
| ^
6+
|
7+
= note: see issue #106655 <https://github.com/rust-lang/rust/issues/106655> for more information
8+
= help: add `#![feature(offset_of_nested)]` to the crate attributes to enable
9+
10+
error[E0658]: only a single ident or integer is stable as the field in offset_of
11+
--> $DIR/feature-gate-offset-of-nested.rs:24:33
12+
|
13+
LL | offset_of!((u32, (S, T)), 1.1);
14+
| _____----------------------------^-
15+
| | |
16+
| | in this macro invocation
17+
LL | | offset_of!(S, b.0);
18+
LL | | offset_of!((S, ()), 0.c);
19+
LL | | offset_of!(S, c.t);
20+
... |
21+
|
22+
= note: see issue #106655 <https://github.com/rust-lang/rust/issues/106655> for more information
23+
= help: add `#![feature(offset_of_nested)]` to the crate attributes to enable
24+
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
25+
26+
error[E0658]: only a single ident or integer is stable as the field in offset_of
27+
--> $DIR/feature-gate-offset-of-nested.rs:25:21
28+
|
29+
LL | offset_of!(S, b.0);
30+
| ^
31+
|
32+
= note: see issue #106655 <https://github.com/rust-lang/rust/issues/106655> for more information
33+
= help: add `#![feature(offset_of_nested)]` to the crate attributes to enable
34+
35+
error[E0658]: only a single ident or integer is stable as the field in offset_of
36+
--> $DIR/feature-gate-offset-of-nested.rs:26:27
37+
|
38+
LL | offset_of!((S, ()), 0.c);
39+
| ^
40+
|
41+
= note: see issue #106655 <https://github.com/rust-lang/rust/issues/106655> for more information
42+
= help: add `#![feature(offset_of_nested)]` to the crate attributes to enable
43+
44+
error[E0658]: only a single ident or integer is stable as the field in offset_of
45+
--> $DIR/feature-gate-offset-of-nested.rs:27:21
46+
|
47+
LL | offset_of!(S, c.t);
48+
| ^
49+
|
50+
= note: see issue #106655 <https://github.com/rust-lang/rust/issues/106655> for more information
51+
= help: add `#![feature(offset_of_nested)]` to the crate attributes to enable
52+
53+
error: aborting due to 5 previous errors
54+
55+
For more information about this error, try `rustc --explain E0658`.

‎tests/ui/lint/dead-code/offset-of-correct-param-env.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// check-pass
22

3-
#![feature(offset_of)]
3+
#![feature(offset_of_nested)]
44
#![deny(dead_code)]
55

66
// This struct contains a projection that can only be normalized after getting the field type.

‎tests/ui/lint/dead-code/offset-of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(offset_of)]
1+
#![feature(offset_of_nested)]
22
#![deny(dead_code)]
33

44
use std::mem::offset_of;

‎tests/ui/offset-of/offset-of-arg-count.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(offset_of)]
2-
31
use std::mem::offset_of;
42

53
fn main() {

‎tests/ui/offset-of/offset-of-arg-count.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unexpected end of macro invocation
2-
--> $DIR/offset-of-arg-count.rs:6:34
2+
--> $DIR/offset-of-arg-count.rs:4:34
33
|
44
LL | offset_of!(NotEnoughArguments);
55
| ^ missing tokens in macro arguments
@@ -8,7 +8,7 @@ note: while trying to match `,`
88
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
99

1010
error: unexpected end of macro invocation
11-
--> $DIR/offset-of-arg-count.rs:7:45
11+
--> $DIR/offset-of-arg-count.rs:5:45
1212
|
1313
LL | offset_of!(NotEnoughArgumentsWithAComma, );
1414
| ^ missing tokens in macro arguments
@@ -17,33 +17,33 @@ note: while trying to match meta-variable `$fields:expr`
1717
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
1818

1919
error: no rules expected the token `too`
20-
--> $DIR/offset-of-arg-count.rs:8:34
20+
--> $DIR/offset-of-arg-count.rs:6:34
2121
|
2222
LL | offset_of!(Container, field, too many arguments);
2323
| ^^^ no rules expected this token in macro call
2424
|
2525
= note: while trying to match sequence end
2626

2727
error: unexpected token: `)`
28-
--> $DIR/offset-of-arg-count.rs:11:21
28+
--> $DIR/offset-of-arg-count.rs:9:21
2929
|
3030
LL | offset_of!(S, f.);
3131
| ^
3232

3333
error: unexpected token: `,`
34-
--> $DIR/offset-of-arg-count.rs:12:21
34+
--> $DIR/offset-of-arg-count.rs:10:21
3535
|
3636
LL | offset_of!(S, f.,);
3737
| ^
3838

3939
error: offset_of expects dot-separated field and variant names
40-
--> $DIR/offset-of-arg-count.rs:13:19
40+
--> $DIR/offset-of-arg-count.rs:11:19
4141
|
4242
LL | offset_of!(S, f..);
4343
| ^^^
4444

4545
error: offset_of expects dot-separated field and variant names
46-
--> $DIR/offset-of-arg-count.rs:14:19
46+
--> $DIR/offset-of-arg-count.rs:12:19
4747
|
4848
LL | offset_of!(S, f..,);
4949
| ^^^

‎tests/ui/offset-of/offset-of-dst-field.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(offset_of, extern_types)]
1+
#![feature(extern_types)]
22

33
use std::mem::offset_of;
44

‎tests/ui/offset-of/offset-of-enum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(offset_of, offset_of_enum)]
1+
#![feature(offset_of_enum, offset_of_nested)]
22

33
use std::mem::offset_of;
44

‎tests/ui/offset-of/offset-of-inference.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Test that inference types in `offset_of!` don't ICE.
22

3-
#![feature(offset_of)]
4-
53
struct Foo<T> {
64
x: T,
75
}

‎tests/ui/offset-of/offset-of-inference.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/offset-of-inference.rs:10:35
2+
--> $DIR/offset-of-inference.rs:8:35
33
|
44
LL | let _ = core::mem::offset_of!(Foo<_>, x);
55
| ^^^^^^ cannot infer type

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

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// check-pass
22

3-
#![feature(offset_of)]
43
#![warn(unused)]
54

65
fn main() {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
warning: unused return value of `must_use` that must be used
2-
--> $DIR/offset-of-must-use.rs:7:5
2+
--> $DIR/offset-of-must-use.rs:6:5
33
|
44
LL | core::mem::offset_of!((String,), 0);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: the lint level is defined here
8-
--> $DIR/offset-of-must-use.rs:4:9
8+
--> $DIR/offset-of-must-use.rs:3:9
99
|
1010
LL | #![warn(unused)]
1111
| ^^^^^^

‎tests/ui/offset-of/offset-of-output-type.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(offset_of)]
2-
31
use std::mem::offset_of;
42

53
struct S {

‎tests/ui/offset-of/offset-of-output-type.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
error[E0308]: mismatched types
2-
--> $DIR/offset-of-output-type.rs:12:17
2+
--> $DIR/offset-of-output-type.rs:10:17
33
|
44
LL | let _: u8 = offset_of!(S, v);
55
| ^^^^^^^^^^^^^^^^ expected `u8`, found `usize`
66
|
77
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
88

99
error[E0308]: mismatched types
10-
--> $DIR/offset-of-output-type.rs:13:18
10+
--> $DIR/offset-of-output-type.rs:11:18
1111
|
1212
LL | let _: u16 = offset_of!(S, v);
1313
| ^^^^^^^^^^^^^^^^ expected `u16`, found `usize`
1414
|
1515
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
1616

1717
error[E0308]: mismatched types
18-
--> $DIR/offset-of-output-type.rs:14:18
18+
--> $DIR/offset-of-output-type.rs:12:18
1919
|
2020
LL | let _: u32 = offset_of!(S, v);
2121
| ^^^^^^^^^^^^^^^^ expected `u32`, found `usize`
2222
|
2323
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
2424

2525
error[E0308]: mismatched types
26-
--> $DIR/offset-of-output-type.rs:15:18
26+
--> $DIR/offset-of-output-type.rs:13:18
2727
|
2828
LL | let _: u64 = offset_of!(S, v);
2929
| ^^^^^^^^^^^^^^^^ expected `u64`, found `usize`
3030
|
3131
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
3232

3333
error[E0308]: mismatched types
34-
--> $DIR/offset-of-output-type.rs:16:20
34+
--> $DIR/offset-of-output-type.rs:14:20
3535
|
3636
LL | let _: isize = offset_of!(S, v);
3737
| ^^^^^^^^^^^^^^^^ expected `isize`, found `usize`
3838
|
3939
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
4040

4141
error[E0308]: mismatched types
42-
--> $DIR/offset-of-output-type.rs:19:5
42+
--> $DIR/offset-of-output-type.rs:17:5
4343
|
4444
LL | fn main() {
4545
| - expected `()` because of default return type

‎tests/ui/offset-of/offset-of-private.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(offset_of, offset_of_enum)]
1+
#![feature(offset_of_enum, offset_of_nested)]
22

33
use std::mem::offset_of;
44

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(offset_of)]
1+
#![feature(offset_of_nested)]
22

33
use std::mem::offset_of;
44

‎tests/ui/offset-of/offset-of-tuple-nested.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Test for issue #112204 -- make sure this goes through the entire compilation pipeline,
33
// similar to why `offset-of-unsized.rs` is also build-pass
44

5-
#![feature(offset_of)]
5+
#![feature(offset_of_nested)]
66

77
use std::mem::offset_of;
88

‎tests/ui/offset-of/offset-of-tuple.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(offset_of)]
1+
#![feature(offset_of_nested)]
22
#![feature(builtin_syntax)]
33

44
use std::mem::offset_of;

‎tests/ui/offset-of/offset-of-unsized.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// regression test for #112051, not in `offset-of-dst` as the issue is in codegen,
33
// and isn't triggered in the presence of typeck errors
44

5-
#![feature(offset_of)]
6-
75
struct S<T: ?Sized> {
86
a: u64,
97
b: T,

‎tests/ui/offset-of/offset-of-unstable-with-feature.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// check-pass
22
// aux-build:offset-of-staged-api.rs
33

4-
#![feature(offset_of, unstable_test_feature)]
4+
#![feature(offset_of_nested, unstable_test_feature)]
55

66
use std::mem::offset_of;
77

‎tests/ui/offset-of/offset-of-unstable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// aux-build:offset-of-staged-api.rs
22

3-
#![feature(offset_of)]
3+
#![feature(offset_of_nested)]
44

55
use std::mem::offset_of;
66

0 commit comments

Comments
 (0)
Please sign in to comment.