Skip to content

Commit 03abb1b

Browse files
committed
Auto merge of #42627 - michaelwoerister:no-ident-in-def-path, r=eddyb
incr.comp.: Don't use Ident in DefPath because that's unstable across compilation sessions Fixes #42550. cc @jseyfried @nikomatsakis r? @eddyb
2 parents 9b5b514 + d714b97 commit 03abb1b

File tree

3 files changed

+93
-87
lines changed

3 files changed

+93
-87
lines changed

src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2686,7 +2686,7 @@ impl<'a> LoweringContext<'a> {
26862686
let parent_def = self.parent_def.unwrap();
26872687
let def_id = {
26882688
let defs = self.resolver.definitions();
2689-
let def_path_data = DefPathData::Binding(Ident::with_empty_ctxt(name));
2689+
let def_path_data = DefPathData::Binding(name);
26902690
let def_index = defs
26912691
.create_def_with_parent(parent_def, id, def_path_data, REGULAR_SPACE, Mark::root());
26922692
DefId::local(def_index)

src/librustc/hir/map/def_collector.rs

+20-19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use syntax::ast::*;
1515
use syntax::ext::hygiene::Mark;
1616
use syntax::visit;
1717
use syntax::symbol::keywords;
18+
use syntax::symbol::Symbol;
1819

1920
use hir::map::{ITEM_LIKE_SPACE, REGULAR_SPACE};
2021

@@ -103,14 +104,14 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
103104
DefPathData::Impl,
104105
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) | ItemKind::Trait(..) |
105106
ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) | ItemKind::Ty(..) =>
106-
DefPathData::TypeNs(i.ident.modern()),
107+
DefPathData::TypeNs(i.ident.name),
107108
ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {
108109
return visit::walk_item(self, i);
109110
}
110-
ItemKind::Mod(..) => DefPathData::Module(i.ident.modern()),
111+
ItemKind::Mod(..) => DefPathData::Module(i.ident.name),
111112
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
112-
DefPathData::ValueNs(i.ident.modern()),
113-
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.modern()),
113+
DefPathData::ValueNs(i.ident.name),
114+
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name),
114115
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id, false),
115116
ItemKind::GlobalAsm(..) => DefPathData::Misc,
116117
ItemKind::Use(ref view_path) => {
@@ -138,13 +139,13 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
138139
for v in &enum_definition.variants {
139140
let variant_def_index =
140141
this.create_def(v.node.data.id(),
141-
DefPathData::EnumVariant(v.node.name.modern()),
142+
DefPathData::EnumVariant(v.node.name.name),
142143
REGULAR_SPACE);
143144
this.with_parent(variant_def_index, |this| {
144145
for (index, field) in v.node.data.fields().iter().enumerate() {
145-
let ident = field.ident.map(Ident::modern)
146-
.unwrap_or_else(|| Ident::from_str(&index.to_string()));
147-
this.create_def(field.id, DefPathData::Field(ident), REGULAR_SPACE);
146+
let name = field.ident.map(|ident| ident.name)
147+
.unwrap_or_else(|| Symbol::intern(&index.to_string()));
148+
this.create_def(field.id, DefPathData::Field(name), REGULAR_SPACE);
148149
}
149150

150151
if let Some(ref expr) = v.node.disr_expr {
@@ -162,9 +163,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
162163
}
163164

164165
for (index, field) in struct_def.fields().iter().enumerate() {
165-
let ident = field.ident.map(Ident::modern)
166-
.unwrap_or_else(|| Ident::from_str(&index.to_string()));
167-
this.create_def(field.id, DefPathData::Field(ident), REGULAR_SPACE);
166+
let name = field.ident.map(|ident| ident.name)
167+
.unwrap_or_else(|| Symbol::intern(&index.to_string()));
168+
this.create_def(field.id, DefPathData::Field(name), REGULAR_SPACE);
168169
}
169170
}
170171
_ => {}
@@ -175,7 +176,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
175176

176177
fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {
177178
let def = self.create_def(foreign_item.id,
178-
DefPathData::ValueNs(foreign_item.ident.modern()),
179+
DefPathData::ValueNs(foreign_item.ident.name),
179180
REGULAR_SPACE);
180181

181182
self.with_parent(def, |this| {
@@ -186,7 +187,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
186187
fn visit_generics(&mut self, generics: &'a Generics) {
187188
for ty_param in generics.ty_params.iter() {
188189
self.create_def(ty_param.id,
189-
DefPathData::TypeParam(ty_param.ident.modern()),
190+
DefPathData::TypeParam(ty_param.ident.name),
190191
REGULAR_SPACE);
191192
}
192193

@@ -196,8 +197,8 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
196197
fn visit_trait_item(&mut self, ti: &'a TraitItem) {
197198
let def_data = match ti.node {
198199
TraitItemKind::Method(..) | TraitItemKind::Const(..) =>
199-
DefPathData::ValueNs(ti.ident.modern()),
200-
TraitItemKind::Type(..) => DefPathData::TypeNs(ti.ident.modern()),
200+
DefPathData::ValueNs(ti.ident.name),
201+
TraitItemKind::Type(..) => DefPathData::TypeNs(ti.ident.name),
201202
TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id, false),
202203
};
203204

@@ -214,8 +215,8 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
214215
fn visit_impl_item(&mut self, ii: &'a ImplItem) {
215216
let def_data = match ii.node {
216217
ImplItemKind::Method(..) | ImplItemKind::Const(..) =>
217-
DefPathData::ValueNs(ii.ident.modern()),
218-
ImplItemKind::Type(..) => DefPathData::TypeNs(ii.ident.modern()),
218+
DefPathData::ValueNs(ii.ident.name),
219+
ImplItemKind::Type(..) => DefPathData::TypeNs(ii.ident.name),
219220
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id, false),
220221
};
221222

@@ -236,7 +237,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
236237
PatKind::Mac(..) => return self.visit_macro_invoc(pat.id, false),
237238
PatKind::Ident(_, id, _) => {
238239
let def = self.create_def(pat.id,
239-
DefPathData::Binding(id.node.modern()),
240+
DefPathData::Binding(id.node.name),
240241
REGULAR_SPACE);
241242
self.parent_def = Some(def);
242243
}
@@ -281,7 +282,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
281282

282283
fn visit_lifetime_def(&mut self, def: &'a LifetimeDef) {
283284
self.create_def(def.lifetime.id,
284-
DefPathData::LifetimeDef(def.lifetime.ident.modern()),
285+
DefPathData::LifetimeDef(def.lifetime.ident.name),
285286
REGULAR_SPACE);
286287
}
287288

src/librustc/hir/map/definitions.rs

+72-67
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use rustc_data_structures::stable_hasher::StableHasher;
2424
use serialize::{Encodable, Decodable, Encoder, Decoder};
2525
use std::fmt::Write;
2626
use std::hash::Hash;
27-
use syntax::ast::{self, Ident};
28-
use syntax::ext::hygiene::{Mark, SyntaxContext};
27+
use syntax::ast;
28+
use syntax::ext::hygiene::Mark;
2929
use syntax::symbol::{Symbol, InternedString};
3030
use ty::TyCtxt;
3131
use util::nodemap::NodeMap;
@@ -248,7 +248,39 @@ impl DefKey {
248248
// and the special "root_parent" below.
249249
0u8.hash(&mut hasher);
250250
parent_hash.hash(&mut hasher);
251-
self.disambiguated_data.hash(&mut hasher);
251+
252+
let DisambiguatedDefPathData {
253+
ref data,
254+
disambiguator,
255+
} = self.disambiguated_data;
256+
257+
::std::mem::discriminant(data).hash(&mut hasher);
258+
match *data {
259+
DefPathData::TypeNs(name) |
260+
DefPathData::ValueNs(name) |
261+
DefPathData::Module(name) |
262+
DefPathData::MacroDef(name) |
263+
DefPathData::TypeParam(name) |
264+
DefPathData::LifetimeDef(name) |
265+
DefPathData::EnumVariant(name) |
266+
DefPathData::Binding(name) |
267+
DefPathData::Field(name) |
268+
DefPathData::GlobalMetaData(name) => {
269+
(*name.as_str()).hash(&mut hasher);
270+
}
271+
272+
DefPathData::Impl |
273+
DefPathData::CrateRoot |
274+
DefPathData::Misc |
275+
DefPathData::ClosureExpr |
276+
DefPathData::StructCtor |
277+
DefPathData::Initializer |
278+
DefPathData::ImplTrait |
279+
DefPathData::Typeof => {}
280+
};
281+
282+
disambiguator.hash(&mut hasher);
283+
252284
DefPathHash(hasher.finish())
253285
}
254286

@@ -354,7 +386,7 @@ impl DefPath {
354386
}
355387
}
356388

357-
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
389+
#[derive(Clone, Debug, Eq, PartialEq, Hash, RustcEncodable, RustcDecodable)]
358390
pub enum DefPathData {
359391
// Root: these should only be used for the root nodes, because
360392
// they are treated specially by the `def_path` function.
@@ -368,31 +400,31 @@ pub enum DefPathData {
368400
/// An impl
369401
Impl,
370402
/// Something in the type NS
371-
TypeNs(Ident),
403+
TypeNs(Symbol),
372404
/// Something in the value NS
373-
ValueNs(Ident),
405+
ValueNs(Symbol),
374406
/// A module declaration
375-
Module(Ident),
407+
Module(Symbol),
376408
/// A macro rule
377-
MacroDef(Ident),
409+
MacroDef(Symbol),
378410
/// A closure expression
379411
ClosureExpr,
380412

381413
// Subportions of items
382414
/// A type parameter (generic parameter)
383-
TypeParam(Ident),
415+
TypeParam(Symbol),
384416
/// A lifetime definition
385-
LifetimeDef(Ident),
417+
LifetimeDef(Symbol),
386418
/// A variant of a enum
387-
EnumVariant(Ident),
419+
EnumVariant(Symbol),
388420
/// A struct field
389-
Field(Ident),
421+
Field(Symbol),
390422
/// Implicit ctor for a tuple-like struct
391423
StructCtor,
392424
/// Initializer for a const
393425
Initializer,
394426
/// Pattern binding
395-
Binding(Ident),
427+
Binding(Symbol),
396428
/// An `impl Trait` type node.
397429
ImplTrait,
398430
/// A `typeof` type node.
@@ -401,7 +433,7 @@ pub enum DefPathData {
401433
/// GlobalMetaData identifies a piece of crate metadata that is global to
402434
/// a whole crate (as opposed to just one item). GlobalMetaData components
403435
/// are only supposed to show up right below the crate root.
404-
GlobalMetaData(Ident)
436+
GlobalMetaData(Symbol)
405437
}
406438

407439
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
@@ -604,19 +636,19 @@ impl Definitions {
604636
}
605637

606638
impl DefPathData {
607-
pub fn get_opt_ident(&self) -> Option<Ident> {
639+
pub fn get_opt_name(&self) -> Option<Symbol> {
608640
use self::DefPathData::*;
609641
match *self {
610-
TypeNs(ident) |
611-
ValueNs(ident) |
612-
Module(ident) |
613-
MacroDef(ident) |
614-
TypeParam(ident) |
615-
LifetimeDef(ident) |
616-
EnumVariant(ident) |
617-
Binding(ident) |
618-
Field(ident) |
619-
GlobalMetaData(ident) => Some(ident),
642+
TypeNs(name) |
643+
ValueNs(name) |
644+
Module(name) |
645+
MacroDef(name) |
646+
TypeParam(name) |
647+
LifetimeDef(name) |
648+
EnumVariant(name) |
649+
Binding(name) |
650+
Field(name) |
651+
GlobalMetaData(name) => Some(name),
620652

621653
Impl |
622654
CrateRoot |
@@ -629,24 +661,20 @@ impl DefPathData {
629661
}
630662
}
631663

632-
pub fn get_opt_name(&self) -> Option<ast::Name> {
633-
self.get_opt_ident().map(|ident| ident.name)
634-
}
635-
636664
pub fn as_interned_str(&self) -> InternedString {
637665
use self::DefPathData::*;
638666
let s = match *self {
639-
TypeNs(ident) |
640-
ValueNs(ident) |
641-
Module(ident) |
642-
MacroDef(ident) |
643-
TypeParam(ident) |
644-
LifetimeDef(ident) |
645-
EnumVariant(ident) |
646-
Binding(ident) |
647-
Field(ident) |
648-
GlobalMetaData(ident) => {
649-
return ident.name.as_str();
667+
TypeNs(name) |
668+
ValueNs(name) |
669+
Module(name) |
670+
MacroDef(name) |
671+
TypeParam(name) |
672+
LifetimeDef(name) |
673+
EnumVariant(name) |
674+
Binding(name) |
675+
Field(name) |
676+
GlobalMetaData(name) => {
677+
return name.as_str();
650678
}
651679

652680
// note that this does not show up in user printouts
@@ -669,29 +697,6 @@ impl DefPathData {
669697
}
670698
}
671699

672-
impl Eq for DefPathData {}
673-
impl PartialEq for DefPathData {
674-
fn eq(&self, other: &DefPathData) -> bool {
675-
::std::mem::discriminant(self) == ::std::mem::discriminant(other) &&
676-
self.get_opt_ident() == other.get_opt_ident()
677-
}
678-
}
679-
680-
impl ::std::hash::Hash for DefPathData {
681-
fn hash<H: ::std::hash::Hasher>(&self, hasher: &mut H) {
682-
::std::mem::discriminant(self).hash(hasher);
683-
if let Some(ident) = self.get_opt_ident() {
684-
if ident.ctxt == SyntaxContext::empty() && ident.name == ident.name.interned() {
685-
ident.name.as_str().hash(hasher)
686-
} else {
687-
// FIXME(jseyfried) implement stable hashing for idents with macros 2.0 hygiene info
688-
ident.hash(hasher)
689-
}
690-
}
691-
}
692-
}
693-
694-
695700
// We define the GlobalMetaDataKind enum with this macro because we want to
696701
// make sure that we exhaustively iterate over all variants when registering
697702
// the corresponding DefIndices in the DefTable.
@@ -712,7 +717,7 @@ macro_rules! define_global_metadata_kind {
712717
definitions.create_def_with_parent(
713718
CRATE_DEF_INDEX,
714719
ast::DUMMY_NODE_ID,
715-
DefPathData::GlobalMetaData(instance.ident()),
720+
DefPathData::GlobalMetaData(instance.name()),
716721
DefIndexAddressSpace::High,
717722
Mark::root()
718723
);
@@ -726,15 +731,15 @@ macro_rules! define_global_metadata_kind {
726731
let def_key = DefKey {
727732
parent: Some(CRATE_DEF_INDEX),
728733
disambiguated_data: DisambiguatedDefPathData {
729-
data: DefPathData::GlobalMetaData(self.ident()),
734+
data: DefPathData::GlobalMetaData(self.name()),
730735
disambiguator: 0,
731736
}
732737
};
733738

734739
def_path_table.key_to_index[&def_key]
735740
}
736741

737-
fn ident(&self) -> Ident {
742+
fn name(&self) -> Symbol {
738743

739744
let string = match *self {
740745
$(
@@ -744,7 +749,7 @@ macro_rules! define_global_metadata_kind {
744749
)*
745750
};
746751

747-
Ident::from_str(string)
752+
Symbol::intern(string)
748753
}
749754
}
750755
)

0 commit comments

Comments
 (0)