Skip to content

Commit 50c483b

Browse files
committed
extend some comments and clarify some names around enum tag type computation
1 parent 15283f6 commit 50c483b

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

compiler/rustc_abi/src/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
814814
let (max, min) = largest_niche
815815
// We might have no inhabited variants, so pretend there's at least one.
816816
.unwrap_or((0, 0));
817-
let (min_ity, signed) = discr_range_of_repr(min, max); //Integer::repr_discr(tcx, ty, &repr, min, max);
817+
let (min_ity, signed) = discr_range_of_repr(min, max); //Integer::discr_range_of_repr(tcx, ty, &repr, min, max);
818818

819819
let mut align = dl.aggregate_align;
820820
let mut max_repr_align = repr.align;

compiler/rustc_abi/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ impl ReprOptions {
183183

184184
/// Returns the discriminant type, given these `repr` options.
185185
/// This must only be called on enums!
186+
///
187+
/// This is the "typeck type" of the discriminant, which is effectively the maximum size:
188+
/// discriminant values will be wrapped to fit (with a lint). Layout can later decide to use a
189+
/// smaller type for the tag that stores the discriminant at runtime and that will work just
190+
/// fine, it just induces casts when getting/setting the discriminant.
186191
pub fn discr_type(&self) -> IntegerType {
187192
self.int.unwrap_or(IntegerType::Pointer(true))
188193
}

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ impl abi::Integer {
7272
/// signed discriminant range and `#[repr]` attribute.
7373
/// N.B.: `u128` values above `i128::MAX` will be treated as signed, but
7474
/// that shouldn't affect anything, other than maybe debuginfo.
75-
fn repr_discr<'tcx>(
75+
///
76+
/// This is the basis for computing the type of the *tag* of an enum (which can be smaller than
77+
/// the type of the *discriminant*, which is determined by [`ReprOptions::discr_type`]).
78+
fn discr_range_of_repr<'tcx>(
7679
tcx: TyCtxt<'tcx>,
7780
ty: Ty<'tcx>,
7881
repr: &ReprOptions,

compiler/rustc_ty_utils/src/layout.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,8 @@ fn layout_of_uncached<'tcx>(
595595
// UnsafeCell and UnsafePinned both disable niche optimizations
596596
let is_special_no_niche = def.is_unsafe_cell() || def.is_unsafe_pinned();
597597

598-
let get_discriminant_type =
599-
|min, max| abi::Integer::repr_discr(tcx, ty, &def.repr(), min, max);
598+
let discr_range_of_repr =
599+
|min, max| abi::Integer::discr_range_of_repr(tcx, ty, &def.repr(), min, max);
600600

601601
let discriminants_iter = || {
602602
def.is_enum()
@@ -619,7 +619,7 @@ fn layout_of_uncached<'tcx>(
619619
def.is_enum(),
620620
is_special_no_niche,
621621
tcx.layout_scalar_valid_range(def.did()),
622-
get_discriminant_type,
622+
discr_range_of_repr,
623623
discriminants_iter(),
624624
!maybe_unsized,
625625
)
@@ -644,7 +644,7 @@ fn layout_of_uncached<'tcx>(
644644
def.is_enum(),
645645
is_special_no_niche,
646646
tcx.layout_scalar_valid_range(def.did()),
647-
get_discriminant_type,
647+
discr_range_of_repr,
648648
discriminants_iter(),
649649
!maybe_unsized,
650650
) else {

0 commit comments

Comments
 (0)