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

+14-4
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

+8-3
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

+1
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

+1
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

+25
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() {}
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

+6
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")
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+
}
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+
]
+19
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
PRINT-BANG INPUT (DISPLAY): "hi" 1 + (25) + 1 (1 + 1)
2+
PRINT-BANG INPUT (DEBUG): TokenStream [
3+
Literal {
4+
kind: Str,
5+
symbol: "hi",
6+
suffix: None,
7+
span: $DIR/nodelim-groups.rs:13:42: 13:46 (#3),
8+
},
9+
Group {
10+
delimiter: None,
11+
stream: TokenStream [
12+
Literal {
13+
kind: Integer,
14+
symbol: "1",
15+
suffix: None,
16+
span: $DIR/nodelim-groups.rs:17:16: 17:17 (#0),
17+
},
18+
Punct {
19+
ch: '+',
20+
spacing: Alone,
21+
span: $DIR/nodelim-groups.rs:17:18: 17:19 (#0),
22+
},
23+
Group {
24+
delimiter: Parenthesis,
25+
stream: TokenStream [
26+
Literal {
27+
kind: Integer,
28+
symbol: "25",
29+
suffix: None,
30+
span: $DIR/nodelim-groups.rs:17:21: 17:23 (#0),
31+
},
32+
],
33+
span: $DIR/nodelim-groups.rs:17:20: 17:24 (#0),
34+
},
35+
Punct {
36+
ch: '+',
37+
spacing: Alone,
38+
span: $DIR/nodelim-groups.rs:17:25: 17:26 (#0),
39+
},
40+
Literal {
41+
kind: Integer,
42+
symbol: "1",
43+
suffix: None,
44+
span: $DIR/nodelim-groups.rs:17:27: 17:28 (#0),
45+
},
46+
],
47+
span: $DIR/nodelim-groups.rs:13:47: 13:51 (#3),
48+
},
49+
Group {
50+
delimiter: Parenthesis,
51+
stream: TokenStream [
52+
Literal {
53+
kind: Integer,
54+
symbol: "1",
55+
suffix: None,
56+
span: $DIR/nodelim-groups.rs:13:53: 13:54 (#3),
57+
},
58+
Punct {
59+
ch: '+',
60+
spacing: Alone,
61+
span: $DIR/nodelim-groups.rs:13:55: 13:56 (#3),
62+
},
63+
Literal {
64+
kind: Integer,
65+
symbol: "1",
66+
suffix: None,
67+
span: $DIR/nodelim-groups.rs:13:57: 13:58 (#3),
68+
},
69+
],
70+
span: $DIR/nodelim-groups.rs:13:52: 13:59 (#3),
71+
},
72+
]
73+
PRINT-BANG INPUT (DISPLAY): "hi" "hello".len() + "world".len() (1 + 1)
74+
PRINT-BANG RE-COLLECTED (DISPLAY): "hi" "hello" . len() + "world" . len() (1 + 1)
75+
PRINT-BANG INPUT (DEBUG): TokenStream [
76+
Literal {
77+
kind: Str,
78+
symbol: "hi",
79+
suffix: None,
80+
span: $DIR/nodelim-groups.rs:13:42: 13:46 (#8),
81+
},
82+
Group {
83+
delimiter: None,
84+
stream: TokenStream [
85+
Literal {
86+
kind: Str,
87+
symbol: "hello",
88+
suffix: None,
89+
span: $DIR/nodelim-groups.rs:13:47: 13:51 (#8),
90+
},
91+
Punct {
92+
ch: '.',
93+
spacing: Alone,
94+
span: $DIR/nodelim-groups.rs:13:47: 13:51 (#8),
95+
},
96+
Ident {
97+
ident: "len",
98+
span: $DIR/nodelim-groups.rs:13:47: 13:51 (#8),
99+
},
100+
Group {
101+
delimiter: Parenthesis,
102+
stream: TokenStream [],
103+
span: $DIR/nodelim-groups.rs:13:47: 13:51 (#8),
104+
},
105+
Punct {
106+
ch: '+',
107+
spacing: Alone,
108+
span: $DIR/nodelim-groups.rs:13:47: 13:51 (#8),
109+
},
110+
Literal {
111+
kind: Str,
112+
symbol: "world",
113+
suffix: None,
114+
span: $DIR/nodelim-groups.rs:13:47: 13:51 (#8),
115+
},
116+
Punct {
117+
ch: '.',
118+
spacing: Alone,
119+
span: $DIR/nodelim-groups.rs:13:47: 13:51 (#8),
120+
},
121+
Ident {
122+
ident: "len",
123+
span: $DIR/nodelim-groups.rs:13:47: 13:51 (#8),
124+
},
125+
Group {
126+
delimiter: Parenthesis,
127+
stream: TokenStream [],
128+
span: $DIR/nodelim-groups.rs:13:47: 13:51 (#8),
129+
},
130+
],
131+
span: $DIR/nodelim-groups.rs:13:47: 13:51 (#8),
132+
},
133+
Group {
134+
delimiter: Parenthesis,
135+
stream: TokenStream [
136+
Literal {
137+
kind: Integer,
138+
symbol: "1",
139+
suffix: None,
140+
span: $DIR/nodelim-groups.rs:13:53: 13:54 (#8),
141+
},
142+
Punct {
143+
ch: '+',
144+
spacing: Alone,
145+
span: $DIR/nodelim-groups.rs:13:55: 13:56 (#8),
146+
},
147+
Literal {
148+
kind: Integer,
149+
symbol: "1",
150+
suffix: None,
151+
span: $DIR/nodelim-groups.rs:13:57: 13:58 (#8),
152+
},
153+
],
154+
span: $DIR/nodelim-groups.rs:13:52: 13:59 (#8),
155+
},
156+
]
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// build-pass
2+
#![feature(rustc_attrs)]
3+
4+
macro_rules! apply {
5+
($val:expr) => {
6+
#[rustc_layout_scalar_valid_range_start($val)]
7+
#[repr(transparent)]
8+
pub(crate) struct NonZero<T>(pub(crate) T);
9+
}
10+
}
11+
12+
apply!(1);
13+
14+
fn main() {
15+
let _x = unsafe { NonZero(1) };
16+
}

0 commit comments

Comments
 (0)