Skip to content

Commit 6f9a8a7

Browse files
committed
Auto merge of #76485 - estebank:format_arg_capture_spans, r=davidtwco
Point at named argument not found when using `format_args_capture` instead of whole format string
2 parents fd15e61 + 2ac89ff commit 6f9a8a7

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

compiler/rustc_builtin_macros/src/format.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,12 @@ impl<'a, 'b> Context<'a, 'b> {
543543
let idx = self.args.len();
544544
self.arg_types.push(Vec::new());
545545
self.arg_unique_types.push(Vec::new());
546-
self.args.push(
547-
self.ecx.expr_ident(self.fmtsp, Ident::new(name, self.fmtsp)),
548-
);
546+
let span = if self.is_literal {
547+
*self.arg_spans.get(self.curpiece).unwrap_or(&self.fmtsp)
548+
} else {
549+
self.fmtsp
550+
};
551+
self.args.push(self.ecx.expr_ident(span, Ident::new(name, span)));
549552
self.names.insert(name, idx);
550553
self.verify_arg_type(Exact(idx), ty)
551554
} else {

src/test/ui/fmt/format-args-capture-missing-variables.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main() {
55
//~^ ERROR: cannot find value `foo` in this scope
66
//~^^ ERROR: cannot find value `bar` in this scope
77

8-
format!("{foo}"); //~ ERROR: cannot find value `foo` in this scope
8+
format!("{foo}"); //~ ERROR: cannot find value `foo` in this scope
99

1010
format!("{valuea} {valueb}", valuea=5, valuec=7);
1111
//~^ ERROR cannot find value `valueb` in this scope
@@ -16,7 +16,7 @@ fn main() {
1616
{foo}
1717
1818
"##);
19-
//~^^^^^ ERROR: cannot find value `foo` in this scope
19+
//~^^^ ERROR: cannot find value `foo` in this scope
2020

21-
panic!("{foo} {bar}", bar=1); //~ ERROR: cannot find value `foo` in this scope
21+
panic!("{foo} {bar}", bar=1); //~ ERROR: cannot find value `foo` in this scope
2222
}

src/test/ui/fmt/format-args-capture-missing-variables.stderr

+13-18
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,40 @@ LL | format!("{valuea} {valueb}", valuea=5, valuec=7);
77
| formatting specifier missing
88

99
error[E0425]: cannot find value `foo` in this scope
10-
--> $DIR/format-args-capture-missing-variables.rs:4:13
10+
--> $DIR/format-args-capture-missing-variables.rs:4:17
1111
|
1212
LL | format!("{} {foo} {} {bar} {}", 1, 2, 3);
13-
| ^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
13+
| ^^^^^ not found in this scope
1414

1515
error[E0425]: cannot find value `bar` in this scope
16-
--> $DIR/format-args-capture-missing-variables.rs:4:13
16+
--> $DIR/format-args-capture-missing-variables.rs:4:26
1717
|
1818
LL | format!("{} {foo} {} {bar} {}", 1, 2, 3);
19-
| ^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
19+
| ^^^^^ not found in this scope
2020

2121
error[E0425]: cannot find value `foo` in this scope
22-
--> $DIR/format-args-capture-missing-variables.rs:8:13
22+
--> $DIR/format-args-capture-missing-variables.rs:8:14
2323
|
2424
LL | format!("{foo}");
25-
| ^^^^^^^ not found in this scope
25+
| ^^^^^ not found in this scope
2626

2727
error[E0425]: cannot find value `valueb` in this scope
28-
--> $DIR/format-args-capture-missing-variables.rs:10:13
28+
--> $DIR/format-args-capture-missing-variables.rs:10:23
2929
|
3030
LL | format!("{valuea} {valueb}", valuea=5, valuec=7);
31-
| ^^^^^^^^^^^^^^^^^^^ not found in this scope
31+
| ^^^^^^^^ not found in this scope
3232

3333
error[E0425]: cannot find value `foo` in this scope
34-
--> $DIR/format-args-capture-missing-variables.rs:14:13
34+
--> $DIR/format-args-capture-missing-variables.rs:16:9
3535
|
36-
LL | format!(r##"
37-
| _____________^
38-
LL | |
39-
LL | | {foo}
40-
LL | |
41-
LL | | "##);
42-
| |_______^ not found in this scope
36+
LL | {foo}
37+
| ^^^^^ not found in this scope
4338

4439
error[E0425]: cannot find value `foo` in this scope
45-
--> $DIR/format-args-capture-missing-variables.rs:21:12
40+
--> $DIR/format-args-capture-missing-variables.rs:21:13
4641
|
4742
LL | panic!("{foo} {bar}", bar=1);
48-
| ^^^^^^^^^^^^^ not found in this scope
43+
| ^^^^^ not found in this scope
4944

5045
error: aborting due to 7 previous errors
5146

0 commit comments

Comments
 (0)