From 4199a3c13a05d05fcc863d506ee2aed74228b8e7 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 4 Aug 2023 17:01:05 -0300 Subject: [PATCH 1/4] Convert unsafety using the stable method and reuse mir::Safety --- compiler/rustc_smir/src/rustc_smir/mod.rs | 7 ++----- compiler/rustc_smir/src/stable_mir/ty.rs | 10 ++-------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index e377843cdb436..6eb7e93042560 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -826,7 +826,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 +835,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 }, diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index df28fc4eed9ff..be917b2e14013 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)] @@ -140,16 +140,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, From 496faa857c4bc66a73d36e9bb06fbedd99f7fe1c Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 4 Aug 2023 17:23:26 -0300 Subject: [PATCH 2/4] Convert trait declaration to SMIR --- compiler/rustc_smir/src/rustc_internal/mod.rs | 4 ++ compiler/rustc_smir/src/rustc_smir/mod.rs | 52 +++++++++++++++++-- compiler/rustc_smir/src/stable_mir/mod.rs | 3 +- compiler/rustc_smir/src/stable_mir/ty.rs | 21 ++++++++ 4 files changed, 76 insertions(+), 4 deletions(-) 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 6eb7e93042560..239b61b433d64 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -41,6 +41,13 @@ impl<'tcx> Context for Tables<'tcx> { fn entry_fn(&mut self) -> Option { Some(self.crate_item(self.tcx.entry_fn(())?.0)) } + + 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 +522,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() @@ -1045,7 +1052,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; @@ -1053,7 +1060,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; @@ -1091,3 +1098,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..0f32f07dcb5da 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; @@ -84,6 +84,7 @@ 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 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 be917b2e14013..03089fef01a99 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -11,6 +11,7 @@ impl Ty { } pub(crate) type Const = Opaque; +type Ident = Opaque; pub(crate) type Region = Opaque; type Span = Opaque; @@ -258,3 +259,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, +} From 0e69a8ad20522467ea1b0c32720bbc7faa1eef13 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 4 Aug 2023 17:44:41 -0300 Subject: [PATCH 3/4] Add all_trait_decls to SMIR --- compiler/rustc_smir/src/rustc_smir/mod.rs | 8 ++++++++ compiler/rustc_smir/src/stable_mir/mod.rs | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 239b61b433d64..d12de92db8a64 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -42,6 +42,14 @@ impl<'tcx> Context for Tables<'tcx> { 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); diff --git a/compiler/rustc_smir/src/stable_mir/mod.rs b/compiler/rustc_smir/src/stable_mir/mod.rs index 0f32f07dcb5da..d93f25249b9ef 100644 --- a/compiler/rustc_smir/src/stable_mir/mod.rs +++ b/compiler/rustc_smir/src/stable_mir/mod.rs @@ -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,7 @@ 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; From 6e4d7bd90e58ad0e26458b277d1c7e7f4401734d Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sat, 5 Aug 2023 22:08:30 -0300 Subject: [PATCH 4/4] Add TraitDef::trait_decl method --- compiler/rustc_smir/src/stable_mir/ty.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index 03089fef01a99..c487db5b732a5 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -105,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);