Skip to content

Commit

Permalink
format macro argument parsing fix
Browse files Browse the repository at this point in the history
When the character next to `{}` is "shifted" (when mapping a byte index
in the format string to span) we should avoid shifting the span end
index, so first map the index of `}` to span, then bump the span,
instead of first mapping the next byte index to a span (which causes
bumping the end span too much).

Regression test added.

Fixes #83344
  • Loading branch information
osa1 committed Mar 21, 2021
1 parent bbf07c0 commit 6c45ebe
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
10 changes: 6 additions & 4 deletions compiler/rustc_parse_format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,13 @@ impl<'a> Iterator for Parser<'a> {
Some(String(self.string(pos + 1)))
} else {
let arg = self.argument();
if let Some(end) = self.must_consume('}') {
let start = self.to_span_index(pos);
let end = self.to_span_index(end + 1);
if let Some(rbrace_byte_idx) = self.must_consume('}') {
let lbrace_inner_offset = self.to_span_index(pos);
let rbrace_inner_offset = self.to_span_index(rbrace_byte_idx);
if self.is_literal {
self.arg_places.push(start.to(end));
self.arg_places.push(
lbrace_inner_offset.to(InnerOffset(rbrace_inner_offset.0 + 1)),
);
}
}
Some(NextArgument(arg))
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/macros/issue-83344.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// chekc-fail

fn main() {
println!("{}\
"); //~^ ERROR: 1 positional argument in format string, but no arguments were given
}
8 changes: 8 additions & 0 deletions src/test/ui/macros/issue-83344.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: 1 positional argument in format string, but no arguments were given
--> $DIR/issue-83344.rs:4:15
|
LL | println!("{}\
| ^^

error: aborting due to previous error

0 comments on commit 6c45ebe

Please sign in to comment.