Skip to content

Commit d854c36

Browse files
committed
Fix def id collection for const_integers in the AST.
1 parent f34e49d commit d854c36

File tree

3 files changed

+46
-28
lines changed

3 files changed

+46
-28
lines changed

src/librustc/hir/map/def_collector.rs

+29-20
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ pub struct DefCollector<'a> {
2727
hir_crate: Option<&'a hir::Crate>,
2828
definitions: &'a mut Definitions,
2929
parent_def: Option<DefIndex>,
30-
pub visit_macro_invoc: Option<&'a mut FnMut(NodeId, DefIndex)>,
30+
pub visit_macro_invoc: Option<&'a mut FnMut(MacroInvocationData)>,
31+
}
32+
33+
pub struct MacroInvocationData {
34+
pub id: NodeId,
35+
pub def_index: DefIndex,
36+
pub const_integer: bool,
3137
}
3238

3339
impl<'a> DefCollector<'a> {
@@ -93,16 +99,15 @@ impl<'a> DefCollector<'a> {
9399
self.parent_def = parent;
94100
}
95101

96-
fn visit_ast_const_integer(&mut self, expr: &Expr) {
97-
// Find the node which will be used after lowering.
98-
if let ExprKind::Paren(ref inner) = expr.node {
99-
return self.visit_ast_const_integer(inner);
100-
}
101-
102-
// FIXME(eddyb) Closures should have separate
103-
// function definition IDs and expression IDs.
104-
if let ExprKind::Closure(..) = expr.node {
105-
return;
102+
pub fn visit_ast_const_integer(&mut self, expr: &Expr) {
103+
match expr.node {
104+
// Find the node which will be used after lowering.
105+
ExprKind::Paren(ref inner) => return self.visit_ast_const_integer(inner),
106+
ExprKind::Mac(..) => return self.visit_macro_invoc(expr.id, true),
107+
// FIXME(eddyb) Closures should have separate
108+
// function definition IDs and expression IDs.
109+
ExprKind::Closure(..) => return,
110+
_ => {}
106111
}
107112

108113
self.create_def(expr.id, DefPathData::Initializer);
@@ -118,9 +123,13 @@ impl<'a> DefCollector<'a> {
118123
self.create_def(expr.id, DefPathData::Initializer);
119124
}
120125

121-
fn visit_macro_invoc(&mut self, id: NodeId) {
126+
fn visit_macro_invoc(&mut self, id: NodeId, const_integer: bool) {
122127
if let Some(ref mut visit) = self.visit_macro_invoc {
123-
visit(id, self.parent_def.unwrap());
128+
visit(MacroInvocationData {
129+
id: id,
130+
const_integer: const_integer,
131+
def_index: self.parent_def.unwrap(),
132+
})
124133
}
125134
}
126135
}
@@ -144,7 +153,7 @@ impl<'a> visit::Visitor for DefCollector<'a> {
144153
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
145154
DefPathData::ValueNs(i.ident.name.as_str()),
146155
ItemKind::Mac(..) if i.id == DUMMY_NODE_ID => return, // Scope placeholder
147-
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id),
156+
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id, false),
148157
ItemKind::Use(..) => DefPathData::Misc,
149158
};
150159
let def = self.create_def(i.id, def_data);
@@ -210,7 +219,7 @@ impl<'a> visit::Visitor for DefCollector<'a> {
210219
TraitItemKind::Method(..) | TraitItemKind::Const(..) =>
211220
DefPathData::ValueNs(ti.ident.name.as_str()),
212221
TraitItemKind::Type(..) => DefPathData::TypeNs(ti.ident.name.as_str()),
213-
TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id),
222+
TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id, false),
214223
};
215224

216225
let def = self.create_def(ti.id, def_data);
@@ -228,7 +237,7 @@ impl<'a> visit::Visitor for DefCollector<'a> {
228237
ImplItemKind::Method(..) | ImplItemKind::Const(..) =>
229238
DefPathData::ValueNs(ii.ident.name.as_str()),
230239
ImplItemKind::Type(..) => DefPathData::TypeNs(ii.ident.name.as_str()),
231-
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id),
240+
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id, false),
232241
};
233242

234243
let def = self.create_def(ii.id, def_data);
@@ -245,7 +254,7 @@ impl<'a> visit::Visitor for DefCollector<'a> {
245254
let parent_def = self.parent_def;
246255

247256
match pat.node {
248-
PatKind::Mac(..) => return self.visit_macro_invoc(pat.id),
257+
PatKind::Mac(..) => return self.visit_macro_invoc(pat.id, false),
249258
PatKind::Ident(_, id, _) => {
250259
let def = self.create_def(pat.id, DefPathData::Binding(id.node.name.as_str()));
251260
self.parent_def = Some(def);
@@ -261,7 +270,7 @@ impl<'a> visit::Visitor for DefCollector<'a> {
261270
let parent_def = self.parent_def;
262271

263272
match expr.node {
264-
ExprKind::Mac(..) => return self.visit_macro_invoc(expr.id),
273+
ExprKind::Mac(..) => return self.visit_macro_invoc(expr.id, false),
265274
ExprKind::Repeat(_, ref count) => self.visit_ast_const_integer(count),
266275
ExprKind::Closure(..) => {
267276
let def = self.create_def(expr.id, DefPathData::ClosureExpr);
@@ -276,7 +285,7 @@ impl<'a> visit::Visitor for DefCollector<'a> {
276285

277286
fn visit_ty(&mut self, ty: &Ty) {
278287
match ty.node {
279-
TyKind::Mac(..) => return self.visit_macro_invoc(ty.id),
288+
TyKind::Mac(..) => return self.visit_macro_invoc(ty.id, false),
280289
TyKind::FixedLengthVec(_, ref length) => self.visit_ast_const_integer(length),
281290
TyKind::ImplTrait(..) => {
282291
self.create_def(ty.id, DefPathData::ImplTrait);
@@ -296,7 +305,7 @@ impl<'a> visit::Visitor for DefCollector<'a> {
296305

297306
fn visit_stmt(&mut self, stmt: &Stmt) {
298307
match stmt.node {
299-
StmtKind::Mac(..) => self.visit_macro_invoc(stmt.id),
308+
StmtKind::Mac(..) => self.visit_macro_invoc(stmt.id, false),
300309
_ => visit::walk_stmt(self, stmt),
301310
}
302311
}

src/librustc/hir/map/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
pub use self::Node::*;
1212
use self::MapEntry::*;
1313
use self::collector::NodeCollector;
14-
pub use self::def_collector::DefCollector;
14+
pub use self::def_collector::{DefCollector, MacroInvocationData};
1515
pub use self::definitions::{Definitions, DefKey, DefPath, DefPathData,
1616
DisambiguatedDefPathData, InlinedRootPath};
1717

src/librustc_resolve/macros.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use {Module, Resolver};
1212
use build_reduced_graph::BuildReducedGraphVisitor;
1313
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefIndex};
14-
use rustc::hir::map::DefCollector;
14+
use rustc::hir::map::{self, DefCollector};
1515
use std::rc::Rc;
1616
use syntax::ast;
1717
use syntax::errors::DiagnosticBuilder;
@@ -27,13 +27,17 @@ use syntax::util::lev_distance::find_best_match_for_name;
2727
pub struct ExpansionData<'a> {
2828
pub module: Module<'a>,
2929
def_index: DefIndex,
30+
// True if this expansion is in a `const_integer` position, for example `[u32; m!()]`.
31+
// c.f. `DefCollector::visit_ast_const_integer`.
32+
const_integer: bool,
3033
}
3134

3235
impl<'a> ExpansionData<'a> {
3336
pub fn root(graph_root: Module<'a>) -> Self {
3437
ExpansionData {
3538
module: graph_root,
3639
def_index: CRATE_DEF_INDEX,
40+
const_integer: false,
3741
}
3842
}
3943
}
@@ -49,6 +53,7 @@ impl<'a> base::Resolver for Resolver<'a> {
4953
self.expansion_data.insert(mark.as_u32(), ExpansionData {
5054
module: module,
5155
def_index: module.def_id().unwrap().index,
56+
const_integer: false,
5257
});
5358
mark
5459
}
@@ -156,17 +161,21 @@ impl<'a> Resolver<'a> {
156161

157162
fn collect_def_ids(&mut self, mark: Mark, expansion: &Expansion) {
158163
let expansion_data = &mut self.expansion_data;
159-
let module = self.current_module;
160-
let def_index = expansion_data[&mark.as_u32()].def_index;
161-
let visit_macro_invoc = &mut |id: ast::NodeId, def_index| {
162-
expansion_data.insert(id.as_u32(), ExpansionData {
163-
def_index: def_index,
164+
let ExpansionData { def_index, const_integer, module } = expansion_data[&mark.as_u32()];
165+
let visit_macro_invoc = &mut |invoc: map::MacroInvocationData| {
166+
expansion_data.entry(invoc.id.as_u32()).or_insert(ExpansionData {
167+
def_index: invoc.def_index,
168+
const_integer: invoc.const_integer,
164169
module: module,
165170
});
166171
};
167172

168173
let mut def_collector = DefCollector::new(&mut self.definitions);
169174
def_collector.visit_macro_invoc = Some(visit_macro_invoc);
170-
def_collector.with_parent(def_index, |def_collector| expansion.visit_with(def_collector));
175+
def_collector.with_parent(def_index, |def_collector| if !const_integer {
176+
expansion.visit_with(def_collector)
177+
} else if let Expansion::Expr(ref expr) = *expansion {
178+
def_collector.visit_ast_const_integer(expr);
179+
});
171180
}
172181
}

0 commit comments

Comments
 (0)