Skip to content

Commit 636299a

Browse files
jseyfriedbrson
authored andcommitted
Fix const expression macro invocations.
1 parent 3f45364 commit 636299a

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

src/librustc/hir/map/def_collector.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct DefCollector<'a> {
2626
pub struct MacroInvocationData {
2727
pub mark: Mark,
2828
pub def_index: DefIndex,
29-
pub const_integer: bool,
29+
pub const_expr: bool,
3030
}
3131

3232
impl<'a> DefCollector<'a> {
@@ -65,10 +65,10 @@ impl<'a> DefCollector<'a> {
6565
self.parent_def = parent;
6666
}
6767

68-
pub fn visit_ast_const_integer(&mut self, expr: &Expr) {
68+
pub fn visit_const_expr(&mut self, expr: &Expr) {
6969
match expr.node {
7070
// Find the node which will be used after lowering.
71-
ExprKind::Paren(ref inner) => return self.visit_ast_const_integer(inner),
71+
ExprKind::Paren(ref inner) => return self.visit_const_expr(inner),
7272
ExprKind::Mac(..) => return self.visit_macro_invoc(expr.id, true),
7373
// FIXME(eddyb) Closures should have separate
7474
// function definition IDs and expression IDs.
@@ -79,11 +79,11 @@ impl<'a> DefCollector<'a> {
7979
self.create_def(expr.id, DefPathData::Initializer);
8080
}
8181

82-
fn visit_macro_invoc(&mut self, id: NodeId, const_integer: bool) {
82+
fn visit_macro_invoc(&mut self, id: NodeId, const_expr: bool) {
8383
if let Some(ref mut visit) = self.visit_macro_invoc {
8484
visit(MacroInvocationData {
8585
mark: Mark::from_placeholder_id(id),
86-
const_integer: const_integer,
86+
const_expr: const_expr,
8787
def_index: self.parent_def.unwrap(),
8888
})
8989
}
@@ -142,7 +142,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
142142
}
143143

144144
if let Some(ref expr) = v.node.disr_expr {
145-
this.visit_ast_const_integer(expr);
145+
this.visit_const_expr(expr);
146146
}
147147
});
148148
}
@@ -194,7 +194,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
194194
let def = self.create_def(ti.id, def_data);
195195
self.with_parent(def, |this| {
196196
if let TraitItemKind::Const(_, Some(ref expr)) = ti.node {
197-
this.create_def(expr.id, DefPathData::Initializer);
197+
this.visit_const_expr(expr);
198198
}
199199

200200
visit::walk_trait_item(this, ti);
@@ -212,7 +212,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
212212
let def = self.create_def(ii.id, def_data);
213213
self.with_parent(def, |this| {
214214
if let ImplItemKind::Const(_, ref expr) = ii.node {
215-
this.create_def(expr.id, DefPathData::Initializer);
215+
this.visit_const_expr(expr);
216216
}
217217

218218
visit::walk_impl_item(this, ii);
@@ -240,7 +240,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
240240

241241
match expr.node {
242242
ExprKind::Mac(..) => return self.visit_macro_invoc(expr.id, false),
243-
ExprKind::Repeat(_, ref count) => self.visit_ast_const_integer(count),
243+
ExprKind::Repeat(_, ref count) => self.visit_const_expr(count),
244244
ExprKind::Closure(..) => {
245245
let def = self.create_def(expr.id, DefPathData::ClosureExpr);
246246
self.parent_def = Some(def);
@@ -255,10 +255,11 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
255255
fn visit_ty(&mut self, ty: &'a Ty) {
256256
match ty.node {
257257
TyKind::Mac(..) => return self.visit_macro_invoc(ty.id, false),
258-
TyKind::Array(_, ref length) => self.visit_ast_const_integer(length),
258+
TyKind::Array(_, ref length) => self.visit_const_expr(length),
259259
TyKind::ImplTrait(..) => {
260260
self.create_def(ty.id, DefPathData::ImplTrait);
261261
}
262+
TyKind::Typeof(ref expr) => self.visit_const_expr(expr),
262263
_ => {}
263264
}
264265
visit::walk_ty(self, ty);

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ impl<'a> Resolver<'a> {
498498
let invocation = self.arenas.alloc_invocation_data(InvocationData {
499499
module: Cell::new(self.get_extern_crate_root(def_id.krate)),
500500
def_index: CRATE_DEF_INDEX,
501-
const_integer: false,
501+
const_expr: false,
502502
legacy_scope: Cell::new(LegacyScope::Empty),
503503
expansion: Cell::new(LegacyScope::Empty),
504504
});

src/librustc_resolve/macros.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ use syntax_pos::{Span, DUMMY_SP};
3939
pub struct InvocationData<'a> {
4040
pub module: Cell<Module<'a>>,
4141
pub def_index: DefIndex,
42-
// True if this expansion is in a `const_integer` position, for example `[u32; m!()]`.
43-
// c.f. `DefCollector::visit_ast_const_integer`.
44-
pub const_integer: bool,
42+
// True if this expansion is in a `const_expr` position, for example `[u32; m!()]`.
43+
// c.f. `DefCollector::visit_const_expr`.
44+
pub const_expr: bool,
4545
// The scope in which the invocation path is resolved.
4646
pub legacy_scope: Cell<LegacyScope<'a>>,
4747
// The smallest scope that includes this invocation's expansion,
@@ -54,7 +54,7 @@ impl<'a> InvocationData<'a> {
5454
InvocationData {
5555
module: Cell::new(graph_root),
5656
def_index: CRATE_DEF_INDEX,
57-
const_integer: false,
57+
const_expr: false,
5858
legacy_scope: Cell::new(LegacyScope::Empty),
5959
expansion: Cell::new(LegacyScope::Empty),
6060
}
@@ -92,7 +92,7 @@ impl<'a> base::Resolver for Resolver<'a> {
9292
self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
9393
module: Cell::new(module),
9494
def_index: module.def_id().unwrap().index,
95-
const_integer: false,
95+
const_expr: false,
9696
legacy_scope: Cell::new(LegacyScope::Empty),
9797
expansion: Cell::new(LegacyScope::Empty),
9898
}));
@@ -413,13 +413,13 @@ impl<'a> Resolver<'a> {
413413

414414
fn collect_def_ids(&mut self, invocation: &'a InvocationData<'a>, expansion: &Expansion) {
415415
let Resolver { ref mut invocations, arenas, graph_root, .. } = *self;
416-
let InvocationData { def_index, const_integer, .. } = *invocation;
416+
let InvocationData { def_index, const_expr, .. } = *invocation;
417417

418418
let visit_macro_invoc = &mut |invoc: map::MacroInvocationData| {
419419
invocations.entry(invoc.mark).or_insert_with(|| {
420420
arenas.alloc_invocation_data(InvocationData {
421421
def_index: invoc.def_index,
422-
const_integer: invoc.const_integer,
422+
const_expr: invoc.const_expr,
423423
module: Cell::new(graph_root),
424424
expansion: Cell::new(LegacyScope::Empty),
425425
legacy_scope: Cell::new(LegacyScope::Empty),
@@ -430,9 +430,9 @@ impl<'a> Resolver<'a> {
430430
let mut def_collector = DefCollector::new(&mut self.definitions);
431431
def_collector.visit_macro_invoc = Some(visit_macro_invoc);
432432
def_collector.with_parent(def_index, |def_collector| {
433-
if const_integer {
433+
if const_expr {
434434
if let Expansion::Expr(ref expr) = *expansion {
435-
def_collector.visit_ast_const_integer(expr);
435+
def_collector.visit_const_expr(expr);
436436
}
437437
}
438438
expansion.visit_with(def_collector)

src/test/run-pass/issue-40136.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(associated_consts)]
12+
13+
macro_rules! m { () => { 0 } }
14+
15+
trait T {
16+
const C: i32 = m!();
17+
}
18+
19+
struct S;
20+
impl S {
21+
const C: i32 = m!();
22+
}
23+
24+
fn main() {}

0 commit comments

Comments
 (0)