Skip to content

Commit af6299a

Browse files
committed
Add stable_mir::DefId as new type wrapper
1 parent e9710f1 commit af6299a

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 16 additions & 6 deletions
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::{
@@ -67,21 +68,30 @@ pub fn impl_def(did: DefId) -> stable_mir::ty::ImplDef {
6768
with_tables(|t| t.impl_def(did))
6869
}
6970

71+
impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {
72+
type Output = DefId;
73+
74+
#[inline(always)]
75+
fn index(&self, index: stable_mir::DefId) -> &Self::Output {
76+
&self.def_ids[index.0]
77+
}
78+
}
79+
7080
impl<'tcx> Tables<'tcx> {
7181
pub fn item_def_id(&self, item: &stable_mir::CrateItem) -> DefId {
72-
self.def_ids[item.0]
82+
self[item.0]
7383
}
7484

7585
pub fn trait_def_id(&self, trait_def: &stable_mir::ty::TraitDef) -> DefId {
76-
self.def_ids[trait_def.0]
86+
self[trait_def.0]
7787
}
7888

7989
pub fn impl_trait_def_id(&self, impl_def: &stable_mir::ty::ImplDef) -> DefId {
80-
self.def_ids[impl_def.0]
90+
self[impl_def.0]
8191
}
8292

8393
pub fn generic_def_id(&self, generic_def: &stable_mir::ty::GenericDef) -> DefId {
84-
self.def_ids[generic_def.0]
94+
self[generic_def.0]
8595
}
8696

8797
pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem {
@@ -140,12 +150,12 @@ impl<'tcx> Tables<'tcx> {
140150
// FIXME: this becomes inefficient when we have too many ids
141151
for (i, &d) in self.def_ids.iter().enumerate() {
142152
if d == did {
143-
return i;
153+
return stable_mir::DefId(i);
144154
}
145155
}
146156
let id = self.def_ids.len();
147157
self.def_ids.push(did);
148-
id
158+
stable_mir::DefId(id)
149159
}
150160
}
151161

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 5 additions & 8 deletions
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, 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;
@@ -103,16 +103,13 @@ impl<'tcx> Context for Tables<'tcx> {
103103
}
104104

105105
fn generics_of(&mut self, def_id: stable_mir::DefId) -> stable_mir::ty::Generics {
106-
let def_id = self.def_ids[def_id];
106+
let def_id = self[def_id];
107107
let generics = self.tcx.generics_of(def_id);
108108
generics.stable(self)
109109
}
110110

111-
fn predicates_of(
112-
&mut self,
113-
def_id: stable_mir::DefId,
114-
) -> stable_mir::GenericPredicates {
115-
let def_id = self.def_ids[def_id];
111+
fn predicates_of(&mut self, def_id: stable_mir::DefId) -> stable_mir::GenericPredicates {
112+
let def_id = self[def_id];
116113
let ty::GenericPredicates { parent, predicates } = self.tcx.predicates_of(def_id);
117114
stable_mir::GenericPredicates {
118115
parent: parent.map(|did| self.trait_def(did)),

compiler/rustc_smir/src/stable_mir/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ pub type Symbol = String;
2929
pub type CrateNum = usize;
3030

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

3435
/// A list of crate items.
3536
pub type CrateItems = Vec<CrateItem>;

0 commit comments

Comments
 (0)