Skip to content

Input to a #[proc_macro_derive] sees underlying derives on the item #85854

Closed
@MihirLuthra

Description

@MihirLuthra

Input to #[proc_macro_derive] is able to see underlying derive attributes. This is not supposed to happen as per the change in version 1.17.0. A pull request was merged to remove this effect.

But version 1.52.0 onwards, underlying attributes are visible again to derive and this is not mentioned in RELEASES.md.

Code

Example code is available here: https://github.com/MihirLuthra/derive_issue_example

In the following code below, #[derive(Abc)] generates a function named print_all_attributes() which prints all derive attributes visible to it.

// main.rs

use abc_derive::Abc;

#[derive(Abc)]
#[derive(Debug, PartialEq, Eq)]
struct Xyz;

fn main() {
    print_all_attributes();
}

#[derive(Abc)] is defined as:

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(Abc)]
pub fn abc_derive(input: TokenStream) -> TokenStream {
    let derive_input = parse_macro_input!(input as DeriveInput);

    let attrs = &derive_input.attrs;

    (quote! {
        fn print_all_attributes() {
            println!(
                stringify!(#( #attrs )*)
            );
        }
    })
    .into()
}

Output on Version 1.51.0

Ouputs a blank line:


rustc --version --verbose:

rustc 1.51.0 (2fd73fabe 2021-03-23)
binary: rustc
commit-hash: 2fd73fabe469357a12c2c974c140f67e7cdd76d0
commit-date: 2021-03-23
host: x86_64-apple-darwin
release: 1.51.0
LLVM version: 11.0.1

Output on Version 1.52.1

#[derive(Debug, PartialEq, Eq)]

rustc --version --verbose:

rustc 1.52.1 (9bc8c42bb 2021-05-09)
binary: rustc
commit-hash: 9bc8c42bb2f19e745a63f3445f1ac248fb015e53
commit-date: 2021-05-09
host: x86_64-apple-darwin
release: 1.52.1
LLVM version: 12.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-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