Skip to content

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

Closed

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)

Activity

  1. added
    A-attributesArea: Attributes (`#[…]`, `#![…]`)
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    C-bugCategory: This is a bug.
    on Aug 26, 2020
  2. petrochenkov commented on Aug 26, 2020

    @petrochenkov
    Contributor

    The relevant code is impl<'a> MutVisitor for StripUnconfigured<'a>, it's quite possible that cfgs are not expanded in a bunch of other positions as well. (I never got to auditing that code.)

  3. added 2 commits that reference this issue on Aug 30, 2020
    de5646d
    69dc972
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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