Skip to content

Commit add37c0

Browse files
committed
extend some comments and clarify some names around enum tag type computation
1 parent 90b6588 commit add37c0

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
@@ -812,7 +812,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
812812
let (max, min) = largest_niche
813813
// We might have no inhabited variants, so pretend there's at least one.
814814
.unwrap_or((0, 0));
815-
let (min_ity, signed) = discr_range_of_repr(min, max); //Integer::repr_discr(tcx, ty, &repr, min, max);
815+
let (min_ity, signed) = discr_range_of_repr(min, max); //Integer::discr_range_of_repr(tcx, ty, &repr, min, max);
816816

817817
let mut align = dl.aggregate_align;
818818
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
@@ -639,8 +639,8 @@ fn layout_of_uncached<'tcx>(
639639
// UnsafeCell and UnsafePinned both disable niche optimizations
640640
let is_special_no_niche = def.is_unsafe_cell() || def.is_unsafe_pinned();
641641

642-
let get_discriminant_type =
643-
|min, max| abi::Integer::repr_discr(tcx, ty, &def.repr(), min, max);
642+
let discr_range_of_repr =
643+
|min, max| abi::Integer::discr_range_of_repr(tcx, ty, &def.repr(), min, max);
644644

645645
let discriminants_iter = || {
646646
def.is_enum()
@@ -663,7 +663,7 @@ fn layout_of_uncached<'tcx>(
663663
def.is_enum(),
664664
is_special_no_niche,
665665
tcx.layout_scalar_valid_range(def.did()),
666-
get_discriminant_type,
666+
discr_range_of_repr,
667667
discriminants_iter(),
668668
!maybe_unsized,
669669
)
@@ -688,7 +688,7 @@ fn layout_of_uncached<'tcx>(
688688
def.is_enum(),
689689
is_special_no_niche,
690690
tcx.layout_scalar_valid_range(def.did()),
691-
get_discriminant_type,
691+
discr_range_of_repr,
692692
discriminants_iter(),
693693
!maybe_unsized,
694694
) else {

0 commit comments

Comments
 (0)