Skip to content

Commit 973fb4b

Browse files
authored
Rollup merge of #83348 - osa1:issue83344, r=jackh726
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 --- r? ```@estebank```
2 parents 1f33a6a + 5b9bac2 commit 973fb4b

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
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+
// check-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+

src/tools/clippy/tests/ui/write_literal_2.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ LL | "1", "2", "3",
7575
|
7676
help: try this
7777
|
78-
LL | "some 1{} / {}", "2", "3",
79-
| ^ --
78+
LL | "some 1/
79+
LL | {} / {}", "2", "3",
80+
|
8081

8182
error: literal with an empty format string
8283
--> $DIR/write_literal_2.rs:25:14

0 commit comments

Comments
 (0)