Skip to content

Commit b081bc8

Browse files
committed
rustc_abi: stabilize VariantIdx and FieldIdx
1 parent 0028f34 commit b081bc8

3 files changed

Lines changed: 61 additions & 57 deletions

File tree

compiler/rustc_abi/src/layout.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,64 @@ mod simple;
2121
mod ty;
2222

2323
#[cfg(feature = "nightly")]
24-
pub use ty::{FIRST_VARIANT, FieldIdx, Layout, TyAbiInterface, TyAndLayout, VariantIdx};
24+
pub use ty::{Layout, TyAbiInterface, TyAndLayout};
25+
26+
rustc_index::newtype_index! {
27+
/// The *source-order* index of a field in a variant.
28+
///
29+
/// This is how most code after type checking refers to fields, rather than
30+
/// using names (as names have hygiene complications and more complex lookup).
31+
///
32+
/// Particularly for `repr(Rust)` types, this may not be the same as *layout* order.
33+
/// (It is for `repr(C)` `struct`s, however.)
34+
///
35+
/// For example, in the following types,
36+
/// ```rust
37+
/// # enum Never {}
38+
/// # #[repr(u16)]
39+
/// enum Demo1 {
40+
/// Variant0 { a: Never, b: i32 } = 100,
41+
/// Variant1 { c: u8, d: u64 } = 10,
42+
/// }
43+
/// struct Demo2 { e: u8, f: u16, g: u8 }
44+
/// ```
45+
/// `b` is `FieldIdx(1)` in `VariantIdx(0)`,
46+
/// `d` is `FieldIdx(1)` in `VariantIdx(1)`, and
47+
/// `f` is `FieldIdx(1)` in `VariantIdx(0)`.
48+
#[cfg_attr(feature = "nightly", derive(rustc_macros::HashStable_Generic))]
49+
#[encodable]
50+
#[orderable]
51+
#[gate_rustc_only]
52+
pub struct FieldIdx {}
53+
}
54+
55+
impl FieldIdx {
56+
/// The second field, at index 1.
57+
///
58+
/// For use alongside [`FieldIdx::ZERO`], particularly with scalar pairs.
59+
pub const ONE: FieldIdx = FieldIdx::from_u32(1);
60+
}
61+
62+
rustc_index::newtype_index! {
63+
/// The *source-order* index of a variant in a type.
64+
///
65+
/// For enums, these are always `0..variant_count`, regardless of any
66+
/// custom discriminants that may have been defined, and including any
67+
/// variants that may end up uninhabited due to field types. (Some of the
68+
/// variants may not be present in a monomorphized ABI [`Variants`], but
69+
/// those skipped variants are always counted when determining the *index*.)
70+
///
71+
/// `struct`s, `tuples`, and `unions`s are considered to have a single variant
72+
/// with variant index zero, aka [`FIRST_VARIANT`].
73+
#[cfg_attr(feature = "nightly", derive(rustc_macros::HashStable_Generic))]
74+
#[encodable]
75+
#[orderable]
76+
#[gate_rustc_only]
77+
pub struct VariantIdx {
78+
/// Equivalent to `VariantIdx(0)`.
79+
const FIRST_VARIANT = 0;
80+
}
81+
}
2582

2683
// A variant is absent if it's uninhabited and only has ZST fields.
2784
// Present uninhabited variants only require space for their fields,

compiler/rustc_abi/src/layout/ty.rs

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,14 @@ use std::ops::Deref;
44
use rustc_data_structures::intern::Interned;
55
use rustc_macros::HashStable_Generic;
66

7+
use crate::layout::{FieldIdx, VariantIdx};
78
use crate::{
89
AbiAlign, Align, BackendRepr, FieldsShape, Float, HasDataLayout, LayoutData, Niche,
910
PointeeInfo, Primitive, Size, Variants,
1011
};
1112

1213
// Explicitly import `Float` to avoid ambiguity with `Primitive::Float`.
1314

14-
rustc_index::newtype_index! {
15-
/// The *source-order* index of a field in a variant.
16-
///
17-
/// This is how most code after type checking refers to fields, rather than
18-
/// using names (as names have hygiene complications and more complex lookup).
19-
///
20-
/// Particularly for `repr(Rust)` types, this may not be the same as *layout* order.
21-
/// (It is for `repr(C)` `struct`s, however.)
22-
///
23-
/// For example, in the following types,
24-
/// ```rust
25-
/// # enum Never {}
26-
/// # #[repr(u16)]
27-
/// enum Demo1 {
28-
/// Variant0 { a: Never, b: i32 } = 100,
29-
/// Variant1 { c: u8, d: u64 } = 10,
30-
/// }
31-
/// struct Demo2 { e: u8, f: u16, g: u8 }
32-
/// ```
33-
/// `b` is `FieldIdx(1)` in `VariantIdx(0)`,
34-
/// `d` is `FieldIdx(1)` in `VariantIdx(1)`, and
35-
/// `f` is `FieldIdx(1)` in `VariantIdx(0)`.
36-
#[derive(HashStable_Generic)]
37-
#[encodable]
38-
#[orderable]
39-
pub struct FieldIdx {}
40-
}
41-
42-
impl FieldIdx {
43-
/// The second field, at index 1.
44-
///
45-
/// For use alongside [`FieldIdx::ZERO`], particularly with scalar pairs.
46-
pub const ONE: FieldIdx = FieldIdx::from_u32(1);
47-
}
48-
49-
rustc_index::newtype_index! {
50-
/// The *source-order* index of a variant in a type.
51-
///
52-
/// For enums, these are always `0..variant_count`, regardless of any
53-
/// custom discriminants that may have been defined, and including any
54-
/// variants that may end up uninhabited due to field types. (Some of the
55-
/// variants may not be present in a monomorphized ABI [`Variants`], but
56-
/// those skipped variants are always counted when determining the *index*.)
57-
///
58-
/// `struct`s, `tuples`, and `unions`s are considered to have a single variant
59-
/// with variant index zero, aka [`FIRST_VARIANT`].
60-
#[derive(HashStable_Generic)]
61-
#[encodable]
62-
#[orderable]
63-
pub struct VariantIdx {
64-
/// Equivalent to `VariantIdx(0)`.
65-
const FIRST_VARIANT = 0;
66-
}
67-
}
6815
#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable_Generic)]
6916
#[rustc_pass_by_value]
7017
pub struct Layout<'a>(pub Interned<'a, LayoutData<FieldIdx, VariantIdx>>);

compiler/rustc_abi/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ pub use canon_abi::{ArmCall, CanonAbi, InterruptKind, X86Call};
6464
#[cfg(feature = "nightly")]
6565
pub use extern_abi::CVariadicStatus;
6666
pub use extern_abi::{ExternAbi, all_names};
67+
pub use layout::{FIRST_VARIANT, FieldIdx, LayoutCalculator, LayoutCalculatorError, VariantIdx};
6768
#[cfg(feature = "nightly")]
68-
pub use layout::{FIRST_VARIANT, FieldIdx, Layout, TyAbiInterface, TyAndLayout, VariantIdx};
69-
pub use layout::{LayoutCalculator, LayoutCalculatorError};
69+
pub use layout::{Layout, TyAbiInterface, TyAndLayout};
7070

7171
/// Requirements for a `StableHashingContext` to be used in this crate.
7272
/// This is a hack to allow using the `HashStable_Generic` derive macro

0 commit comments

Comments
 (0)