Skip to content

Commit

Permalink
WIP stabilize simple offset_of
Browse files Browse the repository at this point in the history
  • Loading branch information
GKFX committed Dec 10, 2023
1 parent 84f6130 commit 721b7f7
Show file tree
Hide file tree
Showing 30 changed files with 126 additions and 48 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_error_codes/src/error_codes/E0795.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Invalid argument for the `offset_of!` macro.
Erroneous code example:

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

```
#![feature(offset_of, offset_of_enum)]
#![feature(offset_of_enum, offset_of_nested)]
let x: usize = std::mem::offset_of!(Option<u8>, Some.0);
```
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ declare_features! (
(unstable, object_safe_for_dispatch, "1.40.0", Some(43561), None),
/// Allows using enums in offset_of!
(unstable, offset_of_enum, "1.75.0", Some(106655), None),
/// Allows using multiple nested field accesses in offset_of!
(unstable, offset_of_nested, "CURRENT_RUSTC_VERSION", Some(106655), None),
/// Allows using `#[optimize(X)]`.
(unstable, optimize_attribute, "1.34.0", Some(54882), None),
/// Allows macro attributes on expressions, statements and non-inline modules.
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3261,6 +3261,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) -> Ty<'tcx> {
let container = self.to_ty(container).normalized;

if let Some(ident_2) = fields.get(1)
&& !self.tcx.features().offset_of_nested
{
rustc_session::parse::feature_err(
&self.tcx.sess.parse_sess,
sym::offset_of_nested,
ident_2.span,
"only a single ident or integer is stable as the field in offset_of",
)
.emit();
}

let mut field_indices = Vec::with_capacity(fields.len());
let mut current_container = container;
let mut fields = fields.into_iter();
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,7 @@ symbols! {
offset,
offset_of,
offset_of_enum,
offset_of_nested,
ok_or_else,
omit_gdb_pretty_printer_section,
on,
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
//
// Library features:
// tidy-alphabetical-start
#![cfg_attr(not(bootstrap), feature(offset_of_nested))]
#![feature(char_indices_offset)]
#![feature(const_align_of_val)]
#![feature(const_align_of_val_raw)]
Expand Down Expand Up @@ -178,7 +179,6 @@
#![feature(isqrt)]
#![feature(maybe_uninit_uninit_array)]
#![feature(non_null_convenience)]
#![feature(offset_of)]
#![feature(offset_of_enum)]
#![feature(ptr_alignment_type)]
#![feature(ptr_metadata)]
Expand Down
12 changes: 5 additions & 7 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,11 +1303,12 @@ impl<T> SizedTypeProperties for T {}
/// Enum variants may be traversed as if they were fields. Variants themselves do
/// not have an offset.
///
/// However, on stable only a single field name is supported, which blocks the use of
/// enum support.
///
/// Visibility is respected - all types and fields must be visible to the call site:
///
/// ```
/// #![feature(offset_of)]
///
/// mod nested {
/// #[repr(C)]
/// pub struct Struct {
Expand All @@ -1330,8 +1331,6 @@ impl<T> SizedTypeProperties for T {}
/// not *identical*, e.g.:
///
/// ```
/// #![feature(offset_of)]
///
/// struct Wrapper<T, U>(T, U);
///
/// type A = Wrapper<u8, u8>;
Expand Down Expand Up @@ -1359,8 +1358,7 @@ impl<T> SizedTypeProperties for T {}
/// # Examples
///
/// ```

Check failure on line 1360 in library/core/src/mem/mod.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check

unknown feature `offset_of_nested`
/// #![feature(offset_of)]
/// # #![feature(offset_of_enum)]
/// #![feature(offset_of_enum, offset_of_nested)]
///
/// use std::mem;
/// #[repr(C)]
Expand Down Expand Up @@ -1395,7 +1393,7 @@ impl<T> SizedTypeProperties for T {}
///
/// assert_eq!(mem::offset_of!(Option<&u8>, Some.0), 0);
/// ```
#[unstable(feature = "offset_of", issue = "106655")]
#[stable(feature = "offset_of", since = "CURRENT_RUSTC_VERSION")]
#[allow_internal_unstable(builtin_syntax, hint_must_use)]
pub macro offset_of($Container:ty, $($fields:tt).+ $(,)?) {
// The `{}` is for better error messages
Expand Down
1 change: 0 additions & 1 deletion library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
#![feature(utf8_chunks)]
#![feature(is_ascii_octdigit)]
#![feature(get_many_mut)]
#![feature(offset_of)]
#![feature(iter_map_windows)]
#![allow(internal_features)]
#![deny(unsafe_op_in_unsafe_fn)]
Expand Down
1 change: 0 additions & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@
#![feature(maybe_uninit_slice)]
#![feature(maybe_uninit_uninit_array)]
#![feature(maybe_uninit_write_slice)]
#![feature(offset_of)]
#![feature(panic_can_unwind)]
#![feature(panic_info_message)]
#![feature(panic_internals)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/feature-gates/feature-gate-offset-of-enum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(offset_of)]
#![feature(offset_of_nested)]

use std::mem::offset_of;

Expand Down
28 changes: 28 additions & 0 deletions tests/ui/feature-gates/feature-gate-offset-of-nested.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![feature(offset_of_enum)]

use std::mem::offset_of;

struct S {
a: u8,
b: (u8, u8),
c: T,
}

struct T {
t: &'static str,
}

enum Alpha {
One(u8),
Two(u8),
}

fn main() {
offset_of!(Alpha, Two.0); //~ ERROR only a single ident or integer is stable as the field in offset_of
offset_of!(S, a);
offset_of!((u8, S), 1);
offset_of!((u32, (S, T)), 1.1); //~ ERROR only a single ident or integer is stable as the field in offset_of
offset_of!(S, b.0); //~ ERROR only a single ident or integer is stable as the field in offset_of
offset_of!((S, ()), 0.c); //~ ERROR only a single ident or integer is stable as the field in offset_of
offset_of!(S, c.t); //~ ERROR only a single ident or integer is stable as the field in offset_of
}
48 changes: 48 additions & 0 deletions tests/ui/feature-gates/feature-gate-offset-of-nested.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
error[E0658]: only a single ident or integer is stable as the field in offset_of
--> $DIR/feature-gate-offset-of-nested.rs:21:27
|
LL | offset_of!(Alpha, Two.0);
| ^
|
= note: see issue #106655 <https://github.com/rust-lang/rust/issues/106655> for more information
= help: add `#![feature(offset_of_nested)]` to the crate attributes to enable

error[E0658]: only a single ident or integer is stable as the field in offset_of
--> $DIR/feature-gate-offset-of-nested.rs:24:33
|
LL | offset_of!((u32, (S, T)), 1.1);
| ^
|
= note: see issue #106655 <https://github.com/rust-lang/rust/issues/106655> for more information
= help: add `#![feature(offset_of_nested)]` to the crate attributes to enable

error[E0658]: only a single ident or integer is stable as the field in offset_of
--> $DIR/feature-gate-offset-of-nested.rs:25:21
|
LL | offset_of!(S, b.0);
| ^
|
= note: see issue #106655 <https://github.com/rust-lang/rust/issues/106655> for more information
= help: add `#![feature(offset_of_nested)]` to the crate attributes to enable

error[E0658]: only a single ident or integer is stable as the field in offset_of
--> $DIR/feature-gate-offset-of-nested.rs:26:27
|
LL | offset_of!((S, ()), 0.c);
| ^
|
= note: see issue #106655 <https://github.com/rust-lang/rust/issues/106655> for more information
= help: add `#![feature(offset_of_nested)]` to the crate attributes to enable

error[E0658]: only a single ident or integer is stable as the field in offset_of
--> $DIR/feature-gate-offset-of-nested.rs:27:21
|
LL | offset_of!(S, c.t);
| ^
|
= note: see issue #106655 <https://github.com/rust-lang/rust/issues/106655> for more information
= help: add `#![feature(offset_of_nested)]` to the crate attributes to enable

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0658`.
2 changes: 1 addition & 1 deletion tests/ui/lint/dead-code/offset-of-correct-param-env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// check-pass

#![feature(offset_of)]
#![feature(offset_of_nested)]
#![deny(dead_code)]

// This struct contains a projection that can only be normalized after getting the field type.
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lint/dead-code/offset-of.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(offset_of)]
#![feature(offset_of_nested)]
#![deny(dead_code)]

use std::mem::offset_of;
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/offset-of/offset-of-arg-count.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(offset_of)]

use std::mem::offset_of;

fn main() {
Expand Down
14 changes: 7 additions & 7 deletions tests/ui/offset-of/offset-of-arg-count.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: unexpected end of macro invocation
--> $DIR/offset-of-arg-count.rs:6:34
--> $DIR/offset-of-arg-count.rs:4:34
|
LL | offset_of!(NotEnoughArguments);
| ^ missing tokens in macro arguments
Expand All @@ -8,7 +8,7 @@ note: while trying to match `,`
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL

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

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

error: unexpected end of macro invocation
--> $DIR/offset-of-arg-count.rs:11:21
--> $DIR/offset-of-arg-count.rs:9:21
|
LL | offset_of!(S, f.);
| ^ missing tokens in macro arguments
Expand All @@ -34,21 +34,21 @@ note: while trying to match meta-variable `$fields:tt`
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL

error: expected identifier, found `,`
--> $DIR/offset-of-arg-count.rs:12:21
--> $DIR/offset-of-arg-count.rs:10:21
|
LL | offset_of!(S, f.,);
| ^ expected identifier

error: no rules expected the token `..`
--> $DIR/offset-of-arg-count.rs:13:20
--> $DIR/offset-of-arg-count.rs:11:20
|
LL | offset_of!(S, f..);
| ^^ no rules expected this token in macro call
|
= note: while trying to match sequence start

error: no rules expected the token `..`
--> $DIR/offset-of-arg-count.rs:14:20
--> $DIR/offset-of-arg-count.rs:12:20
|
LL | offset_of!(S, f..,);
| ^^ no rules expected this token in macro call
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/offset-of/offset-of-dst-field.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(offset_of, extern_types)]
#![feature(extern_types)]

use std::mem::offset_of;

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/offset-of/offset-of-enum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(offset_of, offset_of_enum)]
#![feature(offset_of_enum, offset_of_nested)]

use std::mem::offset_of;

Expand Down
2 changes: 0 additions & 2 deletions tests/ui/offset-of/offset-of-inference.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Test that inference types in `offset_of!` don't ICE.

#![feature(offset_of)]

struct Foo<T> {
x: T,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/offset-of/offset-of-inference.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0282]: type annotations needed
--> $DIR/offset-of-inference.rs:10:35
--> $DIR/offset-of-inference.rs:8:35
|
LL | let _ = core::mem::offset_of!(Foo<_>, x);
| ^^^^^^ cannot infer type
Expand Down
1 change: 0 additions & 1 deletion tests/ui/offset-of/offset-of-must-use.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// check-pass

#![feature(offset_of)]
#![warn(unused)]

fn main() {
Expand Down
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,11 +1,11 @@
warning: unused return value of `must_use` that must be used
--> $DIR/offset-of-must-use.rs:7:5
--> $DIR/offset-of-must-use.rs:6:5
|
LL | core::mem::offset_of!((String,), 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/offset-of-must-use.rs:4:9
--> $DIR/offset-of-must-use.rs:3:9
|
LL | #![warn(unused)]
| ^^^^^^
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/offset-of/offset-of-output-type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(offset_of)]

use std::mem::offset_of;

struct S {
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/offset-of/offset-of-output-type.stderr
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
error[E0308]: mismatched types
--> $DIR/offset-of-output-type.rs:12:17
--> $DIR/offset-of-output-type.rs:10:17
|
LL | let _: u8 = offset_of!(S, v);
| ^^^^^^^^^^^^^^^^ expected `u8`, found `usize`
|
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)

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

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

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

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

error[E0308]: mismatched types
--> $DIR/offset-of-output-type.rs:19:5
--> $DIR/offset-of-output-type.rs:17:5
|
LL | fn main() {
| - expected `()` because of default return type
Expand Down
Loading

0 comments on commit 721b7f7

Please sign in to comment.