Skip to content

Commit 3c714c7

Browse files
authored
Rollup merge of rust-lang#102214 - cassaundra:fix-format-args-span, r=cjgillot
Fix span of byte-escaped left format args brace Fix rust-lang#102057 (see issue for example). Previously, the use of escaped left braces (`\x7B`) in format args resulted in an incorrectly offset span. This patch fixes that by considering any escaped characters within the string instead of using a constant offset.
2 parents 65445a5 + e5096d4 commit 3c714c7

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

compiler/rustc_parse_format/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ impl<'a> Iterator for Parser<'a> {
224224
'{' => {
225225
let curr_last_brace = self.last_opening_brace;
226226
let byte_pos = self.to_span_index(pos);
227-
let lbrace_end = InnerOffset(byte_pos.0 + 1);
227+
let lbrace_end = self.to_span_index(pos + 1);
228228
self.last_opening_brace = Some(byte_pos.to(lbrace_end));
229229
self.cur.next();
230230
if self.consume('{') {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
fn main() {
2+
format!("\x7Ba}");
3+
//~^ ERROR cannot find value `a` in this scope
4+
format!("\x7Ba\x7D");
5+
//~^ ERROR cannot find value `a` in this scope
6+
7+
let a = 0;
8+
9+
format!("\x7Ba} {b}");
10+
//~^ ERROR cannot find value `b` in this scope
11+
format!("\x7Ba\x7D {b}");
12+
//~^ ERROR cannot find value `b` in this scope
13+
format!("\x7Ba} \x7Bb}");
14+
//~^ ERROR cannot find value `b` in this scope
15+
format!("\x7Ba\x7D \x7Bb}");
16+
//~^ ERROR cannot find value `b` in this scope
17+
format!("\x7Ba\x7D \x7Bb\x7D");
18+
//~^ ERROR cannot find value `b` in this scope
19+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error[E0425]: cannot find value `a` in this scope
2+
--> $DIR/format-args-capture-issue-102057.rs:2:18
3+
|
4+
LL | format!("\x7Ba}");
5+
| ^ not found in this scope
6+
7+
error[E0425]: cannot find value `a` in this scope
8+
--> $DIR/format-args-capture-issue-102057.rs:4:18
9+
|
10+
LL | format!("\x7Ba\x7D");
11+
| ^ not found in this scope
12+
13+
error[E0425]: cannot find value `b` in this scope
14+
--> $DIR/format-args-capture-issue-102057.rs:9:22
15+
|
16+
LL | format!("\x7Ba} {b}");
17+
| ^ help: a local variable with a similar name exists: `a`
18+
19+
error[E0425]: cannot find value `b` in this scope
20+
--> $DIR/format-args-capture-issue-102057.rs:11:25
21+
|
22+
LL | format!("\x7Ba\x7D {b}");
23+
| ^ help: a local variable with a similar name exists: `a`
24+
25+
error[E0425]: cannot find value `b` in this scope
26+
--> $DIR/format-args-capture-issue-102057.rs:13:25
27+
|
28+
LL | format!("\x7Ba} \x7Bb}");
29+
| ^ help: a local variable with a similar name exists: `a`
30+
31+
error[E0425]: cannot find value `b` in this scope
32+
--> $DIR/format-args-capture-issue-102057.rs:15:28
33+
|
34+
LL | format!("\x7Ba\x7D \x7Bb}");
35+
| ^ help: a local variable with a similar name exists: `a`
36+
37+
error[E0425]: cannot find value `b` in this scope
38+
--> $DIR/format-args-capture-issue-102057.rs:17:28
39+
|
40+
LL | format!("\x7Ba\x7D \x7Bb\x7D");
41+
| ^ help: a local variable with a similar name exists: `a`
42+
43+
error: aborting due to 7 previous errors
44+
45+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)