Skip to content

Commit

Permalink
Auto merge of #117537 - GKFX:offset-of-enum-feature, r=cjgillot
Browse files Browse the repository at this point in the history
Feature gate enums in offset_of

As requested at #106655 (comment), put enums in offset_of behind their own feature gate.

`@rustbot` label F-offset_of
  • Loading branch information
bors committed Nov 5, 2023
2 parents 04817ff + 00a9ed3 commit 992943d
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 5 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)]
#![feature(offset_of, offset_of_enum)]
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)]
#![feature(offset_of, offset_of_enum)]
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 @@ -526,6 +526,8 @@ declare_features! (
/// In that case, `dyn Trait: Trait` does not hold. Moreover, coercions and
/// casts in safe Rust to `dyn Trait` for such a `Trait` is also forbidden.
(unstable, object_safe_for_dispatch, "1.40.0", Some(43561), None),
/// Allows using enums in offset_of!
(unstable, offset_of_enum, "CURRENT_RUSTC_VERSION", Some(106655), None),
/// Allows using `#[optimize(X)]`.
(unstable, optimize_attribute, "1.34.0", Some(54882), None),
/// Allows exhaustive integer pattern matching on `usize` and `isize`.
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3119,6 +3119,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let (ident, _def_scope) =
self.tcx.adjust_ident_and_get_scope(field, container_def.did(), block);

if !self.tcx.features().offset_of_enum {
rustc_session::parse::feature_err(
&self.tcx.sess.parse_sess,
sym::offset_of_enum,
ident.span,
"using enums in offset_of is experimental",
).emit();
}

let Some((index, variant)) = container_def.variants()
.iter_enumerated()
.find(|(_, v)| v.ident(self.tcx).normalize_to_macros_2_0() == ident) else {
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 @@ -1114,6 +1114,7 @@ symbols! {
off,
offset,
offset_of,
offset_of_enum,
omit_gdb_pretty_printer_section,
on,
on_unimplemented,
Expand Down
1 change: 1 addition & 0 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,7 @@ impl<T> SizedTypeProperties for T {}
///
/// ```
/// #![feature(offset_of)]
/// # #![cfg_attr(not(bootstrap), feature(offset_of_enum))]
///
/// use std::mem;
/// #[repr(C)]
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/const_prop/offset_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// unit-test: ConstProp
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY

#![feature(offset_of)]
#![feature(offset_of, offset_of_enum)]

use std::marker::PhantomData;
use std::mem::offset_of;
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/feature-gates/feature-gate-offset-of-enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![feature(offset_of)]

use std::mem::offset_of;

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

fn main() {
offset_of!(Alpha::One, 0); //~ ERROR expected type, found variant `Alpha::One`
offset_of!(Alpha, One); //~ ERROR `One` is an enum variant; expected field at end of `offset_of`
//~| ERROR using enums in offset_of is experimental
offset_of!(Alpha, Two.0); //~ ERROR using enums in offset_of is experimental
}
37 changes: 37 additions & 0 deletions tests/ui/feature-gates/feature-gate-offset-of-enum.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
error[E0573]: expected type, found variant `Alpha::One`
--> $DIR/feature-gate-offset-of-enum.rs:11:16
|
LL | offset_of!(Alpha::One, 0);
| ^^^^^^^^^^
| |
| not a type
| help: try using the variant's enum: `Alpha`

error[E0658]: using enums in offset_of is experimental
--> $DIR/feature-gate-offset-of-enum.rs:12:23
|
LL | offset_of!(Alpha, One);
| ^^^
|
= note: see issue #106655 <https://github.com/rust-lang/rust/issues/106655> for more information
= help: add `#![feature(offset_of_enum)]` to the crate attributes to enable

error[E0795]: `One` is an enum variant; expected field at end of `offset_of`
--> $DIR/feature-gate-offset-of-enum.rs:12:23
|
LL | offset_of!(Alpha, One);
| ^^^ enum variant

error[E0658]: using enums in offset_of is experimental
--> $DIR/feature-gate-offset-of-enum.rs:14:23
|
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_enum)]` to the crate attributes to enable

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0573, E0658, E0795.
For more information about an error, try `rustc --explain E0573`.
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)]
#![feature(offset_of, offset_of_enum)]

use std::mem::offset_of;

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

use std::mem::offset_of;

Expand Down

0 comments on commit 992943d

Please sign in to comment.