Skip to content

Non-items dropped in custom derive #48038

Open
@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

Labels

A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-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