From 4dec571ec69e14673b4e185e9881e4b71cd42aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 1 Sep 2019 15:09:05 -0700 Subject: [PATCH] Be accurate on `format!` parse error expectations --- src/libsyntax_ext/format.rs | 16 ++++++++++++---- src/test/ui/codemap_tests/bad-format-args.rs | 2 +- src/test/ui/codemap_tests/bad-format-args.stderr | 4 ++-- src/test/ui/type/ascription/issue-54516.rs | 2 +- src/test/ui/type/ascription/issue-54516.stderr | 4 ++-- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/libsyntax_ext/format.rs b/src/libsyntax_ext/format.rs index 47394c02b4186..d9ca693aca504 100644 --- a/src/libsyntax_ext/format.rs +++ b/src/libsyntax_ext/format.rs @@ -138,15 +138,23 @@ fn parse_args<'a>( } let fmtstr = p.parse_expr()?; + let mut first = true; let mut named = false; while p.token != token::Eof { if !p.eat(&token::Comma) { - let mut err = ecx.struct_span_err(p.token.span, "expected token: `,`"); - err.span_label(p.token.span, "expected `,`"); - p.maybe_annotate_with_ascription(&mut err, false); - return Err(err); + if first { + // After `format!(""` we always expect *only* a comma... + let mut err = ecx.struct_span_err(p.token.span, "expected token: `,`"); + err.span_label(p.token.span, "expected `,`"); + p.maybe_annotate_with_ascription(&mut err, false); + return Err(err); + } else { + // ...after that delegate to `expect` to also include the other expected tokens. + return Err(p.expect(&token::Comma).err().unwrap()); + } } + first = false; if p.token == token::Eof { break; } // accept trailing commas diff --git a/src/test/ui/codemap_tests/bad-format-args.rs b/src/test/ui/codemap_tests/bad-format-args.rs index 9f90185774467..dff248344a53d 100644 --- a/src/test/ui/codemap_tests/bad-format-args.rs +++ b/src/test/ui/codemap_tests/bad-format-args.rs @@ -1,5 +1,5 @@ fn main() { format!(); //~ ERROR requires at least a format string argument format!("" 1); //~ ERROR expected token: `,` - format!("", 1 1); //~ ERROR expected token: `,` + format!("", 1 1); //~ ERROR expected one of } diff --git a/src/test/ui/codemap_tests/bad-format-args.stderr b/src/test/ui/codemap_tests/bad-format-args.stderr index 5b01314d8ad4f..3372ef6dea1fc 100644 --- a/src/test/ui/codemap_tests/bad-format-args.stderr +++ b/src/test/ui/codemap_tests/bad-format-args.stderr @@ -12,11 +12,11 @@ error: expected token: `,` LL | format!("" 1); | ^ expected `,` -error: expected token: `,` +error: expected one of `,`, `.`, `?`, or an operator, found `1` --> $DIR/bad-format-args.rs:4:19 | LL | format!("", 1 1); - | ^ expected `,` + | ^ expected one of `,`, `.`, `?`, or an operator here error: aborting due to 3 previous errors diff --git a/src/test/ui/type/ascription/issue-54516.rs b/src/test/ui/type/ascription/issue-54516.rs index 6d65760e299b5..b53bfe5df03f3 100644 --- a/src/test/ui/type/ascription/issue-54516.rs +++ b/src/test/ui/type/ascription/issue-54516.rs @@ -2,5 +2,5 @@ use std::collections::BTreeMap; fn main() { println!("{}", std::mem:size_of::>()); - //~^ ERROR expected token: `,` + //~^ ERROR expected one of } diff --git a/src/test/ui/type/ascription/issue-54516.stderr b/src/test/ui/type/ascription/issue-54516.stderr index a846f3bc320e6..97942904a0ffa 100644 --- a/src/test/ui/type/ascription/issue-54516.stderr +++ b/src/test/ui/type/ascription/issue-54516.stderr @@ -1,8 +1,8 @@ -error: expected token: `,` +error: expected one of `!`, `,`, or `::`, found `(` --> $DIR/issue-54516.rs:4:58 | LL | println!("{}", std::mem:size_of::>()); - | - ^ expected `,` + | - ^ expected one of `!`, `,`, or `::` here | | | help: maybe write a path separator here: `::` |