未验证 提交 1d5829d8 编写于 作者: P Pietro Albini 提交者: GitHub

Rollup merge of #55750 - oli-obk:node_id_x, r=michaelwoerister

Make `NodeId` and `HirLocalId` `newtype_index`
......@@ -63,7 +63,7 @@
use mir::interpret::GlobalId;
use hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
use hir::map::DefPathHash;
use hir::{HirId, ItemLocalId};
use hir::HirId;
use ich::{Fingerprint, StableHashingContext};
use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
......@@ -790,11 +790,11 @@ impl<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> DepNodeParams<'a, 'gcx, 'tcx> for HirId {
fn to_fingerprint(&self, tcx: TyCtxt<'_, '_, '_>) -> Fingerprint {
let HirId {
owner,
local_id: ItemLocalId(local_id),
local_id,
} = *self;
let def_path_hash = tcx.def_path_hash(DefId::local(owner));
let local_id = Fingerprint::from_smaller_hash(local_id as u64);
let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into());
def_path_hash.0.combine(local_id)
}
......
......@@ -49,7 +49,6 @@
use super::itemlikevisit::DeepVisitor;
use std::cmp;
use std::u32;
#[derive(Copy, Clone)]
pub enum FnKind<'a> {
......@@ -1152,8 +1151,8 @@ pub struct IdRange {
impl IdRange {
pub fn max() -> IdRange {
IdRange {
min: NodeId::from_u32(u32::MAX),
max: NodeId::from_u32(u32::MIN),
min: NodeId::MAX,
max: NodeId::from_u32(0),
}
}
......
......@@ -588,7 +588,7 @@ fn lower_node_id(&mut self, ast_node_id: NodeId) -> LoweredNodeId {
*local_id_counter += 1;
hir::HirId {
owner: def_index,
local_id: hir::ItemLocalId(local_id),
local_id: hir::ItemLocalId::from_u32(local_id),
}
})
}
......@@ -616,7 +616,7 @@ fn lower_node_id_with_owner(&mut self, ast_node_id: NodeId, owner: NodeId) -> Lo
hir::HirId {
owner: def_index,
local_id: hir::ItemLocalId(local_id),
local_id: hir::ItemLocalId::from_u32(local_id),
}
})
}
......
......@@ -101,7 +101,7 @@ fn check<F: FnOnce(&mut HirIdValidator<'a, 'hir>)>(&mut self,
if max != self.hir_ids_seen.len() - 1 {
// Collect the missing ItemLocalIds
let missing: Vec<_> = (0 .. max as u32 + 1)
.filter(|&i| !self.hir_ids_seen.contains_key(&ItemLocalId(i)))
.filter(|&i| !self.hir_ids_seen.contains_key(&ItemLocalId::from_u32(i)))
.collect();
// Try to map those to something more useful
......@@ -110,7 +110,7 @@ fn check<F: FnOnce(&mut HirIdValidator<'a, 'hir>)>(&mut self,
for local_id in missing {
let hir_id = HirId {
owner: owner_def_index,
local_id: ItemLocalId(local_id as u32),
local_id: ItemLocalId::from_u32(local_id),
};
trace!("missing hir id {:#?}", hir_id);
......@@ -124,7 +124,7 @@ fn check<F: FnOnce(&mut HirIdValidator<'a, 'hir>)>(&mut self,
.enumerate()
.find(|&(_, &entry)| hir_id == entry)
.expect("no node_to_hir_id entry");
let node_id = NodeId::new(node_id);
let node_id = NodeId::from_usize(node_id);
missing_items.push(format!("[local_id: {}, node:{}]",
local_id,
self.hir_map.node_to_string(node_id)));
......
......@@ -37,7 +37,6 @@
use ty::AdtKind;
use ty::query::Providers;
use rustc_data_structures::indexed_vec;
use rustc_data_structures::sync::{ParallelIterator, par_iter, Send, Sync, scope};
use rustc_data_structures::thin_vec::ThinVec;
......@@ -121,40 +120,28 @@ fn default_decode<D: Decoder>(d: &mut D) -> Result<HirId, D::Error> {
}
}
/// An `ItemLocalId` uniquely identifies something within a given "item-like",
/// that is within a hir::Item, hir::TraitItem, or hir::ImplItem. There is no
/// guarantee that the numerical value of a given `ItemLocalId` corresponds to
/// the node's position within the owning item in any way, but there is a
/// guarantee that the `LocalItemId`s within an owner occupy a dense range of
/// integers starting at zero, so a mapping that maps all or most nodes within
/// an "item-like" to something else can be implement by a `Vec` instead of a
/// tree or hash map.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug,
RustcEncodable, RustcDecodable)]
pub struct ItemLocalId(pub u32);
impl ItemLocalId {
pub fn as_usize(&self) -> usize {
self.0 as usize
// hack to ensure that we don't try to access the private parts of `ItemLocalId` in this module
mod item_local_id_inner {
use rustc_data_structures::indexed_vec::Idx;
/// An `ItemLocalId` uniquely identifies something within a given "item-like",
/// that is within a hir::Item, hir::TraitItem, or hir::ImplItem. There is no
/// guarantee that the numerical value of a given `ItemLocalId` corresponds to
/// the node's position within the owning item in any way, but there is a
/// guarantee that the `LocalItemId`s within an owner occupy a dense range of
/// integers starting at zero, so a mapping that maps all or most nodes within
/// an "item-like" to something else can be implement by a `Vec` instead of a
/// tree or hash map.
newtype_index! {
pub struct ItemLocalId { .. }
}
}
impl indexed_vec::Idx for ItemLocalId {
fn new(idx: usize) -> Self {
debug_assert!((idx as u32) as usize == idx);
ItemLocalId(idx as u32)
}
fn index(self) -> usize {
self.0 as usize
}
}
pub use self::item_local_id_inner::ItemLocalId;
/// The `HirId` corresponding to CRATE_NODE_ID and CRATE_DEF_INDEX
pub const CRATE_HIR_ID: HirId = HirId {
owner: CRATE_DEF_INDEX,
local_id: ItemLocalId(0)
local_id: ItemLocalId::from_u32_const(0)
};
pub const DUMMY_HIR_ID: HirId = HirId {
......@@ -162,7 +149,7 @@ fn index(self) -> usize {
local_id: DUMMY_ITEM_LOCAL_ID,
};
pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId(!0);
pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX;
#[derive(Clone, RustcEncodable, RustcDecodable, Copy)]
pub struct Label {
......
......@@ -79,7 +79,14 @@ fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> DefPathHash {
}
}
impl_stable_hash_for!(tuple_struct hir::ItemLocalId { index });
impl<'a> HashStable<StableHashingContext<'a>> for hir::ItemLocalId {
#[inline]
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
self.as_u32().hash_stable(hcx, hasher);
}
}
impl<'a> ToStableHashKey<StableHashingContext<'a>>
for hir::ItemLocalId {
......@@ -800,7 +807,7 @@ fn hash_stable<W: StableHasherResult>(&self,
.iter()
.map(|id| {
let (def_path_hash, local_id) = id.id.to_stable_hash_key(hcx);
debug_assert_eq!(local_id, hir::ItemLocalId(0));
debug_assert_eq!(local_id, hir::ItemLocalId::from_u32(0));
def_path_hash.0
}).fold(Fingerprint::ZERO, |a, b| {
a.combine_commutative(b)
......
......@@ -393,7 +393,7 @@ pub fn reserve_node_ids(&self, count: usize) -> ast::NodeId {
match id.as_usize().checked_add(count) {
Some(next) => {
self.next_node_id.set(ast::NodeId::new(next));
self.next_node_id.set(ast::NodeId::from_usize(next));
}
None => bug!("Input too large, ran out of node ids!"),
}
......@@ -1160,7 +1160,7 @@ pub fn build_session_(
recursion_limit: Once::new(),
type_length_limit: Once::new(),
const_eval_stack_frame_limit: 100,
next_node_id: OneThread::new(Cell::new(NodeId::new(1))),
next_node_id: OneThread::new(Cell::new(NodeId::from_u32(1))),
allocator_kind: Once::new(),
injected_panic_runtime: Once::new(),
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),
......
......@@ -425,7 +425,7 @@ fn post(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Resu
pprust_hir::AnnNode::Item(item) => {
s.s.space()?;
s.synth_comment(format!("node_id: {} hir local_id: {}",
item.id, item.hir_id.local_id.0))
item.id, item.hir_id.local_id.as_u32()))
}
pprust_hir::AnnNode::SubItem(id) => {
s.s.space()?;
......@@ -434,18 +434,18 @@ fn post(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Resu
pprust_hir::AnnNode::Block(blk) => {
s.s.space()?;
s.synth_comment(format!("block node_id: {} hir local_id: {}",
blk.id, blk.hir_id.local_id.0))
blk.id, blk.hir_id.local_id.as_u32()))
}
pprust_hir::AnnNode::Expr(expr) => {
s.s.space()?;
s.synth_comment(format!("node_id: {} hir local_id: {}",
expr.id, expr.hir_id.local_id.0))?;
expr.id, expr.hir_id.local_id.as_u32()))?;
s.pclose()
}
pprust_hir::AnnNode::Pat(pat) => {
s.s.space()?;
s.synth_comment(format!("pat node_id: {} hir local_id: {}",
pat.id, pat.hir_id.local_id.0))
pat.id, pat.hir_id.local_id.as_u32()))
}
}
}
......@@ -566,7 +566,7 @@ impl FromStr for UserIdentifiedItem {
type Err = ();
fn from_str(s: &str) -> Result<UserIdentifiedItem, ()> {
Ok(s.parse()
.map(ast::NodeId::new)
.map(ast::NodeId::from_u32)
.map(ItemViaNode)
.unwrap_or_else(|_| ItemViaPath(s.split("::").map(|s| s.to_string()).collect())))
}
......
......@@ -232,20 +232,20 @@ pub fn create_simple_region_hierarchy(&mut self) {
// children of 1, etc
let dscope = region::Scope {
id: hir::ItemLocalId(1),
id: hir::ItemLocalId::from_u32(1),
data: region::ScopeData::Destruction,
};
self.region_scope_tree.record_scope_parent(dscope, None);
self.create_region_hierarchy(
&RH {
id: hir::ItemLocalId(1),
id: hir::ItemLocalId::from_u32(1),
sub: &[
RH {
id: hir::ItemLocalId(10),
id: hir::ItemLocalId::from_u32(10),
sub: &[],
},
RH {
id: hir::ItemLocalId(11),
id: hir::ItemLocalId::from_u32(11),
sub: &[],
},
],
......@@ -400,7 +400,7 @@ pub fn t_rptr_late_bound_with_debruijn(
pub fn t_rptr_scope(&self, id: u32) -> Ty<'tcx> {
let r = ty::ReScope(region::Scope {
id: hir::ItemLocalId(id),
id: hir::ItemLocalId::from_u32(id),
data: region::ScopeData::Node,
});
self.infcx
......
......@@ -663,7 +663,7 @@ struct UniformPathsCanaryResults<'a> {
let mut errors = false;
let mut seen_spans = FxHashSet::default();
let mut error_vec = Vec::new();
let mut prev_root_id: NodeId = NodeId::new(0);
let mut prev_root_id: NodeId = NodeId::from_u32(0);
for i in 0 .. self.determined_imports.len() {
let import = self.determined_imports[i];
let error = self.finalize_import(import);
......
......@@ -243,7 +243,7 @@ fn add_test(&mut self, _: String, _: LangString, _: usize) {
if tests.found_tests == 0 {
let mut diag = cx.tcx.struct_span_lint_node(
lint::builtin::MISSING_DOC_CODE_EXAMPLES,
NodeId::new(0),
NodeId::from_u32(0),
span_of_attrs(&item.attrs),
"Missing code example in this documentation");
diag.emit();
......@@ -281,14 +281,14 @@ fn fold_item(&mut self, mut item: Item) -> Option<Item> {
let current_item = match item.inner {
ModuleItem(..) => {
if item.attrs.inner_docs {
if item_node_id.unwrap() != NodeId::new(0) {
if item_node_id.unwrap() != NodeId::from_u32(0) {
item.name.clone()
} else {
None
}
} else {
match parent_node.or(self.mod_ids.last().cloned()) {
Some(parent) if parent != NodeId::new(0) => {
Some(parent) if parent != NodeId::from_u32(0) => {
//FIXME: can we pull the parent module's name from elsewhere?
Some(self.cx.tcx.hir.name(parent).to_string())
}
......@@ -538,13 +538,13 @@ fn resolution_failure(
);
diag = cx.tcx.struct_span_lint_node(lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE,
NodeId::new(0),
NodeId::from_u32(0),
sp,
&msg);
diag.span_label(sp, "cannot be resolved, ignoring");
} else {
diag = cx.tcx.struct_span_lint_node(lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE,
NodeId::new(0),
NodeId::from_u32(0),
sp,
&msg);
......@@ -564,7 +564,7 @@ fn resolution_failure(
diag
} else {
cx.tcx.struct_span_lint_node(lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE,
NodeId::new(0),
NodeId::from_u32(0),
sp,
&msg)
};
......
......@@ -18,7 +18,6 @@
use ext::hygiene::{Mark, SyntaxContext};
use print::pprust;
use ptr::P;
use rustc_data_structures::indexed_vec;
use rustc_data_structures::indexed_vec::Idx;
#[cfg(target_arch = "x86_64")]
use rustc_data_structures::static_assert;
......@@ -33,7 +32,6 @@
use rustc_data_structures::sync::Lrc;
use serialize::{self, Decoder, Encoder};
use std::fmt;
use std::u32;
pub use rustc_target::abi::FloatTy;
......@@ -215,71 +213,54 @@ pub struct ParenthesisedArgs {
pub output: Option<P<Ty>>,
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct NodeId(u32);
impl NodeId {
pub fn new(x: usize) -> NodeId {
assert!(x < (u32::MAX as usize));
NodeId(x as u32)
}
pub fn from_u32(x: u32) -> NodeId {
NodeId(x)
}
pub fn as_usize(&self) -> usize {
self.0 as usize
// hack to ensure that we don't try to access the private parts of `NodeId` in this module
mod node_id_inner {
use rustc_data_structures::indexed_vec::Idx;
newtype_index! {
pub struct NodeId {
ENCODABLE = custom
DEBUG_FORMAT = "NodeId({})"
}
}
}
pub fn as_u32(&self) -> u32 {
self.0
}
pub use self::node_id_inner::NodeId;
impl NodeId {
pub fn placeholder_from_mark(mark: Mark) -> Self {
NodeId(mark.as_u32())
NodeId::from_u32(mark.as_u32())
}
pub fn placeholder_to_mark(self) -> Mark {
Mark::from_u32(self.0)
Mark::from_u32(self.as_u32())
}
}
impl fmt::Display for NodeId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
fmt::Display::fmt(&self.as_u32(), f)
}
}
impl serialize::UseSpecializedEncodable for NodeId {
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_u32(self.0)
s.emit_u32(self.as_u32())
}
}
impl serialize::UseSpecializedDecodable for NodeId {
fn default_decode<D: Decoder>(d: &mut D) -> Result<NodeId, D::Error> {
d.read_u32().map(NodeId)
}
}
impl indexed_vec::Idx for NodeId {
fn new(idx: usize) -> Self {
NodeId::new(idx)
}
fn index(self) -> usize {
self.as_usize()
d.read_u32().map(NodeId::from_u32)
}
}
/// Node id used to represent the root of the crate.
pub const CRATE_NODE_ID: NodeId = NodeId(0);
pub const CRATE_NODE_ID: NodeId = NodeId::from_u32_const(0);
/// When parsing and doing expansions, we initially give all AST nodes this AST
/// node value. Then later, in the renumber pass, we renumber them to have
/// small, positive ids.
pub const DUMMY_NODE_ID: NodeId = NodeId(!0);
pub const DUMMY_NODE_ID: NodeId = NodeId::MAX;
/// A modifier on a bound, currently this is only used for `?Sized`, where the
/// modifier is `Maybe`. Negative bounds should also be handled here.
......
......@@ -26,6 +26,7 @@
#![feature(rustc_diagnostic_macros)]
#![feature(slice_sort_by_cached_key)]
#![feature(str_escape)]
#![feature(step_trait)]
#![feature(try_trait)]
#![feature(unicode_internals)]
......@@ -37,7 +38,7 @@
#[macro_use] extern crate log;
pub extern crate rustc_errors as errors;
extern crate syntax_pos;
extern crate rustc_data_structures;
#[macro_use] extern crate rustc_data_structures;
extern crate rustc_target;
#[macro_use] extern crate scoped_tls;
#[macro_use]
......
......@@ -43,7 +43,7 @@ fn expand_mbe_matches(cx: &mut ExtCtxt, _: Span, args: &[TokenTree])
&[],
Edition::Edition2015,
// not used...
NodeId::new(0));
NodeId::from_u32(0));
let map = match TokenTree::parse(cx, &mbe_matcher, args.iter().cloned().collect()) {
Success(map) => map,
Failure(_, tok) => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册