Skip to content

Generic parameter definitions are not cfg-stripped before #[derive] macros are invoked #75930

Closed
@Aaron1011

Description

@Aaron1011

The following code (repository at https://github.com/Aaron1011/derive-bug):

// lib.rs
use proc_macro::TokenStream;

#[proc_macro_derive(MyDerive)]
pub fn my_derive(input: TokenStream) -> TokenStream {
    println!("Derive input: {}", input);
    TokenStream::new()
}


// main.rs

#[derive(derive_bug::MyDerive)]
struct Foo<#[cfg(FALSE)] T> {
    #[cfg(FALSE)] field: T,
    val: String,
    array: [u8; {
        #[cfg(FALSE)] struct Bar;
        0
    }]
}

fn main() {}

produces the following output:

Derive input: struct Foo < #[cfg(FALSE)] T > { val : String, array : [u8 ; { 0 }], }

Note how all #[cfg(FALSE)] targets are stripped, except for the generic parameter T. This seems quite inconsistent.

Tested on rustc 1.47.0-nightly (de521cbb3 2020-08-21)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-proc-macrosArea: Procedural macrosC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions