Skip to content

Commit d99466d

Browse files
authored
Rollup merge of #115092 - ouz-a:smir_generic_of, r=spastorino
Add generics_of to smir Continuing our covering of smir. r? `@spastorino`
2 parents 21411c4 + 015b5cb commit d99466d

File tree

4 files changed

+129
-13
lines changed

4 files changed

+129
-13
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ impl<'tcx> Tables<'tcx> {
8080
self.def_ids[impl_def.0]
8181
}
8282

83+
pub fn generic_def_id(&self, generic_def: &stable_mir::ty::GenericDef) -> DefId {
84+
self.def_ids[generic_def.0]
85+
}
86+
8387
pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem {
8488
stable_mir::CrateItem(self.create_def_id(did))
8589
}
@@ -120,6 +124,10 @@ impl<'tcx> Tables<'tcx> {
120124
stable_mir::ty::TraitDef(self.create_def_id(did))
121125
}
122126

127+
pub fn generic_def(&mut self, did: DefId) -> stable_mir::ty::GenericDef {
128+
stable_mir::ty::GenericDef(self.create_def_id(did))
129+
}
130+
123131
pub fn const_def(&mut self, did: DefId) -> stable_mir::ty::ConstDef {
124132
stable_mir::ty::ConstDef(self.create_def_id(did))
125133
}

compiler/rustc_smir/src/rustc_smir/mod.rs

+72-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
use crate::rustc_internal::{self, opaque};
1111
use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx};
1212
use crate::stable_mir::ty::{
13-
allocation_filter, new_allocation, Const, FloatTy, IntTy, Movability, RigidTy, TyKind, UintTy,
13+
allocation_filter, new_allocation, Const, FloatTy, GenericDef, GenericParamDef, IntTy,
14+
Movability, RigidTy, TyKind, UintTy,
1415
};
1516
use crate::stable_mir::{self, Context};
1617
use rustc_hir as hir;
@@ -101,6 +102,12 @@ impl<'tcx> Context for Tables<'tcx> {
101102
let ty = self.types[ty.0];
102103
ty.stable(self)
103104
}
105+
106+
fn generics_of(&mut self, generic_def: &GenericDef) -> stable_mir::ty::Generics {
107+
let def_id = self.generic_def_id(generic_def);
108+
let generic_def = self.tcx.generics_of(def_id);
109+
generic_def.stable(self)
110+
}
104111
}
105112

106113
pub struct Tables<'tcx> {
@@ -1205,3 +1212,67 @@ impl<'tcx> Stable<'tcx> for ty::TraitRef<'tcx> {
12051212
TraitRef { def_id: rustc_internal::trait_def(self.def_id), args: self.args.stable(tables) }
12061213
}
12071214
}
1215+
1216+
impl<'tcx> Stable<'tcx> for ty::Generics {
1217+
type T = stable_mir::ty::Generics;
1218+
1219+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1220+
use stable_mir::ty::Generics;
1221+
1222+
let params: Vec<_> = self.params.iter().map(|param| param.stable(tables)).collect();
1223+
let param_def_id_to_index =
1224+
params.iter().map(|param| (param.def_id, param.index)).collect();
1225+
1226+
Generics {
1227+
parent: self.parent.map(|did| tables.generic_def(did)),
1228+
parent_count: self.parent_count,
1229+
params,
1230+
param_def_id_to_index,
1231+
has_self: self.has_self,
1232+
has_late_bound_regions: self
1233+
.has_late_bound_regions
1234+
.as_ref()
1235+
.map(|late_bound_regions| late_bound_regions.stable(tables)),
1236+
host_effect_index: self.host_effect_index,
1237+
}
1238+
}
1239+
}
1240+
1241+
impl<'tcx> Stable<'tcx> for rustc_span::Span {
1242+
type T = stable_mir::ty::Span;
1243+
1244+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
1245+
opaque(self)
1246+
}
1247+
}
1248+
1249+
impl<'tcx> Stable<'tcx> for rustc_middle::ty::GenericParamDefKind {
1250+
type T = stable_mir::ty::GenericParamDefKind;
1251+
1252+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
1253+
use stable_mir::ty::GenericParamDefKind;
1254+
match self {
1255+
ty::GenericParamDefKind::Lifetime => GenericParamDefKind::Lifetime,
1256+
ty::GenericParamDefKind::Type { has_default, synthetic } => {
1257+
GenericParamDefKind::Type { has_default: *has_default, synthetic: *synthetic }
1258+
}
1259+
ty::GenericParamDefKind::Const { has_default } => {
1260+
GenericParamDefKind::Const { has_default: *has_default }
1261+
}
1262+
}
1263+
}
1264+
}
1265+
1266+
impl<'tcx> Stable<'tcx> for rustc_middle::ty::GenericParamDef {
1267+
type T = stable_mir::ty::GenericParamDef;
1268+
1269+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1270+
GenericParamDef {
1271+
name: self.name.to_string(),
1272+
def_id: tables.generic_def(self.def_id),
1273+
index: self.index,
1274+
pure_wrt_drop: self.pure_wrt_drop,
1275+
kind: self.kind.stable(tables),
1276+
}
1277+
}
1278+
}

compiler/rustc_smir/src/stable_mir/mod.rs

+2-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::{ImplDef, ImplTrait, TraitDecl, TraitDef, Ty, TyKind};
18+
use self::ty::{GenericDef, Generics, ImplDef, ImplTrait, TraitDecl, TraitDef, Ty, TyKind};
1919

2020
pub mod mir;
2121
pub mod ty;
@@ -110,6 +110,7 @@ pub trait Context {
110110
fn trait_decl(&mut self, trait_def: &TraitDef) -> TraitDecl;
111111
fn all_trait_impls(&mut self) -> ImplTraitDecls;
112112
fn trait_impl(&mut self, trait_impl: &ImplDef) -> ImplTrait;
113+
fn generics_of(&mut self, generic_def: &GenericDef) -> Generics;
113114
/// Get information about the local crate.
114115
fn local_crate(&self) -> Crate;
115116
/// Retrieve a list of all external crates.

compiler/rustc_smir/src/stable_mir/ty.rs

+47-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct Const {
2222

2323
type Ident = Opaque;
2424
pub(crate) type Region = Opaque;
25-
type Span = Opaque;
25+
pub type Span = Opaque;
2626

2727
#[derive(Clone, Debug)]
2828
pub enum TyKind {
@@ -87,34 +87,37 @@ pub enum Movability {
8787
Movable,
8888
}
8989

90-
#[derive(Clone, PartialEq, Eq, Debug)]
90+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
9191
pub struct ForeignDef(pub(crate) DefId);
9292

93-
#[derive(Clone, PartialEq, Eq, Debug)]
93+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
9494
pub struct FnDef(pub(crate) DefId);
9595

96-
#[derive(Clone, PartialEq, Eq, Debug)]
96+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
9797
pub struct ClosureDef(pub(crate) DefId);
9898

99-
#[derive(Clone, PartialEq, Eq, Debug)]
99+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
100100
pub struct GeneratorDef(pub(crate) DefId);
101101

102-
#[derive(Clone, PartialEq, Eq, Debug)]
102+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
103103
pub struct ParamDef(pub(crate) DefId);
104104

105-
#[derive(Clone, PartialEq, Eq, Debug)]
105+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
106106
pub struct BrNamedDef(pub(crate) DefId);
107107

108-
#[derive(Clone, PartialEq, Eq, Debug)]
108+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
109109
pub struct AdtDef(pub(crate) DefId);
110110

111-
#[derive(Clone, PartialEq, Eq, Debug)]
111+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
112112
pub struct AliasDef(pub(crate) DefId);
113113

114-
#[derive(Clone, PartialEq, Eq, Debug)]
114+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
115115
pub struct TraitDef(pub(crate) DefId);
116116

117-
#[derive(Clone, PartialEq, Eq, Debug)]
117+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
118+
pub struct GenericDef(pub(crate) DefId);
119+
120+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
118121
pub struct ConstDef(pub(crate) DefId);
119122

120123
impl TraitDef {
@@ -132,6 +135,12 @@ impl ImplDef {
132135
}
133136
}
134137

138+
impl GenericDef {
139+
pub fn generics_of(&self) -> Generics {
140+
with(|tcx| tcx.generics_of(self))
141+
}
142+
}
143+
135144
#[derive(Clone, Debug)]
136145
pub struct GenericArgs(pub Vec<GenericArgKind>);
137146

@@ -461,3 +470,30 @@ pub struct TraitRef {
461470
pub def_id: TraitDef,
462471
pub args: GenericArgs,
463472
}
473+
474+
#[derive(Clone, Debug)]
475+
pub struct Generics {
476+
pub parent: Option<GenericDef>,
477+
pub parent_count: usize,
478+
pub params: Vec<GenericParamDef>,
479+
pub param_def_id_to_index: Vec<(GenericDef, u32)>,
480+
pub has_self: bool,
481+
pub has_late_bound_regions: Option<Span>,
482+
pub host_effect_index: Option<usize>,
483+
}
484+
485+
#[derive(Clone, Debug)]
486+
pub enum GenericParamDefKind {
487+
Lifetime,
488+
Type { has_default: bool, synthetic: bool },
489+
Const { has_default: bool },
490+
}
491+
492+
#[derive(Clone, Debug)]
493+
pub struct GenericParamDef {
494+
pub name: super::Symbol,
495+
pub def_id: GenericDef,
496+
pub index: u32,
497+
pub pure_wrt_drop: bool,
498+
pub kind: GenericParamDefKind,
499+
}

0 commit comments

Comments
 (0)