Skip to content

Commit 58f26bb

Browse files
Rollup merge of rust-lang#59116 - estebank:comma-sugg, r=petrochenkov
Be more discerning on when to attempt suggesting a comma in a macro invocation Fix rust-lang#58796.
2 parents e113402 + 27abd52 commit 58f26bb

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

src/libsyntax/tokenstream.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,11 @@ impl TokenStream {
178178
while let Some((pos, ts)) = iter.next() {
179179
if let Some((_, next)) = iter.peek() {
180180
let sp = match (&ts, &next) {
181-
((TokenTree::Token(_, token::Token::Comma), NonJoint), _) |
182-
(_, (TokenTree::Token(_, token::Token::Comma), NonJoint)) => continue,
183-
((TokenTree::Token(sp, _), NonJoint), _) => *sp,
181+
(_, (TokenTree::Token(_, token::Token::Comma), _)) => continue,
182+
((TokenTree::Token(sp, token_left), NonJoint),
183+
(TokenTree::Token(_, token_right), _))
184+
if (token_left.is_ident() || token_left.is_lit()) &&
185+
(token_right.is_ident() || token_right.is_lit()) => *sp,
184186
((TokenTree::Delimited(sp, ..), NonJoint), _) => sp.entire(),
185187
_ => continue,
186188
};

src/test/ui/macros/missing-comma.rs

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ macro_rules! foo {
66
($a:ident, $b:ident, $c:ident, $d:ident, $e:ident) => ();
77
}
88

9+
macro_rules! bar {
10+
($lvl:expr, $($arg:tt)+) => {}
11+
}
12+
13+
914
fn main() {
1015
println!("{}" a);
1116
//~^ ERROR expected token: `,`
@@ -17,4 +22,6 @@ fn main() {
1722
//~^ ERROR no rules expected the token `d`
1823
foo!(a, b, c d e);
1924
//~^ ERROR no rules expected the token `d`
25+
bar!(Level::Error, );
26+
//~^ ERROR unexpected end of macro invocation
2027
}

src/test/ui/macros/missing-comma.stderr

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: expected token: `,`
2-
--> $DIR/missing-comma.rs:10:19
2+
--> $DIR/missing-comma.rs:15:19
33
|
44
LL | println!("{}" a);
55
| ^
66

77
error: no rules expected the token `b`
8-
--> $DIR/missing-comma.rs:12:12
8+
--> $DIR/missing-comma.rs:17:12
99
|
1010
LL | macro_rules! foo {
1111
| ---------------- when calling this macro
@@ -16,7 +16,7 @@ LL | foo!(a b);
1616
| help: missing comma here
1717

1818
error: no rules expected the token `e`
19-
--> $DIR/missing-comma.rs:14:21
19+
--> $DIR/missing-comma.rs:19:21
2020
|
2121
LL | macro_rules! foo {
2222
| ---------------- when calling this macro
@@ -27,7 +27,7 @@ LL | foo!(a, b, c, d e);
2727
| help: missing comma here
2828

2929
error: no rules expected the token `d`
30-
--> $DIR/missing-comma.rs:16:18
30+
--> $DIR/missing-comma.rs:21:18
3131
|
3232
LL | macro_rules! foo {
3333
| ---------------- when calling this macro
@@ -38,13 +38,22 @@ LL | foo!(a, b, c d, e);
3838
| help: missing comma here
3939

4040
error: no rules expected the token `d`
41-
--> $DIR/missing-comma.rs:18:18
41+
--> $DIR/missing-comma.rs:23:18
4242
|
4343
LL | macro_rules! foo {
4444
| ---------------- when calling this macro
4545
...
4646
LL | foo!(a, b, c d e);
4747
| ^ no rules expected this token in macro call
4848

49-
error: aborting due to 5 previous errors
49+
error: unexpected end of macro invocation
50+
--> $DIR/missing-comma.rs:25:23
51+
|
52+
LL | macro_rules! bar {
53+
| ---------------- when calling this macro
54+
...
55+
LL | bar!(Level::Error, );
56+
| ^ missing tokens in macro arguments
57+
58+
error: aborting due to 6 previous errors
5059

0 commit comments

Comments
 (0)