Skip to content

Commit 26a7544

Browse files
authored
Rollup merge of rust-lang#60647 - petrochenkov:nospace, r=michaelwoerister
cleanup: Remove `DefIndexAddressSpace` The scheme with two address spaces for `DefIndex` was needed in the past, but apparently not needed anymore (after removing `DefId`s from locals and `HirId`-ification).
2 parents a74313b + ee6d315 commit 26a7544

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+246
-403
lines changed

src/librustc/hir/def_id.rs

+11-56
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::ty::{self, TyCtxt};
2-
use crate::hir::map::definitions::FIRST_FREE_HIGH_DEF_INDEX;
2+
use crate::hir::map::definitions::FIRST_FREE_DEF_INDEX;
33
use rustc_data_structures::indexed_vec::Idx;
44
use serialize;
55
use std::fmt;
@@ -99,17 +99,6 @@ impl serialize::UseSpecializedDecodable for CrateNum {}
9999
/// A DefIndex is an index into the hir-map for a crate, identifying a
100100
/// particular definition. It should really be considered an interned
101101
/// shorthand for a particular DefPath.
102-
///
103-
/// At the moment we are allocating the numerical values of DefIndexes from two
104-
/// address spaces: DefIndexAddressSpace::Low and DefIndexAddressSpace::High.
105-
/// This allows us to allocate the DefIndexes of all item-likes
106-
/// (Items, TraitItems, and ImplItems) into one of these spaces and
107-
/// consequently use a simple array for lookup tables keyed by DefIndex and
108-
/// known to be densely populated. This is especially important for the HIR map.
109-
///
110-
/// Since the DefIndex is mostly treated as an opaque ID, you probably
111-
/// don't have to care about these address spaces.
112-
113102
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
114103
pub struct DefIndex(u32);
115104

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

120109
impl fmt::Debug for DefIndex {
121110
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122-
write!(f,
123-
"DefIndex({}:{})",
124-
self.address_space().index(),
125-
self.as_array_index())
111+
write!(f, "DefIndex({})", self.as_array_index())
126112
}
127113
}
128114

129115
impl DefIndex {
130-
#[inline]
131-
pub fn address_space(&self) -> DefIndexAddressSpace {
132-
match self.0 & 1 {
133-
0 => DefIndexAddressSpace::Low,
134-
1 => DefIndexAddressSpace::High,
135-
_ => unreachable!()
136-
}
137-
}
138-
139116
/// Converts this DefIndex into a zero-based array index.
140-
/// This index is the offset within the given DefIndexAddressSpace.
141117
#[inline]
142118
pub fn as_array_index(&self) -> usize {
143-
(self.0 >> 1) as usize
119+
self.0 as usize
144120
}
145121

146122
#[inline]
147-
pub fn from_array_index(i: usize, address_space: DefIndexAddressSpace) -> DefIndex {
148-
DefIndex::from_raw_u32(((i << 1) | (address_space as usize)) as u32)
123+
pub fn from_array_index(i: usize) -> DefIndex {
124+
DefIndex(i as u32)
149125
}
150126

151127
// Proc macros from a proc-macro crate have a kind of virtual DefIndex. This
152128
// function maps the index of the macro within the crate (which is also the
153129
// index of the macro in the CrateMetadata::proc_macros array) to the
154130
// corresponding DefIndex.
155131
pub fn from_proc_macro_index(proc_macro_index: usize) -> DefIndex {
156-
// DefIndex for proc macros start from FIRST_FREE_HIGH_DEF_INDEX,
157-
// because the first FIRST_FREE_HIGH_DEF_INDEX indexes are reserved
132+
// DefIndex for proc macros start from FIRST_FREE_DEF_INDEX,
133+
// because the first FIRST_FREE_DEF_INDEX indexes are reserved
158134
// for internal use.
159135
let def_index = DefIndex::from_array_index(
160-
proc_macro_index.checked_add(FIRST_FREE_HIGH_DEF_INDEX)
161-
.expect("integer overflow adding `proc_macro_index`"),
162-
DefIndexAddressSpace::High);
136+
proc_macro_index.checked_add(FIRST_FREE_DEF_INDEX)
137+
.expect("integer overflow adding `proc_macro_index`"));
163138
assert!(def_index != CRATE_DEF_INDEX);
164139
def_index
165140
}
166141

167142
// This function is the reverse of from_proc_macro_index() above.
168143
pub fn to_proc_macro_index(self: DefIndex) -> usize {
169-
assert_eq!(self.address_space(), DefIndexAddressSpace::High);
170-
171-
self.as_array_index().checked_sub(FIRST_FREE_HIGH_DEF_INDEX)
144+
self.as_array_index().checked_sub(FIRST_FREE_DEF_INDEX)
172145
.unwrap_or_else(|| {
173146
bug!("using local index {:?} as proc-macro index", self)
174147
})
175148
}
176149

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

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

191-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
192-
pub enum DefIndexAddressSpace {
193-
Low = 0,
194-
High = 1,
195-
}
196-
197-
impl DefIndexAddressSpace {
198-
#[inline]
199-
pub fn index(&self) -> usize {
200-
*self as usize
201-
}
202-
}
203-
204162
/// A `DefId` identifies a particular *definition*, by combining a crate
205163
/// index and a def index.
206164
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
@@ -211,10 +169,7 @@ pub struct DefId {
211169

212170
impl fmt::Debug for DefId {
213171
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
214-
write!(f, "DefId({}/{}:{}",
215-
self.krate,
216-
self.index.address_space().index(),
217-
self.index.as_array_index())?;
172+
write!(f, "DefId({}:{}", self.krate, self.index.as_array_index())?;
218173

219174
ty::tls::with_opt(|opt_tcx| {
220175
if let Some(tcx) = opt_tcx {

src/librustc/hir/lowering.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::dep_graph::DepGraph;
3636
use crate::hir::{self, ParamName};
3737
use crate::hir::HirVec;
3838
use crate::hir::map::{DefKey, DefPathData, Definitions};
39-
use crate::hir::def_id::{DefId, DefIndex, DefIndexAddressSpace, CRATE_DEF_INDEX};
39+
use crate::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
4040
use crate::hir::def::{Res, DefKind, PartialRes, PerNS};
4141
use crate::hir::{GenericArg, ConstArg};
4242
use crate::lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
@@ -418,7 +418,6 @@ impl<'a> LoweringContext<'a> {
418418
owner,
419419
id,
420420
DefPathData::Misc,
421-
DefIndexAddressSpace::High,
422421
Mark::root(),
423422
tree.prefix.span,
424423
);
@@ -962,7 +961,6 @@ impl<'a> LoweringContext<'a> {
962961
parent_index,
963962
node_id,
964963
DefPathData::LifetimeNs(str_name),
965-
DefIndexAddressSpace::High,
966964
Mark::root(),
967965
span,
968966
);
@@ -1763,7 +1761,6 @@ impl<'a> LoweringContext<'a> {
17631761
self.parent,
17641762
def_node_id,
17651763
DefPathData::LifetimeNs(name.ident().as_interned_str()),
1766-
DefIndexAddressSpace::High,
17671764
Mark::root(),
17681765
lifetime.span,
17691766
);

src/librustc/hir/map/collector.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,10 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
145145
);
146146
}
147147

148-
let (lo, hi) = definitions.def_index_counts_lo_hi();
149-
150148
let mut collector = NodeCollector {
151149
krate,
152150
source_map: sess.source_map(),
153-
map: [
154-
repeat(None).take(lo).collect(),
155-
repeat(None).take(hi).collect(),
156-
],
151+
map: vec![None; definitions.def_index_count()],
157152
parent_node: hir::CRATE_HIR_ID,
158153
current_signature_dep_index: root_mod_sig_dep_index,
159154
current_full_dep_index: root_mod_full_dep_index,
@@ -231,7 +226,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
231226

232227
fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>) {
233228
debug!("hir_map: {:?} => {:?}", id, entry);
234-
let local_map = &mut self.map[id.owner.address_space().index()][id.owner.as_array_index()];
229+
let local_map = &mut self.map[id.owner.as_array_index()];
235230
let i = id.local_id.as_u32() as usize;
236231
if local_map.is_none() {
237232
*local_map = Some(IndexVec::with_capacity(i + 1));

src/librustc/hir/map/def_collector.rs

+13-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::hir::map::definitions::*;
2-
use crate::hir::def_id::{CRATE_DEF_INDEX, DefIndex, DefIndexAddressSpace};
2+
use crate::hir::def_id::{CRATE_DEF_INDEX, DefIndex};
33
use crate::session::CrateDisambiguator;
44

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

13-
use crate::hir::map::{ITEM_LIKE_SPACE, REGULAR_SPACE};
14-
1513
/// Creates `DefId`s for nodes in the AST.
1614
pub struct DefCollector<'a> {
1715
definitions: &'a mut Definitions,
@@ -47,13 +45,12 @@ impl<'a> DefCollector<'a> {
4745
fn create_def(&mut self,
4846
node_id: NodeId,
4947
data: DefPathData,
50-
address_space: DefIndexAddressSpace,
5148
span: Span)
5249
-> DefIndex {
5350
let parent_def = self.parent_def.unwrap();
5451
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
5552
self.definitions
56-
.create_def_with_parent(parent_def, node_id, data, address_space, self.expansion, span)
53+
.create_def_with_parent(parent_def, node_id, data, self.expansion, span)
5754
}
5855

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

9289
visit::walk_generics(this, generics);
9390

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

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

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

192189
fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) {
193-
self.create_def(id, DefPathData::Misc, ITEM_LIKE_SPACE, use_tree.span);
190+
self.create_def(id, DefPathData::Misc, use_tree.span);
194191
visit::walk_use_tree(self, use_tree, id);
195192
}
196193

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

202199
let def = self.create_def(foreign_item.id,
203200
DefPathData::ValueNs(foreign_item.ident.as_interned_str()),
204-
REGULAR_SPACE,
205201
foreign_item.span);
206202

207203
self.with_parent(def, |this| {
@@ -212,11 +208,10 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
212208
fn visit_variant(&mut self, v: &'a Variant, g: &'a Generics, item_id: NodeId) {
213209
let def = self.create_def(v.node.id,
214210
DefPathData::TypeNs(v.node.ident.as_interned_str()),
215-
REGULAR_SPACE,
216211
v.span);
217212
self.with_parent(def, |this| {
218213
if let Some(ctor_hir_id) = v.node.data.ctor_id() {
219-
this.create_def(ctor_hir_id, DefPathData::Ctor, REGULAR_SPACE, v.span);
214+
this.create_def(ctor_hir_id, DefPathData::Ctor, v.span);
220215
}
221216
visit::walk_variant(this, v, g, item_id)
222217
});
@@ -229,7 +224,6 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
229224
.unwrap_or_else(|| Symbol::intern(&index.to_string()));
230225
let def = self.create_def(field.id,
231226
DefPathData::ValueNs(name.as_interned_str()),
232-
REGULAR_SPACE,
233227
field.span);
234228
self.with_parent(def, |this| this.visit_struct_field(field));
235229
}
@@ -242,7 +236,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
242236
GenericParamKind::Type { .. } => DefPathData::TypeNs(name),
243237
GenericParamKind::Const { .. } => DefPathData::ValueNs(name),
244238
};
245-
self.create_def(param.id, def_path_data, REGULAR_SPACE, param.ident.span);
239+
self.create_def(param.id, def_path_data, param.ident.span);
246240

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

260-
let def = self.create_def(ti.id, def_data, ITEM_LIKE_SPACE, ti.span);
254+
let def = self.create_def(ti.id, def_data, ti.span);
261255
self.with_parent(def, |this| visit::walk_trait_item(this, ti));
262256
}
263257

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

289-
let def = self.create_def(ii.id, def_data, ITEM_LIKE_SPACE, ii.span);
283+
let def = self.create_def(ii.id, def_data, ii.span);
290284
self.with_parent(def, |this| visit::walk_impl_item(this, ii));
291285
}
292286

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

@@ -322,15 +314,13 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
322314
if let IsAsync::Async { closure_id, .. } = asyncness {
323315
let async_def = self.create_def(*closure_id,
324316
DefPathData::ClosureExpr,
325-
REGULAR_SPACE,
326317
expr.span);
327318
self.parent_def = Some(async_def);
328319
}
329320
}
330321
ExprKind::Async(_, async_id, _) => {
331322
let async_def = self.create_def(async_id,
332323
DefPathData::ClosureExpr,
333-
REGULAR_SPACE,
334324
expr.span);
335325
self.parent_def = Some(async_def);
336326
}
@@ -345,7 +335,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
345335
match ty.node {
346336
TyKind::Mac(..) => return self.visit_macro_invoc(ty.id),
347337
TyKind::ImplTrait(node_id, _) => {
348-
self.create_def(node_id, DefPathData::ImplTrait, REGULAR_SPACE, ty.span);
338+
self.create_def(node_id, DefPathData::ImplTrait, ty.span);
349339
}
350340
_ => {}
351341
}

0 commit comments

Comments
 (0)