Skip to content

Commit 721b7f7

Browse files
committed
WIP stabilize simple offset_of
1 parent 84f6130 commit 721b7f7

30 files changed

+126
-48
lines changed

compiler/rustc_error_codes/src/error_codes/E0795.md

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ declare_features! (
549549
(unstable, object_safe_for_dispatch, "1.40.0", Some(43561), None),
550550
/// Allows using enums in offset_of!
551551
(unstable, offset_of_enum, "1.75.0", Some(106655), None),
552+
/// Allows using multiple nested field accesses in offset_of!
553+
(unstable, offset_of_nested, "CURRENT_RUSTC_VERSION", Some(106655), None),
552554
/// Allows using `#[optimize(X)]`.
553555
(unstable, optimize_attribute, "1.34.0", Some(54882), None),
554556
/// Allows macro attributes on expressions, statements and non-inline modules.

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3261,6 +3261,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32613261
) -> Ty<'tcx> {
32623262
let container = self.to_ty(container).normalized;
32633263

3264+
if let Some(ident_2) = fields.get(1)
3265+
&& !self.tcx.features().offset_of_nested
3266+
{
3267+
rustc_session::parse::feature_err(
3268+
&self.tcx.sess.parse_sess,
3269+
sym::offset_of_nested,
3270+
ident_2.span,
3271+
"only a single ident or integer is stable as the field in offset_of",
3272+
)
3273+
.emit();
3274+
}
3275+
32643276
let mut field_indices = Vec::with_capacity(fields.len());
32653277
let mut current_container = container;
32663278
let mut fields = fields.into_iter();

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,7 @@ symbols! {
11421142
offset,
11431143
offset_of,
11441144
offset_of_enum,
1145+
offset_of_nested,
11451146
ok_or_else,
11461147
omit_gdb_pretty_printer_section,
11471148
on,

library/core/src/lib.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 7 deletions
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)]
@@ -1395,7 +1393,7 @@ impl<T> SizedTypeProperties for T {}
13951393
///
13961394
/// assert_eq!(mem::offset_of!(Option<&u8>, Some.0), 0);
13971395
/// ```
1398-
#[unstable(feature = "offset_of", issue = "106655")]
1396+
#[stable(feature = "offset_of", since = "CURRENT_RUSTC_VERSION")]
13991397
#[allow_internal_unstable(builtin_syntax, hint_must_use)]
14001398
pub macro offset_of($Container:ty, $($fields:tt).+ $(,)?) {
14011399
// The `{}` is for better error messages

library/core/tests/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@
115115
#![feature(utf8_chunks)]
116116
#![feature(is_ascii_octdigit)]
117117
#![feature(get_many_mut)]
118-
#![feature(offset_of)]
119118
#![feature(iter_map_windows)]
120119
#![allow(internal_features)]
121120
#![deny(unsafe_op_in_unsafe_fn)]

library/std/src/lib.rs

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 28 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)