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

Confusing diagnostic message for a derive-macro implementation bug #107977

Open
kiscad opened this issue Feb 13, 2023 · 1 comment
Open

Confusing diagnostic message for a derive-macro implementation bug #107977

kiscad opened this issue Feb 13, 2023 · 1 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.

Comments

@kiscad
Copy link
Contributor

kiscad commented Feb 13, 2023

Code

// the code is from THE BOOK
// https://doc.rust-lang.org/book/ch19-06-macros.html#how-to-write-a-custom-derive-macro
// but I forgot to append `!` to `stringify`, in the `hello_macro_derive/src/lib.rs/impl_hello_macro`.
// However, the `hello_macro_derive` crate was built successfully, but the `pancake` crate,
// which uses the derive-macro, was built failed with a confusing diagnostic message.

//
// pancake/src/main.rs
//
use hello_macro::HelloMacro;
use hello_macro_derive::HelloMacro;

#[derive(HelloMacro)]
struct Pancake;

fn main() {
    Pancake::hello_macro();
}

//
// hello_macro/hello_macro_derive/src/lib.rs
//
use proc_macro::TokenStream;
use quote::quote;
use syn;

#[proc_macro_derive(HelloMacro)]
pub fn hello_macro_derive(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).unwrap();
    impl_hello_macro(&ast)
}

fn impl_hello_macro(ast: &syn::DeriveInput) -> TokenStream {
    let name = &ast.ident;
    let gen = quote! {
        impl HelloMacro for #name {
            fn hello_macro() {
                println!("Hello Macro! This is {}", stringify(#name))
            }
        }
    };
    gen.into()
}

Current output

error[E0423]: expected function, found macro `stringify`
 --> src/main.rs:4:10
  |
4 | #[derive(HelloMacro)]
  |          ^^^^^^^^^^ not a function
  |
  = note: this error originates in the derive macro `HelloMacro` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use `!` to invoke the macro
  |
4 | #[derive(HelloMacro!)]
  |                    +

For more information about this error, try `rustc --explain E0423`.
error: could not compile `pancakes` due to previous error

Desired output

`hello_macro_derive` crate should be failed to build due to `stringify`, since this does not pass the compilation in the normal crate.

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

@kiscad kiscad 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 Feb 13, 2023
@chenyukang
Copy link
Member

Similar issue #107113

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 16, 2023
Do not provide suggestions when the spans come from expanded code that doesn't point at user code

Hide invalid proc-macro suggestions and track spans
coming from proc-macros pointing at attribute.

Effectively, unless the proc-macro keeps user spans,
suggestions will not be produced for the code they
produce.

r? `@ghost`

Fix rust-lang#107113, fix rust-lang#107976, fix rust-lang#107977, fix rust-lang#108748, fix rust-lang#106720, fix rust-lang#90557.

Could potentially address rust-lang#50141, rust-lang#67373, rust-lang#55146, rust-lang#78862, rust-lang#74043, rust-lang#88514, rust-lang#83320, rust-lang#91520, rust-lang#104071. CC rust-lang#50122, rust-lang#76360.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 17, 2023
Do not provide suggestions when the spans come from expanded code that doesn't point at user code

Hide invalid proc-macro suggestions and track spans
coming from proc-macros pointing at attribute.

Effectively, unless the proc-macro keeps user spans,
suggestions will not be produced for the code they
produce.

r? ``@ghost``

Fix rust-lang#107113, fix rust-lang#107976, fix rust-lang#107977, fix rust-lang#108748, fix rust-lang#106720, fix rust-lang#90557.

Could potentially address rust-lang#50141, rust-lang#67373, rust-lang#55146, rust-lang#78862, rust-lang#74043, rust-lang#88514, rust-lang#83320, rust-lang#91520, rust-lang#104071. CC rust-lang#50122, rust-lang#76360.
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
2 participants