diff --git a/compiler/rustc_parse_format/src/lib.rs b/compiler/rustc_parse_format/src/lib.rs index 2338268a874f0..c7ffc1e5f3681 100644 --- a/compiler/rustc_parse_format/src/lib.rs +++ b/compiler/rustc_parse_format/src/lib.rs @@ -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, @@ -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, diff --git a/tests/ui/fmt/format-args-non-identifier-diagnostics.fixed b/tests/ui/fmt/format-args-non-identifier-diagnostics.fixed index bd4db9480674c..a9a10f0e7eb9c 100644 --- a/tests/ui/fmt/format-args-non-identifier-diagnostics.fixed +++ b/tests/ui/fmt/format-args-non-identifier-diagnostics.fixed @@ -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: . +// identifier is expected in a format arg) to use positional arg instead, with a note to move +// the expression into a local variable. +// Issue: . //@ run-rustfix fn main() { diff --git a/tests/ui/fmt/format-args-non-identifier-diagnostics.rs b/tests/ui/fmt/format-args-non-identifier-diagnostics.rs index aab705341f71d..f0f714a9af913 100644 --- a/tests/ui/fmt/format-args-non-identifier-diagnostics.rs +++ b/tests/ui/fmt/format-args-non-identifier-diagnostics.rs @@ -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: . +// identifier is expected in a format arg) to use positional arg instead, with a note to move +// the expression into a local variable. +// Issue: . //@ run-rustfix fn main() { diff --git a/tests/ui/fmt/format-args-non-identifier-diagnostics.stderr b/tests/ui/fmt/format-args-non-identifier-diagnostics.stderr index af6bb58071fff..4555ce7c8b705 100644 --- a/tests/ui/fmt/format-args-non-identifier-diagnostics.stderr +++ b/tests/ui/fmt/format-args-non-identifier-diagnostics.stderr @@ -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}"); diff --git a/tests/ui/fmt/struct-field-as-captured-argument.fixed b/tests/ui/fmt/struct-field-as-captured-argument.fixed index 0da40737354f7..65e4dd2d58c56 100644 --- a/tests/ui/fmt/struct-field-as-captured-argument.fixed +++ b/tests/ui/fmt/struct-field-as-captured-argument.fixed @@ -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 diff --git a/tests/ui/fmt/struct-field-as-captured-argument.rs b/tests/ui/fmt/struct-field-as-captured-argument.rs index 325b4e3a21878..13087cceb671b 100644 --- a/tests/ui/fmt/struct-field-as-captured-argument.rs +++ b/tests/ui/fmt/struct-field-as-captured-argument.rs @@ -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 diff --git a/tests/ui/fmt/struct-field-as-captured-argument.stderr b/tests/ui/fmt/struct-field-as-captured-argument.stderr index 388c14f932bbc..23576d6a462ca 100644 --- a/tests/ui/fmt/struct-field-as-captured-argument.stderr +++ b/tests/ui/fmt/struct-field-as-captured-argument.stderr @@ -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}"); @@ -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"); @@ -25,9 +27,23 @@ 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"); @@ -35,11 +51,12 @@ 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); @@ -47,11 +64,12 @@ 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); @@ -59,11 +77,12 @@ 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); @@ -71,16 +90,17 @@ 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