Skip to content

Commit 12551a5

Browse files
Rollup merge of #114599 - spastorino:add-impl-trait-smir, r=oli-obk
Add impl trait declarations to SMIR r? `@oli-obk`
2 parents a9b2c6a + 5f0e662 commit 12551a5

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ pub fn trait_def(did: DefId) -> stable_mir::ty::TraitDef {
6363
with_tables(|t| t.trait_def(did))
6464
}
6565

66+
pub fn impl_def(did: DefId) -> stable_mir::ty::ImplDef {
67+
with_tables(|t| t.impl_def(did))
68+
}
69+
6670
impl<'tcx> Tables<'tcx> {
6771
pub fn item_def_id(&self, item: &stable_mir::CrateItem) -> DefId {
6872
self.def_ids[item.0]
@@ -72,6 +76,10 @@ impl<'tcx> Tables<'tcx> {
7276
self.def_ids[trait_def.0]
7377
}
7478

79+
pub fn impl_trait_def_id(&self, impl_def: &stable_mir::ty::ImplDef) -> DefId {
80+
self.def_ids[impl_def.0]
81+
}
82+
7583
pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem {
7684
stable_mir::CrateItem(self.create_def_id(did))
7785
}
@@ -116,6 +124,10 @@ impl<'tcx> Tables<'tcx> {
116124
stable_mir::ty::ConstDef(self.create_def_id(did))
117125
}
118126

127+
pub fn impl_def(&mut self, did: DefId) -> stable_mir::ty::ImplDef {
128+
stable_mir::ty::ImplDef(self.create_def_id(did))
129+
}
130+
119131
fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
120132
// FIXME: this becomes inefficient when we have too many ids
121133
for (i, &d) in self.def_ids.iter().enumerate() {

compiler/rustc_smir/src/rustc_smir/mod.rs

+36
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ impl<'tcx> Context for Tables<'tcx> {
5959
trait_def.stable(self)
6060
}
6161

62+
fn all_trait_impls(&mut self) -> stable_mir::ImplTraitDecls {
63+
self.tcx
64+
.trait_impls_in_crate(LOCAL_CRATE)
65+
.iter()
66+
.map(|impl_def_id| self.impl_def(*impl_def_id))
67+
.collect()
68+
}
69+
70+
fn trait_impl(&mut self, impl_def: &stable_mir::ty::ImplDef) -> stable_mir::ty::ImplTrait {
71+
let def_id = self.impl_trait_def_id(impl_def);
72+
let impl_trait = self.tcx.impl_trait_ref(def_id).unwrap();
73+
impl_trait.stable(self)
74+
}
75+
6276
fn mir_body(&mut self, item: &stable_mir::CrateItem) -> stable_mir::mir::Body {
6377
let def_id = self.item_def_id(item);
6478
let mir = self.tcx.optimized_mir(def_id);
@@ -840,6 +854,19 @@ where
840854
}
841855
}
842856

857+
impl<'tcx, S, V> Stable<'tcx> for ty::EarlyBinder<S>
858+
where
859+
S: Stable<'tcx, T = V>,
860+
{
861+
type T = stable_mir::ty::EarlyBinder<V>;
862+
863+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
864+
use stable_mir::ty::EarlyBinder;
865+
866+
EarlyBinder { value: self.as_ref().skip_binder().stable(tables) }
867+
}
868+
}
869+
843870
impl<'tcx> Stable<'tcx> for ty::FnSig<'tcx> {
844871
type T = stable_mir::ty::FnSig;
845872
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
@@ -1154,3 +1181,12 @@ impl<'tcx> Stable<'tcx> for rustc_middle::mir::ConstantKind<'tcx> {
11541181
}
11551182
}
11561183
}
1184+
1185+
impl<'tcx> Stable<'tcx> for ty::TraitRef<'tcx> {
1186+
type T = stable_mir::ty::TraitRef;
1187+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1188+
use stable_mir::ty::TraitRef;
1189+
1190+
TraitRef { def_id: rustc_internal::trait_def(self.def_id), args: self.args.stable(tables) }
1191+
}
1192+
}

compiler/rustc_smir/src/stable_mir/mod.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::cell::Cell;
1515

1616
use crate::rustc_smir::Tables;
1717

18-
use self::ty::{TraitDecl, TraitDef, Ty, TyKind};
18+
use self::ty::{ImplDef, ImplTrait, TraitDecl, TraitDef, Ty, TyKind};
1919

2020
pub mod mir;
2121
pub mod ty;
@@ -32,9 +32,12 @@ pub type DefId = usize;
3232
/// A list of crate items.
3333
pub type CrateItems = Vec<CrateItem>;
3434

35-
/// A list of crate items.
35+
/// A list of trait decls.
3636
pub type TraitDecls = Vec<TraitDef>;
3737

38+
/// A list of impl trait decls.
39+
pub type ImplTraitDecls = Vec<ImplDef>;
40+
3841
/// Holds information about a crate.
3942
#[derive(Clone, PartialEq, Eq, Debug)]
4043
pub struct Crate {
@@ -89,6 +92,8 @@ pub trait Context {
8992
fn mir_body(&mut self, item: &CrateItem) -> mir::Body;
9093
fn all_trait_decls(&mut self) -> TraitDecls;
9194
fn trait_decl(&mut self, trait_def: &TraitDef) -> TraitDecl;
95+
fn all_trait_impls(&mut self) -> ImplTraitDecls;
96+
fn trait_impl(&mut self, trait_impl: &ImplDef) -> ImplTrait;
9297
/// Get information about the local crate.
9398
fn local_crate(&self) -> Crate;
9499
/// Retrieve a list of all external crates.

compiler/rustc_smir/src/stable_mir/ty.rs

+21
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ impl TraitDef {
119119
}
120120
}
121121

122+
#[derive(Clone, PartialEq, Eq, Debug)]
123+
pub struct ImplDef(pub(crate) DefId);
124+
125+
impl ImplDef {
126+
pub fn trait_impl(&self) -> ImplTrait {
127+
with(|cx| cx.trait_impl(self))
128+
}
129+
}
130+
122131
#[derive(Clone, Debug)]
123132
pub struct GenericArgs(pub Vec<GenericArgKind>);
124133

@@ -196,6 +205,11 @@ pub struct Binder<T> {
196205
pub bound_vars: Vec<BoundVariableKind>,
197206
}
198207

208+
#[derive(Clone, Debug)]
209+
pub struct EarlyBinder<T> {
210+
pub value: T,
211+
}
212+
199213
#[derive(Clone, Debug)]
200214
pub enum BoundVariableKind {
201215
Ty(BoundTyKind),
@@ -432,3 +446,10 @@ pub struct TraitDecl {
432446
pub implement_via_object: bool,
433447
pub deny_explicit_impl: bool,
434448
}
449+
450+
pub type ImplTrait = EarlyBinder<TraitRef>;
451+
452+
pub struct TraitRef {
453+
pub def_id: TraitDef,
454+
pub args: GenericArgs,
455+
}

0 commit comments

Comments
 (0)