Rollup merge of #114485 - spastorino:add-trait-decls, r=oli-obk

Add trait decls to SMIR

r? `@oli-obk`

Closes https://github.com/rust-lang/project-stable-mir/issues/20
This commit is contained in:
Matthias Krüger 2023-08-07 16:47:55 +02:00 committed by GitHub
commit 7a52fe5396
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 17 deletions

View File

@ -68,6 +68,10 @@ pub fn item_def_id(&self, item: &stable_mir::CrateItem) -> DefId {
self.def_ids[item.0]
}
pub fn trait_def_id(&self, trait_def: &stable_mir::ty::TraitDef) -> DefId {
self.def_ids[trait_def.0]
}
pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem {
stable_mir::CrateItem(self.create_def_id(did))
}

View File

@ -41,6 +41,21 @@ fn all_local_items(&mut self) -> stable_mir::CrateItems {
fn entry_fn(&mut self) -> Option<stable_mir::CrateItem> {
Some(self.crate_item(self.tcx.entry_fn(())?.0))
}
fn all_trait_decls(&mut self) -> stable_mir::TraitDecls {
self.tcx
.traits(LOCAL_CRATE)
.iter()
.map(|trait_def_id| self.trait_def(*trait_def_id))
.collect()
}
fn trait_decl(&mut self, trait_def: &stable_mir::ty::TraitDef) -> stable_mir::ty::TraitDecl {
let def_id = self.trait_def_id(trait_def);
let trait_def = self.tcx.trait_def(def_id);
trait_def.stable(self)
}
fn mir_body(&mut self, item: &stable_mir::CrateItem) -> stable_mir::mir::Body {
let def_id = self.item_def_id(item);
let mir = self.tcx.optimized_mir(def_id);
@ -515,7 +530,7 @@ fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
}
}
impl<'tcx> Stable<'tcx> for rustc_middle::ty::UserTypeAnnotationIndex {
impl<'tcx> Stable<'tcx> for ty::UserTypeAnnotationIndex {
type T = usize;
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
self.as_usize()
@ -826,7 +841,7 @@ impl<'tcx> Stable<'tcx> for ty::FnSig<'tcx> {
type T = stable_mir::ty::FnSig;
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
use rustc_target::spec::abi;
use stable_mir::ty::{Abi, FnSig, Unsafety};
use stable_mir::ty::{Abi, FnSig};
FnSig {
inputs_and_output: self
@ -835,10 +850,7 @@ fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
.map(|ty| tables.intern_ty(ty))
.collect(),
c_variadic: self.c_variadic,
unsafety: match self.unsafety {
hir::Unsafety::Normal => Unsafety::Normal,
hir::Unsafety::Unsafe => Unsafety::Unsafe,
},
unsafety: self.unsafety.stable(tables),
abi: match self.abi {
abi::Abi::Rust => Abi::Rust,
abi::Abi::C { unwind } => Abi::C { unwind },
@ -1048,7 +1060,7 @@ fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
}
}
impl<'tcx> Stable<'tcx> for rustc_middle::ty::ParamTy {
impl<'tcx> Stable<'tcx> for ty::ParamTy {
type T = stable_mir::ty::ParamTy;
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
use stable_mir::ty::ParamTy;
@ -1056,7 +1068,7 @@ fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
}
}
impl<'tcx> Stable<'tcx> for rustc_middle::ty::BoundTy {
impl<'tcx> Stable<'tcx> for ty::BoundTy {
type T = stable_mir::ty::BoundTy;
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
use stable_mir::ty::BoundTy;
@ -1094,3 +1106,42 @@ fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
}
}
}
impl<'tcx> Stable<'tcx> for ty::trait_def::TraitSpecializationKind {
type T = stable_mir::ty::TraitSpecializationKind;
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
use stable_mir::ty::TraitSpecializationKind;
match self {
ty::trait_def::TraitSpecializationKind::None => TraitSpecializationKind::None,
ty::trait_def::TraitSpecializationKind::Marker => TraitSpecializationKind::Marker,
ty::trait_def::TraitSpecializationKind::AlwaysApplicable => {
TraitSpecializationKind::AlwaysApplicable
}
}
}
}
impl<'tcx> Stable<'tcx> for ty::TraitDef {
type T = stable_mir::ty::TraitDecl;
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
use stable_mir::ty::TraitDecl;
TraitDecl {
def_id: rustc_internal::trait_def(self.def_id),
unsafety: self.unsafety.stable(tables),
paren_sugar: self.paren_sugar,
has_auto_impl: self.has_auto_impl,
is_marker: self.is_marker,
is_coinductive: self.is_coinductive,
skip_array_during_method_dispatch: self.skip_array_during_method_dispatch,
specialization_kind: self.specialization_kind.stable(tables),
must_implement_one_of: self
.must_implement_one_of
.as_ref()
.map(|idents| idents.iter().map(|ident| opaque(ident)).collect()),
implement_via_object: self.implement_via_object,
deny_explicit_impl: self.deny_explicit_impl,
}
}
}

View File

@ -15,7 +15,7 @@
use crate::rustc_smir::Tables;
use self::ty::{Ty, TyKind};
use self::ty::{TraitDecl, TraitDef, Ty, TyKind};
pub mod mir;
pub mod ty;
@ -32,6 +32,9 @@
/// A list of crate items.
pub type CrateItems = Vec<CrateItem>;
/// A list of crate items.
pub type TraitDecls = Vec<TraitDef>;
/// Holds information about a crate.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Crate {
@ -84,6 +87,8 @@ pub trait Context {
/// Retrieve all items of the local crate that have a MIR associated with them.
fn all_local_items(&mut self) -> CrateItems;
fn mir_body(&mut self, item: &CrateItem) -> mir::Body;
fn all_trait_decls(&mut self) -> TraitDecls;
fn trait_decl(&mut self, trait_def: &TraitDef) -> TraitDecl;
/// Get information about the local crate.
fn local_crate(&self) -> Crate;
/// Retrieve a list of all external crates.

View File

@ -1,4 +1,4 @@
use super::{mir::Mutability, with, DefId};
use super::{mir::Mutability, mir::Safety, with, DefId};
use crate::rustc_internal::Opaque;
#[derive(Copy, Clone, Debug)]
@ -11,6 +11,7 @@ pub fn kind(&self) -> TyKind {
}
pub(crate) type Const = Opaque;
type Ident = Opaque;
pub(crate) type Region = Opaque;
type Span = Opaque;
@ -104,6 +105,12 @@ pub enum Movability {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TraitDef(pub(crate) DefId);
impl TraitDef {
pub fn trait_decl(&self) -> TraitDecl {
with(|cx| cx.trait_decl(self))
}
}
#[derive(Clone, Debug)]
pub struct GenericArgs(pub Vec<GenericArgKind>);
@ -140,16 +147,10 @@ pub struct AliasTy {
pub struct FnSig {
pub inputs_and_output: Vec<Ty>,
pub c_variadic: bool,
pub unsafety: Unsafety,
pub unsafety: Safety,
pub abi: Abi,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Unsafety {
Unsafe,
Normal,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Abi {
Rust,
@ -264,3 +265,23 @@ pub struct Allocation {
pub align: Align,
pub mutability: Mutability,
}
pub enum TraitSpecializationKind {
None,
Marker,
AlwaysApplicable,
}
pub struct TraitDecl {
pub def_id: TraitDef,
pub unsafety: Safety,
pub paren_sugar: bool,
pub has_auto_impl: bool,
pub is_marker: bool,
pub is_coinductive: bool,
pub skip_array_during_method_dispatch: bool,
pub specialization_kind: TraitSpecializationKind,
pub must_implement_one_of: Option<Vec<Ident>>,
pub implement_via_object: bool,
pub deny_explicit_impl: bool,
}