}
pub struct LocalTableInContext<'a, V> {
- local_id_root: Option<DefId>,
+ hir_owner: Option<LocalDefId>,
data: &'a ItemLocalMap<V>,
}
/// Validate that the given HirId (respectively its `local_id` part) can be
/// safely used as a key in the tables of a TypeckTable. For that to be
/// the case, the HirId must have the same `owner` as all the other IDs in
-/// this table (signified by `local_id_root`). Otherwise the HirId
+/// this table (signified by `hir_owner`). Otherwise the HirId
/// would be in a different frame of reference and using its `local_id`
/// would result in lookup errors, or worse, in silently wrong data being
/// stored/returned.
fn validate_hir_id_for_typeck_tables(
- local_id_root: Option<DefId>,
+ hir_owner: Option<LocalDefId>,
hir_id: hir::HirId,
mut_access: bool,
) {
- if let Some(local_id_root) = local_id_root {
- if hir_id.owner.to_def_id() != local_id_root {
+ if let Some(hir_owner) = hir_owner {
+ if hir_id.owner != hir_owner {
ty::tls::with(|tcx| {
bug!(
"node {} with HirId::owner {:?} cannot be placed in \
- TypeckTables with local_id_root {:?}",
+ TypeckTables with hir_owner {:?}",
tcx.hir().node_to_string(hir_id),
hir_id.owner,
- local_id_root
+ hir_owner
)
});
}
} else {
// We use "Null Object" TypeckTables in some of the analysis passes.
- // These are just expected to be empty and their `local_id_root` is
+ // These are just expected to be empty and their `hir_owner` is
// `None`. Therefore we cannot verify whether a given `HirId` would
// be a valid key for the given table. Instead we make sure that
// nobody tries to write to such a Null Object table.
impl<'a, V> LocalTableInContext<'a, V> {
pub fn contains_key(&self, id: hir::HirId) -> bool {
- validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
+ validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
self.data.contains_key(&id.local_id)
}
pub fn get(&self, id: hir::HirId) -> Option<&V> {
- validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
+ validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
self.data.get(&id.local_id)
}
}
pub struct LocalTableInContextMut<'a, V> {
- local_id_root: Option<DefId>,
+ hir_owner: Option<LocalDefId>,
data: &'a mut ItemLocalMap<V>,
}
impl<'a, V> LocalTableInContextMut<'a, V> {
pub fn get_mut(&mut self, id: hir::HirId) -> Option<&mut V> {
- validate_hir_id_for_typeck_tables(self.local_id_root, id, true);
+ validate_hir_id_for_typeck_tables(self.hir_owner, id, true);
self.data.get_mut(&id.local_id)
}
pub fn entry(&mut self, id: hir::HirId) -> Entry<'_, hir::ItemLocalId, V> {
- validate_hir_id_for_typeck_tables(self.local_id_root, id, true);
+ validate_hir_id_for_typeck_tables(self.hir_owner, id, true);
self.data.entry(id.local_id)
}
pub fn insert(&mut self, id: hir::HirId, val: V) -> Option<V> {
- validate_hir_id_for_typeck_tables(self.local_id_root, id, true);
+ validate_hir_id_for_typeck_tables(self.hir_owner, id, true);
self.data.insert(id.local_id, val)
}
pub fn remove(&mut self, id: hir::HirId) -> Option<V> {
- validate_hir_id_for_typeck_tables(self.local_id_root, id, true);
+ validate_hir_id_for_typeck_tables(self.hir_owner, id, true);
self.data.remove(&id.local_id)
}
}
#[derive(RustcEncodable, RustcDecodable, Debug)]
pub struct TypeckTables<'tcx> {
- /// The HirId::owner all ItemLocalIds in this table are relative to.
- pub local_id_root: Option<DefId>,
+ /// The `HirId::owner` all `ItemLocalId`s in this table are relative to.
+ pub hir_owner: Option<LocalDefId>,
/// Resolved definitions for `<T>::X` associated paths and
/// method calls, including those of overloaded operators.
}
impl<'tcx> TypeckTables<'tcx> {
- pub fn empty(local_id_root: Option<DefId>) -> TypeckTables<'tcx> {
+ pub fn empty(hir_owner: Option<LocalDefId>) -> TypeckTables<'tcx> {
TypeckTables {
- local_id_root,
+ hir_owner,
type_dependent_defs: Default::default(),
field_indices: Default::default(),
user_provided_types: Default::default(),
pub fn type_dependent_defs(
&self,
) -> LocalTableInContext<'_, Result<(DefKind, DefId), ErrorReported>> {
- LocalTableInContext { local_id_root: self.local_id_root, data: &self.type_dependent_defs }
+ LocalTableInContext { hir_owner: self.hir_owner, data: &self.type_dependent_defs }
}
pub fn type_dependent_def(&self, id: HirId) -> Option<(DefKind, DefId)> {
- validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
+ validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
self.type_dependent_defs.get(&id.local_id).cloned().and_then(|r| r.ok())
}
pub fn type_dependent_defs_mut(
&mut self,
) -> LocalTableInContextMut<'_, Result<(DefKind, DefId), ErrorReported>> {
- LocalTableInContextMut {
- local_id_root: self.local_id_root,
- data: &mut self.type_dependent_defs,
- }
+ LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.type_dependent_defs }
}
pub fn field_indices(&self) -> LocalTableInContext<'_, usize> {
- LocalTableInContext { local_id_root: self.local_id_root, data: &self.field_indices }
+ LocalTableInContext { hir_owner: self.hir_owner, data: &self.field_indices }
}
pub fn field_indices_mut(&mut self) -> LocalTableInContextMut<'_, usize> {
- LocalTableInContextMut { local_id_root: self.local_id_root, data: &mut self.field_indices }
+ LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.field_indices }
}
pub fn user_provided_types(&self) -> LocalTableInContext<'_, CanonicalUserType<'tcx>> {
- LocalTableInContext { local_id_root: self.local_id_root, data: &self.user_provided_types }
+ LocalTableInContext { hir_owner: self.hir_owner, data: &self.user_provided_types }
}
pub fn user_provided_types_mut(
&mut self,
) -> LocalTableInContextMut<'_, CanonicalUserType<'tcx>> {
- LocalTableInContextMut {
- local_id_root: self.local_id_root,
- data: &mut self.user_provided_types,
- }
+ LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.user_provided_types }
}
pub fn node_types(&self) -> LocalTableInContext<'_, Ty<'tcx>> {
- LocalTableInContext { local_id_root: self.local_id_root, data: &self.node_types }
+ LocalTableInContext { hir_owner: self.hir_owner, data: &self.node_types }
}
pub fn node_types_mut(&mut self) -> LocalTableInContextMut<'_, Ty<'tcx>> {
- LocalTableInContextMut { local_id_root: self.local_id_root, data: &mut self.node_types }
+ LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.node_types }
}
pub fn node_type(&self, id: hir::HirId) -> Ty<'tcx> {
}
pub fn node_type_opt(&self, id: hir::HirId) -> Option<Ty<'tcx>> {
- validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
+ validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
self.node_types.get(&id.local_id).cloned()
}
pub fn node_substs_mut(&mut self) -> LocalTableInContextMut<'_, SubstsRef<'tcx>> {
- LocalTableInContextMut { local_id_root: self.local_id_root, data: &mut self.node_substs }
+ LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.node_substs }
}
pub fn node_substs(&self, id: hir::HirId) -> SubstsRef<'tcx> {
- validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
+ validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
self.node_substs.get(&id.local_id).cloned().unwrap_or_else(|| InternalSubsts::empty())
}
pub fn node_substs_opt(&self, id: hir::HirId) -> Option<SubstsRef<'tcx>> {
- validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
+ validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
self.node_substs.get(&id.local_id).cloned()
}
}
pub fn adjustments(&self) -> LocalTableInContext<'_, Vec<ty::adjustment::Adjustment<'tcx>>> {
- LocalTableInContext { local_id_root: self.local_id_root, data: &self.adjustments }
+ LocalTableInContext { hir_owner: self.hir_owner, data: &self.adjustments }
}
pub fn adjustments_mut(
&mut self,
) -> LocalTableInContextMut<'_, Vec<ty::adjustment::Adjustment<'tcx>>> {
- LocalTableInContextMut { local_id_root: self.local_id_root, data: &mut self.adjustments }
+ LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.adjustments }
}
pub fn expr_adjustments(&self, expr: &hir::Expr<'_>) -> &[ty::adjustment::Adjustment<'tcx>] {
- validate_hir_id_for_typeck_tables(self.local_id_root, expr.hir_id, false);
+ validate_hir_id_for_typeck_tables(self.hir_owner, expr.hir_id, false);
self.adjustments.get(&expr.hir_id.local_id).map_or(&[], |a| &a[..])
}
}
pub fn pat_binding_modes(&self) -> LocalTableInContext<'_, BindingMode> {
- LocalTableInContext { local_id_root: self.local_id_root, data: &self.pat_binding_modes }
+ LocalTableInContext { hir_owner: self.hir_owner, data: &self.pat_binding_modes }
}
pub fn pat_binding_modes_mut(&mut self) -> LocalTableInContextMut<'_, BindingMode> {
- LocalTableInContextMut {
- local_id_root: self.local_id_root,
- data: &mut self.pat_binding_modes,
- }
+ LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.pat_binding_modes }
}
pub fn pat_adjustments(&self) -> LocalTableInContext<'_, Vec<Ty<'tcx>>> {
- LocalTableInContext { local_id_root: self.local_id_root, data: &self.pat_adjustments }
+ LocalTableInContext { hir_owner: self.hir_owner, data: &self.pat_adjustments }
}
pub fn pat_adjustments_mut(&mut self) -> LocalTableInContextMut<'_, Vec<Ty<'tcx>>> {
- LocalTableInContextMut {
- local_id_root: self.local_id_root,
- data: &mut self.pat_adjustments,
- }
+ LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.pat_adjustments }
}
pub fn upvar_capture(&self, upvar_id: ty::UpvarId) -> ty::UpvarCapture<'tcx> {
}
pub fn closure_kind_origins(&self) -> LocalTableInContext<'_, (Span, ast::Name)> {
- LocalTableInContext { local_id_root: self.local_id_root, data: &self.closure_kind_origins }
+ LocalTableInContext { hir_owner: self.hir_owner, data: &self.closure_kind_origins }
}
pub fn closure_kind_origins_mut(&mut self) -> LocalTableInContextMut<'_, (Span, ast::Name)> {
- LocalTableInContextMut {
- local_id_root: self.local_id_root,
- data: &mut self.closure_kind_origins,
- }
+ LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.closure_kind_origins }
}
pub fn liberated_fn_sigs(&self) -> LocalTableInContext<'_, ty::FnSig<'tcx>> {
- LocalTableInContext { local_id_root: self.local_id_root, data: &self.liberated_fn_sigs }
+ LocalTableInContext { hir_owner: self.hir_owner, data: &self.liberated_fn_sigs }
}
pub fn liberated_fn_sigs_mut(&mut self) -> LocalTableInContextMut<'_, ty::FnSig<'tcx>> {
- LocalTableInContextMut {
- local_id_root: self.local_id_root,
- data: &mut self.liberated_fn_sigs,
- }
+ LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.liberated_fn_sigs }
}
pub fn fru_field_types(&self) -> LocalTableInContext<'_, Vec<Ty<'tcx>>> {
- LocalTableInContext { local_id_root: self.local_id_root, data: &self.fru_field_types }
+ LocalTableInContext { hir_owner: self.hir_owner, data: &self.fru_field_types }
}
pub fn fru_field_types_mut(&mut self) -> LocalTableInContextMut<'_, Vec<Ty<'tcx>>> {
- LocalTableInContextMut {
- local_id_root: self.local_id_root,
- data: &mut self.fru_field_types,
- }
+ LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.fru_field_types }
}
pub fn is_coercion_cast(&self, hir_id: hir::HirId) -> bool {
- validate_hir_id_for_typeck_tables(self.local_id_root, hir_id, true);
+ validate_hir_id_for_typeck_tables(self.hir_owner, hir_id, true);
self.coercion_casts.contains(&hir_id.local_id)
}
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for TypeckTables<'tcx> {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
let ty::TypeckTables {
- local_id_root,
+ hir_owner,
ref type_dependent_defs,
ref field_indices,
ref user_provided_types,
hash_stable_hashmap(hcx, hasher, upvar_capture_map, |up_var_id, hcx| {
let ty::UpvarId { var_path, closure_expr_id } = *up_var_id;
- let local_id_root = local_id_root.expect("trying to hash invalid TypeckTables");
+ assert_eq!(Some(var_path.hir_id.owner), hir_owner);
- let var_owner_def_id = DefId {
- krate: local_id_root.krate,
- index: var_path.hir_id.owner.local_def_index,
- };
- let closure_def_id =
- DefId { krate: local_id_root.krate, index: closure_expr_id.local_def_index };
(
- hcx.def_path_hash(var_owner_def_id),
+ hcx.local_def_path_hash(var_path.hir_id.owner),
var_path.hir_id.local_id,
- hcx.def_path_hash(closure_def_id),
+ hcx.local_def_path_hash(closure_expr_id),
)
});
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, DiagnosticId};
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind, Res};
-use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LOCAL_CRATE};
+use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::{ExprKind, GenericArg, HirIdMap, Item, ItemKind, Node, PatKind, QPath};
/// `F: for<'b, 'tcx> where 'tcx FnOnce(Inherited<'b, 'tcx>)`.
pub struct InheritedBuilder<'tcx> {
infcx: infer::InferCtxtBuilder<'tcx>,
- def_id: DefId,
+ def_id: LocalDefId,
}
impl Inherited<'_, 'tcx> {
- pub fn build(tcx: TyCtxt<'tcx>, def_id: DefId) -> InheritedBuilder<'tcx> {
- let hir_id_root = if let Some(def_id) = def_id.as_local() {
- tcx.hir().local_def_id_to_hir_id(def_id).owner.to_def_id()
- } else {
- def_id
- };
+ pub fn build(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> InheritedBuilder<'tcx> {
+ let hir_owner = tcx.hir().local_def_id_to_hir_id(def_id).owner;
InheritedBuilder {
- infcx: tcx.infer_ctxt().with_fresh_in_progress_tables(hir_id_root),
+ infcx: tcx.infer_ctxt().with_fresh_in_progress_tables(hir_owner),
def_id,
}
}
}
impl Inherited<'a, 'tcx> {
- fn new(infcx: InferCtxt<'a, 'tcx>, def_id: DefId) -> Self {
+ fn new(infcx: InferCtxt<'a, 'tcx>, def_id: LocalDefId) -> Self {
let tcx = infcx.tcx;
- let item_id = tcx.hir().as_local_hir_id(def_id);
- let body_id = item_id.and_then(|id| tcx.hir().maybe_body_owned_by(id));
+ let item_id = tcx.hir().local_def_id_to_hir_id(def_id);
+ let body_id = tcx.hir().maybe_body_owned_by(item_id);
let implicit_region_bound = body_id.map(|body_id| {
let body = tcx.hir().body(body_id);
tcx.mk_region(ty::ReScope(region::Scope {
});
let body = tcx.hir().body(body_id);
- let tables = Inherited::build(tcx, def_id).enter(|inh| {
+ let tables = Inherited::build(tcx, def_id.expect_local()).enter(|inh| {
let param_env = tcx.param_env(def_id);
let fcx = if let (Some(header), Some(decl)) = (fn_header, fn_decl) {
let fn_sig = if crate::collect::get_infer_ret_ty(&decl.output).is_some() {
// Consistency check our TypeckTables instance can hold all ItemLocalIds
// it will need to hold.
- assert_eq!(tables.local_id_root, Some(id.owner.to_def_id()));
+ assert_eq!(tables.hir_owner, Some(id.owner));
tables
}
WritebackCx {
fcx,
- tables: ty::TypeckTables::empty(Some(owner.to_def_id())),
+ tables: ty::TypeckTables::empty(Some(owner)),
body,
rustc_dump_user_substs,
}
fn visit_closures(&mut self) {
let fcx_tables = self.fcx.tables.borrow();
- debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
- let common_local_id_root = fcx_tables.local_id_root.unwrap();
+ assert_eq!(fcx_tables.hir_owner, self.tables.hir_owner);
+ let common_hir_owner = fcx_tables.hir_owner.unwrap();
for (&id, &origin) in fcx_tables.closure_kind_origins().iter() {
- let hir_id = hir::HirId { owner: common_local_id_root.expect_local(), local_id: id };
+ let hir_id = hir::HirId { owner: common_hir_owner, local_id: id };
self.tables.closure_kind_origins_mut().insert(hir_id, origin);
}
}
fn visit_coercion_casts(&mut self) {
let fcx_tables = self.fcx.tables.borrow();
let fcx_coercion_casts = fcx_tables.coercion_casts();
- debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
+ assert_eq!(fcx_tables.hir_owner, self.tables.hir_owner);
for local_id in fcx_coercion_casts {
self.tables.set_coercion_cast(*local_id);
fn visit_user_provided_tys(&mut self) {
let fcx_tables = self.fcx.tables.borrow();
- debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
- let common_local_id_root = fcx_tables.local_id_root.unwrap();
+ assert_eq!(fcx_tables.hir_owner, self.tables.hir_owner);
+ let common_hir_owner = fcx_tables.hir_owner.unwrap();
let mut errors_buffer = Vec::new();
for (&local_id, c_ty) in fcx_tables.user_provided_types().iter() {
- let hir_id = hir::HirId { owner: common_local_id_root.expect_local(), local_id };
+ let hir_id = hir::HirId { owner: common_hir_owner, local_id };
if cfg!(debug_assertions) && c_ty.has_local_value() {
span_bug!(hir_id.to_span(self.fcx.tcx), "writeback: `{:?}` is a local value", c_ty);
fn visit_user_provided_sigs(&mut self) {
let fcx_tables = self.fcx.tables.borrow();
- debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
+ assert_eq!(fcx_tables.hir_owner, self.tables.hir_owner);
for (&def_id, c_sig) in fcx_tables.user_provided_sigs.iter() {
if cfg!(debug_assertions) && c_sig.has_local_value() {
fn visit_generator_interior_types(&mut self) {
let fcx_tables = self.fcx.tables.borrow();
- debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
+ assert_eq!(fcx_tables.hir_owner, self.tables.hir_owner);
self.tables.generator_interior_types = fcx_tables.generator_interior_types.clone();
}
fn visit_liberated_fn_sigs(&mut self) {
let fcx_tables = self.fcx.tables.borrow();
- debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
- let common_local_id_root = fcx_tables.local_id_root.unwrap();
+ assert_eq!(fcx_tables.hir_owner, self.tables.hir_owner);
+ let common_hir_owner = fcx_tables.hir_owner.unwrap();
for (&local_id, fn_sig) in fcx_tables.liberated_fn_sigs().iter() {
- let hir_id = hir::HirId { owner: common_local_id_root.expect_local(), local_id };
+ let hir_id = hir::HirId { owner: common_hir_owner, local_id };
let fn_sig = self.resolve(fn_sig, &hir_id);
self.tables.liberated_fn_sigs_mut().insert(hir_id, fn_sig.clone());
}
fn visit_fru_field_types(&mut self) {
let fcx_tables = self.fcx.tables.borrow();
- debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
- let common_local_id_root = fcx_tables.local_id_root.unwrap();
+ assert_eq!(fcx_tables.hir_owner, self.tables.hir_owner);
+ let common_hir_owner = fcx_tables.hir_owner.unwrap();
for (&local_id, ftys) in fcx_tables.fru_field_types().iter() {
- let hir_id = hir::HirId { owner: common_local_id_root.expect_local(), local_id };
+ let hir_id = hir::HirId { owner: common_hir_owner, local_id };
let ftys = self.resolve(ftys, &hir_id);
self.tables.fru_field_types_mut().insert(hir_id, ftys);
}