Skip to content

Commit 7c09b99

Browse files
committed
Feature gate enums in offset_of
1 parent 1d6f05f commit 7c09b99

File tree

9 files changed

+68
-4
lines changed

9 files changed

+68
-4
lines changed

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
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 using `#![plugin(myplugin)]`.

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3118,6 +3118,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
31183118
let (ident, _def_scope) =
31193119
self.tcx.adjust_ident_and_get_scope(field, container_def.did(), block);
31203120

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

compiler/rustc_span/src/symbol.rs

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ impl<T> SizedTypeProperties for T {}
13551355
/// # Examples
13561356
///
13571357
/// ```
1358-
/// #![feature(offset_of)]
1358+
/// #![feature(offset_of, offset_of_enum)]
13591359
///
13601360
/// use std::mem;
13611361
/// #[repr(C)]

tests/mir-opt/const_prop/offset_of.rs

Lines changed: 1 addition & 1 deletion
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;
Lines changed: 15 additions & 0 deletions
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+
}
Lines changed: 37 additions & 0 deletions
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

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, offset_of_enum)]
22

33
use std::mem::offset_of;
44

tests/ui/offset-of/offset-of-private.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, offset_of_enum)]
22

33
use std::mem::offset_of;
44

0 commit comments

Comments
 (0)