Skip to content

Commit

Permalink
Be accurate on format! parse error expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Sep 1, 2019
1 parent fba38ac commit 4dec571
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
16 changes: 12 additions & 4 deletions src/libsyntax_ext/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/codemap_tests/bad-format-args.rs
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions src/test/ui/codemap_tests/bad-format-args.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion src/test/ui/type/ascription/issue-54516.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ use std::collections::BTreeMap;

fn main() {
println!("{}", std::mem:size_of::<BTreeMap<u32, u32>>());
//~^ ERROR expected token: `,`
//~^ ERROR expected one of
}
4 changes: 2 additions & 2 deletions src/test/ui/type/ascription/issue-54516.stderr
Original file line number Diff line number Diff line change
@@ -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::<BTreeMap<u32, u32>>());
| - ^ expected `,`
| - ^ expected one of `!`, `,`, or `::` here
| |
| help: maybe write a path separator here: `::`
|
Expand Down

0 comments on commit 4dec571

Please sign in to comment.