Skip to content

Commit 3a4b906

Browse files
committed
refactor: rewrite adt_repr(), adt_discr_for_variant() and coroutine_discr_for_variant()
1 parent 34a1151 commit 3a4b906

File tree

4 files changed

+88
-19
lines changed

4 files changed

+88
-19
lines changed

compiler/rustc_smir/src/rustc_smir/context/impls.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
use std::iter;
66

7-
use rustc_abi::{Endian, Layout};
7+
use rustc_abi::{Endian, Layout, ReprOptions};
88
use rustc_hir::def::DefKind;
99
use rustc_hir::{Attribute, LangItem};
1010
use rustc_middle::mir::interpret::{AllocId, ConstAllocation, ErrorHandled, GlobalAlloc, Scalar};
1111
use rustc_middle::mir::{BinOp, Body, Const as MirConst, ConstValue, UnOp};
1212
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
1313
use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths};
14+
use rustc_middle::ty::util::Discr;
1415
use rustc_middle::ty::{
1516
AdtDef, AdtKind, AssocItem, Binder, ClosureKind, CoroutineArgsExt, EarlyBinder,
1617
ExistentialTraitRef, FnSig, GenericArgsRef, Instance, InstanceKind, IntrinsicDef, List,
@@ -363,6 +364,11 @@ impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> {
363364
self.tcx.is_lang_item(def_id, LangItem::CStr)
364365
}
365366

367+
/// Returns the representation options for this ADT.
368+
pub fn adt_repr(&self, def: AdtDef<'tcx>) -> ReprOptions {
369+
def.repr()
370+
}
371+
366372
/// Retrieve the function signature for the given generic arguments.
367373
pub fn fn_sig(
368374
&self,
@@ -394,6 +400,25 @@ impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> {
394400
def.variants().len()
395401
}
396402

403+
/// Discriminant for a given variant index of AdtDef.
404+
pub fn adt_discr_for_variant(
405+
&self,
406+
adt: AdtDef<'tcx>,
407+
variant: rustc_abi::VariantIdx,
408+
) -> Discr<'tcx> {
409+
adt.discriminant_for_variant(self.tcx, variant)
410+
}
411+
412+
/// Discriminant for a given variand index and args of a coroutine.
413+
pub fn coroutine_discr_for_variant(
414+
&self,
415+
coroutine: DefId,
416+
args: GenericArgsRef<'tcx>,
417+
variant: rustc_abi::VariantIdx,
418+
) -> Discr<'tcx> {
419+
args.as_coroutine().discriminant_for_variant(coroutine, self.tcx, variant)
420+
}
421+
397422
/// The name of a variant.
398423
pub fn variant_name(&self, def: &'tcx VariantDef) -> String {
399424
def.name.to_string()

compiler/rustc_smir/src/stable_mir/compiler_interface.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ pub(crate) trait SmirInterface {
152152
/// Returns whether this definition is a C string.
153153
fn adt_is_cstr(&self, def: AdtDef) -> bool;
154154

155+
/// Returns the representation options for this ADT.
156+
fn adt_repr(&self, def: AdtDef) -> ReprOptions;
157+
155158
/// Retrieve the function signature for the given generic arguments.
156159
fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig;
157160

@@ -167,6 +170,17 @@ pub(crate) trait SmirInterface {
167170
/// The number of variants in this ADT.
168171
fn adt_variants_len(&self, def: AdtDef) -> usize;
169172

173+
/// Discriminant for a given variant index of AdtDef.
174+
fn adt_discr_for_variant(&self, adt: AdtDef, variant: VariantIdx) -> Discr;
175+
176+
/// Discriminant for a given variand index and args of a coroutine.
177+
fn coroutine_discr_for_variant(
178+
&self,
179+
coroutine: CoroutineDef,
180+
args: &GenericArgs,
181+
variant: VariantIdx,
182+
) -> Discr;
183+
170184
/// The name of a variant.
171185
fn variant_name(&self, def: VariantDef) -> Symbol;
172186
fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef>;
@@ -588,8 +602,10 @@ impl<'tcx> SmirInterface for SmirContainer<'tcx, BridgeTys> {
588602
}
589603

590604
/// Returns the representation options for this ADT
591-
pub(crate) fn adt_repr(&self, def: AdtDef) -> ReprOptions {
592-
self.cx.adt_repr(def)
605+
fn adt_repr(&self, def: AdtDef) -> ReprOptions {
606+
let mut tables = self.tables.borrow_mut();
607+
let cx = &*self.cx.borrow();
608+
cx.adt_repr(def.internal(&mut *tables, cx.tcx)).stable(&mut *tables, cx)
593609
}
594610

595611
/// Retrieve the function signature for the given generic arguments.
@@ -632,19 +648,31 @@ impl<'tcx> SmirInterface for SmirContainer<'tcx, BridgeTys> {
632648
cx.adt_variants_len(def.internal(&mut *tables, cx.tcx))
633649
}
634650

635-
/// Discriminant for a given variant index of AdtDef
636-
pub(crate) fn adt_discr_for_variant(&self, adt: AdtDef, variant: VariantIdx) -> Discr {
637-
self.cx.adt_discr_for_variant(adt, variant)
651+
/// Discriminant for a given variant index of AdtDef.
652+
fn adt_discr_for_variant(&self, adt: AdtDef, variant: VariantIdx) -> Discr {
653+
let mut tables = self.tables.borrow_mut();
654+
let cx = &*self.cx.borrow();
655+
cx.adt_discr_for_variant(
656+
adt.internal(&mut *tables, cx.tcx),
657+
variant.internal(&mut *tables, cx.tcx),
658+
)
659+
.stable(&mut *tables, cx)
638660
}
639661

640-
/// Discriminant for a given variand index and args of a coroutine
641-
pub(crate) fn coroutine_discr_for_variant(
662+
/// Discriminant for a given variand index and args of a coroutine.
663+
fn coroutine_discr_for_variant(
642664
&self,
643665
coroutine: CoroutineDef,
644666
args: &GenericArgs,
645667
variant: VariantIdx,
646668
) -> Discr {
647-
self.cx.coroutine_discr_for_variant(coroutine, args, variant)
669+
let mut tables = self.tables.borrow_mut();
670+
let cx = &*self.cx.borrow();
671+
let tcx = cx.tcx;
672+
let def = coroutine.def_id().internal(&mut *tables, tcx);
673+
let args_ref = args.internal(&mut *tables, tcx);
674+
cx.coroutine_discr_for_variant(def, args_ref, variant.internal(&mut *tables, tcx))
675+
.stable(&mut *tables, cx)
648676
}
649677

650678
/// The name of a variant.

compiler/rustc_smir/src/stable_mir/unstable/convert/stable/abi.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,11 @@ impl<'tcx> Stable<'tcx> for rustc_abi::WrappingRange {
362362
impl<'tcx> Stable<'tcx> for rustc_abi::ReprFlags {
363363
type T = ReprFlags;
364364

365-
fn stable(&self, _tables: &mut Tables<'_>) -> Self::T {
365+
fn stable<'cx>(
366+
&self,
367+
_tables: &mut Tables<'cx, BridgeTys>,
368+
_cx: &SmirCtxt<'cx, BridgeTys>,
369+
) -> Self::T {
366370
ReprFlags {
367371
is_simd: self.intersects(Self::IS_SIMD),
368372
is_c: self.intersects(Self::IS_C),
@@ -375,11 +379,15 @@ impl<'tcx> Stable<'tcx> for rustc_abi::ReprFlags {
375379
impl<'tcx> Stable<'tcx> for rustc_abi::IntegerType {
376380
type T = IntegerType;
377381

378-
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
382+
fn stable<'cx>(
383+
&self,
384+
tables: &mut Tables<'cx, BridgeTys>,
385+
cx: &SmirCtxt<'cx, BridgeTys>,
386+
) -> Self::T {
379387
match self {
380388
rustc_abi::IntegerType::Pointer(signed) => IntegerType::Pointer { is_signed: *signed },
381389
rustc_abi::IntegerType::Fixed(integer, signed) => {
382-
IntegerType::Fixed { length: integer.stable(tables), is_signed: *signed }
390+
IntegerType::Fixed { length: integer.stable(tables, cx), is_signed: *signed }
383391
}
384392
}
385393
}
@@ -388,12 +396,16 @@ impl<'tcx> Stable<'tcx> for rustc_abi::IntegerType {
388396
impl<'tcx> Stable<'tcx> for rustc_abi::ReprOptions {
389397
type T = ReprOptions;
390398

391-
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
399+
fn stable<'cx>(
400+
&self,
401+
tables: &mut Tables<'cx, BridgeTys>,
402+
cx: &SmirCtxt<'cx, BridgeTys>,
403+
) -> Self::T {
392404
ReprOptions {
393-
int: self.int.map(|int| int.stable(tables)),
394-
align: self.align.map(|align| align.stable(tables)),
395-
pack: self.pack.map(|pack| pack.stable(tables)),
396-
flags: self.flags.stable(tables),
405+
int: self.int.map(|int| int.stable(tables, cx)),
406+
align: self.align.map(|align| align.stable(tables, cx)),
407+
pack: self.pack.map(|pack| pack.stable(tables, cx)),
408+
flags: self.flags.stable(tables, cx),
397409
}
398410
}
399411
}

compiler/rustc_smir/src/stable_mir/unstable/convert/stable/ty.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,11 @@ impl<'tcx> Stable<'tcx> for ty::ImplTraitInTraitData {
11401140
impl<'tcx> Stable<'tcx> for rustc_middle::ty::util::Discr<'tcx> {
11411141
type T = stable_mir::ty::Discr;
11421142

1143-
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
1144-
stable_mir::ty::Discr { val: self.val, ty: self.ty.stable(tables) }
1143+
fn stable<'cx>(
1144+
&self,
1145+
tables: &mut Tables<'cx, BridgeTys>,
1146+
cx: &SmirCtxt<'cx, BridgeTys>,
1147+
) -> Self::T {
1148+
stable_mir::ty::Discr { val: self.val, ty: self.ty.stable(tables, cx) }
11451149
}
11461150
}

0 commit comments

Comments
 (0)