Skip to content

Commit a51e830

Browse files
authored
Rollup merge of #115300 - spastorino:smir-tweaks, r=oli-obk
Tweaks and improvements on SMIR around generics_of and predicates_of r? `@oli-obk` This allows an API like the following ... ```rust let trait_decls = stable_mir::all_trait_decls().iter().map(|trait_def| { let trait_decl = stable_mir::trait_decl(trait_def); let generics = trait_decl.generics_of(); let predicates = trait_decl.predicates_of().predicates; ``` I didn't like that much `trait_def.trait_decl()` which is it possible but adding a method to a def_id that loads up a whole trait definition looks backwards to me.
2 parents 61c367c + 5ab9616 commit a51e830

File tree

4 files changed

+41
-71
lines changed

4 files changed

+41
-71
lines changed

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

+11-17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! until stable MIR is complete.
55
66
use std::fmt::Debug;
7+
use std::ops::Index;
78
use std::string::ToString;
89

910
use crate::rustc_internal;
@@ -24,7 +25,7 @@ fn with_tables<R>(mut f: impl FnMut(&mut Tables<'_>) -> R) -> R {
2425
}
2526

2627
pub fn item_def_id(item: &stable_mir::CrateItem) -> DefId {
27-
with_tables(|t| t.item_def_id(item))
28+
with_tables(|t| t[item.0])
2829
}
2930

3031
pub fn crate_item(did: DefId) -> stable_mir::CrateItem {
@@ -71,23 +72,16 @@ pub fn impl_def(did: DefId) -> stable_mir::ty::ImplDef {
7172
with_tables(|t| t.impl_def(did))
7273
}
7374

74-
impl<'tcx> Tables<'tcx> {
75-
pub fn item_def_id(&self, item: &stable_mir::CrateItem) -> DefId {
76-
self.def_ids[item.0]
77-
}
78-
79-
pub fn trait_def_id(&self, trait_def: &stable_mir::ty::TraitDef) -> DefId {
80-
self.def_ids[trait_def.0]
81-
}
75+
impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {
76+
type Output = DefId;
8277

83-
pub fn impl_trait_def_id(&self, impl_def: &stable_mir::ty::ImplDef) -> DefId {
84-
self.def_ids[impl_def.0]
85-
}
86-
87-
pub fn generic_def_id(&self, generic_def: &stable_mir::ty::GenericDef) -> DefId {
88-
self.def_ids[generic_def.0]
78+
#[inline(always)]
79+
fn index(&self, index: stable_mir::DefId) -> &Self::Output {
80+
&self.def_ids[index.0]
8981
}
82+
}
9083

84+
impl<'tcx> Tables<'tcx> {
9185
pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem {
9286
stable_mir::CrateItem(self.create_def_id(did))
9387
}
@@ -144,12 +138,12 @@ impl<'tcx> Tables<'tcx> {
144138
// FIXME: this becomes inefficient when we have too many ids
145139
for (i, &d) in self.def_ids.iter().enumerate() {
146140
if d == did {
147-
return i;
141+
return stable_mir::DefId(i);
148142
}
149143
}
150144
let id = self.def_ids.len();
151145
self.def_ids.push(did);
152-
id
146+
stable_mir::DefId(id)
153147
}
154148
}
155149

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

+13-16
Original file line numberDiff line numberDiff line change
@@ -10,8 +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, GenericDef, GenericParamDef, IntTy,
14-
Movability, RigidTy, TyKind, UintTy,
13+
allocation_filter, new_allocation, Const, FloatTy, GenericParamDef, IntTy, Movability, RigidTy,
14+
TyKind, UintTy,
1515
};
1616
use crate::stable_mir::{self, Context};
1717
use rustc_hir as hir;
@@ -54,7 +54,7 @@ impl<'tcx> Context for Tables<'tcx> {
5454
}
5555

5656
fn trait_decl(&mut self, trait_def: &stable_mir::ty::TraitDef) -> stable_mir::ty::TraitDecl {
57-
let def_id = self.trait_def_id(trait_def);
57+
let def_id = self[trait_def.0];
5858
let trait_def = self.tcx.trait_def(def_id);
5959
trait_def.stable(self)
6060
}
@@ -68,13 +68,13 @@ impl<'tcx> Context for Tables<'tcx> {
6868
}
6969

7070
fn trait_impl(&mut self, impl_def: &stable_mir::ty::ImplDef) -> stable_mir::ty::ImplTrait {
71-
let def_id = self.impl_trait_def_id(impl_def);
71+
let def_id = self[impl_def.0];
7272
let impl_trait = self.tcx.impl_trait_ref(def_id).unwrap();
7373
impl_trait.stable(self)
7474
}
7575

7676
fn mir_body(&mut self, item: &stable_mir::CrateItem) -> stable_mir::mir::Body {
77-
let def_id = self.item_def_id(item);
77+
let def_id = self[item.0];
7878
let mir = self.tcx.optimized_mir(def_id);
7979
stable_mir::mir::Body {
8080
blocks: mir
@@ -102,19 +102,16 @@ impl<'tcx> Context for Tables<'tcx> {
102102
ty.stable(self)
103103
}
104104

105-
fn generics_of(&mut self, generic_def: &GenericDef) -> stable_mir::ty::Generics {
106-
let def_id = self.generic_def_id(generic_def);
107-
let generic_def = self.tcx.generics_of(def_id);
108-
generic_def.stable(self)
105+
fn generics_of(&mut self, def_id: stable_mir::DefId) -> stable_mir::ty::Generics {
106+
let def_id = self[def_id];
107+
let generics = self.tcx.generics_of(def_id);
108+
generics.stable(self)
109109
}
110110

111-
fn predicates_of(
112-
&mut self,
113-
trait_def: &stable_mir::ty::TraitDef,
114-
) -> stable_mir::GenericPredicates {
115-
let trait_def_id = self.trait_def_id(trait_def);
116-
let ty::GenericPredicates { parent, predicates } = self.tcx.predicates_of(trait_def_id);
117-
stable_mir::GenericPredicates {
111+
fn predicates_of(&mut self, def_id: stable_mir::DefId) -> stable_mir::ty::GenericPredicates {
112+
let def_id = self[def_id];
113+
let ty::GenericPredicates { parent, predicates } = self.tcx.predicates_of(def_id);
114+
stable_mir::ty::GenericPredicates {
118115
parent: parent.map(|did| self.trait_def(did)),
119116
predicates: predicates
120117
.iter()

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

+5-18
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use std::cell::Cell;
1515

1616
use self::ty::{
17-
GenericDef, Generics, ImplDef, ImplTrait, PredicateKind, Span, TraitDecl, TraitDef, Ty, TyKind,
17+
GenericPredicates, Generics, ImplDef, ImplTrait, Span, TraitDecl, TraitDef, Ty, TyKind,
1818
};
1919
use crate::rustc_smir::Tables;
2020

@@ -28,7 +28,8 @@ pub type Symbol = String;
2828
pub type CrateNum = usize;
2929

3030
/// A unique identification number for each item accessible for the current compilation unit.
31-
pub type DefId = usize;
31+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
32+
pub struct DefId(pub(crate) usize);
3233

3334
/// A list of crate items.
3435
pub type CrateItems = Vec<CrateItem>;
@@ -39,12 +40,6 @@ pub type TraitDecls = Vec<TraitDef>;
3940
/// A list of impl trait decls.
4041
pub type ImplTraitDecls = Vec<ImplDef>;
4142

42-
/// A list of predicates.
43-
pub struct GenericPredicates {
44-
pub parent: Option<TraitDef>,
45-
pub predicates: Vec<(PredicateKind, Span)>,
46-
}
47-
4843
/// Holds information about a crate.
4944
#[derive(Clone, PartialEq, Eq, Debug)]
5045
pub struct Crate {
@@ -108,14 +103,6 @@ pub fn trait_impl(trait_impl: &ImplDef) -> ImplTrait {
108103
with(|cx| cx.trait_impl(trait_impl))
109104
}
110105

111-
pub fn generics_of(generic_def: &GenericDef) -> Generics {
112-
with(|cx| cx.generics_of(generic_def))
113-
}
114-
115-
pub fn predicates_of(trait_def: &TraitDef) -> GenericPredicates {
116-
with(|cx| cx.predicates_of(trait_def))
117-
}
118-
119106
pub trait Context {
120107
fn entry_fn(&mut self) -> Option<CrateItem>;
121108
/// Retrieve all items of the local crate that have a MIR associated with them.
@@ -125,8 +112,8 @@ pub trait Context {
125112
fn trait_decl(&mut self, trait_def: &TraitDef) -> TraitDecl;
126113
fn all_trait_impls(&mut self) -> ImplTraitDecls;
127114
fn trait_impl(&mut self, trait_impl: &ImplDef) -> ImplTrait;
128-
fn generics_of(&mut self, generic_def: &GenericDef) -> Generics;
129-
fn predicates_of(&mut self, trait_def: &TraitDef) -> GenericPredicates;
115+
fn generics_of(&mut self, def_id: DefId) -> Generics;
116+
fn predicates_of(&mut self, def_id: DefId) -> GenericPredicates;
130117
/// Get information about the local crate.
131118
fn local_crate(&self) -> Crate;
132119
/// Retrieve a list of all external crates.

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

+12-20
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,9 @@ pub struct GenericDef(pub(crate) DefId);
120120
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
121121
pub struct ConstDef(pub(crate) DefId);
122122

123-
impl TraitDef {
124-
pub fn trait_decl(&self) -> TraitDecl {
125-
with(|cx| cx.trait_decl(self))
126-
}
127-
}
128-
129123
#[derive(Clone, PartialEq, Eq, Debug)]
130124
pub struct ImplDef(pub(crate) DefId);
131125

132-
impl ImplDef {
133-
pub fn trait_impl(&self) -> ImplTrait {
134-
with(|cx| cx.trait_impl(self))
135-
}
136-
}
137-
138-
impl GenericDef {
139-
pub fn generics_of(&self) -> Generics {
140-
with(|tcx| tcx.generics_of(self))
141-
}
142-
}
143-
144126
#[derive(Clone, Debug)]
145127
pub struct GenericArgs(pub Vec<GenericArgKind>);
146128

@@ -463,6 +445,16 @@ pub struct TraitDecl {
463445
pub deny_explicit_impl: bool,
464446
}
465447

448+
impl TraitDecl {
449+
pub fn generics_of(&self) -> Generics {
450+
with(|cx| cx.generics_of(self.def_id.0))
451+
}
452+
453+
pub fn predicates_of(&self) -> GenericPredicates {
454+
with(|cx| cx.predicates_of(self.def_id.0))
455+
}
456+
}
457+
466458
pub type ImplTrait = EarlyBinder<TraitRef>;
467459

468460
#[derive(Clone, Debug)]
@@ -499,8 +491,8 @@ pub struct GenericParamDef {
499491
}
500492

501493
pub struct GenericPredicates {
502-
pub parent: Option<DefId>,
503-
pub predicates: Vec<PredicateKind>,
494+
pub parent: Option<TraitDef>,
495+
pub predicates: Vec<(PredicateKind, Span)>,
504496
}
505497

506498
#[derive(Clone, Debug)]

0 commit comments

Comments
 (0)