Skip to content

Commit

Permalink
Rollup merge of rust-lang#59116 - estebank:comma-sugg, r=petrochenkov
Browse files Browse the repository at this point in the history
Be more discerning on when to attempt suggesting a comma in a macro invocation

Fix rust-lang#58796.
  • Loading branch information
Mark-Simulacrum committed Mar 14, 2019
2 parents e113402 + 27abd52 commit 58f26bb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/libsyntax/tokenstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,11 @@ impl TokenStream {
while let Some((pos, ts)) = iter.next() {
if let Some((_, next)) = iter.peek() {
let sp = match (&ts, &next) {
((TokenTree::Token(_, token::Token::Comma), NonJoint), _) |
(_, (TokenTree::Token(_, token::Token::Comma), NonJoint)) => continue,
((TokenTree::Token(sp, _), NonJoint), _) => *sp,
(_, (TokenTree::Token(_, token::Token::Comma), _)) => continue,
((TokenTree::Token(sp, token_left), NonJoint),
(TokenTree::Token(_, token_right), _))
if (token_left.is_ident() || token_left.is_lit()) &&
(token_right.is_ident() || token_right.is_lit()) => *sp,
((TokenTree::Delimited(sp, ..), NonJoint), _) => sp.entire(),
_ => continue,
};
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/macros/missing-comma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ macro_rules! foo {
($a:ident, $b:ident, $c:ident, $d:ident, $e:ident) => ();
}

macro_rules! bar {
($lvl:expr, $($arg:tt)+) => {}
}


fn main() {
println!("{}" a);
//~^ ERROR expected token: `,`
Expand All @@ -17,4 +22,6 @@ fn main() {
//~^ ERROR no rules expected the token `d`
foo!(a, b, c d e);
//~^ ERROR no rules expected the token `d`
bar!(Level::Error, );
//~^ ERROR unexpected end of macro invocation
}
21 changes: 15 additions & 6 deletions src/test/ui/macros/missing-comma.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error: expected token: `,`
--> $DIR/missing-comma.rs:10:19
--> $DIR/missing-comma.rs:15:19
|
LL | println!("{}" a);
| ^

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

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

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

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

error: aborting due to 5 previous errors
error: unexpected end of macro invocation
--> $DIR/missing-comma.rs:25:23
|
LL | macro_rules! bar {
| ---------------- when calling this macro
...
LL | bar!(Level::Error, );
| ^ missing tokens in macro arguments

error: aborting due to 6 previous errors

0 comments on commit 58f26bb

Please sign in to comment.