Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions compiler/rustc_parse_format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,11 @@ impl<'input> Parser<'input> {
0,
ParseError {
description: "field access isn't supported".to_string(),
note: None,
note: Some(
"consider moving this expression to a local variable and then \
using the local here instead"
.to_owned(),
),
label: "not supported".to_string(),
span: arg.position_span.start..field.position_span.end,
secondary_label: None,
Expand All @@ -947,7 +951,11 @@ impl<'input> Parser<'input> {
0,
ParseError {
description: "tuple index access isn't supported".to_string(),
note: None,
note: Some(
"consider moving this expression to a local variable and then \
using the local here instead"
.to_owned(),
),
label: "not supported".to_string(),
span: arg.position_span.start..field.position_span.end,
secondary_label: None,
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/fmt/format-args-non-identifier-diagnostics.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Checks that there is a suggestion for simple tuple index access expression (used where an
// identifier is expected in a format arg) to use positional arg instead.
// Issue: <https://github.com/rust-lang/rust/issues/122535>.
// identifier is expected in a format arg) to use positional arg instead, with a note to move
// the expression into a local variable.
// Issue: <https://github.com/rust-lang/rust/issues/114108>.
//@ run-rustfix

fn main() {
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/fmt/format-args-non-identifier-diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Checks that there is a suggestion for simple tuple index access expression (used where an
// identifier is expected in a format arg) to use positional arg instead.
// Issue: <https://github.com/rust-lang/rust/issues/122535>.
// identifier is expected in a format arg) to use positional arg instead, with a note to move
// the expression into a local variable.
// Issue: <https://github.com/rust-lang/rust/issues/114108>.
//@ run-rustfix

fn main() {
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/fmt/format-args-non-identifier-diagnostics.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
error: invalid format string: tuple index access isn't supported
--> $DIR/format-args-non-identifier-diagnostics.rs:8:16
--> $DIR/format-args-non-identifier-diagnostics.rs:9:16
|
LL | println!("{x.0}");
| ^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - println!("{x.0}");
Expand Down
1 change: 1 addition & 0 deletions tests/ui/fmt/struct-field-as-captured-argument.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn main() {
let bar = 3;
let _ = format!("{0}", foo.field); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{1} {} {bar}", "aa", foo.field); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{0:value$} {bar}", foo.field, value = 1); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{2} {} {1} {bar}", "aa", "bb", foo.field); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{1} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{1:?} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
Expand Down
1 change: 1 addition & 0 deletions tests/ui/fmt/struct-field-as-captured-argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn main() {
let bar = 3;
let _ = format!("{foo.field}"); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field} {} {bar}", "aa"); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field:value$} {bar}", value = 1); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field} {} {1} {bar}", "aa", "bb"); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field:?} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
Expand Down
30 changes: 25 additions & 5 deletions tests/ui/fmt/struct-field-as-captured-argument.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ error: invalid format string: field access isn't supported
LL | let _ = format!("{foo.field}");
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field}");
Expand All @@ -16,6 +17,7 @@ error: invalid format string: field access isn't supported
LL | let _ = format!("{foo.field} {} {bar}", "aa");
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field} {} {bar}", "aa");
Expand All @@ -25,62 +27,80 @@ LL + let _ = format!("{1} {} {bar}", "aa", foo.field);
error: invalid format string: field access isn't supported
--> $DIR/struct-field-as-captured-argument.rs:13:23
|
LL | let _ = format!("{foo.field:value$} {bar}", value = 1);
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field:value$} {bar}", value = 1);
LL + let _ = format!("{0:value$} {bar}", foo.field, value = 1);
|

error: invalid format string: field access isn't supported
--> $DIR/struct-field-as-captured-argument.rs:14:23
|
LL | let _ = format!("{foo.field} {} {1} {bar}", "aa", "bb");
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field} {} {1} {bar}", "aa", "bb");
LL + let _ = format!("{2} {} {1} {bar}", "aa", "bb", foo.field);
|

error: invalid format string: field access isn't supported
--> $DIR/struct-field-as-captured-argument.rs:14:23
--> $DIR/struct-field-as-captured-argument.rs:15:23
|
LL | let _ = format!("{foo.field} {} {baz}", "aa", baz = 3);
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field} {} {baz}", "aa", baz = 3);
LL + let _ = format!("{1} {} {baz}", "aa", foo.field, baz = 3);
|

error: invalid format string: field access isn't supported
--> $DIR/struct-field-as-captured-argument.rs:15:23
--> $DIR/struct-field-as-captured-argument.rs:16:23
|
LL | let _ = format!("{foo.field:?} {} {baz}", "aa", baz = 3);
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field:?} {} {baz}", "aa", baz = 3);
LL + let _ = format!("{1:?} {} {baz}", "aa", foo.field, baz = 3);
|

error: invalid format string: field access isn't supported
--> $DIR/struct-field-as-captured-argument.rs:16:23
--> $DIR/struct-field-as-captured-argument.rs:17:23
|
LL | let _ = format!("{foo.field:#?} {} {baz}", "aa", baz = 3);
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field:#?} {} {baz}", "aa", baz = 3);
LL + let _ = format!("{1:#?} {} {baz}", "aa", foo.field, baz = 3);
|

error: invalid format string: field access isn't supported
--> $DIR/struct-field-as-captured-argument.rs:17:23
--> $DIR/struct-field-as-captured-argument.rs:18:23
|
LL | let _ = format!("{foo.field:.3} {} {baz}", "aa", baz = 3);
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field:.3} {} {baz}", "aa", baz = 3);
LL + let _ = format!("{1:.3} {} {baz}", "aa", foo.field, baz = 3);
|

error: aborting due to 7 previous errors
error: aborting due to 8 previous errors

Loading