Skip to content

Non-items emitted by custom derive get discarded #48038

@antoyo

Description

@antoyo

Hi.
It looks like when you generate non-items in a custom derive, they are silently dropped.
For instance, with the following custom derive:

#[proc_macro_derive(HelloWorld)]
pub fn hello_world(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).unwrap();
    let gen = impl_hello_world(&ast);
    gen.into()
}

fn impl_hello_world(ast: &syn::DeriveInput) -> quote::Tokens {
    let name = &ast.ident;
    quote! {{
        fn hello_world() {
            println!("Hello, World! My name is {}", stringify!(#name));
        }
        hello_world();
    }}
}

and the following code:

fn main() {
    #[derive(HelloWorld)]
    struct _Toto {
    }
}

nothing is printed on the screen.
So, is it normal that non-items are dropped?
Could they be generated as well if it makes sense (like in a struct that is defined within a function)?
If not, if would be nice to have a least a warning when this happen.
Thanks.

cc @jseyfried @alexcrichton @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-proc-macrosArea: Procedural macrosC-bugCategory: This is a bug.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions