Skip to content

Commit 7a52fe5

Browse files
authored
Rollup merge of #114485 - spastorino:add-trait-decls, r=oli-obk
Add trait decls to SMIR r? `@oli-obk` Closes rust-lang/project-stable-mir#20
2 parents 2a643b2 + 6e4d7bd commit 7a52fe5

File tree

4 files changed

+98
-17
lines changed

4 files changed

+98
-17
lines changed

Diff for: compiler/rustc_smir/src/rustc_internal/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ impl<'tcx> Tables<'tcx> {
6868
self.def_ids[item.0]
6969
}
7070

71+
pub fn trait_def_id(&self, trait_def: &stable_mir::ty::TraitDef) -> DefId {
72+
self.def_ids[trait_def.0]
73+
}
74+
7175
pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem {
7276
stable_mir::CrateItem(self.create_def_id(did))
7377
}

Diff for: compiler/rustc_smir/src/rustc_smir/mod.rs

+59-8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ impl<'tcx> Context for Tables<'tcx> {
4141
fn entry_fn(&mut self) -> Option<stable_mir::CrateItem> {
4242
Some(self.crate_item(self.tcx.entry_fn(())?.0))
4343
}
44+
45+
fn all_trait_decls(&mut self) -> stable_mir::TraitDecls {
46+
self.tcx
47+
.traits(LOCAL_CRATE)
48+
.iter()
49+
.map(|trait_def_id| self.trait_def(*trait_def_id))
50+
.collect()
51+
}
52+
53+
fn trait_decl(&mut self, trait_def: &stable_mir::ty::TraitDef) -> stable_mir::ty::TraitDecl {
54+
let def_id = self.trait_def_id(trait_def);
55+
let trait_def = self.tcx.trait_def(def_id);
56+
trait_def.stable(self)
57+
}
58+
4459
fn mir_body(&mut self, item: &stable_mir::CrateItem) -> stable_mir::mir::Body {
4560
let def_id = self.item_def_id(item);
4661
let mir = self.tcx.optimized_mir(def_id);
@@ -515,7 +530,7 @@ impl<'tcx> Stable<'tcx> for mir::RetagKind {
515530
}
516531
}
517532

518-
impl<'tcx> Stable<'tcx> for rustc_middle::ty::UserTypeAnnotationIndex {
533+
impl<'tcx> Stable<'tcx> for ty::UserTypeAnnotationIndex {
519534
type T = usize;
520535
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
521536
self.as_usize()
@@ -826,7 +841,7 @@ impl<'tcx> Stable<'tcx> for ty::FnSig<'tcx> {
826841
type T = stable_mir::ty::FnSig;
827842
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
828843
use rustc_target::spec::abi;
829-
use stable_mir::ty::{Abi, FnSig, Unsafety};
844+
use stable_mir::ty::{Abi, FnSig};
830845

831846
FnSig {
832847
inputs_and_output: self
@@ -835,10 +850,7 @@ impl<'tcx> Stable<'tcx> for ty::FnSig<'tcx> {
835850
.map(|ty| tables.intern_ty(ty))
836851
.collect(),
837852
c_variadic: self.c_variadic,
838-
unsafety: match self.unsafety {
839-
hir::Unsafety::Normal => Unsafety::Normal,
840-
hir::Unsafety::Unsafe => Unsafety::Unsafe,
841-
},
853+
unsafety: self.unsafety.stable(tables),
842854
abi: match self.abi {
843855
abi::Abi::Rust => Abi::Rust,
844856
abi::Abi::C { unwind } => Abi::C { unwind },
@@ -1048,15 +1060,15 @@ impl<'tcx> Stable<'tcx> for Ty<'tcx> {
10481060
}
10491061
}
10501062

1051-
impl<'tcx> Stable<'tcx> for rustc_middle::ty::ParamTy {
1063+
impl<'tcx> Stable<'tcx> for ty::ParamTy {
10521064
type T = stable_mir::ty::ParamTy;
10531065
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
10541066
use stable_mir::ty::ParamTy;
10551067
ParamTy { index: self.index, name: self.name.to_string() }
10561068
}
10571069
}
10581070

1059-
impl<'tcx> Stable<'tcx> for rustc_middle::ty::BoundTy {
1071+
impl<'tcx> Stable<'tcx> for ty::BoundTy {
10601072
type T = stable_mir::ty::BoundTy;
10611073
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
10621074
use stable_mir::ty::BoundTy;
@@ -1094,3 +1106,42 @@ impl<'tcx> Stable<'tcx> for mir::interpret::Allocation {
10941106
}
10951107
}
10961108
}
1109+
1110+
impl<'tcx> Stable<'tcx> for ty::trait_def::TraitSpecializationKind {
1111+
type T = stable_mir::ty::TraitSpecializationKind;
1112+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
1113+
use stable_mir::ty::TraitSpecializationKind;
1114+
1115+
match self {
1116+
ty::trait_def::TraitSpecializationKind::None => TraitSpecializationKind::None,
1117+
ty::trait_def::TraitSpecializationKind::Marker => TraitSpecializationKind::Marker,
1118+
ty::trait_def::TraitSpecializationKind::AlwaysApplicable => {
1119+
TraitSpecializationKind::AlwaysApplicable
1120+
}
1121+
}
1122+
}
1123+
}
1124+
1125+
impl<'tcx> Stable<'tcx> for ty::TraitDef {
1126+
type T = stable_mir::ty::TraitDecl;
1127+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1128+
use stable_mir::ty::TraitDecl;
1129+
1130+
TraitDecl {
1131+
def_id: rustc_internal::trait_def(self.def_id),
1132+
unsafety: self.unsafety.stable(tables),
1133+
paren_sugar: self.paren_sugar,
1134+
has_auto_impl: self.has_auto_impl,
1135+
is_marker: self.is_marker,
1136+
is_coinductive: self.is_coinductive,
1137+
skip_array_during_method_dispatch: self.skip_array_during_method_dispatch,
1138+
specialization_kind: self.specialization_kind.stable(tables),
1139+
must_implement_one_of: self
1140+
.must_implement_one_of
1141+
.as_ref()
1142+
.map(|idents| idents.iter().map(|ident| opaque(ident)).collect()),
1143+
implement_via_object: self.implement_via_object,
1144+
deny_explicit_impl: self.deny_explicit_impl,
1145+
}
1146+
}
1147+
}

Diff for: compiler/rustc_smir/src/stable_mir/mod.rs

+6-1
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::{Ty, TyKind};
18+
use self::ty::{TraitDecl, TraitDef, Ty, TyKind};
1919

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

35+
/// A list of crate items.
36+
pub type TraitDecls = Vec<TraitDef>;
37+
3538
/// Holds information about a crate.
3639
#[derive(Clone, PartialEq, Eq, Debug)]
3740
pub struct Crate {
@@ -84,6 +87,8 @@ pub trait Context {
8487
/// Retrieve all items of the local crate that have a MIR associated with them.
8588
fn all_local_items(&mut self) -> CrateItems;
8689
fn mir_body(&mut self, item: &CrateItem) -> mir::Body;
90+
fn all_trait_decls(&mut self) -> TraitDecls;
91+
fn trait_decl(&mut self, trait_def: &TraitDef) -> TraitDecl;
8792
/// Get information about the local crate.
8893
fn local_crate(&self) -> Crate;
8994
/// Retrieve a list of all external crates.

Diff for: compiler/rustc_smir/src/stable_mir/ty.rs

+29-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{mir::Mutability, with, DefId};
1+
use super::{mir::Mutability, mir::Safety, with, DefId};
22
use crate::rustc_internal::Opaque;
33

44
#[derive(Copy, Clone, Debug)]
@@ -11,6 +11,7 @@ impl Ty {
1111
}
1212

1313
pub(crate) type Const = Opaque;
14+
type Ident = Opaque;
1415
pub(crate) type Region = Opaque;
1516
type Span = Opaque;
1617

@@ -104,6 +105,12 @@ pub struct AliasDef(pub(crate) DefId);
104105
#[derive(Clone, PartialEq, Eq, Debug)]
105106
pub struct TraitDef(pub(crate) DefId);
106107

108+
impl TraitDef {
109+
pub fn trait_decl(&self) -> TraitDecl {
110+
with(|cx| cx.trait_decl(self))
111+
}
112+
}
113+
107114
#[derive(Clone, Debug)]
108115
pub struct GenericArgs(pub Vec<GenericArgKind>);
109116

@@ -140,16 +147,10 @@ pub type PolyFnSig = Binder<FnSig>;
140147
pub struct FnSig {
141148
pub inputs_and_output: Vec<Ty>,
142149
pub c_variadic: bool,
143-
pub unsafety: Unsafety,
150+
pub unsafety: Safety,
144151
pub abi: Abi,
145152
}
146153

147-
#[derive(Clone, PartialEq, Eq, Debug)]
148-
pub enum Unsafety {
149-
Unsafe,
150-
Normal,
151-
}
152-
153154
#[derive(Clone, PartialEq, Eq, Debug)]
154155
pub enum Abi {
155156
Rust,
@@ -264,3 +265,23 @@ pub struct Allocation {
264265
pub align: Align,
265266
pub mutability: Mutability,
266267
}
268+
269+
pub enum TraitSpecializationKind {
270+
None,
271+
Marker,
272+
AlwaysApplicable,
273+
}
274+
275+
pub struct TraitDecl {
276+
pub def_id: TraitDef,
277+
pub unsafety: Safety,
278+
pub paren_sugar: bool,
279+
pub has_auto_impl: bool,
280+
pub is_marker: bool,
281+
pub is_coinductive: bool,
282+
pub skip_array_during_method_dispatch: bool,
283+
pub specialization_kind: TraitSpecializationKind,
284+
pub must_implement_one_of: Option<Vec<Ident>>,
285+
pub implement_via_object: bool,
286+
pub deny_explicit_impl: bool,
287+
}

0 commit comments

Comments
 (0)