Skip to content

proc_macro_derive(attributes(path::to)) both does and doesn't work #55168

Closed

Description

@CAD97
// crate `derive`
// lib.proc-macro = true
#[proc_macro_derive(WellThen, attributes(well::then))]
pub fn derive(i: ::proc_macro::TokenStream) -> ::proc_macro::TokenStream {
    i
}

// crate `user`
use derive::WellThen;
#[derive(WellThen)]
#[well::then]
struct A;
error[E0433]: failed to resolve. Use of undeclared type or module `well`
  --> user\main.rs:3:3
   |
 3 | #[well::then]
   |   ^^^^ Use of undeclared type or module `well`

I would expect either for this to work or for #[proc_macro_derive(attributes(..))] to reject a path and only accept an ident, as specifying a path here doesn't work.

Activity

  1. petrochenkov commented on Oct 18, 2018

    @petrochenkov
    Contributor

    This may be an accidental regression from #50030, similarly to #53489.

    This cannot work without significant work and introducing an entirely new entity into name resolution ("derive helper modules" or something), so #[proc_macro_derive(attributes(..))] needs to validate its arguments harder and reject non-identifier paths.

  2. added
    A-attributesArea: Attributes (`#[…]`, `#![…]`)
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    C-bugCategory: This is a bug.
    on Oct 18, 2018
  3. shepmaster commented on Feb 27, 2019

    @shepmaster
    Member

    reject non-identifier paths

    I don't think you can do this, as these work for attributes within the type on stable 1.32:

    use repro_derive::Example;
    
    #[derive(Example)]
    #[example::attr]     // Does not work
    struct Demo {
        #[example::attr] // Works
        field: i32,
    }
    
    fn main() {}
    extern crate proc_macro;
    
    use proc_macro::TokenStream;
    
    #[proc_macro_derive(Example, attributes(example::attr))]
    pub fn example_derive(_input: TokenStream) -> TokenStream {
        TokenStream::new()
    }
  4. petrochenkov commented on Feb 27, 2019

    @petrochenkov
    Contributor

    Oh god.
    If not reject, then issue a deprecation warning immediately.

    Self-assigning, I should be able to fix it this weekend.

  5. self-assigned this
    on Feb 27, 2019
  6. shepmaster commented on Feb 27, 2019

    @shepmaster
    Member

    Is there any suggested way to have my desired syntax of

    #[derive(Example)]
    #[example::attr]
    struct Demo {
        #[example::attr]
        field: i32,
    }
  7. petrochenkov commented on Feb 27, 2019

    @petrochenkov
    Contributor

    @shepmaster
    Not right now, but something like #57921 may help.

    Actually, the derive macro itself could be able to introduce "tool modules" into scope (like rustfmt in rustfmt::skip, but not global to the whole crate).
    #[proc_macro_derive(Example, tools(example))] or something.
    Right now this is almost trivial to add for "macro expanded" attribute targets, like the item itself, but some additional work is needed to support it for other targets, e.g. fields (as you can see #[rustfmt::skip] doesn't work on fields yet).

  8. Centril commented on Feb 28, 2019

    @Centril
    Contributor

    @petrochenkov Is there a reason we cannot let #[proc_macro_derive(Example, attributes(example::attr))] mean #[proc_macro_derive(Example, tools(example))] plus some validation for the ::attr part? handwave

    From a user facing perspective at least, @shepmaster's #55168 (comment) example seems reasonable and "expected".

  9. petrochenkov commented on Mar 3, 2019

    @petrochenkov
    Contributor

    Fixed in #58899

    Is there a reason we cannot let #[proc_macro_derive(Example, attributes(example::attr))] ...

    I'd prefer to see crater results first.

  10. removed their assignment
    on Mar 9, 2019
  11. added 2 commits that reference this issue on Mar 10, 2019
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, ..)C-bugCategory: This is a bug.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions