Skip to content

Conversation

@Unique-Usman
Copy link

@Unique-Usman Unique-Usman commented Jan 7, 2026

Detect the {ident?} pattern where ? is immediately followed by } and emit a clearer diagnostic explaining that : is required for Debug formatting. This avoids falling back to a generic “invalid format string” error and adds a targeted UI test for the case.

@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 Jan 7, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 7, 2026

r? @SparrowLii

rustbot has assigned @SparrowLii.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@Unique-Usman
Copy link
Author

r? @estebank

@rustbot rustbot assigned estebank and unassigned SparrowLii Jan 7, 2026
@Unique-Usman
Copy link
Author

This pr serves as an efforts toward issues 145718.

Following up with our conversation @estebank, I was working on the issue but, I saw this and I thought on working on it while i can keep working on other part of the issue.

If you have a

fn main() {
    let x = 4;
    println!("{x?}, world!",);
}

Someone will get this error

error: invalid format string: expected `}`, found `?`
 --> testing.rs:3:17
  |
3 |     println!("{x?}, world!",);
  |               - ^ expected `}` in format string
  |               |
  |               because of this opening brace
  |
  = note: if you intended to print `{`, you can escape it using `{{`

error: aborting due to 1 previous error

But, I believe, missing colon : is a better diagnostic here.

My proposed improvement was to sort of combine the current diagnostic error with the missing colon. After this pr, this is what will be output which I believe is a better diagnostic.

error: invalid format string: expected `}`, found `?` or missing `:` before `?`
 --> ../testing.rs:3:17
  |
3 |     println!("{x?}, world!",);
  |                 ^ expected `}` in format string or missing `:` before `?` in format string
  |
  = note: to print `{`, you can escape it using `{{`, to format with `Debug`, use `:?`

error: aborting due to 1 previous error

I am also thinking having only missing colon is okay here, what do you think ? We can also improve the above diagnostic message here.

@estebank estebank changed the title rustc_parse_format: improve error for missing : before ? in forma… rustc_parse_format: improve error for missing : before ? in format args Jan 7, 2026
@Unique-Usman
Copy link
Author

Unique-Usman commented Jan 7, 2026

Thanks for the review @estebank, made the requested change.

…t args

Detect the `{ident?}` pattern where `?` is immediately followed by `}` and emit
a clearer diagnostic explaining that `:` is required for Debug formatting.
This avoids falling back to a generic “invalid format string” error and adds
a targeted UI test for the case.

Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
@estebank
Copy link
Contributor

estebank commented Jan 8, 2026

@bors r+

@rust-bors rust-bors bot added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jan 8, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 8, 2026

📌 Commit b0e65da has been approved by estebank

It is now in the queue for this repository.

@rust-bors rust-bors bot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 8, 2026
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 9, 2026
rustc_parse_format: improve error for missing `:` before `?` in format args

Detect the `{ident?}` pattern where `?` is immediately followed by `}` and emit a clearer diagnostic explaining that `:` is required for Debug formatting. This avoids falling back to a generic “invalid format string” error and adds a targeted UI test for the case.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 9, 2026
rustc_parse_format: improve error for missing `:` before `?` in format args

Detect the `{ident?}` pattern where `?` is immediately followed by `}` and emit a clearer diagnostic explaining that `:` is required for Debug formatting. This avoids falling back to a generic “invalid format string” error and adds a targeted UI test for the case.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 9, 2026
rustc_parse_format: improve error for missing `:` before `?` in format args

Detect the `{ident?}` pattern where `?` is immediately followed by `}` and emit a clearer diagnostic explaining that `:` is required for Debug formatting. This avoids falling back to a generic “invalid format string” error and adds a targeted UI test for the case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

4 participants