X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibrustc%2Fty%2Fmod.rs;h=80781dd9bc7360805c35402ceaf0565f9dd0fd89;hb=231b0375f6778bb409305cd3c34bc820d727aab4;hp=fa993325e271233e362d6c850626eb58f6e631f5;hpb=07d0b578c15f600c7b798cc0295d74973c8ccb69;p=rust.git diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index fa993325e27..80781dd9bc7 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -8,7 +8,7 @@ pub use self::IntVarValue::*; pub use self::fold::TypeFoldable; -use crate::hir::{map as hir_map, UpvarMap, GlobMap, TraitMap}; +use crate::hir::{map as hir_map, GlobMap, TraitMap}; use crate::hir::Node; use crate::hir::def::{Res, DefKind, CtorOf, CtorKind, ExportMap}; use crate::hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE}; @@ -51,6 +51,7 @@ use syntax_pos::Span; use smallvec; +use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::indexed_vec::{Idx, IndexVec}; use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult, HashStable}; @@ -75,7 +76,7 @@ pub use self::binding::BindingMode; pub use self::binding::BindingMode::*; -pub use self::context::{TyCtxt, FreeRegionInfo, GlobalArenas, AllArenas, tls, keep_local}; +pub use self::context::{TyCtxt, FreeRegionInfo, AllArenas, tls, keep_local}; pub use self::context::{Lift, TypeckTables, CtxtInterners, GlobalCtxt}; pub use self::context::{ UserTypeAnnotationIndex, UserType, CanonicalUserType, @@ -122,7 +123,6 @@ #[derive(Clone)] pub struct Resolutions { - pub upvars: UpvarMap, pub trait_map: TraitMap, pub maybe_unused_trait_imports: NodeSet, pub maybe_unused_extern_crates: Vec<(NodeId, Span)>, @@ -808,7 +808,7 @@ pub struct UpvarBorrow<'tcx> { pub region: ty::Region<'tcx>, } -pub type UpvarListMap = FxHashMap>; +pub type UpvarListMap = FxHashMap>; pub type UpvarCaptureMap<'tcx> = FxHashMap>; #[derive(Copy, Clone)] @@ -1090,7 +1090,7 @@ pub enum Predicate<'tcx> { /// See the `ProjectionPredicate` struct for details. Projection(PolyProjectionPredicate<'tcx>), - /// no syntax: `T` well-formed + /// No syntax: `T` well-formed. WellFormed(Ty<'tcx>), /// Trait must be object-safe. @@ -1245,19 +1245,17 @@ pub fn self_ty(&self) -> Ty<'tcx> { impl<'tcx> PolyTraitPredicate<'tcx> { pub fn def_id(&self) -> DefId { - // ok to skip binder since trait def-id does not care about regions + // Ok to skip binder since trait def-ID does not care about regions. self.skip_binder().def_id() } } #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, RustcEncodable, RustcDecodable, HashStable)] -pub struct OutlivesPredicate(pub A, pub B); // `A: B` -pub type PolyOutlivesPredicate = ty::Binder>; -pub type RegionOutlivesPredicate<'tcx> = OutlivesPredicate, - ty::Region<'tcx>>; -pub type TypeOutlivesPredicate<'tcx> = OutlivesPredicate, - ty::Region<'tcx>>; +pub struct OutlivesPredicate(pub A, pub B); // `A: B` +pub type PolyOutlivesPredicate = ty::Binder>; +pub type RegionOutlivesPredicate<'tcx> = OutlivesPredicate, ty::Region<'tcx>>; +pub type TypeOutlivesPredicate<'tcx> = OutlivesPredicate, ty::Region<'tcx>>; pub type PolyRegionOutlivesPredicate<'tcx> = ty::Binder>; pub type PolyTypeOutlivesPredicate<'tcx> = ty::Binder>; @@ -1314,7 +1312,7 @@ pub fn ty(&self) -> Binder> { /// Note that this is not the `DefId` of the `TraitRef` containing this /// associated type, which is in `tcx.associated_item(projection_def_id()).container`. pub fn projection_def_id(&self) -> DefId { - // okay to skip binder since trait def-id does not care about regions + // Ok to skip binder since trait def-ID does not care about regions. self.skip_binder().projection_ty.item_def_id } } @@ -1371,7 +1369,7 @@ fn to_predicate(&self) -> Predicate<'tcx> { } } -// A custom iterator used by Predicate::walk_tys. +// A custom iterator used by `Predicate::walk_tys`. enum WalkTysIter<'tcx, I, J, K> where I: Iterator>, J: Iterator>, @@ -1505,7 +1503,7 @@ pub fn to_opt_type_outlives(&self) -> Option> { /// /// Example: /// -/// struct Foo> { ... } +/// struct Foo> { ... } /// /// Here, the `GenericPredicates` for `Foo` would contain a list of bounds like /// `[[], [U:Bar]]`. Now if there were some particular reference @@ -2305,7 +2303,7 @@ pub fn predicates(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> &'tcx GenericPredicates /// Returns an iterator over all fields contained /// by this ADT. #[inline] - pub fn all_fields<'s>(&'s self) -> impl Iterator { + pub fn all_fields<'s>(&'s self) -> impl Iterator + Clone { self.variants.iter().flat_map(|v| v.fields.iter()) } @@ -2785,10 +2783,10 @@ pub fn expr_span(self, id: NodeId) -> Span { e.span } Some(f) => { - bug!("Node id {} is not an expr: {:?}", id, f); + bug!("node-ID {} is not an expr: {:?}", id, f); } None => { - bug!("Node id {} is not present in the node map", id); + bug!("node-ID {} is not present in the node map", id); } } } @@ -2886,7 +2884,7 @@ pub fn field_index(self, hir_id: hir::HirId, tables: &TypeckTables<'_>) -> usize pub fn find_field_index(self, ident: Ident, variant: &VariantDef) -> Option { variant.fields.iter().position(|field| { - self.adjust_ident(ident, variant.def_id, hir::DUMMY_HIR_ID).0 == field.ident.modern() + self.hygienic_eq(ident, field.ident, variant.def_id) }) } @@ -3085,19 +3083,31 @@ pub fn span_of_impl(self, impl_did: DefId) -> Result { /// its supposed definition name (`def_name`). The method also needs `DefId` of the supposed /// definition's parent/scope to perform comparison. pub fn hygienic_eq(self, use_name: Ident, def_name: Ident, def_parent_def_id: DefId) -> bool { - self.adjust_ident(use_name, def_parent_def_id, hir::DUMMY_HIR_ID).0 == def_name.modern() + // We could use `Ident::eq` here, but we deliberately don't. The name + // comparison fails frequently, and we want to avoid the expensive + // `modern()` calls required for the span comparison whenever possible. + use_name.name == def_name.name && + use_name.span.ctxt().hygienic_eq(def_name.span.ctxt(), + self.expansion_that_defined(def_parent_def_id)) } - pub fn adjust_ident(self, mut ident: Ident, scope: DefId, block: hir::HirId) -> (Ident, DefId) { - ident = ident.modern(); - let target_expansion = match scope.krate { + fn expansion_that_defined(self, scope: DefId) -> Mark { + match scope.krate { LOCAL_CRATE => self.hir().definitions().expansion_that_defined(scope.index), _ => Mark::root(), - }; - let scope = match ident.span.adjust(target_expansion) { + } + } + + pub fn adjust_ident(self, mut ident: Ident, scope: DefId) -> Ident { + ident.span.modernize_and_adjust(self.expansion_that_defined(scope)); + ident + } + + pub fn adjust_ident_and_get_scope(self, mut ident: Ident, scope: DefId, block: hir::HirId) + -> (Ident, DefId) { + let scope = match ident.span.modernize_and_adjust(self.expansion_that_defined(scope)) { Some(actual_expansion) => self.hir().definitions().parent_module_of_macro_def(actual_expansion), - None if block == hir::DUMMY_HIR_ID => DefId::local(CRATE_DEF_INDEX), // Dummy DefId None => self.hir().get_module_parent_by_hir_id(block), }; (ident, scope)