Skip to content

Commit 60a5d2d

Browse files
authored
Rollup merge of #113969 - ericmarkmartin:smir-ty-dynamic, r=spastorino
add dynamic for smir r? spastorino
2 parents 48c0c25 + badb617 commit 60a5d2d

File tree

3 files changed

+127
-4
lines changed

3 files changed

+127
-4
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ pub fn br_named_def(did: DefId) -> stable_mir::ty::BrNamedDef {
5959
with_tables(|t| t.br_named_def(did))
6060
}
6161

62+
pub fn trait_def(did: DefId) -> stable_mir::ty::TraitDef {
63+
with_tables(|t| t.trait_def(did))
64+
}
65+
6266
impl<'tcx> Tables<'tcx> {
6367
pub fn item_def_id(&self, item: &stable_mir::CrateItem) -> DefId {
6468
self.def_ids[item.0]
@@ -100,6 +104,10 @@ impl<'tcx> Tables<'tcx> {
100104
stable_mir::ty::BrNamedDef(self.create_def_id(did))
101105
}
102106

107+
pub fn trait_def(&mut self, did: DefId) -> stable_mir::ty::TraitDef {
108+
stable_mir::ty::TraitDef(self.create_def_id(did))
109+
}
110+
103111
fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
104112
// FIXME: this becomes inefficient when we have too many ids
105113
for (i, &d) in self.def_ids.iter().enumerate() {

compiler/rustc_smir/src/rustc_smir/mod.rs

+83-4
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,72 @@ impl<'tcx> Stable<'tcx> for ty::AliasTy<'tcx> {
258258
}
259259
}
260260

261+
impl<'tcx> Stable<'tcx> for ty::DynKind {
262+
type T = stable_mir::ty::DynKind;
263+
264+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
265+
use ty::DynKind;
266+
match self {
267+
DynKind::Dyn => stable_mir::ty::DynKind::Dyn,
268+
DynKind::DynStar => stable_mir::ty::DynKind::DynStar,
269+
}
270+
}
271+
}
272+
273+
impl<'tcx> Stable<'tcx> for ty::ExistentialPredicate<'tcx> {
274+
type T = stable_mir::ty::ExistentialPredicate;
275+
276+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
277+
use stable_mir::ty::ExistentialPredicate::*;
278+
match self {
279+
ty::ExistentialPredicate::Trait(existential_trait_ref) => {
280+
Trait(existential_trait_ref.stable(tables))
281+
}
282+
ty::ExistentialPredicate::Projection(existential_projection) => {
283+
Projection(existential_projection.stable(tables))
284+
}
285+
ty::ExistentialPredicate::AutoTrait(def_id) => AutoTrait(tables.trait_def(*def_id)),
286+
}
287+
}
288+
}
289+
290+
impl<'tcx> Stable<'tcx> for ty::ExistentialTraitRef<'tcx> {
291+
type T = stable_mir::ty::ExistentialTraitRef;
292+
293+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
294+
let ty::ExistentialTraitRef { def_id, args } = self;
295+
stable_mir::ty::ExistentialTraitRef {
296+
def_id: tables.trait_def(*def_id),
297+
generic_args: args.stable(tables),
298+
}
299+
}
300+
}
301+
302+
impl<'tcx> Stable<'tcx> for ty::TermKind<'tcx> {
303+
type T = stable_mir::ty::TermKind;
304+
305+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
306+
use stable_mir::ty::TermKind;
307+
match self {
308+
ty::TermKind::Ty(ty) => TermKind::Type(tables.intern_ty(*ty)),
309+
ty::TermKind::Const(const_) => TermKind::Const(opaque(const_)),
310+
}
311+
}
312+
}
313+
314+
impl<'tcx> Stable<'tcx> for ty::ExistentialProjection<'tcx> {
315+
type T = stable_mir::ty::ExistentialProjection;
316+
317+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
318+
let ty::ExistentialProjection { def_id, args, term } = self;
319+
stable_mir::ty::ExistentialProjection {
320+
def_id: tables.trait_def(*def_id),
321+
generic_args: args.stable(tables),
322+
term: term.unpack().stable(tables),
323+
}
324+
}
325+
}
326+
261327
impl<'tcx> Stable<'tcx> for ty::adjustment::PointerCoercion {
262328
type T = stable_mir::mir::PointerCoercion;
263329
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
@@ -525,13 +591,17 @@ impl<'tcx> Stable<'tcx> for ty::GenericArgs<'tcx> {
525591
}
526592
}
527593

528-
impl<'tcx> Stable<'tcx> for ty::PolyFnSig<'tcx> {
529-
type T = stable_mir::ty::PolyFnSig;
594+
impl<'tcx, S, V> Stable<'tcx> for ty::Binder<'tcx, S>
595+
where
596+
S: Stable<'tcx, T = V>,
597+
{
598+
type T = stable_mir::ty::Binder<V>;
599+
530600
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
531601
use stable_mir::ty::Binder;
532602

533603
Binder {
534-
value: self.skip_binder().stable(tables),
604+
value: self.as_ref().skip_binder().stable(tables),
535605
bound_vars: self
536606
.bound_vars()
537607
.iter()
@@ -671,7 +741,16 @@ impl<'tcx> Stable<'tcx> for Ty<'tcx> {
671741
generic_args.stable(tables),
672742
)),
673743
ty::FnPtr(poly_fn_sig) => TyKind::RigidTy(RigidTy::FnPtr(poly_fn_sig.stable(tables))),
674-
ty::Dynamic(_, _, _) => todo!(),
744+
ty::Dynamic(existential_predicates, region, dyn_kind) => {
745+
TyKind::RigidTy(RigidTy::Dynamic(
746+
existential_predicates
747+
.iter()
748+
.map(|existential_predicate| existential_predicate.stable(tables))
749+
.collect(),
750+
opaque(region),
751+
dyn_kind.stable(tables),
752+
))
753+
}
675754
ty::Closure(def_id, generic_args) => TyKind::RigidTy(RigidTy::Closure(
676755
rustc_internal::closure_def(*def_id),
677756
generic_args.stable(tables),

compiler/rustc_smir/src/stable_mir/ty.rs

+36
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub enum RigidTy {
3838
FnPtr(PolyFnSig),
3939
Closure(ClosureDef, GenericArgs),
4040
Generator(GeneratorDef, GenericArgs, Movability),
41+
Dynamic(Vec<Binder<ExistentialPredicate>>, Region, DynKind),
4142
Never,
4243
Tuple(Vec<Ty>),
4344
}
@@ -98,6 +99,9 @@ pub struct AdtDef(pub(crate) DefId);
9899
#[derive(Clone, PartialEq, Eq, Debug)]
99100
pub struct AliasDef(pub(crate) DefId);
100101

102+
#[derive(Clone, PartialEq, Eq, Debug)]
103+
pub struct TraitDef(pub(crate) DefId);
104+
101105
#[derive(Clone, Debug)]
102106
pub struct GenericArgs(pub Vec<GenericArgKind>);
103107

@@ -108,6 +112,12 @@ pub enum GenericArgKind {
108112
Const(Const),
109113
}
110114

115+
#[derive(Clone, Debug)]
116+
pub enum TermKind {
117+
Type(Ty),
118+
Const(Const),
119+
}
120+
111121
#[derive(Clone, Debug)]
112122
pub enum AliasKind {
113123
Projection,
@@ -192,3 +202,29 @@ pub enum BoundRegionKind {
192202
BrNamed(BrNamedDef, String),
193203
BrEnv,
194204
}
205+
206+
#[derive(Clone, Debug)]
207+
pub enum DynKind {
208+
Dyn,
209+
DynStar,
210+
}
211+
212+
#[derive(Clone, Debug)]
213+
pub enum ExistentialPredicate {
214+
Trait(ExistentialTraitRef),
215+
Projection(ExistentialProjection),
216+
AutoTrait(TraitDef),
217+
}
218+
219+
#[derive(Clone, Debug)]
220+
pub struct ExistentialTraitRef {
221+
pub def_id: TraitDef,
222+
pub generic_args: GenericArgs,
223+
}
224+
225+
#[derive(Clone, Debug)]
226+
pub struct ExistentialProjection {
227+
pub def_id: TraitDef,
228+
pub generic_args: GenericArgs,
229+
pub term: TermKind,
230+
}

0 commit comments

Comments
 (0)