Skip to content
Permalink
Browse files

cleanup: Remove `DefIndexAddressSpace`

  • Loading branch information...
petrochenkov committed May 8, 2019
1 parent e630580 commit ee6d31520071b38f9a2996d60a66d6e2cd965fe7
Showing with 246 additions and 403 deletions.
  1. +11 −56 src/librustc/hir/def_id.rs
  2. +1 −4 src/librustc/hir/lowering.rs
  3. +2 −7 src/librustc/hir/map/collector.rs
  4. +13 −23 src/librustc/hir/map/def_collector.rs
  5. +51 −108 src/librustc/hir/map/definitions.rs
  6. +20 −32 src/librustc/hir/map/mod.rs
  7. +1 −3 src/librustc/traits/specialize/mod.rs
  8. +1 −2 src/librustc_driver/pretty.rs
  9. +2 −4 src/librustc_metadata/decoder.rs
  10. +10 −24 src/librustc_metadata/index.rs
  11. +1 −1 src/librustc_metadata/index_builder.rs
  12. +1 −2 src/librustc_mir/util/graphviz.rs
  13. +1 −1 src/librustc_resolve/lib.rs
  14. +2 −4 src/librustc_resolve/macros.rs
  15. +6 −9 src/librustdoc/core.rs
  16. +1 −1 src/test/mir-opt/graphviz.rs
  17. +1 −1 src/test/mir-opt/inline-closure-borrows-arg.rs
  18. +1 −1 src/test/mir-opt/inline-closure.rs
  19. +1 −1 src/test/mir-opt/retag.rs
  20. +3 −3 src/test/ui/nll/closure-requirements/escape-argument-callee.stderr
  21. +3 −3 src/test/ui/nll/closure-requirements/escape-argument.stderr
  22. +3 −3 src/test/ui/nll/closure-requirements/escape-upvar-nested.stderr
  23. +2 −2 src/test/ui/nll/closure-requirements/escape-upvar-ref.stderr
  24. +3 −3 src/test/ui/nll/closure-requirements/propagate-approximated-fail-no-postdom.stderr
  25. +3 −3 src/test/ui/nll/closure-requirements/propagate-approximated-ref.stderr
  26. +6 −6 ...i/nll/closure-requirements/propagate-approximated-shorter-to-static-comparing-against-free.stderr
  27. +3 −3 src/test/ui/nll/closure-requirements/propagate-approximated-shorter-to-static-no-bound.stderr
  28. +3 −3 src/test/ui/nll/closure-requirements/propagate-approximated-shorter-to-static-wrong-bound.stderr
  29. +3 −3 src/test/ui/nll/closure-requirements/propagate-approximated-val.stderr
  30. +3 −3 src/test/ui/nll/closure-requirements/propagate-despite-same-free-region.stderr
  31. +3 −3 src/test/ui/nll/closure-requirements/propagate-fail-to-approximate-longer-no-bounds.stderr
  32. +3 −3 src/test/ui/nll/closure-requirements/propagate-fail-to-approximate-longer-wrong-bounds.stderr
  33. +2 −2 src/test/ui/nll/closure-requirements/propagate-from-trait-match.stderr
  34. +3 −3 src/test/ui/nll/closure-requirements/return-wrong-bound-region.stderr
  35. +8 −8 src/test/ui/nll/ty-outlives/projection-no-regions-closure.stderr
  36. +9 −9 src/test/ui/nll/ty-outlives/projection-one-region-closure.stderr
  37. +10 −10 src/test/ui/nll/ty-outlives/projection-one-region-trait-bound-closure.stderr
  38. +10 −10 src/test/ui/nll/ty-outlives/projection-one-region-trait-bound-static-closure.stderr
  39. +17 −17 src/test/ui/nll/ty-outlives/projection-two-region-trait-bound-closure.stderr
  40. +7 −7 src/test/ui/nll/ty-outlives/ty-param-closure-approximate-lower-bound.stderr
  41. +2 −2 src/test/ui/nll/ty-outlives/ty-param-closure-outlives-from-return-type.stderr
  42. +10 −10 src/test/ui/nll/ty-outlives/ty-param-closure-outlives-from-where-clause.stderr
@@ -1,5 +1,5 @@
use crate::ty::{self, TyCtxt};
use crate::hir::map::definitions::FIRST_FREE_HIGH_DEF_INDEX;
use crate::hir::map::definitions::FIRST_FREE_DEF_INDEX;
use rustc_data_structures::indexed_vec::Idx;
use serialize;
use std::fmt;
@@ -99,17 +99,6 @@ impl serialize::UseSpecializedDecodable for CrateNum {}
/// A DefIndex is an index into the hir-map for a crate, identifying a
/// particular definition. It should really be considered an interned
/// shorthand for a particular DefPath.
///
/// At the moment we are allocating the numerical values of DefIndexes from two
/// address spaces: DefIndexAddressSpace::Low and DefIndexAddressSpace::High.
/// This allows us to allocate the DefIndexes of all item-likes
/// (Items, TraitItems, and ImplItems) into one of these spaces and
/// consequently use a simple array for lookup tables keyed by DefIndex and
/// known to be densely populated. This is especially important for the HIR map.
///
/// Since the DefIndex is mostly treated as an opaque ID, you probably
/// don't have to care about these address spaces.

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
pub struct DefIndex(u32);

@@ -119,67 +108,49 @@ pub const CRATE_DEF_INDEX: DefIndex = DefIndex(0);

impl fmt::Debug for DefIndex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,
"DefIndex({}:{})",
self.address_space().index(),
self.as_array_index())
write!(f, "DefIndex({})", self.as_array_index())
}
}

impl DefIndex {
#[inline]
pub fn address_space(&self) -> DefIndexAddressSpace {
match self.0 & 1 {
0 => DefIndexAddressSpace::Low,
1 => DefIndexAddressSpace::High,
_ => unreachable!()
}
}

/// Converts this DefIndex into a zero-based array index.
/// This index is the offset within the given DefIndexAddressSpace.
#[inline]
pub fn as_array_index(&self) -> usize {
(self.0 >> 1) as usize
self.0 as usize
}

#[inline]
pub fn from_array_index(i: usize, address_space: DefIndexAddressSpace) -> DefIndex {
DefIndex::from_raw_u32(((i << 1) | (address_space as usize)) as u32)
pub fn from_array_index(i: usize) -> DefIndex {
DefIndex(i as u32)
}

// Proc macros from a proc-macro crate have a kind of virtual DefIndex. This
// function maps the index of the macro within the crate (which is also the
// index of the macro in the CrateMetadata::proc_macros array) to the
// corresponding DefIndex.
pub fn from_proc_macro_index(proc_macro_index: usize) -> DefIndex {
// DefIndex for proc macros start from FIRST_FREE_HIGH_DEF_INDEX,
// because the first FIRST_FREE_HIGH_DEF_INDEX indexes are reserved
// DefIndex for proc macros start from FIRST_FREE_DEF_INDEX,
// because the first FIRST_FREE_DEF_INDEX indexes are reserved
// for internal use.
let def_index = DefIndex::from_array_index(
proc_macro_index.checked_add(FIRST_FREE_HIGH_DEF_INDEX)
.expect("integer overflow adding `proc_macro_index`"),
DefIndexAddressSpace::High);
proc_macro_index.checked_add(FIRST_FREE_DEF_INDEX)
.expect("integer overflow adding `proc_macro_index`"));
assert!(def_index != CRATE_DEF_INDEX);
def_index
}

// This function is the reverse of from_proc_macro_index() above.
pub fn to_proc_macro_index(self: DefIndex) -> usize {
assert_eq!(self.address_space(), DefIndexAddressSpace::High);

self.as_array_index().checked_sub(FIRST_FREE_HIGH_DEF_INDEX)
self.as_array_index().checked_sub(FIRST_FREE_DEF_INDEX)
.unwrap_or_else(|| {
bug!("using local index {:?} as proc-macro index", self)
})
}

// Don't use this if you don't know about the DefIndex encoding.
pub fn from_raw_u32(x: u32) -> DefIndex {
DefIndex(x)
}

// Don't use this if you don't know about the DefIndex encoding.
pub fn as_raw_u32(&self) -> u32 {
self.0
}
@@ -188,19 +159,6 @@ impl DefIndex {
impl serialize::UseSpecializedEncodable for DefIndex {}
impl serialize::UseSpecializedDecodable for DefIndex {}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum DefIndexAddressSpace {
Low = 0,
High = 1,
}

impl DefIndexAddressSpace {
#[inline]
pub fn index(&self) -> usize {
*self as usize
}
}

/// A `DefId` identifies a particular *definition*, by combining a crate
/// index and a def index.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
@@ -211,10 +169,7 @@ pub struct DefId {

impl fmt::Debug for DefId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "DefId({}/{}:{}",
self.krate,
self.index.address_space().index(),
self.index.as_array_index())?;
write!(f, "DefId({}:{}", self.krate, self.index.as_array_index())?;

ty::tls::with_opt(|opt_tcx| {
if let Some(tcx) = opt_tcx {
@@ -36,7 +36,7 @@ use crate::dep_graph::DepGraph;
use crate::hir::{self, ParamName};
use crate::hir::HirVec;
use crate::hir::map::{DefKey, DefPathData, Definitions};
use crate::hir::def_id::{DefId, DefIndex, DefIndexAddressSpace, CRATE_DEF_INDEX};
use crate::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
use crate::hir::def::{Res, DefKind, PartialRes, PerNS};
use crate::hir::{GenericArg, ConstArg};
use crate::lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
@@ -418,7 +418,6 @@ impl<'a> LoweringContext<'a> {
owner,
id,
DefPathData::Misc,
DefIndexAddressSpace::High,
Mark::root(),
tree.prefix.span,
);
@@ -962,7 +961,6 @@ impl<'a> LoweringContext<'a> {
parent_index,
node_id,
DefPathData::LifetimeNs(str_name),
DefIndexAddressSpace::High,
Mark::root(),
span,
);
@@ -1763,7 +1761,6 @@ impl<'a> LoweringContext<'a> {
self.parent,
def_node_id,
DefPathData::LifetimeNs(name.ident().as_interned_str()),
DefIndexAddressSpace::High,
Mark::root(),
lifetime.span,
);
@@ -145,15 +145,10 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
);
}

let (lo, hi) = definitions.def_index_counts_lo_hi();

let mut collector = NodeCollector {
krate,
source_map: sess.source_map(),
map: [
repeat(None).take(lo).collect(),
repeat(None).take(hi).collect(),
],
map: vec![None; definitions.def_index_count()],
parent_node: hir::CRATE_HIR_ID,
current_signature_dep_index: root_mod_sig_dep_index,
current_full_dep_index: root_mod_full_dep_index,
@@ -231,7 +226,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {

fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>) {
debug!("hir_map: {:?} => {:?}", id, entry);
let local_map = &mut self.map[id.owner.address_space().index()][id.owner.as_array_index()];
let local_map = &mut self.map[id.owner.as_array_index()];
let i = id.local_id.as_u32() as usize;
if local_map.is_none() {
*local_map = Some(IndexVec::with_capacity(i + 1));
@@ -1,5 +1,5 @@
use crate::hir::map::definitions::*;
use crate::hir::def_id::{CRATE_DEF_INDEX, DefIndex, DefIndexAddressSpace};
use crate::hir::def_id::{CRATE_DEF_INDEX, DefIndex};
use crate::session::CrateDisambiguator;

use syntax::ast::*;
@@ -10,8 +10,6 @@ use syntax::symbol::Symbol;
use syntax::parse::token::{self, Token};
use syntax_pos::Span;

use crate::hir::map::{ITEM_LIKE_SPACE, REGULAR_SPACE};

/// Creates `DefId`s for nodes in the AST.
pub struct DefCollector<'a> {
definitions: &'a mut Definitions,
@@ -47,13 +45,12 @@ impl<'a> DefCollector<'a> {
fn create_def(&mut self,
node_id: NodeId,
data: DefPathData,
address_space: DefIndexAddressSpace,
span: Span)
-> DefIndex {
let parent_def = self.parent_def.unwrap();
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
self.definitions
.create_def_with_parent(parent_def, node_id, data, address_space, self.expansion, span)
.create_def_with_parent(parent_def, node_id, data, self.expansion, span)
}

pub fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: DefIndex, f: F) {
@@ -85,9 +82,9 @@ impl<'a> DefCollector<'a> {
// For async functions, we need to create their inner defs inside of a
// closure to match their desugared representation.
let fn_def_data = DefPathData::ValueNs(name.as_interned_str());
let fn_def = self.create_def(id, fn_def_data, ITEM_LIKE_SPACE, span);
let fn_def = self.create_def(id, fn_def_data, span);
return self.with_parent(fn_def, |this| {
this.create_def(*return_impl_trait_id, DefPathData::ImplTrait, REGULAR_SPACE, span);
this.create_def(*return_impl_trait_id, DefPathData::ImplTrait, span);

visit::walk_generics(this, generics);

@@ -106,7 +103,7 @@ impl<'a> DefCollector<'a> {
visit::walk_fn_ret_ty(this, &decl.output);

let closure_def = this.create_def(
*closure_id, DefPathData::ClosureExpr, REGULAR_SPACE, span,
*closure_id, DefPathData::ClosureExpr, span,
);
this.with_parent(closure_def, |this| {
use visit::Visitor;
@@ -173,14 +170,14 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
return visit::walk_item(self, i);
}
};
let def = self.create_def(i.id, def_data, ITEM_LIKE_SPACE, i.span);
let def = self.create_def(i.id, def_data, i.span);

self.with_parent(def, |this| {
match i.node {
ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {
// If this is a unit or tuple-like struct, register the constructor.
if let Some(ctor_hir_id) = struct_def.ctor_id() {
this.create_def(ctor_hir_id, DefPathData::Ctor, REGULAR_SPACE, i.span);
this.create_def(ctor_hir_id, DefPathData::Ctor, i.span);
}
}
_ => {}
@@ -190,7 +187,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
}

fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) {
self.create_def(id, DefPathData::Misc, ITEM_LIKE_SPACE, use_tree.span);
self.create_def(id, DefPathData::Misc, use_tree.span);
visit::walk_use_tree(self, use_tree, id);
}

@@ -201,7 +198,6 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {

let def = self.create_def(foreign_item.id,
DefPathData::ValueNs(foreign_item.ident.as_interned_str()),
REGULAR_SPACE,
foreign_item.span);

self.with_parent(def, |this| {
@@ -212,11 +208,10 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
fn visit_variant(&mut self, v: &'a Variant, g: &'a Generics, item_id: NodeId) {
let def = self.create_def(v.node.id,
DefPathData::TypeNs(v.node.ident.as_interned_str()),
REGULAR_SPACE,
v.span);
self.with_parent(def, |this| {
if let Some(ctor_hir_id) = v.node.data.ctor_id() {
this.create_def(ctor_hir_id, DefPathData::Ctor, REGULAR_SPACE, v.span);
this.create_def(ctor_hir_id, DefPathData::Ctor, v.span);
}
visit::walk_variant(this, v, g, item_id)
});
@@ -229,7 +224,6 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
.unwrap_or_else(|| Symbol::intern(&index.to_string()));
let def = self.create_def(field.id,
DefPathData::ValueNs(name.as_interned_str()),
REGULAR_SPACE,
field.span);
self.with_parent(def, |this| this.visit_struct_field(field));
}
@@ -242,7 +236,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
GenericParamKind::Type { .. } => DefPathData::TypeNs(name),
GenericParamKind::Const { .. } => DefPathData::ValueNs(name),
};
self.create_def(param.id, def_path_data, REGULAR_SPACE, param.ident.span);
self.create_def(param.id, def_path_data, param.ident.span);

visit::walk_generic_param(self, param);
}
@@ -257,7 +251,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id),
};

let def = self.create_def(ti.id, def_data, ITEM_LIKE_SPACE, ti.span);
let def = self.create_def(ti.id, def_data, ti.span);
self.with_parent(def, |this| visit::walk_trait_item(this, ti));
}

@@ -286,7 +280,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id),
};

let def = self.create_def(ii.id, def_data, ITEM_LIKE_SPACE, ii.span);
let def = self.create_def(ii.id, def_data, ii.span);
self.with_parent(def, |this| visit::walk_impl_item(this, ii));
}

@@ -300,7 +294,6 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
fn visit_anon_const(&mut self, constant: &'a AnonConst) {
let def = self.create_def(constant.id,
DefPathData::AnonConst,
REGULAR_SPACE,
constant.value.span);
self.with_parent(def, |this| visit::walk_anon_const(this, constant));
}
@@ -313,7 +306,6 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
ExprKind::Closure(_, ref asyncness, ..) => {
let closure_def = self.create_def(expr.id,
DefPathData::ClosureExpr,
REGULAR_SPACE,
expr.span);
self.parent_def = Some(closure_def);

@@ -322,15 +314,13 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
if let IsAsync::Async { closure_id, .. } = asyncness {
let async_def = self.create_def(*closure_id,
DefPathData::ClosureExpr,
REGULAR_SPACE,
expr.span);
self.parent_def = Some(async_def);
}
}
ExprKind::Async(_, async_id, _) => {
let async_def = self.create_def(async_id,
DefPathData::ClosureExpr,
REGULAR_SPACE,
expr.span);
self.parent_def = Some(async_def);
}
@@ -345,7 +335,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
match ty.node {
TyKind::Mac(..) => return self.visit_macro_invoc(ty.id),
TyKind::ImplTrait(node_id, _) => {
self.create_def(node_id, DefPathData::ImplTrait, REGULAR_SPACE, ty.span);
self.create_def(node_id, DefPathData::ImplTrait, ty.span);
}
_ => {}
}
Oops, something went wrong.

0 comments on commit ee6d315

Please sign in to comment.
You can’t perform that action at this time.