Skip to content

Commit b04ebef

Browse files
committed
Auto merge of #40272 - jseyfried:fix_const_macro_invocations, r=petrochenkov
macros: fix const expression invocations Fixes #40136. r? @nrc
2 parents 11bc48a + 9fe7d3f commit b04ebef

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

src/librustc/hir/map/def_collector.rs

+11-11
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,11 +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_ast_const_integer(expr),
262+
TyKind::Typeof(ref expr) => self.visit_const_expr(expr),
263263
_ => {}
264264
}
265265
visit::walk_ty(self, ty);

src/librustc_resolve/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ impl<'a> Resolver<'a> {
511511
let invocation = self.arenas.alloc_invocation_data(InvocationData {
512512
module: Cell::new(self.get_extern_crate_root(def_id.krate)),
513513
def_index: CRATE_DEF_INDEX,
514-
const_integer: false,
514+
const_expr: false,
515515
legacy_scope: Cell::new(LegacyScope::Empty),
516516
expansion: Cell::new(LegacyScope::Empty),
517517
});

src/librustc_resolve/macros.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ use syntax_pos::{Span, DUMMY_SP};
4040
pub struct InvocationData<'a> {
4141
pub module: Cell<Module<'a>>,
4242
pub def_index: DefIndex,
43-
// True if this expansion is in a `const_integer` position, for example `[u32; m!()]`.
44-
// c.f. `DefCollector::visit_ast_const_integer`.
45-
pub const_integer: bool,
43+
// True if this expansion is in a `const_expr` position, for example `[u32; m!()]`.
44+
// c.f. `DefCollector::visit_const_expr`.
45+
pub const_expr: bool,
4646
// The scope in which the invocation path is resolved.
4747
pub legacy_scope: Cell<LegacyScope<'a>>,
4848
// The smallest scope that includes this invocation's expansion,
@@ -55,7 +55,7 @@ impl<'a> InvocationData<'a> {
5555
InvocationData {
5656
module: Cell::new(graph_root),
5757
def_index: CRATE_DEF_INDEX,
58-
const_integer: false,
58+
const_expr: false,
5959
legacy_scope: Cell::new(LegacyScope::Empty),
6060
expansion: Cell::new(LegacyScope::Empty),
6161
}
@@ -93,7 +93,7 @@ impl<'a> base::Resolver for Resolver<'a> {
9393
self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
9494
module: Cell::new(module),
9595
def_index: module.def_id().unwrap().index,
96-
const_integer: false,
96+
const_expr: false,
9797
legacy_scope: Cell::new(LegacyScope::Empty),
9898
expansion: Cell::new(LegacyScope::Empty),
9999
}));
@@ -517,13 +517,13 @@ impl<'a> Resolver<'a> {
517517

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

522522
let visit_macro_invoc = &mut |invoc: map::MacroInvocationData| {
523523
invocations.entry(invoc.mark).or_insert_with(|| {
524524
arenas.alloc_invocation_data(InvocationData {
525525
def_index: invoc.def_index,
526-
const_integer: invoc.const_integer,
526+
const_expr: invoc.const_expr,
527527
module: Cell::new(graph_root),
528528
expansion: Cell::new(LegacyScope::Empty),
529529
legacy_scope: Cell::new(LegacyScope::Empty),
@@ -534,9 +534,9 @@ impl<'a> Resolver<'a> {
534534
let mut def_collector = DefCollector::new(&mut self.definitions);
535535
def_collector.visit_macro_invoc = Some(visit_macro_invoc);
536536
def_collector.with_parent(def_index, |def_collector| {
537-
if const_integer {
537+
if const_expr {
538538
if let Expansion::Expr(ref expr) = *expansion {
539-
def_collector.visit_ast_const_integer(expr);
539+
def_collector.visit_const_expr(expr);
540540
}
541541
}
542542
expansion.visit_with(def_collector)

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

+24
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)