Skip to content

Commit cef9b47

Browse files
committed
Extend format-args capture test.
1 parent fb2d530 commit cef9b47

3 files changed

+25
-1
lines changed

src/test/ui/fmt/format-args-capture-issue-93378.rs

+4
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ fn main() {
44

55
println!("{a} {b} {} {} {c} {}", c = "c");
66
//~^ ERROR: invalid reference to positional arguments 1 and 2 (there is 1 argument)
7+
8+
let n = 1;
9+
println!("{a:.n$} {b:.*}");
10+
//~^ ERROR: invalid reference to positional argument 0 (no arguments were given)
711
}

src/test/ui/fmt/format-args-capture-issue-93378.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,17 @@ LL | println!("{a} {b} {} {} {c} {}", c = "c");
66
|
77
= note: positional arguments are zero-based
88

9-
error: aborting due to previous error
9+
error: invalid reference to positional argument 0 (no arguments were given)
10+
--> $DIR/format-args-capture-issue-93378.rs:9:23
11+
|
12+
LL | println!("{a:.n$} {b:.*}");
13+
| ------- ^^^--^
14+
| | |
15+
| | this precision flag adds an extra required argument at position 0, which is why there are 3 arguments expected
16+
| this parameter corresponds to the precision flag
17+
|
18+
= note: positional arguments are zero-based
19+
= note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html
20+
21+
error: aborting due to 2 previous errors
1022

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

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ fn main() {
55
named_argument_takes_precedence_to_captured();
66
formatting_parameters_can_be_captured();
77
capture_raw_strings_and_idents();
8+
repeated_capture();
89

910
#[cfg(panic = "unwind")]
1011
{
@@ -80,3 +81,10 @@ fn formatting_parameters_can_be_captured() {
8081
let s = format!("{x:-^width$.precision$}");
8182
assert_eq!(&s, "--7.000--");
8283
}
84+
85+
fn repeated_capture() {
86+
let a = 1;
87+
let b = 2;
88+
let s = format!("{a} {b} {a}");
89+
assert_eq!(&s, "1 2 1");
90+
}

0 commit comments

Comments
 (0)