Skip to content

Commit 020e1f5

Browse files
committed
don't unwrap unexpected tokens in format!
Fixes #57512.
1 parent 6ecad33 commit 020e1f5

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/libsyntax_ext/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn parse_args<'a>(
159159
};
160160
let name: &str = &ident.as_str();
161161

162-
p.expect(&token::Eq).unwrap();
162+
p.expect(&token::Eq)?;
163163
let e = p.parse_expr()?;
164164
if let Some(prev) = names.get(name) {
165165
ecx.struct_span_err(e.span, &format!("duplicate argument named `{}`", name))

src/test/ui/macros/format-parse-errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ fn main() {
22
format!(); //~ ERROR requires at least a format string argument
33
format!(struct); //~ ERROR expected expression
44
format!("s", name =); //~ ERROR expected expression
5+
format!("s", foo = foo, bar); //~ ERROR expected `=`
56
format!("s", foo = struct); //~ ERROR expected expression
67
format!("s", struct); //~ ERROR expected expression
78

src/test/ui/macros/format-parse-errors.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,26 @@ error: expected expression, found `<eof>`
1818
LL | format!("s", name =); //~ ERROR expected expression
1919
| ^ expected expression
2020

21+
error: expected `=`, found `<eof>`
22+
--> $DIR/format-parse-errors.rs:5:29
23+
|
24+
LL | format!("s", foo = foo, bar); //~ ERROR expected `=`
25+
| ^^^ expected `=`
26+
2127
error: expected expression, found keyword `struct`
22-
--> $DIR/format-parse-errors.rs:5:24
28+
--> $DIR/format-parse-errors.rs:6:24
2329
|
2430
LL | format!("s", foo = struct); //~ ERROR expected expression
2531
| ^^^^^^ expected expression
2632

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

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

43-
error: aborting due to 6 previous errors
49+
error: aborting due to 7 previous errors
4450

0 commit comments

Comments
 (0)