Skip to content

Commit 5da0576

Browse files
committed
Insert NoDelim groups around nonterminals when lowering macro_rules
1 parent 0ca7f74 commit 5da0576

File tree

12 files changed

+308
-7
lines changed

12 files changed

+308
-7
lines changed

src/librustc_ast/attr/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,9 @@ impl MetaItemKind {
560560
tokens: &mut impl Iterator<Item = TokenTree>,
561561
) -> Option<MetaItemKind> {
562562
match tokens.next() {
563+
Some(TokenTree::Delimited(_, token::NoDelim, inner_tokens)) => {
564+
MetaItemKind::name_value_from_tokens(&mut inner_tokens.trees())
565+
}
563566
Some(TokenTree::Token(token)) => {
564567
Lit::from_token(&token).ok().map(MetaItemKind::NameValue)
565568
}
@@ -619,13 +622,20 @@ impl NestedMetaItem {
619622
where
620623
I: Iterator<Item = TokenTree>,
621624
{
622-
if let Some(TokenTree::Token(token)) = tokens.peek() {
623-
if let Ok(lit) = Lit::from_token(token) {
625+
match tokens.peek() {
626+
Some(TokenTree::Token(token)) => {
627+
if let Ok(lit) = Lit::from_token(token) {
628+
tokens.next();
629+
return Some(NestedMetaItem::Literal(lit));
630+
}
631+
}
632+
Some(TokenTree::Delimited(_, token::NoDelim, inner_tokens)) => {
633+
let inner_tokens = inner_tokens.clone();
624634
tokens.next();
625-
return Some(NestedMetaItem::Literal(lit));
635+
return NestedMetaItem::from_tokens(&mut inner_tokens.into_trees().peekable());
626636
}
637+
_ => {}
627638
}
628-
629639
MetaItem::from_tokens(tokens).map(NestedMetaItem::MetaItem)
630640
}
631641
}

src/librustc_ast_lowering/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ use rustc_ast::ast;
3939
use rustc_ast::ast::*;
4040
use rustc_ast::attr;
4141
use rustc_ast::node_id::NodeMap;
42-
use rustc_ast::token::{self, Nonterminal, Token};
43-
use rustc_ast::tokenstream::{TokenStream, TokenTree};
42+
use rustc_ast::token::{self, DelimToken, Nonterminal, Token};
43+
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
4444
use rustc_ast::visit::{self, AssocCtxt, Visitor};
4545
use rustc_ast::walk_list;
4646
use rustc_ast_pretty::pprust;
@@ -1029,7 +1029,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10291029
match token.kind {
10301030
token::Interpolated(nt, _) => {
10311031
let tts = (self.nt_to_tokenstream)(&nt, &self.sess.parse_sess, token.span);
1032-
self.lower_token_stream(tts)
1032+
TokenTree::Delimited(
1033+
DelimSpan::from_single(token.span),
1034+
DelimToken::NoDelim,
1035+
self.lower_token_stream(tts),
1036+
)
1037+
.into()
10331038
}
10341039
_ => TokenTree::Token(token).into(),
10351040
}

src/librustc_expand/mbe/macro_rules.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ pub fn compile_declarative_macro(
387387
def: &ast::Item,
388388
edition: Edition,
389389
) -> SyntaxExtension {
390+
debug!("compile_declarative_macro: {:?}", def);
390391
let mk_syn_ext = |expander| {
391392
SyntaxExtension::new(
392393
sess,

src/librustc_middle/ty/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,7 @@ impl<'tcx> TyCtxt<'tcx> {
10491049
Some(attr) => attr,
10501050
None => return Bound::Unbounded,
10511051
};
1052+
debug!("layout_scalar_valid_range: attr={:?}", attr);
10521053
for meta in attr.meta_item_list().expect("rustc_layout_scalar_valid_range takes args") {
10531054
match meta.literal().expect("attribute takes lit").kind {
10541055
ast::LitKind::Int(a, _) => return Bound::Included(a),

src/test/ui/macros/doc-comment.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// check-pass
2+
// Tests that we properly handle a nested macro expansion
3+
// involving a `#[doc]` attribute
4+
#![deny(missing_docs)]
5+
//! Crate docs
6+
7+
macro_rules! doc_comment {
8+
($x:expr, $($tt:tt)*) => {
9+
#[doc = $x]
10+
$($tt)*
11+
}
12+
}
13+
14+
macro_rules! make_comment {
15+
() => {
16+
doc_comment!("Function docs",
17+
pub fn bar() {}
18+
);
19+
}
20+
}
21+
22+
23+
make_comment!();
24+
25+
fn main() {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pub struct FirstStruct;
2+
3+
#[macro_export]
4+
macro_rules! outer_macro {
5+
($name:ident) => {
6+
#[macro_export]
7+
macro_rules! inner_macro {
8+
($wrapper:ident) => {
9+
$wrapper!($name)
10+
}
11+
}
12+
}
13+
}
14+
15+
outer_macro!(FirstStruct);

src/test/ui/proc-macro/auxiliary/test-macros.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ pub fn print_bang(input: TokenStream) -> TokenStream {
101101
print_helper(input, "BANG")
102102
}
103103

104+
#[proc_macro]
105+
pub fn print_bang_consume(input: TokenStream) -> TokenStream {
106+
print_helper(input, "BANG");
107+
TokenStream::new()
108+
}
109+
104110
#[proc_macro_attribute]
105111
pub fn print_attr(_: TokenStream, input: TokenStream) -> TokenStream {
106112
print_helper(input, "ATTR")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// run-pass
2+
// aux-build:nested-macro-rules.rs
3+
// aux-build:test-macros.rs
4+
// compile-flags: -Z span-debug
5+
// edition:2018
6+
7+
extern crate nested_macro_rules;
8+
extern crate test_macros;
9+
10+
use test_macros::print_bang;
11+
12+
use nested_macro_rules::FirstStruct;
13+
struct SecondStruct;
14+
15+
fn main() {
16+
nested_macro_rules::inner_macro!(print_bang);
17+
18+
nested_macro_rules::outer_macro!(SecondStruct);
19+
inner_macro!(print_bang);
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
PRINT-BANG INPUT (DISPLAY): FirstStruct
2+
PRINT-BANG INPUT (DEBUG): TokenStream [
3+
Group {
4+
delimiter: None,
5+
stream: TokenStream [
6+
Ident {
7+
ident: "FirstStruct",
8+
span: $DIR/auxiliary/nested-macro-rules.rs:15:14: 15:25 (#3),
9+
},
10+
],
11+
span: $DIR/auxiliary/nested-macro-rules.rs:9:27: 9:32 (#3),
12+
},
13+
]
14+
PRINT-BANG INPUT (DISPLAY): SecondStruct
15+
PRINT-BANG RE-COLLECTED (DISPLAY): SecondStruct
16+
PRINT-BANG INPUT (DEBUG): TokenStream [
17+
Group {
18+
delimiter: None,
19+
stream: TokenStream [
20+
Ident {
21+
ident: "SecondStruct",
22+
span: $DIR/nested-macro-rules.rs:18:38: 18:50 (#9),
23+
},
24+
],
25+
span: $DIR/auxiliary/nested-macro-rules.rs:9:27: 9:32 (#8),
26+
},
27+
]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// run-pass
2+
// aux-build:test-macros.rs
3+
// compile-flags: -Z span-debug
4+
// edition:2018
5+
//
6+
// Tests the pretty-printing behavior of inserting `NoDelim` groups
7+
8+
extern crate test_macros;
9+
use test_macros::print_bang_consume;
10+
11+
macro_rules! expand_it {
12+
(($val1:expr) ($val2:expr)) => { expand_it!($val1 + $val2) };
13+
($val:expr) => { print_bang_consume!("hi" $val (1 + 1)) };
14+
}
15+
16+
fn main() {
17+
expand_it!(1 + (25) + 1);
18+
expand_it!(("hello".len()) ("world".len()));
19+
}

0 commit comments

Comments
 (0)