Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest flipping reversed ?#/?x/?X in format specifiers #131004

Closed
wants to merge 1 commit into from

Conversation

deniskilseev
Copy link

@deniskilseev deniskilseev commented Sep 29, 2024

Edits format parsing to accomodate for helpful messages in case '#?' is incorrectly ordered or '?x', '?X' is written.

Fixes #129966.

…e '?#', '?x', '?X' is written in formatted string.
@rustbot
Copy link
Collaborator

rustbot commented Sep 29, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @jieyouxu (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 29, 2024
@jieyouxu
Copy link
Member

(Note that the issue number in PR description is wrong, it's #129966 not 12996.)

@jieyouxu jieyouxu changed the title Fixes issue 12996. Edits parsing to accomodate helpful message in format. Fixes issue 129966. Edits parsing to accomodate helpful message in format. Sep 29, 2024
Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR! The implementation for reordering format parameters look mostly good, however the remove format parameter suggestion for ?X and ?x seems strange from a user perspective. I have also some other suggestions, we'll work them out together.

@@ -551,6 +551,25 @@ pub(crate) enum InvalidFormatStringSuggestion {
#[primary_span]
span: Span,
},
#[multipart_suggestion(
builtin_macros_format_reorder,
style = "tool-only",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem [REV-TOOL 1/2]: this should not be style = tool-only, the user will still want to see the suggestion in the absence of rustfix and such. Since it's hard to tell that #? got flipped to ?#, I suggest style = "verbose" instead so you get diagnostic like:

help: did you mean to write `x?`?
   |
LL -     let _ = format!("{f:?x}");
LL +     let _ = format!("{f:x?}");

},
#[multipart_suggestion(
builtin_macros_format_remove,
style = "tool-only",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem [REV-TOOL 2/2]: ditto here.

Comment on lines +569 to +572
RemoveCharacter {
#[suggestion_part(code = "")]
span: Span,
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: This removal suggestion seems strange to me. Looking at the test, we currently suggest

help: consider removing the format specifier instead
   |
LL -     let _ = format!("{f:?X}");
LL +     let _ = format!("{f:?}");

Wouldn't we want to suggest reordering for this too? As in,

help: did you mean to write `x?`?
   |
LL -     let _ = format!("{f:?x}");
LL +     let _ = format!("{f:x?}");

builtin_macros_format_remove_raw_ident = remove the `r#`

builtin_macros_format_reorder = consider reordering the format specifier instead
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: I think it's clearer if we outright spell out something like

did you mean to write x??

or #? or X?. Format specifier I think refers to the whole {...} thing.

Comment on lines +305 to +314
parse::Suggestion::ReorderFormat(span) => {
let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
if let Ok(original_string) = ecx.source_map().span_to_snippet(span) {
let reversed_string: String = original_string.chars().rev().collect();
e.sugg_ = Some(errors::InvalidFormatStringSuggestion::ReorderFormat {
span: span,
string: reversed_string,
});
}
}
Copy link
Member

@jieyouxu jieyouxu Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem: we should not be generically reversing arbitrary sequence of formatting parameter fragments here when we already know what we saw. Consider just passing the span of e.g. ?X + a &'static str replacement e.g. X?: we should only be emitting the suggestion for ?X -> X?, ?x -> x?, ?# -> #? reorderings. Then, we can just replace the span with the "correct" ordered string.

// Test checks for the help messages in the error output/
//@ check-run-results

struct Foo(u8, u8);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem [REV-RUSTFIX 2/4]: if we use run-rustfix, some lints fire like dead_code and checks for missing debug impl on Foo.

#![allow(dead_code)]

#[derive(Debug)]
struct Foo(u8, u8);

fn main() {
let f = Foo(1, 2);
format!("{f:?#}");
//~^ ERROR invalid format string: unknown format identifier '?#'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: these UI test error annotations do not care about preceding indent whitespace, they match on a line-by-line basis. Usually we ident them in alignment with surrounding context as suitable.

Suggested change
//~^ ERROR invalid format string: unknown format identifier '?#'
//~^ ERROR invalid format string: unknown format identifier '?#'

fn main() {
let f = Foo(1, 2);
format!("{f:?#}");
//~^ ERROR invalid format string: unknown format identifier '?#'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem [REV-RUSTFIX 3/4]: run-rustfix will reveal that format!() produces a value that is marked as #[must_use], so we need to

let _ = format!("{f:?x}");

fn main() {
let f = Foo(1, 2);
format!("{f:?#}");
//~^ ERROR invalid format string: unknown format identifier '?#'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem [REV-RUSTFIX 4/4]: since we would like the help message to be present even in absence of tools like rustfix, we should also match help messages:

    let _ = format!("{f:?x}");
    //~^ ERROR invalid format string: unknown format identifier '?x'
    //~| HELP did you mean to write `x?`?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion [REV-RUSTFIX OVERALL]: combining the remarks, we can write the test as something like

//! Check that we provide suggestions for wrongly ordered format parameters.
//!
//! Issue: <https://github.com/rust-lang/rust/issues/129966>.

//@ run-rustfix

#![allow(dead_code)]

#[derive(Debug)]
struct Foo(u8, u8);

fn main() {
    let f = Foo(1, 2);
    let _ = format!("{f:?#}");
    //~^ ERROR invalid format string: unknown format identifier '?#'
    //~| HELP did you mean to write `#?`?
    let _ = format!("{f:?x}");
    //~^ ERROR invalid format string: unknown format identifier '?x'
    //~| HELP did you mean to write `x?`?
    let _ = format!("{f:?X}");
    //~^ ERROR invalid format string: unknown format identifier '?X'
    //~| HELP did you mean to write `X?`?
}

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 29, 2024
@jieyouxu jieyouxu changed the title Fixes issue 129966. Edits parsing to accomodate helpful message in format. Suggest flipping reversed #?/?x/?X in format specifiers Oct 10, 2024
@jieyouxu jieyouxu changed the title Suggest flipping reversed #?/?x/?X in format specifiers Suggest flipping reversed ?#/?x/?X in format specifiers Oct 10, 2024
@alex-semenyuk
Copy link
Member

@deniskilseev
From wg-triage. Do you have any updates on comments for this PR?

@alex-semenyuk
Copy link
Member

@deniskilseev
From wg-triage. Closed this PR due to inactivity. Feel free to reopen or raised new one. Thanks for your efforts.

Zalathar added a commit to Zalathar/rust that referenced this pull request Jan 1, 2025
…p-message, r=estebank

add suggestion for wrongly ordered format parameters

Add suggestion for wrongly ordered format parameters like `?#`.

Supersedes rust-lang#131004
Fix rust-lang#129966
Zalathar added a commit to Zalathar/rust that referenced this pull request Jan 1, 2025
…p-message, r=estebank

add suggestion for wrongly ordered format parameters

Add suggestion for wrongly ordered format parameters like `?#`.

Supersedes rust-lang#131004
Fix rust-lang#129966
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 1, 2025
Rollup merge of rust-lang#134877 - DavisRayM:129966-format-string-help-message, r=estebank

add suggestion for wrongly ordered format parameters

Add suggestion for wrongly ordered format parameters like `?#`.

Supersedes rust-lang#131004
Fix rust-lang#129966
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reversed order of ?# in format string gives unhelpful error message
4 participants