diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 8622a6207212c..12209d6725c90 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -63,6 +63,15 @@ impl ModuleItems { self.foreign_items.iter().copied() } + pub fn definitions(&self) -> impl Iterator + '_ { + self.items + .iter() + .map(|id| id.def_id) + .chain(self.trait_items.iter().map(|id| id.def_id)) + .chain(self.impl_items.iter().map(|id| id.def_id)) + .chain(self.foreign_items.iter().map(|id| id.def_id)) + } + pub fn par_items(&self, f: impl Fn(ItemId) + Send + Sync) { par_for_each_in(&self.items[..], |&id| f(id)) } diff --git a/compiler/rustc_typeck/src/variance/constraints.rs b/compiler/rustc_typeck/src/variance/constraints.rs index a7dcbfff207f6..d79450e1ae707 100644 --- a/compiler/rustc_typeck/src/variance/constraints.rs +++ b/compiler/rustc_typeck/src/variance/constraints.rs @@ -64,74 +64,28 @@ pub fn add_constraints_from_crate<'a, 'tcx>( let crate_items = tcx.hir_crate_items(()); - for id in crate_items.items() { - constraint_cx.check_item(id); - } - - for id in crate_items.trait_items() { - if let DefKind::AssocFn = tcx.def_kind(id.def_id) { - constraint_cx.check_node_helper(id.hir_id()); - } - } - - for id in crate_items.impl_items() { - if let DefKind::AssocFn = tcx.def_kind(id.def_id) { - constraint_cx.check_node_helper(id.hir_id()); - } - } - - for id in crate_items.foreign_items() { - if let DefKind::Fn = tcx.def_kind(id.def_id) { - constraint_cx.check_node_helper(id.hir_id()); - } - } - - constraint_cx -} - -impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { - fn check_item(&mut self, id: hir::ItemId) { - let def_kind = self.tcx().def_kind(id.def_id); + for def_id in crate_items.definitions() { + let def_kind = tcx.def_kind(def_id); match def_kind { - DefKind::Struct | DefKind::Union => { - let item = self.tcx().hir().item(id); - - if let hir::ItemKind::Struct(ref struct_def, _) - | hir::ItemKind::Union(ref struct_def, _) = item.kind - { - self.check_node_helper(item.hir_id()); + DefKind::Struct | DefKind::Union | DefKind::Enum => { + constraint_cx.build_constraints_for_item(def_id); - if let hir::VariantData::Tuple(..) = *struct_def { - self.check_node_helper(struct_def.ctor_hir_id().unwrap()); + let adt = tcx.adt_def(def_id); + for variant in adt.variants() { + if let Some(ctor) = variant.ctor_def_id { + constraint_cx.build_constraints_for_item(ctor.expect_local()); } } } - DefKind::Enum => { - let item = self.tcx().hir().item(id); - - if let hir::ItemKind::Enum(ref enum_def, _) = item.kind { - self.check_node_helper(item.hir_id()); - - for variant in enum_def.variants { - if let hir::VariantData::Tuple(..) = variant.data { - self.check_node_helper(variant.data.ctor_hir_id().unwrap()); - } - } - } - } - DefKind::Fn => { - self.check_node_helper(id.hir_id()); - } + DefKind::Fn | DefKind::AssocFn => constraint_cx.build_constraints_for_item(def_id), _ => {} } } - fn check_node_helper(&mut self, id: hir::HirId) { - let tcx = self.terms_cx.tcx; - let def_id = tcx.hir().local_def_id(id); - self.build_constraints_for_item(def_id); - } + constraint_cx +} +impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { fn tcx(&self) -> TyCtxt<'tcx> { self.terms_cx.tcx } @@ -145,8 +99,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { return; } - let id = tcx.hir().local_def_id_to_hir_id(def_id); - let inferred_start = self.terms_cx.inferred_starts[&id]; + let inferred_start = self.terms_cx.inferred_starts[&def_id]; let current_item = &CurrentItem { inferred_start }; match tcx.type_of(def_id).kind() { ty::Adt(def, _) => { @@ -372,8 +325,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { } let (local, remote) = if let Some(def_id) = def_id.as_local() { - let id = self.tcx().hir().local_def_id_to_hir_id(def_id); - (Some(self.terms_cx.inferred_starts[&id]), None) + (Some(self.terms_cx.inferred_starts[&def_id]), None) } else { (None, Some(self.tcx().variances_of(def_id))) }; diff --git a/compiler/rustc_typeck/src/variance/solve.rs b/compiler/rustc_typeck/src/variance/solve.rs index 1a4d88ced0e45..97aca621aa217 100644 --- a/compiler/rustc_typeck/src/variance/solve.rs +++ b/compiler/rustc_typeck/src/variance/solve.rs @@ -96,8 +96,7 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> { self.terms_cx .inferred_starts .iter() - .map(|(&id, &InferredIndex(start))| { - let def_id = tcx.hir().local_def_id(id); + .map(|(&def_id, &InferredIndex(start))| { let generics = tcx.generics_of(def_id); let count = generics.count(); diff --git a/compiler/rustc_typeck/src/variance/terms.rs b/compiler/rustc_typeck/src/variance/terms.rs index ab64befe5dc16..1f763011e0614 100644 --- a/compiler/rustc_typeck/src/variance/terms.rs +++ b/compiler/rustc_typeck/src/variance/terms.rs @@ -10,9 +10,8 @@ // a variable. use rustc_arena::DroplessArena; -use rustc_hir as hir; use rustc_hir::def::DefKind; -use rustc_hir::HirIdMap; +use rustc_hir::def_id::{LocalDefId, LocalDefIdMap}; use rustc_middle::ty::{self, TyCtxt}; use std::fmt; @@ -52,11 +51,11 @@ pub struct TermsContext<'a, 'tcx> { // For marker types, UnsafeCell, and other lang items where // variance is hardcoded, records the item-id and the hardcoded // variance. - pub lang_items: Vec<(hir::HirId, Vec)>, + pub lang_items: Vec<(LocalDefId, Vec)>, // Maps from the node id of an item to the first inferred index // used for its type & region parameters. - pub inferred_starts: HirIdMap, + pub inferred_starts: LocalDefIdMap, // Maps from an InferredIndex to the term for that variable. pub inferred_terms: Vec>, @@ -81,32 +80,31 @@ pub fn determine_parameters_to_be_inferred<'a, 'tcx>( // - https://rustc-dev-guide.rust-lang.org/variance.html let crate_items = tcx.hir_crate_items(()); - for id in crate_items.items() { - terms_cx.check_item(id); - } + for def_id in crate_items.definitions() { + debug!("add_inferreds for item {:?}", def_id); - for id in crate_items.trait_items() { - if let DefKind::AssocFn = tcx.def_kind(id.def_id) { - terms_cx.add_inferreds_for_item(id.hir_id()); - } - } + let def_kind = tcx.def_kind(def_id); - for id in crate_items.impl_items() { - if let DefKind::AssocFn = tcx.def_kind(id.def_id) { - terms_cx.add_inferreds_for_item(id.hir_id()); - } - } + match def_kind { + DefKind::Struct | DefKind::Union | DefKind::Enum => { + terms_cx.add_inferreds_for_item(def_id); - for id in crate_items.foreign_items() { - if let DefKind::Fn = tcx.def_kind(id.def_id) { - terms_cx.add_inferreds_for_item(id.hir_id()); + let adt = tcx.adt_def(def_id); + for variant in adt.variants() { + if let Some(ctor) = variant.ctor_def_id { + terms_cx.add_inferreds_for_item(ctor.expect_local()); + } + } + } + DefKind::Fn | DefKind::AssocFn => terms_cx.add_inferreds_for_item(def_id), + _ => {} } } terms_cx } -fn lang_items(tcx: TyCtxt<'_>) -> Vec<(hir::HirId, Vec)> { +fn lang_items(tcx: TyCtxt<'_>) -> Vec<(LocalDefId, Vec)> { let lang_items = tcx.lang_items(); let all = [ (lang_items.phantom_data(), vec![ty::Covariant]), @@ -114,18 +112,16 @@ fn lang_items(tcx: TyCtxt<'_>) -> Vec<(hir::HirId, Vec)> { ]; all.into_iter() // iterating over (Option, Variance) - .filter(|&(ref d, _)| d.is_some()) - .map(|(d, v)| (d.unwrap(), v)) // (DefId, Variance) .filter_map(|(d, v)| { - d.as_local().map(|d| tcx.hir().local_def_id_to_hir_id(d)).map(|n| (n, v)) - }) // (HirId, Variance) + let def_id = d?.as_local()?; // LocalDefId + Some((def_id, v)) + }) .collect() } impl<'a, 'tcx> TermsContext<'a, 'tcx> { - fn add_inferreds_for_item(&mut self, id: hir::HirId) { + fn add_inferreds_for_item(&mut self, def_id: LocalDefId) { let tcx = self.tcx; - let def_id = tcx.hir().local_def_id(id); let count = tcx.generics_of(def_id).count(); if count == 0 { @@ -134,7 +130,7 @@ impl<'a, 'tcx> TermsContext<'a, 'tcx> { // Record the start of this item's inferreds. let start = self.inferred_terms.len(); - let newly_added = self.inferred_starts.insert(id, InferredIndex(start)).is_none(); + let newly_added = self.inferred_starts.insert(def_id, InferredIndex(start)).is_none(); assert!(newly_added); // N.B., in the code below for writing the results back into the @@ -146,42 +142,4 @@ impl<'a, 'tcx> TermsContext<'a, 'tcx> { (start..(start + count)).map(|i| &*arena.alloc(InferredTerm(InferredIndex(i)))), ); } - - fn check_item(&mut self, id: hir::ItemId) { - debug!("add_inferreds for item {}", self.tcx.hir().node_to_string(id.hir_id())); - - let def_kind = self.tcx.def_kind(id.def_id); - match def_kind { - DefKind::Struct | DefKind::Union => { - let item = self.tcx.hir().item(id); - - if let hir::ItemKind::Struct(ref struct_def, _) - | hir::ItemKind::Union(ref struct_def, _) = item.kind - { - self.add_inferreds_for_item(item.hir_id()); - - if let hir::VariantData::Tuple(..) = *struct_def { - self.add_inferreds_for_item(struct_def.ctor_hir_id().unwrap()); - } - } - } - DefKind::Enum => { - let item = self.tcx.hir().item(id); - - if let hir::ItemKind::Enum(ref enum_def, _) = item.kind { - self.add_inferreds_for_item(item.hir_id()); - - for variant in enum_def.variants { - if let hir::VariantData::Tuple(..) = variant.data { - self.add_inferreds_for_item(variant.data.ctor_hir_id().unwrap()); - } - } - } - } - DefKind::Fn => { - self.add_inferreds_for_item(id.hir_id()); - } - _ => {} - } - } }