Skip to content

Commit 6c45ebe

Browse files
committed
format macro argument parsing fix
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
1 parent bbf07c0 commit 6c45ebe

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

compiler/rustc_parse_format/src/lib.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,13 @@ impl<'a> Iterator for Parser<'a> {
213213
Some(String(self.string(pos + 1)))
214214
} else {
215215
let arg = self.argument();
216-
if let Some(end) = self.must_consume('}') {
217-
let start = self.to_span_index(pos);
218-
let end = self.to_span_index(end + 1);
216+
if let Some(rbrace_byte_idx) = self.must_consume('}') {
217+
let lbrace_inner_offset = self.to_span_index(pos);
218+
let rbrace_inner_offset = self.to_span_index(rbrace_byte_idx);
219219
if self.is_literal {
220-
self.arg_places.push(start.to(end));
220+
self.arg_places.push(
221+
lbrace_inner_offset.to(InnerOffset(rbrace_inner_offset.0 + 1)),
222+
);
221223
}
222224
}
223225
Some(NextArgument(arg))

src/test/ui/macros/issue-83344.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// chekc-fail
2+
3+
fn main() {
4+
println!("{}\
5+
"); //~^ ERROR: 1 positional argument in format string, but no arguments were given
6+
}

src/test/ui/macros/issue-83344.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: 1 positional argument in format string, but no arguments were given
2+
--> $DIR/issue-83344.rs:4:15
3+
|
4+
LL | println!("{}\
5+
| ^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)