Skip to content

Commit 24e54dd

Browse files
Introduce LocalDefId which provides a type-level guarantee that the DefId is from the local crate.
1 parent 2f50e62 commit 24e54dd

File tree

11 files changed

+98
-31
lines changed

11 files changed

+98
-31
lines changed

src/librustc/hir/def_id.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ impl serialize::UseSpecializedDecodable for CrateNum {
9292
/// don't have to care about these ranges.
9393
newtype_index!(DefIndex
9494
{
95+
ENCODABLE = custom
9596
DEBUG_FORMAT = custom,
9697

9798
/// The start of the "high" range of DefIndexes.
@@ -208,13 +209,20 @@ impl fmt::Debug for DefId {
208209

209210
impl DefId {
210211
/// Make a local `DefId` with the given index.
212+
#[inline]
211213
pub fn local(index: DefIndex) -> DefId {
212214
DefId { krate: LOCAL_CRATE, index: index }
213215
}
214216

215-
pub fn is_local(&self) -> bool {
217+
#[inline]
218+
pub fn is_local(self) -> bool {
216219
self.krate == LOCAL_CRATE
217220
}
221+
222+
#[inline]
223+
pub fn to_local(self) -> LocalDefId {
224+
LocalDefId::from_def_id(self)
225+
}
218226
}
219227

220228
impl serialize::UseSpecializedEncodable for DefId {
@@ -242,3 +250,33 @@ impl serialize::UseSpecializedDecodable for DefId {
242250
})
243251
}
244252
}
253+
254+
255+
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
256+
pub struct LocalDefId(DefIndex);
257+
258+
impl LocalDefId {
259+
260+
#[inline]
261+
pub fn from_def_id(def_id: DefId) -> LocalDefId {
262+
assert!(def_id.is_local());
263+
LocalDefId(def_id.index)
264+
}
265+
266+
#[inline]
267+
pub fn to_def_id(self) -> DefId {
268+
DefId {
269+
krate: LOCAL_CRATE,
270+
index: self.0
271+
}
272+
}
273+
}
274+
275+
impl fmt::Debug for LocalDefId {
276+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
277+
self.to_def_id().fmt(f)
278+
}
279+
}
280+
281+
impl serialize::UseSpecializedEncodable for LocalDefId {}
282+
impl serialize::UseSpecializedDecodable for LocalDefId {}

src/librustc/hir/map/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub use self::definitions::{Definitions, DefKey, DefPath, DefPathData,
1717

1818
use dep_graph::{DepGraph, DepNode, DepKind, DepNodeIndex};
1919

20-
use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndexAddressSpace};
20+
use hir::def_id::{CRATE_DEF_INDEX, DefId, LocalDefId, DefIndexAddressSpace};
2121

2222
use syntax::abi::Abi;
2323
use syntax::ast::{self, Name, NodeId, CRATE_NODE_ID};
@@ -359,6 +359,16 @@ impl<'hir> Map<'hir> {
359359
self.definitions.as_local_node_id(DefId::local(def_index)).unwrap()
360360
}
361361

362+
#[inline]
363+
pub fn local_def_id_to_hir_id(&self, def_id: LocalDefId) -> HirId {
364+
self.definitions.def_index_to_hir_id(def_id.to_def_id().index)
365+
}
366+
367+
#[inline]
368+
pub fn local_def_id_to_node_id(&self, def_id: LocalDefId) -> NodeId {
369+
self.definitions.as_local_node_id(def_id.to_def_id()).unwrap()
370+
}
371+
362372
fn entry_count(&self) -> usize {
363373
self.map.len()
364374
}

src/librustc/ich/impls_hir.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
use hir;
1515
use hir::map::DefPathHash;
16-
use hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX};
16+
use hir::def_id::{DefId, LocalDefId, CrateNum, CRATE_DEF_INDEX};
1717
use ich::{StableHashingContext, NodeIdHashingMode};
1818
use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
1919
StableHasher, StableHasherResult};
@@ -38,6 +38,24 @@ impl<'gcx> ToStableHashKey<StableHashingContext<'gcx>> for DefId {
3838
}
3939
}
4040

41+
impl<'gcx> HashStable<StableHashingContext<'gcx>> for LocalDefId {
42+
#[inline]
43+
fn hash_stable<W: StableHasherResult>(&self,
44+
hcx: &mut StableHashingContext<'gcx>,
45+
hasher: &mut StableHasher<W>) {
46+
hcx.def_path_hash(self.to_def_id()).hash_stable(hcx, hasher);
47+
}
48+
}
49+
50+
impl<'gcx> ToStableHashKey<StableHashingContext<'gcx>> for LocalDefId {
51+
type KeyType = DefPathHash;
52+
53+
#[inline]
54+
fn to_stable_hash_key(&self, hcx: &StableHashingContext<'gcx>) -> DefPathHash {
55+
hcx.def_path_hash(self.to_def_id())
56+
}
57+
}
58+
4159
impl<'gcx> HashStable<StableHashingContext<'gcx>> for CrateNum {
4260
#[inline]
4361
fn hash_stable<W: StableHasherResult>(&self,

src/librustc/middle/expr_use_visitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use self::TrackMatchMode::*;
2020
use self::OverloadedCallType::*;
2121

2222
use hir::def::Def;
23-
use hir::def_id::{DefId};
23+
use hir::def_id::DefId;
2424
use infer::InferCtxt;
2525
use middle::mem_categorization as mc;
2626
use middle::region;
@@ -915,7 +915,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
915915
let closure_def_id = self.tcx().hir.local_def_id(closure_expr.id);
916916
let upvar_id = ty::UpvarId {
917917
var_id: var_hir_id,
918-
closure_expr_id: closure_def_id.index
918+
closure_expr_id: closure_def_id.to_local(),
919919
};
920920
let upvar_capture = self.mc.tables.upvar_capture(upvar_id);
921921
let cmt_var = return_if_err!(self.cat_captured_var(closure_expr.id,

src/librustc/middle/mem_categorization.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub use self::Note::*;
7070
use self::Aliasability::*;
7171

7272
use middle::region;
73-
use hir::def_id::{DefId, DefIndex};
73+
use hir::def_id::{DefId, LocalDefId};
7474
use hir::map as hir_map;
7575
use infer::InferCtxt;
7676
use hir::def::{Def, CtorKind};
@@ -191,7 +191,7 @@ pub type cmt<'tcx> = Rc<cmt_<'tcx>>;
191191

192192
pub enum ImmutabilityBlame<'tcx> {
193193
ImmLocal(ast::NodeId),
194-
ClosureEnv(DefIndex),
194+
ClosureEnv(LocalDefId),
195195
LocalDeref(ast::NodeId),
196196
AdtFieldDeref(&'tcx ty::AdtDef, &'tcx ty::FieldDef)
197197
}
@@ -758,11 +758,11 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
758758
}
759759
};
760760

761-
let closure_expr_def_index = self.tcx.hir.local_def_id(fn_node_id).index;
761+
let closure_expr_def_id = self.tcx.hir.local_def_id(fn_node_id);
762762
let var_hir_id = self.tcx.hir.node_to_hir_id(var_id);
763763
let upvar_id = ty::UpvarId {
764764
var_id: var_hir_id,
765-
closure_expr_id: closure_expr_def_index
765+
closure_expr_id: closure_expr_def_id.to_local(),
766766
};
767767

768768
let var_ty = self.node_ty(var_hir_id)?;
@@ -837,7 +837,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
837837
// The environment of a closure is guaranteed to
838838
// outlive any bindings introduced in the body of the
839839
// closure itself.
840-
scope: DefId::local(upvar_id.closure_expr_id),
840+
scope: upvar_id.closure_expr_id.to_def_id(),
841841
bound_region: ty::BrEnv
842842
}));
843843

src/librustc/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for TypeckTables<'gcx> {
769769
};
770770
let closure_def_id = DefId {
771771
krate: local_id_root.krate,
772-
index: closure_expr_id,
772+
index: closure_expr_id.to_def_id().index,
773773
};
774774
(hcx.def_path_hash(var_owner_def_id),
775775
var_id.local_id,

src/librustc/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub use self::fold::TypeFoldable;
1717

1818
use hir::{map as hir_map, FreevarMap, TraitMap};
1919
use hir::def::{Def, CtorKind, ExportMap};
20-
use hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
20+
use hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
2121
use hir::map::DefPathData;
2222
use ich::StableHashingContext;
2323
use middle::const_val::ConstVal;
@@ -573,7 +573,7 @@ impl<T> Slice<T> {
573573
#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
574574
pub struct UpvarId {
575575
pub var_id: hir::HirId,
576-
pub closure_expr_id: DefIndex,
576+
pub closure_expr_id: LocalDefId,
577577
}
578578

579579
#[derive(Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable, Copy)]

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc::middle::dataflow::BitwiseOperator;
2929
use rustc::middle::dataflow::DataFlowOperator;
3030
use rustc::middle::dataflow::KillFrom;
3131
use rustc::middle::borrowck::BorrowCheckResult;
32-
use rustc::hir::def_id::{DefId, DefIndex};
32+
use rustc::hir::def_id::{DefId, LocalDefId};
3333
use rustc::middle::expr_use_visitor as euv;
3434
use rustc::middle::mem_categorization as mc;
3535
use rustc::middle::mem_categorization::Categorization;
@@ -376,9 +376,9 @@ pub enum LoanPathElem<'tcx> {
376376
LpInterior(Option<DefId>, InteriorKind),
377377
}
378378

379-
fn closure_to_block(closure_id: DefIndex,
379+
fn closure_to_block(closure_id: LocalDefId,
380380
tcx: TyCtxt) -> ast::NodeId {
381-
let closure_id = tcx.hir.def_index_to_node_id(closure_id);
381+
let closure_id = tcx.hir.local_def_id_to_node_id(closure_id);
382382
match tcx.hir.get(closure_id) {
383383
hir_map::NodeExpr(expr) => match expr.node {
384384
hir::ExprClosure(.., body_id, _, _) => {
@@ -1101,7 +1101,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
11011101
} else {
11021102
"consider changing this closure to take self by mutable reference"
11031103
};
1104-
let node_id = self.tcx.hir.def_index_to_node_id(id);
1104+
let node_id = self.tcx.hir.local_def_id_to_node_id(id);
11051105
let help_span = self.tcx.hir.span(node_id);
11061106
self.cannot_act_on_capture_in_sharable_fn(span,
11071107
prefix,
@@ -1297,7 +1297,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
12971297
};
12981298
if kind == ty::ClosureKind::Fn {
12991299
let closure_node_id =
1300-
self.tcx.hir.def_index_to_node_id(upvar_id.closure_expr_id);
1300+
self.tcx.hir.local_def_id_to_node_id(upvar_id.closure_expr_id);
13011301
db.span_help(self.tcx.hir.span(closure_node_id),
13021302
"consider changing this closure to take \
13031303
self by mutable reference");

src/librustc_mir/build/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use build;
1313
use hair::cx::Cx;
1414
use hair::LintLevel;
1515
use rustc::hir;
16-
use rustc::hir::def_id::DefId;
16+
use rustc::hir::def_id::{DefId, LocalDefId};
1717
use rustc::middle::region;
1818
use rustc::mir::*;
1919
use rustc::mir::transform::MirSource;
@@ -414,10 +414,10 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
414414
freevars.iter().map(|fv| {
415415
let var_id = fv.var_id();
416416
let var_hir_id = tcx.hir.node_to_hir_id(var_id);
417-
let closure_expr_id = tcx.hir.local_def_id(fn_id).index;
417+
let closure_expr_id = tcx.hir.local_def_id(fn_id);
418418
let capture = hir.tables().upvar_capture(ty::UpvarId {
419419
var_id: var_hir_id,
420-
closure_expr_id,
420+
closure_expr_id: LocalDefId::from_def_id(closure_expr_id),
421421
});
422422
let by_ref = match capture {
423423
ty::UpvarCapture::ByValue => false,

src/librustc_mir/hair/cx/expr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc::ty::{self, AdtKind, VariantDef, Ty};
2020
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow};
2121
use rustc::ty::cast::CastKind as TyCastKind;
2222
use rustc::hir;
23+
use rustc::hir::def_id::LocalDefId;
2324

2425
impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
2526
type Output = Expr<'tcx>;
@@ -783,7 +784,7 @@ fn convert_var<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
783784
// point we need an implicit deref
784785
let upvar_id = ty::UpvarId {
785786
var_id: var_hir_id,
786-
closure_expr_id: closure_def_id.index,
787+
closure_expr_id: LocalDefId::from_def_id(closure_def_id),
787788
};
788789
match cx.tables().upvar_capture(upvar_id) {
789790
ty::UpvarCapture::ByValue => field_kind,
@@ -897,7 +898,7 @@ fn capture_freevar<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
897898
let var_hir_id = cx.tcx.hir.node_to_hir_id(freevar.var_id());
898899
let upvar_id = ty::UpvarId {
899900
var_id: var_hir_id,
900-
closure_expr_id: cx.tcx.hir.local_def_id(closure_expr.id).index,
901+
closure_expr_id: cx.tcx.hir.local_def_id(closure_expr.id).to_local(),
901902
};
902903
let upvar_capture = cx.tables().upvar_capture(upvar_id);
903904
let temp_lifetime = cx.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);

src/librustc_typeck/check/upvar.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use rustc::infer::UpvarRegion;
5050
use syntax::ast;
5151
use syntax_pos::Span;
5252
use rustc::hir;
53-
use rustc::hir::def_id::DefIndex;
53+
use rustc::hir::def_id::LocalDefId;
5454
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
5555
use rustc::util::nodemap::FxHashMap;
5656

@@ -128,7 +128,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
128128
for freevar in freevars {
129129
let upvar_id = ty::UpvarId {
130130
var_id: self.tcx.hir.node_to_hir_id(freevar.var_id()),
131-
closure_expr_id: closure_def_id.index,
131+
closure_expr_id: LocalDefId::from_def_id(closure_def_id),
132132
};
133133
debug!("seed upvar_id {:?}", upvar_id);
134134

@@ -167,7 +167,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
167167
// Write the adjusted values back into the main tables.
168168
if infer_kind {
169169
if let Some(kind) = delegate.adjust_closure_kinds
170-
.remove(&closure_def_id.index) {
170+
.remove(&closure_def_id.to_local()) {
171171
self.tables
172172
.borrow_mut()
173173
.closure_kinds_mut()
@@ -231,7 +231,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
231231
// This may change if abstract return types of some sort are
232232
// implemented.
233233
let tcx = self.tcx;
234-
let closure_def_index = tcx.hir.local_def_id(closure_id).index;
234+
let closure_def_index = tcx.hir.local_def_id(closure_id);
235235

236236
tcx.with_freevars(closure_id, |freevars| {
237237
freevars.iter().map(|freevar| {
@@ -240,7 +240,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
240240
let freevar_ty = self.node_ty(var_hir_id);
241241
let upvar_id = ty::UpvarId {
242242
var_id: var_hir_id,
243-
closure_expr_id: closure_def_index,
243+
closure_expr_id: LocalDefId::from_def_id(closure_def_index),
244244
};
245245
let capture = self.tables.borrow().upvar_capture(upvar_id);
246246

@@ -263,7 +263,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
263263

264264
struct InferBorrowKind<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
265265
fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
266-
adjust_closure_kinds: FxHashMap<DefIndex, (ty::ClosureKind, Option<(Span, ast::Name)>)>,
266+
adjust_closure_kinds: FxHashMap<LocalDefId, (ty::ClosureKind, Option<(Span, ast::Name)>)>,
267267
adjust_upvar_captures: ty::UpvarCaptureMap<'tcx>,
268268
}
269269

@@ -481,7 +481,7 @@ impl<'a, 'gcx, 'tcx> InferBorrowKind<'a, 'gcx, 'tcx> {
481481
}
482482

483483
fn adjust_closure_kind(&mut self,
484-
closure_id: DefIndex,
484+
closure_id: LocalDefId,
485485
new_kind: ty::ClosureKind,
486486
upvar_span: Span,
487487
var_name: ast::Name) {
@@ -490,7 +490,7 @@ impl<'a, 'gcx, 'tcx> InferBorrowKind<'a, 'gcx, 'tcx> {
490490

491491
let closure_kind = self.adjust_closure_kinds.get(&closure_id).cloned()
492492
.or_else(|| {
493-
let closure_id = self.fcx.tcx.hir.def_index_to_hir_id(closure_id);
493+
let closure_id = self.fcx.tcx.hir.local_def_id_to_hir_id(closure_id);
494494
self.fcx.tables.borrow().closure_kinds().get(closure_id).cloned()
495495
});
496496

0 commit comments

Comments
 (0)