Skip to content

Commit 992943d

Browse files
committed
Auto merge of rust-lang#117537 - GKFX:offset-of-enum-feature, r=cjgillot
Feature gate enums in offset_of As requested at rust-lang#106655 (comment), put enums in offset_of behind their own feature gate. `@rustbot` label F-offset_of
2 parents 04817ff + 00a9ed3 commit 992943d

File tree

10 files changed

+70
-5
lines changed

10 files changed

+70
-5
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)]
6+
#![feature(offset_of, offset_of_enum)]
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)]
19+
#![feature(offset_of, offset_of_enum)]
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
@@ -526,6 +526,8 @@ declare_features! (
526526
/// In that case, `dyn Trait: Trait` does not hold. Moreover, coercions and
527527
/// casts in safe Rust to `dyn Trait` for such a `Trait` is also forbidden.
528528
(unstable, object_safe_for_dispatch, "1.40.0", Some(43561), None),
529+
/// Allows using enums in offset_of!
530+
(unstable, offset_of_enum, "CURRENT_RUSTC_VERSION", Some(106655), None),
529531
/// Allows using `#[optimize(X)]`.
530532
(unstable, optimize_attribute, "1.34.0", Some(54882), None),
531533
/// Allows exhaustive integer pattern matching on `usize` and `isize`.

compiler/rustc_hir_typeck/src/expr.rs

+9
Original file line numberDiff line numberDiff line change
@@ -3119,6 +3119,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
31193119
let (ident, _def_scope) =
31203120
self.tcx.adjust_ident_and_get_scope(field, container_def.did(), block);
31213121

3122+
if !self.tcx.features().offset_of_enum {
3123+
rustc_session::parse::feature_err(
3124+
&self.tcx.sess.parse_sess,
3125+
sym::offset_of_enum,
3126+
ident.span,
3127+
"using enums in offset_of is experimental",
3128+
).emit();
3129+
}
3130+
31223131
let Some((index, variant)) = container_def.variants()
31233132
.iter_enumerated()
31243133
.find(|(_, v)| v.ident(self.tcx).normalize_to_macros_2_0() == ident) else {

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,7 @@ symbols! {
11141114
off,
11151115
offset,
11161116
offset_of,
1117+
offset_of_enum,
11171118
omit_gdb_pretty_printer_section,
11181119
on,
11191120
on_unimplemented,

library/core/src/mem/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,7 @@ impl<T> SizedTypeProperties for T {}
13581358
///
13591359
/// ```
13601360
/// #![feature(offset_of)]
1361+
/// # #![cfg_attr(not(bootstrap), feature(offset_of_enum))]
13611362
///
13621363
/// use std::mem;
13631364
/// #[repr(C)]

tests/mir-opt/const_prop/offset_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// unit-test: ConstProp
33
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
44

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

77
use std::marker::PhantomData;
88
use std::mem::offset_of;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(offset_of)]
2+
3+
use std::mem::offset_of;
4+
5+
enum Alpha {
6+
One(u8),
7+
Two(u8),
8+
}
9+
10+
fn main() {
11+
offset_of!(Alpha::One, 0); //~ ERROR expected type, found variant `Alpha::One`
12+
offset_of!(Alpha, One); //~ ERROR `One` is an enum variant; expected field at end of `offset_of`
13+
//~| ERROR using enums in offset_of is experimental
14+
offset_of!(Alpha, Two.0); //~ ERROR using enums in offset_of is experimental
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error[E0573]: expected type, found variant `Alpha::One`
2+
--> $DIR/feature-gate-offset-of-enum.rs:11:16
3+
|
4+
LL | offset_of!(Alpha::One, 0);
5+
| ^^^^^^^^^^
6+
| |
7+
| not a type
8+
| help: try using the variant's enum: `Alpha`
9+
10+
error[E0658]: using enums in offset_of is experimental
11+
--> $DIR/feature-gate-offset-of-enum.rs:12:23
12+
|
13+
LL | offset_of!(Alpha, One);
14+
| ^^^
15+
|
16+
= note: see issue #106655 <https://github.com/rust-lang/rust/issues/106655> for more information
17+
= help: add `#![feature(offset_of_enum)]` to the crate attributes to enable
18+
19+
error[E0795]: `One` is an enum variant; expected field at end of `offset_of`
20+
--> $DIR/feature-gate-offset-of-enum.rs:12:23
21+
|
22+
LL | offset_of!(Alpha, One);
23+
| ^^^ enum variant
24+
25+
error[E0658]: using enums in offset_of is experimental
26+
--> $DIR/feature-gate-offset-of-enum.rs:14:23
27+
|
28+
LL | offset_of!(Alpha, Two.0);
29+
| ^^^
30+
|
31+
= note: see issue #106655 <https://github.com/rust-lang/rust/issues/106655> for more information
32+
= help: add `#![feature(offset_of_enum)]` to the crate attributes to enable
33+
34+
error: aborting due to 4 previous errors
35+
36+
Some errors have detailed explanations: E0573, E0658, E0795.
37+
For more information about an error, try `rustc --explain E0573`.

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)]
1+
#![feature(offset_of, offset_of_enum)]
22

33
use std::mem::offset_of;
44

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)]
1+
#![feature(offset_of, offset_of_enum)]
22

33
use std::mem::offset_of;
44

0 commit comments

Comments
 (0)