Skip to content

Provide #[diagnostic::on_incomplete_macro_args] to customize macro call errors #152494

@estebank

Description

@estebank

In #152493 there's an example of a built-in macro where not passing enough arguments to the macro call produces

error: unexpected end of macro invocation
   --> src/lib.rs:5:28
    |
  5 |             S => write!("S"),
    |                            ^ missing tokens in macro arguments
    |
note: while trying to match `,`
   --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/macros/mod.rs:611:15
    |
611 |     ($dst:expr, $($arg:tt)*) => {
    |               ^

Both built-in and third-party macros could do with a mechanism to customize the error being emitted when a specific macro arm doesn't match and when no macro arm matches. This would allow for crate authors to emit additional information guiding users towards the correct intended usage.

This could be as simple as

#[diagnostic::on_incomplete_macro_args(
    note = "this macro accepts a `std::fmt::Formatter` writer, a format string, and a list of arguments to be used in the format string",
)]
macro_rules! write {
    ($dst:expr, $($arg:tt)*) => { ... };
}

Although ideally we should be able to annotate individual match arms (which might be a change in the grammar?).

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-diagnostic-infraDiagnostics: Issues that affect all diagnostics, or relate to the diagnostic machinery itself.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions