Skip to content

Commit 0a6d444

Browse files
committed
Improve intercrate hygiene.
1 parent 8c5b96f commit 0a6d444

File tree

11 files changed

+112
-74
lines changed

11 files changed

+112
-74
lines changed

src/librustc/hir/def.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub type ExportMap = NodeMap<Vec<Export>>;
117117

118118
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
119119
pub struct Export {
120-
pub name: ast::Name, // The name of the target.
120+
pub ident: ast::Ident, // The name of the target.
121121
pub def: Def, // The definition of the target.
122122
pub span: Span, // The span of the target definition.
123123
}

src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2662,7 +2662,7 @@ impl<'a> LoweringContext<'a> {
26622662
let parent_def = self.parent_def.unwrap();
26632663
let def_id = {
26642664
let defs = self.resolver.definitions();
2665-
let def_path_data = DefPathData::Binding(name.as_str());
2665+
let def_path_data = DefPathData::Binding(Ident::with_empty_ctxt(name));
26662666
let def_index = defs
26672667
.create_def_with_parent(parent_def, id, def_path_data, REGULAR_SPACE, Mark::root());
26682668
DefId::local(def_index)

src/librustc/hir/map/def_collector.rs

+20-22
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use hir::def_id::{CRATE_DEF_INDEX, DefIndex, DefIndexAddressSpace};
1414
use syntax::ast::*;
1515
use syntax::ext::hygiene::Mark;
1616
use syntax::visit;
17-
use syntax::symbol::{Symbol, keywords};
17+
use syntax::symbol::keywords;
1818

1919
use hir::map::{ITEM_LIKE_SPACE, REGULAR_SPACE};
2020

@@ -103,14 +103,14 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
103103
DefPathData::Impl,
104104
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) | ItemKind::Trait(..) |
105105
ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) | ItemKind::Ty(..) =>
106-
DefPathData::TypeNs(i.ident.name.as_str()),
106+
DefPathData::TypeNs(i.ident.modern()),
107107
ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {
108108
return visit::walk_item(self, i);
109109
}
110-
ItemKind::Mod(..) => DefPathData::Module(i.ident.name.as_str()),
110+
ItemKind::Mod(..) => DefPathData::Module(i.ident.modern()),
111111
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
112-
DefPathData::ValueNs(i.ident.name.as_str()),
113-
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name.as_str()),
112+
DefPathData::ValueNs(i.ident.modern()),
113+
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.modern()),
114114
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id, false),
115115
ItemKind::GlobalAsm(..) => DefPathData::Misc,
116116
ItemKind::Use(ref view_path) => {
@@ -138,15 +138,13 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
138138
for v in &enum_definition.variants {
139139
let variant_def_index =
140140
this.create_def(v.node.data.id(),
141-
DefPathData::EnumVariant(v.node.name.name.as_str()),
141+
DefPathData::EnumVariant(v.node.name.modern()),
142142
REGULAR_SPACE);
143143
this.with_parent(variant_def_index, |this| {
144144
for (index, field) in v.node.data.fields().iter().enumerate() {
145-
let name = field.ident.map(|ident| ident.name)
146-
.unwrap_or_else(|| Symbol::intern(&index.to_string()));
147-
this.create_def(field.id,
148-
DefPathData::Field(name.as_str()),
149-
REGULAR_SPACE);
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);
150148
}
151149

152150
if let Some(ref expr) = v.node.disr_expr {
@@ -164,9 +162,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
164162
}
165163

166164
for (index, field) in struct_def.fields().iter().enumerate() {
167-
let name = field.ident.map(|ident| ident.name.as_str())
168-
.unwrap_or(Symbol::intern(&index.to_string()).as_str());
169-
this.create_def(field.id, DefPathData::Field(name), REGULAR_SPACE);
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);
170168
}
171169
}
172170
_ => {}
@@ -177,7 +175,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
177175

178176
fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {
179177
let def = self.create_def(foreign_item.id,
180-
DefPathData::ValueNs(foreign_item.ident.name.as_str()),
178+
DefPathData::ValueNs(foreign_item.ident.modern()),
181179
REGULAR_SPACE);
182180

183181
self.with_parent(def, |this| {
@@ -188,7 +186,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
188186
fn visit_generics(&mut self, generics: &'a Generics) {
189187
for ty_param in generics.ty_params.iter() {
190188
self.create_def(ty_param.id,
191-
DefPathData::TypeParam(ty_param.ident.name.as_str()),
189+
DefPathData::TypeParam(ty_param.ident.modern()),
192190
REGULAR_SPACE);
193191
}
194192

@@ -198,8 +196,8 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
198196
fn visit_trait_item(&mut self, ti: &'a TraitItem) {
199197
let def_data = match ti.node {
200198
TraitItemKind::Method(..) | TraitItemKind::Const(..) =>
201-
DefPathData::ValueNs(ti.ident.name.as_str()),
202-
TraitItemKind::Type(..) => DefPathData::TypeNs(ti.ident.name.as_str()),
199+
DefPathData::ValueNs(ti.ident.modern()),
200+
TraitItemKind::Type(..) => DefPathData::TypeNs(ti.ident.modern()),
203201
TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id, false),
204202
};
205203

@@ -216,8 +214,8 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
216214
fn visit_impl_item(&mut self, ii: &'a ImplItem) {
217215
let def_data = match ii.node {
218216
ImplItemKind::Method(..) | ImplItemKind::Const(..) =>
219-
DefPathData::ValueNs(ii.ident.name.as_str()),
220-
ImplItemKind::Type(..) => DefPathData::TypeNs(ii.ident.name.as_str()),
217+
DefPathData::ValueNs(ii.ident.modern()),
218+
ImplItemKind::Type(..) => DefPathData::TypeNs(ii.ident.modern()),
221219
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id, false),
222220
};
223221

@@ -238,7 +236,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
238236
PatKind::Mac(..) => return self.visit_macro_invoc(pat.id, false),
239237
PatKind::Ident(_, id, _) => {
240238
let def = self.create_def(pat.id,
241-
DefPathData::Binding(id.node.name.as_str()),
239+
DefPathData::Binding(id.node.modern()),
242240
REGULAR_SPACE);
243241
self.parent_def = Some(def);
244242
}
@@ -283,7 +281,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
283281

284282
fn visit_lifetime_def(&mut self, def: &'a LifetimeDef) {
285283
self.create_def(def.lifetime.id,
286-
DefPathData::LifetimeDef(def.lifetime.ident.name.as_str()),
284+
DefPathData::LifetimeDef(def.lifetime.ident.modern()),
287285
REGULAR_SPACE);
288286
}
289287

src/librustc/hir/map/definitions.rs

+58-32
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use rustc_data_structures::stable_hasher::StableHasher;
2323
use serialize::{Encodable, Decodable, Encoder, Decoder};
2424
use std::fmt::Write;
2525
use std::hash::Hash;
26-
use syntax::ast;
27-
use syntax::ext::hygiene::Mark;
26+
use syntax::ast::{self, Ident};
27+
use syntax::ext::hygiene::{Mark, SyntaxContext};
2828
use syntax::symbol::{Symbol, InternedString};
2929
use ty::TyCtxt;
3030
use util::nodemap::NodeMap;
@@ -327,7 +327,7 @@ impl DefPath {
327327
}
328328
}
329329

330-
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
330+
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
331331
pub enum DefPathData {
332332
// Root: these should only be used for the root nodes, because
333333
// they are treated specially by the `def_path` function.
@@ -341,31 +341,31 @@ pub enum DefPathData {
341341
/// An impl
342342
Impl,
343343
/// Something in the type NS
344-
TypeNs(InternedString),
344+
TypeNs(Ident),
345345
/// Something in the value NS
346-
ValueNs(InternedString),
346+
ValueNs(Ident),
347347
/// A module declaration
348-
Module(InternedString),
348+
Module(Ident),
349349
/// A macro rule
350-
MacroDef(InternedString),
350+
MacroDef(Ident),
351351
/// A closure expression
352352
ClosureExpr,
353353

354354
// Subportions of items
355355
/// A type parameter (generic parameter)
356-
TypeParam(InternedString),
356+
TypeParam(Ident),
357357
/// A lifetime definition
358-
LifetimeDef(InternedString),
358+
LifetimeDef(Ident),
359359
/// A variant of a enum
360-
EnumVariant(InternedString),
360+
EnumVariant(Ident),
361361
/// A struct field
362-
Field(InternedString),
362+
Field(Ident),
363363
/// Implicit ctor for a tuple-like struct
364364
StructCtor,
365365
/// Initializer for a const
366366
Initializer,
367367
/// Pattern binding
368-
Binding(InternedString),
368+
Binding(Ident),
369369
/// An `impl Trait` type node.
370370
ImplTrait,
371371
/// A `typeof` type node.
@@ -551,18 +551,18 @@ impl Definitions {
551551
}
552552

553553
impl DefPathData {
554-
pub fn get_opt_name(&self) -> Option<ast::Name> {
554+
pub fn get_opt_ident(&self) -> Option<Ident> {
555555
use self::DefPathData::*;
556556
match *self {
557-
TypeNs(ref name) |
558-
ValueNs(ref name) |
559-
Module(ref name) |
560-
MacroDef(ref name) |
561-
TypeParam(ref name) |
562-
LifetimeDef(ref name) |
563-
EnumVariant(ref name) |
564-
Binding(ref name) |
565-
Field(ref name) => Some(Symbol::intern(name)),
557+
TypeNs(ident) |
558+
ValueNs(ident) |
559+
Module(ident) |
560+
MacroDef(ident) |
561+
TypeParam(ident) |
562+
LifetimeDef(ident) |
563+
EnumVariant(ident) |
564+
Binding(ident) |
565+
Field(ident) => Some(ident),
566566

567567
Impl |
568568
CrateRoot |
@@ -575,19 +575,23 @@ impl DefPathData {
575575
}
576576
}
577577

578+
pub fn get_opt_name(&self) -> Option<ast::Name> {
579+
self.get_opt_ident().map(|ident| ident.name)
580+
}
581+
578582
pub fn as_interned_str(&self) -> InternedString {
579583
use self::DefPathData::*;
580584
let s = match *self {
581-
TypeNs(ref name) |
582-
ValueNs(ref name) |
583-
Module(ref name) |
584-
MacroDef(ref name) |
585-
TypeParam(ref name) |
586-
LifetimeDef(ref name) |
587-
EnumVariant(ref name) |
588-
Binding(ref name) |
589-
Field(ref name) => {
590-
return name.clone();
585+
TypeNs(ident) |
586+
ValueNs(ident) |
587+
Module(ident) |
588+
MacroDef(ident) |
589+
TypeParam(ident) |
590+
LifetimeDef(ident) |
591+
EnumVariant(ident) |
592+
Binding(ident) |
593+
Field(ident) => {
594+
return ident.name.as_str();
591595
}
592596

593597
// note that this does not show up in user printouts
@@ -609,3 +613,25 @@ impl DefPathData {
609613
self.as_interned_str().to_string()
610614
}
611615
}
616+
617+
impl Eq for DefPathData {}
618+
impl PartialEq for DefPathData {
619+
fn eq(&self, other: &DefPathData) -> bool {
620+
::std::mem::discriminant(self) == ::std::mem::discriminant(other) &&
621+
self.get_opt_ident() == other.get_opt_ident()
622+
}
623+
}
624+
625+
impl ::std::hash::Hash for DefPathData {
626+
fn hash<H: ::std::hash::Hasher>(&self, hasher: &mut H) {
627+
::std::mem::discriminant(self).hash(hasher);
628+
if let Some(ident) = self.get_opt_ident() {
629+
if ident.ctxt == SyntaxContext::empty() && ident.name == ident.name.interned() {
630+
ident.name.as_str().hash(hasher)
631+
} else {
632+
// FIXME(jseyfried) implement stable hashing for idents with macros 2.0 hygiene info
633+
ident.hash(hasher)
634+
}
635+
}
636+
}
637+
}

src/librustc/ich/impls_hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for hir::def_id::DefIn
11181118
}
11191119

11201120
impl_stable_hash_for!(struct hir::def::Export {
1121-
name,
1121+
ident,
11221122
def,
11231123
span
11241124
});

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#![feature(conservative_impl_trait)]
2929
#![feature(const_fn)]
3030
#![feature(core_intrinsics)]
31+
#![feature(discriminant_value)]
3132
#![feature(i128_type)]
3233
#![feature(libc)]
3334
#![feature(loop_break_value)]

src/librustc_metadata/decoder.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use std::u32;
3939

4040
use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
4141
use syntax::attr;
42-
use syntax::ast;
42+
use syntax::ast::{self, Ident};
4343
use syntax::codemap;
4444
use syntax::ext::base::MacroKind;
4545
use syntax_pos::{self, Span, BytePos, Pos, DUMMY_SP, NO_EXPANSION};
@@ -667,7 +667,8 @@ impl<'a, 'tcx> CrateMetadata {
667667
},
668668
ext.kind()
669669
);
670-
callback(def::Export { name: name, def: def, span: DUMMY_SP });
670+
let ident = Ident::with_empty_ctxt(name);
671+
callback(def::Export { ident: ident, def: def, span: DUMMY_SP });
671672
}
672673
}
673674
return
@@ -703,7 +704,7 @@ impl<'a, 'tcx> CrateMetadata {
703704
if let Some(def) = self.get_def(child_index) {
704705
callback(def::Export {
705706
def: def,
706-
name: self.item_name(child_index),
707+
ident: Ident::with_empty_ctxt(self.item_name(child_index)),
707708
span: self.entry(child_index).span.decode(self),
708709
});
709710
}
@@ -720,23 +721,24 @@ impl<'a, 'tcx> CrateMetadata {
720721
let span = child.span.decode(self);
721722
if let (Some(def), Some(name)) =
722723
(self.get_def(child_index), def_key.disambiguated_data.data.get_opt_name()) {
723-
callback(def::Export { def: def, name: name, span: span });
724+
let ident = Ident::with_empty_ctxt(name);
725+
callback(def::Export { def: def, ident: ident, span: span });
724726
// For non-reexport structs and variants add their constructors to children.
725727
// Reexport lists automatically contain constructors when necessary.
726728
match def {
727729
Def::Struct(..) => {
728730
if let Some(ctor_def_id) = self.get_struct_ctor_def_id(child_index) {
729731
let ctor_kind = self.get_ctor_kind(child_index);
730732
let ctor_def = Def::StructCtor(ctor_def_id, ctor_kind);
731-
callback(def::Export { def: ctor_def, name: name, span: span });
733+
callback(def::Export { def: ctor_def, ident: ident, span: span });
732734
}
733735
}
734736
Def::Variant(def_id) => {
735737
// Braced variants, unlike structs, generate unusable names in
736738
// value namespace, they are reserved for possible future use.
737739
let ctor_kind = self.get_ctor_kind(child_index);
738740
let ctor_def = Def::VariantCtor(def_id, ctor_kind);
739-
callback(def::Export { def: ctor_def, name: name, span: span });
741+
callback(def::Export { def: ctor_def, ident: ident, span: span });
740742
}
741743
_ => {}
742744
}

src/librustc_resolve/build_reduced_graph.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl<'a> Resolver<'a> {
439439

440440
/// Builds the reduced graph for a single item in an external crate.
441441
fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'a>, child: Export) {
442-
let ident = Ident::with_empty_ctxt(child.name);
442+
let ident = child.ident;
443443
let def = child.def;
444444
let def_id = def.def_id();
445445
let vis = self.session.cstore.visibility(def_id);
@@ -480,9 +480,8 @@ impl<'a> Resolver<'a> {
480480

481481
for child in self.session.cstore.item_children(def_id) {
482482
let ns = if let Def::AssociatedTy(..) = child.def { TypeNS } else { ValueNS };
483-
let ident = Ident::with_empty_ctxt(child.name);
484-
self.define(module, ident, ns, (child.def, ty::Visibility::Public,
485-
DUMMY_SP, expansion));
483+
self.define(module, child.ident, ns,
484+
(child.def, ty::Visibility::Public, DUMMY_SP, expansion));
486485

487486
if self.session.cstore.associated_item_cloned(child.def.def_id())
488487
.method_has_self_argument {
@@ -643,7 +642,7 @@ impl<'a> Resolver<'a> {
643642
let ident = Ident::with_empty_ctxt(name);
644643
let result = self.resolve_ident_in_module(module, ident, MacroNS, false, false, span);
645644
if let Ok(binding) = result {
646-
self.macro_exports.push(Export { name: name, def: binding.def(), span: span });
645+
self.macro_exports.push(Export { ident: ident, def: binding.def(), span: span });
647646
} else {
648647
span_err!(self.session, span, E0470, "reexported macro not found");
649648
}

src/librustc_resolve/macros.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,8 @@ impl<'a> Resolver<'a> {
726726
}));
727727
if attr::contains_name(&item.attrs, "macro_export") {
728728
let def = Def::Macro(def_id, MacroKind::Bang);
729-
self.macro_exports.push(Export { name: ident.name, def: def, span: item.span });
729+
self.macro_exports
730+
.push(Export { ident: ident.modern(), def: def, span: item.span });
730731
} else {
731732
self.unused_macros.insert(def_id);
732733
}

0 commit comments

Comments
 (0)