Skip to content

rustc_parse_format: improve error for missing : before ? in format args - #150765

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
Unique-Usman:ua/missing-colon
Jan 11, 2026
Merged

rustc_parse_format: improve error for missing : before ? in format args#150765
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
Unique-Usman:ua/missing-colon

Conversation

@Unique-Usman

@Unique-UsmanUnique-Usman commented Jan 7, 2026

Copy link
Copy Markdown
Contributor

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.

@rustbotrustbot 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
Copy Markdown
Collaborator

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
Copy Markdown
ContributorAuthor

r? @estebank

@rustbotrustbot assigned estebank and unassigned SparrowLiiJan 7, 2026
@Unique-Usman

Copy link
Copy Markdown
ContributorAuthor

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.

@estebankestebank changed the title rustc_parse_format: improve error for missing : before ? in forma…rustc_parse_format: improve error for missing : before ? in format argsJan 7, 2026
Comment threadcompiler/rustc_parse_format/src/lib.rs Outdated
Comment threadcompiler/rustc_parse_format/src/lib.rs Outdated
Comment threadcompiler/rustc_parse_format/src/lib.rs Outdated
Comment threadcompiler/rustc_parse_format/src/lib.rs Outdated
Comment threadcompiler/rustc_parse_format/src/lib.rs Outdated
@Unique-Usman

Unique-Usman commented Jan 7, 2026

Copy link
Copy Markdown
ContributorAuthor

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
Copy Markdown
Contributor

@bors r+

@rust-borsrust-borsBot 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

rust-borsBot commented Jan 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit b0e65da has been approved by estebank

It is now in the queue for this repository.

@rust-borsrust-borsBot 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.
This was referenced Jan 9, 2026
rust-borsBot added a commit that referenced this pull request Jan 10, 2026
Rollup of 11 pull requests
Successful merges:
- #150269 (Remove inactive nvptx maintainer)
- #150713 (mgca: Type-check fields of struct expr const args)
- #150765 (rustc_parse_format: improve error for missing `:` before `?` in format args)
- #150847 (Fix broken documentation links to SipHash)
- #150867 (rustdoc_json: Remove one call to `std::mem::take` in `after_krate`)
- #150872 (Fix some loop block coercion diagnostics)
- #150874 (Ignore `rustc-src-gpl` in fast try builds)
- #150875 (Refactor artifact keep mode in bootstrap)
- #150876 (Mention that `rustc_codegen_gcc` is a subtree in `rustc-dev-guide`)
- #150882 (Supress unused_parens lint for guard patterns)
- #150884 (Update bors email in CI postprocessing step)
Failed merges:
- #150869 (Emit error instead of delayed bug when meeting mismatch type for const tuple)
r? @ghost
rust-borsBot added a commit that referenced this pull request Jan 10, 2026
Rollup of 11 pull requests
Successful merges:
- #150269 (Remove inactive nvptx maintainer)
- #150713 (mgca: Type-check fields of struct expr const args)
- #150765 (rustc_parse_format: improve error for missing `:` before `?` in format args)
- #150847 (Fix broken documentation links to SipHash)
- #150867 (rustdoc_json: Remove one call to `std::mem::take` in `after_krate`)
- #150872 (Fix some loop block coercion diagnostics)
- #150874 (Ignore `rustc-src-gpl` in fast try builds)
- #150875 (Refactor artifact keep mode in bootstrap)
- #150876 (Mention that `rustc_codegen_gcc` is a subtree in `rustc-dev-guide`)
- #150882 (Supress unused_parens lint for guard patterns)
- #150884 (Update bors email in CI postprocessing step)
Failed merges:
- #150869 (Emit error instead of delayed bug when meeting mismatch type for const tuple)
r? @ghost
@rust-bors
rust-borsBot merged commit a8d66d4 into rust-lang:mainJan 11, 2026
11 checks passed
@rustbotrustbot added this to the 1.94.0 milestone Jan 11, 2026
rust-timer added a commit that referenced this pull request Jan 11, 2026
Rollup merge of #150765 - ua/missing-colon, r=estebank
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.
github-actionsBot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Jan 12, 2026
Rollup of 11 pull requests
Successful merges:
- rust-lang/rust#150269 (Remove inactive nvptx maintainer)
- rust-lang/rust#150713 (mgca: Type-check fields of struct expr const args)
- rust-lang/rust#150765 (rustc_parse_format: improve error for missing `:` before `?` in format args)
- rust-lang/rust#150847 (Fix broken documentation links to SipHash)
- rust-lang/rust#150867 (rustdoc_json: Remove one call to `std::mem::take` in `after_krate`)
- rust-lang/rust#150872 (Fix some loop block coercion diagnostics)
- rust-lang/rust#150874 (Ignore `rustc-src-gpl` in fast try builds)
- rust-lang/rust#150875 (Refactor artifact keep mode in bootstrap)
- rust-lang/rust#150876 (Mention that `rustc_codegen_gcc` is a subtree in `rustc-dev-guide`)
- rust-lang/rust#150882 (Supress unused_parens lint for guard patterns)
- rust-lang/rust#150884 (Update bors email in CI postprocessing step)
Failed merges:
- rust-lang/rust#150869 (Emit error instead of delayed bug when meeting mismatch type for const tuple)
r? @ghost
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

@Unique-Usman@rustbot@estebank@SparrowLii