mirror of
https://github.com/allankoechke/rust.git
synced 2026-11-30 11:05:36 +00:00
Rollup merge of #163368 - h-kurashina:fix-156101, r=mejrs
Don't build format string suggestions from `concat!` offsets Fixes rust-lang/rust#156101 When a format string does not come directly from a string literal in the source, e.g. when it is produced by `concat!`, the parser's inner offsets are relative to the expanded string rather than to the source. The primary error span and the `RemoveRawIdent` suggestion already check `is_source_literal` before calling `fmt_span.from_inner`, but the `UsePositional`, `ReorderFormatParameter` and `AddMissingColon` suggestions did not. As a result, these suggestions could point into the middle of a multibyte character and ICE when rendered, or suggest a bogus argument copied from unrelated source text. Only emit them when the format string is a source literal, like the other suggestions do. For example, before this change `UsePositional` took `at!` from the `concat!` invocation as the captured argument, replaced it with `0`, and suggested passing `at!` as an argument: ``` help: consider using a positional formatting argument instead | 2 - format_args!(concat!("{}{a.b}", "")); 2 + format_args!(conc0("{}{a.b}", ""), at!); ``` The crash test is moved to `tests/ui/fmt`, with cases covering each of these suggestions.
This commit is contained in:
@@ -339,7 +339,9 @@ fn make_format_args(
|
||||
parse::Suggestion::UsePositional => {
|
||||
let captured_arg_span =
|
||||
fmt_span.from_inner(InnerSpan::new(err.span.start, err.span.end));
|
||||
if let Ok(arg) = ecx.source_map().span_to_snippet(captured_arg_span) {
|
||||
if is_source_literal
|
||||
&& let Ok(arg) = ecx.source_map().span_to_snippet(captured_arg_span)
|
||||
{
|
||||
let span = match args.unnamed_args().last() {
|
||||
Some(arg) => arg.expr.span,
|
||||
None => fmt_span,
|
||||
@@ -360,17 +362,21 @@ fn make_format_args(
|
||||
}
|
||||
}
|
||||
parse::Suggestion::ReorderFormatParameter(span, replacement) => {
|
||||
let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
|
||||
e.sugg_ =
|
||||
Some(diagnostics::InvalidFormatStringSuggestion::ReorderFormatParameter {
|
||||
span,
|
||||
replacement,
|
||||
});
|
||||
if is_source_literal {
|
||||
let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
|
||||
e.sugg_ =
|
||||
Some(diagnostics::InvalidFormatStringSuggestion::ReorderFormatParameter {
|
||||
span,
|
||||
replacement,
|
||||
});
|
||||
}
|
||||
}
|
||||
parse::Suggestion::AddMissingColon(span) => {
|
||||
let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
|
||||
e.sugg_ =
|
||||
Some(diagnostics::InvalidFormatStringSuggestion::AddMissingColon { span });
|
||||
if is_source_literal {
|
||||
let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
|
||||
e.sugg_ =
|
||||
Some(diagnostics::InvalidFormatStringSuggestion::AddMissingColon { span });
|
||||
}
|
||||
}
|
||||
parse::Suggestion::UseRustDebugPrintingMacro => {
|
||||
// This targets `println!("{=}", x);` and `println!("{0=}", x);`
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
//@ known-bug: #156101
|
||||
fn main() {
|
||||
format_args!(concat!("", "{f:?#}"));
|
||||
}
|
||||
38
tests/ui/fmt/format-args-concat-multibyte-suggestion.rs
Normal file
38
tests/ui/fmt/format-args-concat-multibyte-suggestion.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
// Regression test for #156101.
|
||||
//
|
||||
// When the format string comes from `concat!`, the parser's inner offsets are relative to the
|
||||
// expanded string, not to the source, so they must not be turned into spans in the source.
|
||||
// Before the fix, suggestions built from those offsets went wrong in two ways:
|
||||
//
|
||||
// - If the span landed inside a multibyte character, rendering the suggestion caused an ICE.
|
||||
// The `ReorderFormatParameter` and `AddMissingColon` cases below are padded so that the span
|
||||
// falls inside `é` (2 bytes) or `` (4 bytes).
|
||||
// - If the span landed on unrelated source text, `UsePositional` suggested a bogus argument
|
||||
// copied from that text.
|
||||
//
|
||||
// After the fix, no suggestion is emitted for these format strings.
|
||||
|
||||
fn main() {
|
||||
// The original reproducer from the issue.
|
||||
format_args!(concat!("", "{f:?#}"));
|
||||
//~^ ERROR invalid format string
|
||||
|
||||
// `Suggestion::ReorderFormatParameter`
|
||||
format_args!(concat!("é", "aaa{:?#}"), 1);
|
||||
//~^ ERROR invalid format string
|
||||
format_args!(concat!("", "a{:?#}"), 1);
|
||||
//~^ ERROR invalid format string
|
||||
|
||||
// `Suggestion::AddMissingColon`
|
||||
format_args!(concat!("é", "aaaa{x?}"));
|
||||
//~^ ERROR invalid format string
|
||||
format_args!(concat!("", "aa{x?}"));
|
||||
//~^ ERROR invalid format string
|
||||
|
||||
// `Suggestion::UsePositional`: these did not ICE, but before the fix they suggested a bogus
|
||||
// argument taken from the `concat!` invocation instead of from the format string.
|
||||
format_args!(concat!("{}{a.b}", ""));
|
||||
//~^ ERROR invalid format string
|
||||
format_args!(concat!("{}{a.0}", ""));
|
||||
//~^ ERROR invalid format string
|
||||
}
|
||||
52
tests/ui/fmt/format-args-concat-multibyte-suggestion.stderr
Normal file
52
tests/ui/fmt/format-args-concat-multibyte-suggestion.stderr
Normal file
@@ -0,0 +1,52 @@
|
||||
error: invalid format string: expected `}`, found `#`
|
||||
--> $DIR/format-args-concat-multibyte-suggestion.rs:17:18
|
||||
|
|
||||
LL | format_args!(concat!("", "{f:?#}"));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ expected `'}'` in format string
|
||||
|
||||
error: invalid format string: expected `}`, found `#`
|
||||
--> $DIR/format-args-concat-multibyte-suggestion.rs:21:18
|
||||
|
|
||||
LL | format_args!(concat!("é", "aaa{:?#}"), 1);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected `'}'` in format string
|
||||
|
||||
error: invalid format string: expected `}`, found `#`
|
||||
--> $DIR/format-args-concat-multibyte-suggestion.rs:23:18
|
||||
|
|
||||
LL | format_args!(concat!("", "a{:?#}"), 1);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ expected `'}'` in format string
|
||||
|
||||
error: invalid format string: expected `}`, found `?`
|
||||
--> $DIR/format-args-concat-multibyte-suggestion.rs:27:18
|
||||
|
|
||||
LL | format_args!(concat!("é", "aaaa{x?}"));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected `:` before `?` to format with `Debug` in format string
|
||||
|
|
||||
= note: to print `{`, you can escape it using `{{`
|
||||
|
||||
error: invalid format string: expected `}`, found `?`
|
||||
--> $DIR/format-args-concat-multibyte-suggestion.rs:29:18
|
||||
|
|
||||
LL | format_args!(concat!("", "aa{x?}"));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ expected `:` before `?` to format with `Debug` in format string
|
||||
|
|
||||
= note: to print `{`, you can escape it using `{{`
|
||||
|
||||
error: invalid format string: field access isn't supported
|
||||
--> $DIR/format-args-concat-multibyte-suggestion.rs:34:18
|
||||
|
|
||||
LL | format_args!(concat!("{}{a.b}", ""));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ not supported in format string
|
||||
|
|
||||
= note: consider moving this expression to a local variable and then using the local here instead
|
||||
|
||||
error: invalid format string: tuple index access isn't supported
|
||||
--> $DIR/format-args-concat-multibyte-suggestion.rs:36:18
|
||||
|
|
||||
LL | format_args!(concat!("{}{a.0}", ""));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ not supported in format string
|
||||
|
|
||||
= note: consider moving this expression to a local variable and then using the local here instead
|
||||
|
||||
error: aborting due to 7 previous errors
|
||||
|
||||
Reference in New Issue
Block a user