diff --git a/compiler/rustc_smir/src/rustc_internal/mod.rs b/compiler/rustc_smir/src/rustc_internal/mod.rs index 6a26a4a22ed42..39541c845b394 100644 --- a/compiler/rustc_smir/src/rustc_internal/mod.rs +++ b/compiler/rustc_smir/src/rustc_internal/mod.rs @@ -68,6 +68,10 @@ impl<'tcx> Tables<'tcx> { 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)) } diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index e377843cdb436..d12de92db8a64 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -41,6 +41,21 @@ impl<'tcx> Context for Tables<'tcx> { fn entry_fn(&mut self) -> Option { 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 @@ impl<'tcx> Stable<'tcx> for mir::RetagKind { } } -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 @@ impl<'tcx> Stable<'tcx> for ty::FnSig<'tcx> { .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 @@ impl<'tcx> Stable<'tcx> for Ty<'tcx> { } } -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 @@ impl<'tcx> Stable<'tcx> for rustc_middle::ty::ParamTy { } } -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 @@ impl<'tcx> Stable<'tcx> for mir::interpret::Allocation { } } } + +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, + } + } +} diff --git a/compiler/rustc_smir/src/stable_mir/mod.rs b/compiler/rustc_smir/src/stable_mir/mod.rs index 5e599a77bcda5..d93f25249b9ef 100644 --- a/compiler/rustc_smir/src/stable_mir/mod.rs +++ b/compiler/rustc_smir/src/stable_mir/mod.rs @@ -15,7 +15,7 @@ use std::cell::Cell; 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 @@ pub type DefId = usize; /// A list of crate items. pub type CrateItems = Vec; +/// A list of crate items. +pub type TraitDecls = Vec; + /// 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. diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index df28fc4eed9ff..c487db5b732a5 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -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 @@ impl Ty { } pub(crate) type Const = Opaque; +type Ident = Opaque; pub(crate) type Region = Opaque; type Span = Opaque; @@ -104,6 +105,12 @@ pub struct AliasDef(pub(crate) DefId); #[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); @@ -140,16 +147,10 @@ pub type PolyFnSig = Binder; pub struct FnSig { pub inputs_and_output: Vec, 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>, + pub implement_via_object: bool, + pub deny_explicit_impl: bool, +}