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

Better diagnostics for proc macro attributes #102923

Closed
afetisov opened this issue Oct 11, 2022 · 1 comment · Fixed by #106407
Closed

Better diagnostics for proc macro attributes #102923

afetisov opened this issue Oct 11, 2022 · 1 comment · Fixed by #106407
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@afetisov
Copy link

afetisov commented Oct 11, 2022

Given the following code:

#[proc_macro]
pub fn mac() {}

#[proc_macro_attribute]
pub fn attrib() {}

#[proc_macro_derive(D)]
pub fn derive() {}

The current output is:

error[E0593]: function is expected to take 1 argument, but it takes 0 arguments
   --> mac/src/lib.rs:9:1
    |
9   | pub fn mac() {}
    | ------------^^^
    | |
    | expected function that takes 1 argument
    | takes 0 arguments
    |
note: required by a bound in `ProcMacro::bang`
   --> /home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/proc_macro/src/bridge/client.rs:506:22
    |
506 |         expand: impl Fn(crate::TokenStream) -> crate::TokenStream + Copy,
    |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `ProcMacro::bang`

error[E0593]: function is expected to take 2 arguments, but it takes 0 arguments
   --> mac/src/lib.rs:12:1
    |
12  | pub fn attrib() {}
    | -------------^^^
    | |
    | expected function that takes 2 arguments
    | takes 0 arguments
    |
note: required by a bound in `ProcMacro::attr`
   --> /home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/proc_macro/src/bridge/client.rs:499:22
    |
499 |         expand: impl Fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream + Copy,
    |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `ProcMacro::attr`

error[E0593]: function is expected to take 1 argument, but it takes 0 arguments
   --> mac/src/lib.rs:15:1
    |
15  | pub fn derive() {}
    | ---------------^^^
    | |
    | expected function that takes 1 argument
    | takes 0 arguments
    |
note: required by a bound in `ProcMacro::custom_derive`
   --> /home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/proc_macro/src/bridge/client.rs:492:22
    |
492 |         expand: impl Fn(crate::TokenStream) -> crate::TokenStream + Copy,
    |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `ProcMacro::custom_derive`

The compiler knows the meaning of #[proc_macro*] attributes, so it could provide more helpful error messages. The ProcMacro::custom_derive etc types are not part of the API that the users normally work with, so listing them is unhelpful. Since the signatures of proc macro functions are fixed, the compiler can directly list them:

error[E0???]: function is expected to take 1 argument, but it takes 0 arguments
   --> mac/src/lib.rs:9:1
    |
9   | pub fn mac() {}
    | ----------^^^
    | |
    | functions annotated with `#[proc_macro]` must have the signature 
    |     `pub fn mac(item: TokenStream) -> TokenStream`

error[E0???]: function is expected to take 2 arguments, but it takes 0 arguments
   --> mac/src/lib.rs:12:1
    |
12  | pub fn attrib() {}
    | ----------^^^
    | |
    | functions annotated with `#[proc_macro_attribute]` must have the signature 
    |     `pub fn attrib(attr: TokenStream, item: TokenStream) -> TokenStream`

error[E0???]: function is expected to take 1 argument, but it takes 0 arguments
   --> mac/src/lib.rs:15:1
    |
15  | pub fn derive() {}
    | ----------^^^
    | |
    | functions annotated with `#[proc_macro_derive(..)]` must have the signature 
    |     `pub fn derive(item: TokenStream) -> TokenStream`

Note that the signature includes the function name and argument names, allowing the proper signature to be copy-pasted in code. This also disambiguates arguments for attribute proc macros, where there may be confusion w.r.t. order of arguments.

@afetisov afetisov added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 11, 2022
@mejrs
Copy link
Contributor

mejrs commented Nov 17, 2022

@rustbot claim

compiler-errors added a commit to compiler-errors/rust that referenced this issue Jan 18, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 23, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 25, 2023
@bors bors closed this as completed in 8ae5116 Jan 26, 2023
thomcc pushed a commit to tcdi/postgrestd that referenced this issue May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants