Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#![feature(proc_macro)] breaks some custom derive attributes #52219

Closed
alexcrichton opened this issue Jul 10, 2018 · 4 comments · Fixed by #52230
Closed

#![feature(proc_macro)] breaks some custom derive attributes #52219

alexcrichton opened this issue Jul 10, 2018 · 4 comments · Fixed by #52230
Assignees
Labels
A-macros-1.2 Area: Declarative macros 1.2 A-macros-2.0 Area: Declarative macros 2.0 (#39412)

Comments

@alexcrichton
Copy link
Member

Originally reported as rust-lang/rustfix#126 this is reduced to:

// foo.rs
#![crate_type = "proc-macro"]

extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro_derive(Foo)]
pub fn foo(a: TokenStream) -> TokenStream {
    "".parse().unwrap()
}

#[proc_macro_derive(Bar, attributes(custom))]
pub fn bar(a: TokenStream) -> TokenStream {
    "".parse().unwrap()
}
// bar.rs
#![cfg_attr(broken, feature(proc_macro))]
#![crate_type = "lib"]

#[macro_use]
extern crate foo;

#[derive(Foo, Bar, Debug)]
#[custom = "test"]
pub enum A{
    B,
    C,
}

compiles with:

$ rustc +nightly foo.rs
$ rustc +nightly bar.rs -L .
$ rustc +nightly bar.rs -L . --cfg broken
error: cannot find attribute macro `custom` in this scope
 --> bar.rs:8:3
  |
8 | #[custom = "test"]
  |   ^^^^^^

error: aborting due to previous error
@alexcrichton alexcrichton added A-macros-2.0 Area: Declarative macros 2.0 (#39412) A-macros-1.2 Area: Declarative macros 1.2 labels Jul 10, 2018
@alexcrichton
Copy link
Member Author

cc @petrochenkov, I'm not really sure what this classifies under, it seems like maybe #50911 although #![feature(use_extern_macros)] doesn't break it, only #![feature(proc_macro)] does. After #52081 though it may be broken by use_extern_macros (I'm not 100% sure where this is happening)

@petrochenkov
Copy link
Contributor

I'm not really sure what this classifies under, it seems like maybe #50911 although #![feature(use_extern_macros)] doesn't break it

This falls under "attribute resolution is mess" and "some things are gated by proc_macro while they should be gated by macro modularization (use_extern_macros)".

@cramertj
Copy link
Member

I hit this error last week when trying to update a crate that used failure's derive macros. I found that useing the macro would give me the attribute error, but #[macro_use]ing the crate worked.

@alexcrichton
Copy link
Member Author

@cramertj I think that may be a separate bug, can you file a different issue?

I believe I've fixed this issue in #52230

alexcrichton added a commit to alexcrichton/rust that referenced this issue Jul 10, 2018
This commit fixes an apparent mistake in librustc_resolve where when the
`proc_macro` feature is enabled (or `rust_2018_preview`) the resolution of
custom attributes for custom derive was tweaked. Previously when an attribute
failed to resolve it was attempted to locate if there is a custom derive also in
scope which declares the attribute, but only the first custom derive directive
was search.

Instead this commit fixes the loop to search all custom derive invocations
looking for any which register the attribute in question.

Closes rust-lang#52219
bors added a commit that referenced this issue Jul 12, 2018
rustc: Search all derives for inert attributes

This commit fixes an apparent mistake in librustc_resolve where when the
`proc_macro` feature is enabled (or `rust_2018_preview`) the resolution of
custom attributes for custom derive was tweaked. Previously when an attribute
failed to resolve it was attempted to locate if there is a custom derive also in
scope which declares the attribute, but only the first custom derive directive
was search.

Instead this commit fixes the loop to search all custom derive invocations
looking for any which register the attribute in question.

Closes #52219
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros-1.2 Area: Declarative macros 1.2 A-macros-2.0 Area: Declarative macros 2.0 (#39412)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants