Skip to content

Commit f8178c7

Browse files
committed
rustc: use LocalDefId instead of DefId in TypeckTables.
1 parent 3c6f982 commit f8178c7

File tree

9 files changed

+94
-125
lines changed

9 files changed

+94
-125
lines changed

src/librustc/ty/context.rs

+50-77
Original file line numberDiff line numberDiff line change
@@ -188,37 +188,37 @@ pub struct CommonConsts<'tcx> {
188188
}
189189

190190
pub struct LocalTableInContext<'a, V> {
191-
local_id_root: Option<DefId>,
191+
hir_owner: Option<LocalDefId>,
192192
data: &'a ItemLocalMap<V>,
193193
}
194194

195195
/// Validate that the given HirId (respectively its `local_id` part) can be
196196
/// safely used as a key in the tables of a TypeckTable. For that to be
197197
/// the case, the HirId must have the same `owner` as all the other IDs in
198-
/// this table (signified by `local_id_root`). Otherwise the HirId
198+
/// this table (signified by `hir_owner`). Otherwise the HirId
199199
/// would be in a different frame of reference and using its `local_id`
200200
/// would result in lookup errors, or worse, in silently wrong data being
201201
/// stored/returned.
202202
fn validate_hir_id_for_typeck_tables(
203-
local_id_root: Option<DefId>,
203+
hir_owner: Option<LocalDefId>,
204204
hir_id: hir::HirId,
205205
mut_access: bool,
206206
) {
207-
if let Some(local_id_root) = local_id_root {
208-
if hir_id.owner.to_def_id() != local_id_root {
207+
if let Some(hir_owner) = hir_owner {
208+
if hir_id.owner != hir_owner {
209209
ty::tls::with(|tcx| {
210210
bug!(
211211
"node {} with HirId::owner {:?} cannot be placed in \
212-
TypeckTables with local_id_root {:?}",
212+
TypeckTables with hir_owner {:?}",
213213
tcx.hir().node_to_string(hir_id),
214214
hir_id.owner,
215-
local_id_root
215+
hir_owner
216216
)
217217
});
218218
}
219219
} else {
220220
// We use "Null Object" TypeckTables in some of the analysis passes.
221-
// These are just expected to be empty and their `local_id_root` is
221+
// These are just expected to be empty and their `hir_owner` is
222222
// `None`. Therefore we cannot verify whether a given `HirId` would
223223
// be a valid key for the given table. Instead we make sure that
224224
// nobody tries to write to such a Null Object table.
@@ -230,12 +230,12 @@ fn validate_hir_id_for_typeck_tables(
230230

231231
impl<'a, V> LocalTableInContext<'a, V> {
232232
pub fn contains_key(&self, id: hir::HirId) -> bool {
233-
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
233+
validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
234234
self.data.contains_key(&id.local_id)
235235
}
236236

237237
pub fn get(&self, id: hir::HirId) -> Option<&V> {
238-
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
238+
validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
239239
self.data.get(&id.local_id)
240240
}
241241

@@ -253,28 +253,28 @@ impl<'a, V> ::std::ops::Index<hir::HirId> for LocalTableInContext<'a, V> {
253253
}
254254

255255
pub struct LocalTableInContextMut<'a, V> {
256-
local_id_root: Option<DefId>,
256+
hir_owner: Option<LocalDefId>,
257257
data: &'a mut ItemLocalMap<V>,
258258
}
259259

260260
impl<'a, V> LocalTableInContextMut<'a, V> {
261261
pub fn get_mut(&mut self, id: hir::HirId) -> Option<&mut V> {
262-
validate_hir_id_for_typeck_tables(self.local_id_root, id, true);
262+
validate_hir_id_for_typeck_tables(self.hir_owner, id, true);
263263
self.data.get_mut(&id.local_id)
264264
}
265265

266266
pub fn entry(&mut self, id: hir::HirId) -> Entry<'_, hir::ItemLocalId, V> {
267-
validate_hir_id_for_typeck_tables(self.local_id_root, id, true);
267+
validate_hir_id_for_typeck_tables(self.hir_owner, id, true);
268268
self.data.entry(id.local_id)
269269
}
270270

271271
pub fn insert(&mut self, id: hir::HirId, val: V) -> Option<V> {
272-
validate_hir_id_for_typeck_tables(self.local_id_root, id, true);
272+
validate_hir_id_for_typeck_tables(self.hir_owner, id, true);
273273
self.data.insert(id.local_id, val)
274274
}
275275

276276
pub fn remove(&mut self, id: hir::HirId) -> Option<V> {
277-
validate_hir_id_for_typeck_tables(self.local_id_root, id, true);
277+
validate_hir_id_for_typeck_tables(self.hir_owner, id, true);
278278
self.data.remove(&id.local_id)
279279
}
280280
}
@@ -322,8 +322,8 @@ pub struct GeneratorInteriorTypeCause<'tcx> {
322322

323323
#[derive(RustcEncodable, RustcDecodable, Debug)]
324324
pub struct TypeckTables<'tcx> {
325-
/// The HirId::owner all ItemLocalIds in this table are relative to.
326-
pub local_id_root: Option<DefId>,
325+
/// The `HirId::owner` all `ItemLocalId`s in this table are relative to.
326+
pub hir_owner: Option<LocalDefId>,
327327

328328
/// Resolved definitions for `<T>::X` associated paths and
329329
/// method calls, including those of overloaded operators.
@@ -431,9 +431,9 @@ pub struct TypeckTables<'tcx> {
431431
}
432432

433433
impl<'tcx> TypeckTables<'tcx> {
434-
pub fn empty(local_id_root: Option<DefId>) -> TypeckTables<'tcx> {
434+
pub fn empty(hir_owner: Option<LocalDefId>) -> TypeckTables<'tcx> {
435435
TypeckTables {
436-
local_id_root,
436+
hir_owner,
437437
type_dependent_defs: Default::default(),
438438
field_indices: Default::default(),
439439
user_provided_types: Default::default(),
@@ -469,11 +469,11 @@ impl<'tcx> TypeckTables<'tcx> {
469469
pub fn type_dependent_defs(
470470
&self,
471471
) -> LocalTableInContext<'_, Result<(DefKind, DefId), ErrorReported>> {
472-
LocalTableInContext { local_id_root: self.local_id_root, data: &self.type_dependent_defs }
472+
LocalTableInContext { hir_owner: self.hir_owner, data: &self.type_dependent_defs }
473473
}
474474

475475
pub fn type_dependent_def(&self, id: HirId) -> Option<(DefKind, DefId)> {
476-
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
476+
validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
477477
self.type_dependent_defs.get(&id.local_id).cloned().and_then(|r| r.ok())
478478
}
479479

@@ -484,39 +484,33 @@ impl<'tcx> TypeckTables<'tcx> {
484484
pub fn type_dependent_defs_mut(
485485
&mut self,
486486
) -> LocalTableInContextMut<'_, Result<(DefKind, DefId), ErrorReported>> {
487-
LocalTableInContextMut {
488-
local_id_root: self.local_id_root,
489-
data: &mut self.type_dependent_defs,
490-
}
487+
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.type_dependent_defs }
491488
}
492489

493490
pub fn field_indices(&self) -> LocalTableInContext<'_, usize> {
494-
LocalTableInContext { local_id_root: self.local_id_root, data: &self.field_indices }
491+
LocalTableInContext { hir_owner: self.hir_owner, data: &self.field_indices }
495492
}
496493

497494
pub fn field_indices_mut(&mut self) -> LocalTableInContextMut<'_, usize> {
498-
LocalTableInContextMut { local_id_root: self.local_id_root, data: &mut self.field_indices }
495+
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.field_indices }
499496
}
500497

501498
pub fn user_provided_types(&self) -> LocalTableInContext<'_, CanonicalUserType<'tcx>> {
502-
LocalTableInContext { local_id_root: self.local_id_root, data: &self.user_provided_types }
499+
LocalTableInContext { hir_owner: self.hir_owner, data: &self.user_provided_types }
503500
}
504501

505502
pub fn user_provided_types_mut(
506503
&mut self,
507504
) -> LocalTableInContextMut<'_, CanonicalUserType<'tcx>> {
508-
LocalTableInContextMut {
509-
local_id_root: self.local_id_root,
510-
data: &mut self.user_provided_types,
511-
}
505+
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.user_provided_types }
512506
}
513507

514508
pub fn node_types(&self) -> LocalTableInContext<'_, Ty<'tcx>> {
515-
LocalTableInContext { local_id_root: self.local_id_root, data: &self.node_types }
509+
LocalTableInContext { hir_owner: self.hir_owner, data: &self.node_types }
516510
}
517511

518512
pub fn node_types_mut(&mut self) -> LocalTableInContextMut<'_, Ty<'tcx>> {
519-
LocalTableInContextMut { local_id_root: self.local_id_root, data: &mut self.node_types }
513+
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.node_types }
520514
}
521515

522516
pub fn node_type(&self, id: hir::HirId) -> Ty<'tcx> {
@@ -526,21 +520,21 @@ impl<'tcx> TypeckTables<'tcx> {
526520
}
527521

528522
pub fn node_type_opt(&self, id: hir::HirId) -> Option<Ty<'tcx>> {
529-
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
523+
validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
530524
self.node_types.get(&id.local_id).cloned()
531525
}
532526

533527
pub fn node_substs_mut(&mut self) -> LocalTableInContextMut<'_, SubstsRef<'tcx>> {
534-
LocalTableInContextMut { local_id_root: self.local_id_root, data: &mut self.node_substs }
528+
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.node_substs }
535529
}
536530

537531
pub fn node_substs(&self, id: hir::HirId) -> SubstsRef<'tcx> {
538-
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
532+
validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
539533
self.node_substs.get(&id.local_id).cloned().unwrap_or_else(|| InternalSubsts::empty())
540534
}
541535

542536
pub fn node_substs_opt(&self, id: hir::HirId) -> Option<SubstsRef<'tcx>> {
543-
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
537+
validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
544538
self.node_substs.get(&id.local_id).cloned()
545539
}
546540

@@ -573,17 +567,17 @@ impl<'tcx> TypeckTables<'tcx> {
573567
}
574568

575569
pub fn adjustments(&self) -> LocalTableInContext<'_, Vec<ty::adjustment::Adjustment<'tcx>>> {
576-
LocalTableInContext { local_id_root: self.local_id_root, data: &self.adjustments }
570+
LocalTableInContext { hir_owner: self.hir_owner, data: &self.adjustments }
577571
}
578572

579573
pub fn adjustments_mut(
580574
&mut self,
581575
) -> LocalTableInContextMut<'_, Vec<ty::adjustment::Adjustment<'tcx>>> {
582-
LocalTableInContextMut { local_id_root: self.local_id_root, data: &mut self.adjustments }
576+
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.adjustments }
583577
}
584578

585579
pub fn expr_adjustments(&self, expr: &hir::Expr<'_>) -> &[ty::adjustment::Adjustment<'tcx>] {
586-
validate_hir_id_for_typeck_tables(self.local_id_root, expr.hir_id, false);
580+
validate_hir_id_for_typeck_tables(self.hir_owner, expr.hir_id, false);
587581
self.adjustments.get(&expr.hir_id.local_id).map_or(&[], |a| &a[..])
588582
}
589583

@@ -618,66 +612,51 @@ impl<'tcx> TypeckTables<'tcx> {
618612
}
619613

620614
pub fn pat_binding_modes(&self) -> LocalTableInContext<'_, BindingMode> {
621-
LocalTableInContext { local_id_root: self.local_id_root, data: &self.pat_binding_modes }
615+
LocalTableInContext { hir_owner: self.hir_owner, data: &self.pat_binding_modes }
622616
}
623617

624618
pub fn pat_binding_modes_mut(&mut self) -> LocalTableInContextMut<'_, BindingMode> {
625-
LocalTableInContextMut {
626-
local_id_root: self.local_id_root,
627-
data: &mut self.pat_binding_modes,
628-
}
619+
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.pat_binding_modes }
629620
}
630621

631622
pub fn pat_adjustments(&self) -> LocalTableInContext<'_, Vec<Ty<'tcx>>> {
632-
LocalTableInContext { local_id_root: self.local_id_root, data: &self.pat_adjustments }
623+
LocalTableInContext { hir_owner: self.hir_owner, data: &self.pat_adjustments }
633624
}
634625

635626
pub fn pat_adjustments_mut(&mut self) -> LocalTableInContextMut<'_, Vec<Ty<'tcx>>> {
636-
LocalTableInContextMut {
637-
local_id_root: self.local_id_root,
638-
data: &mut self.pat_adjustments,
639-
}
627+
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.pat_adjustments }
640628
}
641629

642630
pub fn upvar_capture(&self, upvar_id: ty::UpvarId) -> ty::UpvarCapture<'tcx> {
643631
self.upvar_capture_map[&upvar_id]
644632
}
645633

646634
pub fn closure_kind_origins(&self) -> LocalTableInContext<'_, (Span, ast::Name)> {
647-
LocalTableInContext { local_id_root: self.local_id_root, data: &self.closure_kind_origins }
635+
LocalTableInContext { hir_owner: self.hir_owner, data: &self.closure_kind_origins }
648636
}
649637

650638
pub fn closure_kind_origins_mut(&mut self) -> LocalTableInContextMut<'_, (Span, ast::Name)> {
651-
LocalTableInContextMut {
652-
local_id_root: self.local_id_root,
653-
data: &mut self.closure_kind_origins,
654-
}
639+
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.closure_kind_origins }
655640
}
656641

657642
pub fn liberated_fn_sigs(&self) -> LocalTableInContext<'_, ty::FnSig<'tcx>> {
658-
LocalTableInContext { local_id_root: self.local_id_root, data: &self.liberated_fn_sigs }
643+
LocalTableInContext { hir_owner: self.hir_owner, data: &self.liberated_fn_sigs }
659644
}
660645

661646
pub fn liberated_fn_sigs_mut(&mut self) -> LocalTableInContextMut<'_, ty::FnSig<'tcx>> {
662-
LocalTableInContextMut {
663-
local_id_root: self.local_id_root,
664-
data: &mut self.liberated_fn_sigs,
665-
}
647+
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.liberated_fn_sigs }
666648
}
667649

668650
pub fn fru_field_types(&self) -> LocalTableInContext<'_, Vec<Ty<'tcx>>> {
669-
LocalTableInContext { local_id_root: self.local_id_root, data: &self.fru_field_types }
651+
LocalTableInContext { hir_owner: self.hir_owner, data: &self.fru_field_types }
670652
}
671653

672654
pub fn fru_field_types_mut(&mut self) -> LocalTableInContextMut<'_, Vec<Ty<'tcx>>> {
673-
LocalTableInContextMut {
674-
local_id_root: self.local_id_root,
675-
data: &mut self.fru_field_types,
676-
}
655+
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.fru_field_types }
677656
}
678657

679658
pub fn is_coercion_cast(&self, hir_id: hir::HirId) -> bool {
680-
validate_hir_id_for_typeck_tables(self.local_id_root, hir_id, true);
659+
validate_hir_id_for_typeck_tables(self.hir_owner, hir_id, true);
681660
self.coercion_casts.contains(&hir_id.local_id)
682661
}
683662

@@ -693,7 +672,7 @@ impl<'tcx> TypeckTables<'tcx> {
693672
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for TypeckTables<'tcx> {
694673
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
695674
let ty::TypeckTables {
696-
local_id_root,
675+
hir_owner,
697676
ref type_dependent_defs,
698677
ref field_indices,
699678
ref user_provided_types,
@@ -730,18 +709,12 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for TypeckTables<'tcx> {
730709
hash_stable_hashmap(hcx, hasher, upvar_capture_map, |up_var_id, hcx| {
731710
let ty::UpvarId { var_path, closure_expr_id } = *up_var_id;
732711

733-
let local_id_root = local_id_root.expect("trying to hash invalid TypeckTables");
712+
assert_eq!(Some(var_path.hir_id.owner), hir_owner);
734713

735-
let var_owner_def_id = DefId {
736-
krate: local_id_root.krate,
737-
index: var_path.hir_id.owner.local_def_index,
738-
};
739-
let closure_def_id =
740-
DefId { krate: local_id_root.krate, index: closure_expr_id.local_def_index };
741714
(
742-
hcx.def_path_hash(var_owner_def_id),
715+
hcx.local_def_path_hash(var_path.hir_id.owner),
743716
var_path.hir_id.local_id,
744-
hcx.def_path_hash(closure_def_id),
717+
hcx.local_def_path_hash(closure_expr_id),
745718
)
746719
});
747720

src/librustc_infer/infer/error_reporting/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1784,11 +1784,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17841784
// suggest adding an explicit lifetime bound to it.
17851785
let type_param_span = match (self.in_progress_tables, bound_kind) {
17861786
(Some(ref table), GenericKind::Param(ref param)) => {
1787-
let table = table.borrow();
1788-
table.local_id_root.and_then(|did| {
1789-
let generics = self.tcx.generics_of(did);
1790-
// Account for the case where `did` corresponds to `Self`, which doesn't have
1791-
// the expected type argument.
1787+
let table_owner = table.borrow().hir_owner;
1788+
table_owner.and_then(|table_owner| {
1789+
let generics = self.tcx.generics_of(table_owner.to_def_id());
1790+
// Account for the case where `param` corresponds to `Self`,
1791+
// which doesn't have the expected type argument.
17921792
if !(generics.has_self && param.index == 0) {
17931793
let type_param = generics.type_param(param, self.tcx);
17941794
let hir = &self.tcx.hir();

src/librustc_infer/infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_data_structures::sync::Lrc;
2929
use rustc_data_structures::unify as ut;
3030
use rustc_errors::DiagnosticBuilder;
3131
use rustc_hir as hir;
32-
use rustc_hir::def_id::DefId;
32+
use rustc_hir::def_id::{DefId, LocalDefId};
3333
use rustc_session::config::BorrowckMode;
3434
use rustc_span::symbol::Symbol;
3535
use rustc_span::Span;
@@ -559,7 +559,7 @@ impl TyCtxtInferExt<'tcx> for TyCtxt<'tcx> {
559559
impl<'tcx> InferCtxtBuilder<'tcx> {
560560
/// Used only by `rustc_typeck` during body type-checking/inference,
561561
/// will initialize `in_progress_tables` with fresh `TypeckTables`.
562-
pub fn with_fresh_in_progress_tables(mut self, table_owner: DefId) -> Self {
562+
pub fn with_fresh_in_progress_tables(mut self, table_owner: LocalDefId) -> Self {
563563
self.fresh_tables = Some(RefCell::new(ty::TypeckTables::empty(Some(table_owner))));
564564
self
565565
}

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1105,15 +1105,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
11051105
let generator_did_root = self.tcx.closure_base_def_id(generator_did);
11061106
debug!(
11071107
"maybe_note_obligation_cause_for_async_await: generator_did={:?} \
1108-
generator_did_root={:?} in_progress_tables.local_id_root={:?} span={:?}",
1108+
generator_did_root={:?} in_progress_tables.hir_owner={:?} span={:?}",
11091109
generator_did,
11101110
generator_did_root,
1111-
in_progress_tables.as_ref().map(|t| t.local_id_root),
1111+
in_progress_tables.as_ref().map(|t| t.hir_owner),
11121112
span
11131113
);
11141114
let query_tables;
11151115
let tables: &TypeckTables<'tcx> = match &in_progress_tables {
1116-
Some(t) if t.local_id_root == Some(generator_did_root) => t,
1116+
Some(t) if t.hir_owner.map(|owner| owner.to_def_id()) == Some(generator_did_root) => t,
11171117
_ => {
11181118
query_tables = self.tcx.typeck_tables_of(generator_did);
11191119
&query_tables

0 commit comments

Comments
 (0)