Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't unwrap unexpected tokens in format! #57522

Merged
merged 1 commit into from
Jan 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libsyntax_ext/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn parse_args<'a>(
};
let name: &str = &ident.as_str();

p.expect(&token::Eq).unwrap();
p.expect(&token::Eq)?;
let e = p.parse_expr()?;
if let Some(prev) = names.get(name) {
ecx.struct_span_err(e.span, &format!("duplicate argument named `{}`", name))
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/macros/format-parse-errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ fn main() {
format!(); //~ ERROR requires at least a format string argument
format!(struct); //~ ERROR expected expression
format!("s", name =); //~ ERROR expected expression
format!("s", foo = foo, bar); //~ ERROR expected `=`
format!("s", foo = struct); //~ ERROR expected expression
format!("s", struct); //~ ERROR expected expression

Expand Down
14 changes: 10 additions & 4 deletions src/test/ui/macros/format-parse-errors.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,26 @@ error: expected expression, found `<eof>`
LL | format!("s", name =); //~ ERROR expected expression
| ^ expected expression

error: expected `=`, found `<eof>`
--> $DIR/format-parse-errors.rs:5:29
|
LL | format!("s", foo = foo, bar); //~ ERROR expected `=`
| ^^^ expected `=`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice if this had a custom diagnostic explaining that named format arguments must come after positional format arguments :)


error: expected expression, found keyword `struct`
--> $DIR/format-parse-errors.rs:5:24
--> $DIR/format-parse-errors.rs:6:24
|
LL | format!("s", foo = struct); //~ ERROR expected expression
| ^^^^^^ expected expression

error: expected expression, found keyword `struct`
--> $DIR/format-parse-errors.rs:6:18
--> $DIR/format-parse-errors.rs:7:18
|
LL | format!("s", struct); //~ ERROR expected expression
| ^^^^^^ expected expression

error: format argument must be a string literal
--> $DIR/format-parse-errors.rs:9:13
--> $DIR/format-parse-errors.rs:10:13
|
LL | format!(123); //~ ERROR format argument must be a string literal
| ^^^
Expand All @@ -40,5 +46,5 @@ help: you might be missing a string literal to format with
LL | format!("{}", 123); //~ ERROR format argument must be a string literal
| ^^^^^

error: aborting due to 6 previous errors
error: aborting due to 7 previous errors