Skip to content

Commit bfa2ef6

Browse files
committed
Hygienize librustc_typeck.
1 parent 1f175fa commit bfa2ef6

File tree

14 files changed

+170
-61
lines changed

14 files changed

+170
-61
lines changed

src/librustc/hir/lowering.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use hir::def_id::{DefIndex, DefId, CRATE_DEF_INDEX};
4747
use hir::def::{Def, PathResolution};
4848
use rustc_data_structures::indexed_vec::IndexVec;
4949
use session::Session;
50-
use util::nodemap::{DefIdMap, NodeMap};
50+
use util::nodemap::{DefIdMap, FxHashMap, NodeMap};
5151

5252
use std::collections::BTreeMap;
5353
use std::fmt::Debug;
@@ -77,6 +77,7 @@ pub struct LoweringContext<'a> {
7777
// a definition, then we can properly create the def id.
7878
parent_def: Option<DefIndex>,
7979
resolver: &'a mut Resolver,
80+
name_map: FxHashMap<Ident, Name>,
8081

8182
/// The items being lowered are collected here.
8283
items: BTreeMap<NodeId, hir::Item>,
@@ -126,6 +127,7 @@ pub fn lower_crate(sess: &Session,
126127
sess: sess,
127128
parent_def: None,
128129
resolver: resolver,
130+
name_map: FxHashMap(),
129131
items: BTreeMap::new(),
130132
trait_items: BTreeMap::new(),
131133
impl_items: BTreeMap::new(),
@@ -495,6 +497,14 @@ impl<'a> LoweringContext<'a> {
495497
}
496498
}
497499

500+
fn lower_ident(&mut self, ident: Ident) -> Name {
501+
let ident = ident.modern();
502+
if ident.ctxt == SyntaxContext::empty() {
503+
return ident.name;
504+
}
505+
*self.name_map.entry(ident).or_insert_with(|| Symbol::from_ident(ident))
506+
}
507+
498508
fn lower_opt_sp_ident(&mut self, o_id: Option<Spanned<Ident>>) -> Option<Spanned<Name>> {
499509
o_id.map(|sp_ident| respan(sp_ident.span, sp_ident.node.name))
500510
}
@@ -546,7 +556,7 @@ impl<'a> LoweringContext<'a> {
546556
fn lower_ty_binding(&mut self, b: &TypeBinding) -> hir::TypeBinding {
547557
hir::TypeBinding {
548558
id: self.lower_node_id(b.id),
549-
name: b.ident.name,
559+
name: self.lower_ident(b.ident),
550560
ty: self.lower_ty(&b.ty),
551561
span: b.span,
552562
}
@@ -844,7 +854,7 @@ impl<'a> LoweringContext<'a> {
844854
}
845855

846856
hir::PathSegment {
847-
name: segment.identifier.name,
857+
name: self.lower_ident(segment.identifier),
848858
parameters: parameters,
849859
}
850860
}
@@ -941,7 +951,7 @@ impl<'a> LoweringContext<'a> {
941951
}
942952

943953
fn lower_ty_param(&mut self, tp: &TyParam, add_bounds: &[TyParamBound]) -> hir::TyParam {
944-
let mut name = tp.ident.name;
954+
let mut name = self.lower_ident(tp.ident);
945955

946956
// Don't expose `Self` (recovered "keyword used as ident" parse error).
947957
// `rustc::ty` expects `Self` to be only used for a trait's `Self`.
@@ -1137,7 +1147,11 @@ impl<'a> LoweringContext<'a> {
11371147
hir::StructField {
11381148
span: f.span,
11391149
id: self.lower_node_id(f.id),
1140-
name: f.ident.map(|ident| ident.name).unwrap_or(Symbol::intern(&index.to_string())),
1150+
name: self.lower_ident(match f.ident {
1151+
Some(ident) => ident,
1152+
// FIXME(jseyfried) positional field hygiene
1153+
None => Ident { name: Symbol::intern(&index.to_string()), ctxt: f.span.ctxt },
1154+
}),
11411155
vis: self.lower_visibility(&f.vis, None),
11421156
ty: self.lower_ty(&f.ty),
11431157
attrs: self.lower_attrs(&f.attrs),
@@ -1146,7 +1160,7 @@ impl<'a> LoweringContext<'a> {
11461160

11471161
fn lower_field(&mut self, f: &Field) -> hir::Field {
11481162
hir::Field {
1149-
name: respan(f.ident.span, f.ident.node.name),
1163+
name: respan(f.ident.span, self.lower_ident(f.ident.node)),
11501164
expr: P(self.lower_expr(&f.expr)),
11511165
span: f.span,
11521166
is_shorthand: f.is_shorthand,
@@ -1371,7 +1385,7 @@ impl<'a> LoweringContext<'a> {
13711385
self.with_parent_def(i.id, |this| {
13721386
hir::TraitItem {
13731387
id: this.lower_node_id(i.id),
1374-
name: i.ident.name,
1388+
name: this.lower_ident(i.ident),
13751389
attrs: this.lower_attrs(&i.attrs),
13761390
node: match i.node {
13771391
TraitItemKind::Const(ref ty, ref default) => {
@@ -1421,7 +1435,7 @@ impl<'a> LoweringContext<'a> {
14211435
};
14221436
hir::TraitItemRef {
14231437
id: hir::TraitItemId { node_id: i.id },
1424-
name: i.ident.name,
1438+
name: self.lower_ident(i.ident),
14251439
span: i.span,
14261440
defaultness: self.lower_defaultness(Defaultness::Default, has_default),
14271441
kind: kind,
@@ -1432,7 +1446,7 @@ impl<'a> LoweringContext<'a> {
14321446
self.with_parent_def(i.id, |this| {
14331447
hir::ImplItem {
14341448
id: this.lower_node_id(i.id),
1435-
name: i.ident.name,
1449+
name: this.lower_ident(i.ident),
14361450
attrs: this.lower_attrs(&i.attrs),
14371451
vis: this.lower_visibility(&i.vis, None),
14381452
defaultness: this.lower_defaultness(i.defaultness, true /* [1] */),
@@ -1461,7 +1475,7 @@ impl<'a> LoweringContext<'a> {
14611475
fn lower_impl_item_ref(&mut self, i: &ImplItem) -> hir::ImplItemRef {
14621476
hir::ImplItemRef {
14631477
id: hir::ImplItemId { node_id: i.id },
1464-
name: i.ident.name,
1478+
name: self.lower_ident(i.ident),
14651479
span: i.span,
14661480
vis: self.lower_visibility(&i.vis, Some(i.id)),
14671481
defaultness: self.lower_defaultness(i.defaultness, true /* [1] */),
@@ -1655,7 +1669,7 @@ impl<'a> LoweringContext<'a> {
16551669
Spanned {
16561670
span: f.span,
16571671
node: hir::FieldPat {
1658-
name: f.node.ident.name,
1672+
name: self.lower_ident(f.node.ident),
16591673
pat: self.lower_pat(&f.node.pat),
16601674
is_shorthand: f.node.is_shorthand,
16611675
},
@@ -1825,7 +1839,7 @@ impl<'a> LoweringContext<'a> {
18251839
ExprKind::MethodCall(i, ref tps, ref args) => {
18261840
let tps = tps.iter().map(|x| self.lower_ty(x)).collect();
18271841
let args = args.iter().map(|x| self.lower_expr(x)).collect();
1828-
hir::ExprMethodCall(respan(i.span, i.node.name), tps, args)
1842+
hir::ExprMethodCall(respan(i.span, self.lower_ident(i.node)), tps, args)
18291843
}
18301844
ExprKind::Binary(binop, ref lhs, ref rhs) => {
18311845
let binop = self.lower_binop(binop);
@@ -1924,7 +1938,8 @@ impl<'a> LoweringContext<'a> {
19241938
P(self.lower_expr(er)))
19251939
}
19261940
ExprKind::Field(ref el, ident) => {
1927-
hir::ExprField(P(self.lower_expr(el)), respan(ident.span, ident.node.name))
1941+
hir::ExprField(P(self.lower_expr(el)),
1942+
respan(ident.span, self.lower_ident(ident.node)))
19281943
}
19291944
ExprKind::TupField(ref el, ident) => {
19301945
hir::ExprTupField(P(self.lower_expr(el)), ident)
@@ -2643,10 +2658,8 @@ impl<'a> LoweringContext<'a> {
26432658
let def_id = {
26442659
let defs = self.resolver.definitions();
26452660
let def_path_data = DefPathData::Binding(name.as_str());
2646-
let def_index = defs.create_def_with_parent(parent_def,
2647-
id,
2648-
def_path_data,
2649-
REGULAR_SPACE);
2661+
let def_index = defs
2662+
.create_def_with_parent(parent_def, id, def_path_data, REGULAR_SPACE, Mark::root());
26502663
DefId::local(def_index)
26512664
};
26522665

src/librustc/hir/map/def_collector.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use hir::map::{ITEM_LIKE_SPACE, REGULAR_SPACE};
2222
pub struct DefCollector<'a> {
2323
definitions: &'a mut Definitions,
2424
parent_def: Option<DefIndex>,
25+
expansion: Mark,
2526
pub visit_macro_invoc: Option<&'a mut FnMut(MacroInvocationData)>,
2627
}
2728

@@ -32,9 +33,10 @@ pub struct MacroInvocationData {
3233
}
3334

3435
impl<'a> DefCollector<'a> {
35-
pub fn new(definitions: &'a mut Definitions) -> Self {
36+
pub fn new(definitions: &'a mut Definitions, expansion: Mark) -> Self {
3637
DefCollector {
3738
definitions: definitions,
39+
expansion: expansion,
3840
parent_def: None,
3941
visit_macro_invoc: None,
4042
}
@@ -54,7 +56,8 @@ impl<'a> DefCollector<'a> {
5456
-> DefIndex {
5557
let parent_def = self.parent_def.unwrap();
5658
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
57-
self.definitions.create_def_with_parent(parent_def, node_id, data, address_space)
59+
self.definitions
60+
.create_def_with_parent(parent_def, node_id, data, address_space, self.expansion)
5861
}
5962

6063
pub fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: DefIndex, f: F) {

src/librustc/hir/map/definitions.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use serialize::{Encodable, Decodable, Encoder, Decoder};
2424
use std::fmt::Write;
2525
use std::hash::Hash;
2626
use syntax::ast;
27+
use syntax::ext::hygiene::Mark;
2728
use syntax::symbol::{Symbol, InternedString};
2829
use ty::TyCtxt;
2930
use util::nodemap::NodeMap;
@@ -180,6 +181,8 @@ pub struct Definitions {
180181
node_to_def_index: NodeMap<DefIndex>,
181182
def_index_to_node: [Vec<ast::NodeId>; 2],
182183
pub(super) node_to_hir_id: IndexVec<ast::NodeId, hir::HirId>,
184+
macro_def_scopes: FxHashMap<Mark, DefId>,
185+
expansions: FxHashMap<DefIndex, Mark>,
183186
}
184187

185188
// Unfortunately we have to provide a manual impl of Clone because of the
@@ -194,6 +197,8 @@ impl Clone for Definitions {
194197
self.def_index_to_node[1].clone(),
195198
],
196199
node_to_hir_id: self.node_to_hir_id.clone(),
200+
macro_def_scopes: self.macro_def_scopes.clone(),
201+
expansions: self.expansions.clone(),
197202
}
198203
}
199204
}
@@ -379,6 +384,8 @@ impl Definitions {
379384
node_to_def_index: NodeMap(),
380385
def_index_to_node: [vec![], vec![]],
381386
node_to_hir_id: IndexVec::new(),
387+
macro_def_scopes: FxHashMap(),
388+
expansions: FxHashMap(),
382389
}
383390
}
384391

@@ -472,7 +479,8 @@ impl Definitions {
472479
parent: DefIndex,
473480
node_id: ast::NodeId,
474481
data: DefPathData,
475-
address_space: DefIndexAddressSpace)
482+
address_space: DefIndexAddressSpace,
483+
expansion: Mark)
476484
-> DefIndex {
477485
debug!("create_def_with_parent(parent={:?}, node_id={:?}, data={:?})",
478486
parent, node_id, data);
@@ -510,6 +518,7 @@ impl Definitions {
510518
assert_eq!(index.as_array_index(),
511519
self.def_index_to_node[address_space.index()].len());
512520
self.def_index_to_node[address_space.index()].push(node_id);
521+
self.expansions.insert(index, expansion);
513522

514523
debug!("create_def_with_parent: def_index_to_node[{:?} <-> {:?}", index, node_id);
515524
self.node_to_def_index.insert(node_id, index);
@@ -525,6 +534,18 @@ impl Definitions {
525534
"Trying initialize NodeId -> HirId mapping twice");
526535
self.node_to_hir_id = mapping;
527536
}
537+
538+
pub fn expansion(&self, index: DefIndex) -> Mark {
539+
self.expansions[&index]
540+
}
541+
542+
pub fn macro_def_scope(&self, mark: Mark) -> DefId {
543+
self.macro_def_scopes[&mark]
544+
}
545+
546+
pub fn add_macro_def_scope(&mut self, mark: Mark, scope: DefId) {
547+
self.macro_def_scopes.insert(mark, scope);
548+
}
528549
}
529550

530551
impl DefPathData {

src/librustc/hir/map/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,14 +637,15 @@ impl<'hir> Map<'hir> {
637637

638638
/// Returns the NodeId of `id`'s nearest module parent, or `id` itself if no
639639
/// module parent is in this map.
640-
pub fn get_module_parent(&self, id: NodeId) -> NodeId {
641-
match self.walk_parent_nodes(id, |node| match *node {
640+
pub fn get_module_parent(&self, id: NodeId) -> DefId {
641+
let id = match self.walk_parent_nodes(id, |node| match *node {
642642
NodeItem(&Item { node: Item_::ItemMod(_), .. }) => true,
643643
_ => false,
644644
}) {
645645
Ok(id) => id,
646646
Err(id) => id,
647-
}
647+
};
648+
self.local_def_id(id)
648649
}
649650

650651
/// Returns the nearest enclosing scope. A scope is an item or block.

src/librustc/ty/mod.rs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ use std::rc::Rc;
4545
use std::slice;
4646
use std::vec::IntoIter;
4747
use std::mem;
48-
use syntax::ast::{self, DUMMY_NODE_ID, Name, NodeId};
48+
use syntax::ast::{self, DUMMY_NODE_ID, Name, Ident, NodeId};
4949
use syntax::attr;
50+
use syntax::ext::hygiene::{Mark, SyntaxContext};
5051
use syntax::symbol::{Symbol, InternedString};
5152
use syntax_pos::{DUMMY_SP, Span};
5253
use rustc_const_math::ConstInt;
@@ -268,7 +269,7 @@ impl Visibility {
268269
def => Visibility::Restricted(def.def_id()),
269270
},
270271
hir::Inherited => {
271-
Visibility::Restricted(tcx.hir.local_def_id(tcx.hir.get_module_parent(id)))
272+
Visibility::Restricted(tcx.hir.get_module_parent(id))
272273
}
273274
}
274275
}
@@ -1823,17 +1824,22 @@ impl<'a, 'gcx, 'tcx> AdtDef {
18231824

18241825
impl<'a, 'gcx, 'tcx> VariantDef {
18251826
#[inline]
1826-
pub fn find_field_named(&self,
1827-
name: ast::Name)
1828-
-> Option<&FieldDef> {
1829-
self.fields.iter().find(|f| f.name == name)
1827+
pub fn find_field_named(&self, name: ast::Name) -> Option<&FieldDef> {
1828+
self.index_of_field_named(name).map(|index| &self.fields[index])
18301829
}
18311830

1832-
#[inline]
1833-
pub fn index_of_field_named(&self,
1834-
name: ast::Name)
1835-
-> Option<usize> {
1836-
self.fields.iter().position(|f| f.name == name)
1831+
pub fn index_of_field_named(&self, name: ast::Name) -> Option<usize> {
1832+
if let Some(index) = self.fields.iter().position(|f| f.name == name) {
1833+
return Some(index);
1834+
}
1835+
let mut ident = name.to_ident();
1836+
while ident.ctxt != SyntaxContext::empty() {
1837+
ident.ctxt.remove_mark();
1838+
if let Some(field) = self.fields.iter().position(|f| f.name.to_ident() == ident) {
1839+
return Some(field);
1840+
}
1841+
}
1842+
None
18371843
}
18381844

18391845
#[inline]
@@ -2257,10 +2263,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
22572263
}
22582264
}
22592265

2260-
pub fn vis_is_accessible_from(self, vis: Visibility, block: NodeId) -> bool {
2261-
vis.is_accessible_from(self.hir.local_def_id(self.hir.get_module_parent(block)), self)
2262-
}
2263-
22642266
pub fn item_name(self, id: DefId) -> ast::Name {
22652267
if let Some(id) = self.hir.as_local_node_id(id) {
22662268
self.hir.name(id)
@@ -2372,6 +2374,22 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
23722374
Err(self.sess.cstore.crate_name(impl_did.krate))
23732375
}
23742376
}
2377+
2378+
pub fn adjust(self, name: Name, scope: DefId, block: NodeId) -> (Ident, DefId) {
2379+
self.adjust_ident(name.to_ident(), scope, block)
2380+
}
2381+
2382+
pub fn adjust_ident(self, mut ident: Ident, scope: DefId, block: NodeId) -> (Ident, DefId) {
2383+
let expansion = match scope.krate {
2384+
LOCAL_CRATE => self.hir.definitions().expansion(scope.index),
2385+
_ => Mark::root(),
2386+
};
2387+
let scope = match ident.ctxt.adjust(expansion) {
2388+
Some(macro_def) => self.hir.definitions().macro_def_scope(macro_def),
2389+
None => self.hir.get_module_parent(block),
2390+
};
2391+
(ident, scope)
2392+
}
23752393
}
23762394

23772395
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

src/librustc_const_eval/check_match.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
154154
}
155155
}
156156

157-
let module = self.tcx.hir.local_def_id(self.tcx.hir.get_module_parent(scrut.id));
157+
let module = self.tcx.hir.get_module_parent(scrut.id);
158158
MatchCheckCtxt::create_and_enter(self.tcx, module, |ref mut cx| {
159159
let mut have_errors = false;
160160

@@ -182,7 +182,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
182182
// Then, if the match has no arms, check whether the scrutinee
183183
// is uninhabited.
184184
let pat_ty = self.tables.node_id_to_type(scrut.id);
185-
let module = self.tcx.hir.local_def_id(self.tcx.hir.get_module_parent(scrut.id));
185+
let module = self.tcx.hir.get_module_parent(scrut.id);
186186
if inlined_arms.is_empty() {
187187
let scrutinee_is_uninhabited = if self.tcx.sess.features.borrow().never_type {
188188
pat_ty.is_uninhabited_from(module, self.tcx)
@@ -231,7 +231,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
231231
"local binding"
232232
};
233233

234-
let module = self.tcx.hir.local_def_id(self.tcx.hir.get_module_parent(pat.id));
234+
let module = self.tcx.hir.get_module_parent(pat.id);
235235
MatchCheckCtxt::create_and_enter(self.tcx, module, |ref mut cx| {
236236
let mut patcx = PatternContext::new(self.tcx, self.tables);
237237
let pattern = patcx.lower_pattern(pat);

0 commit comments

Comments
 (0)