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

return of #39347: feature(proc_macro) breaks attributes on custom derive #44925

Closed
kardeiz opened this issue Sep 29, 2017 · 8 comments
Closed
Labels
A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-bug Category: This is a bug.

Comments

@kardeiz
Copy link

kardeiz commented Sep 29, 2017

Recent versions of Rust nightly seem to have the exact same issue as initially reported in #39347 (albeit with a slightly different error message: e.g., error: cannot find attribute macro belongs_to in this scope).

See the recent comment on #39347 by LukasKalbertodt for more details.

I know nothing about Rust's internals, but it seems like the issue is overzealous checking by proc_macro in https://github.com/rust-lang/rust/blob/master/src/librustc_resolve/macros.rs#L587.

@TimNN TimNN added A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-bug Category: This is a bug. labels Oct 1, 2017
@kardeiz
Copy link
Author

kardeiz commented Oct 2, 2017

This just got a whole lot more confusing (to me at least). I just discovered that I can successfully use serde's proc_macro_derive attributes, but not Diesel's.

So for example, evesteras's serde example in #39347 compiles fine, but LukasKalbertodt's Diesel example does not. This is within the same project, so rustc/cargo/enabled features, etc. are exactly the same.

I'm not familiar enough with the whole proc_macro environment to get what serde and Diesel are doing differently. Considering that Diesel's derive attributes work fine when the proc_macro feature is not enabled, it doesn't really seem like a bug in Diesel (although maybe there is a way to work around it)?

@kardeiz
Copy link
Author

kardeiz commented Oct 2, 2017

I looked into this a little more. When using Diesel, it is common for specific structs to derive multiple Diesel-related traits (e.g., Queryable, Identifiable, Insertable). These may each have their own permitted attributes (e.g. table_name, belongs_to). However, when proc_macro is enabled, it looks like only the first permitted attribute is let through—the others lead to the compilation error above.

This could be fixed by "namespacing"the attributes (e.g., #[diesel(table_name="something")]) or by listing out each potential attribute that might be used on a specific struct. So I guess this might not be a bug per se, but it will cause problems for at least some libraries if it goes forward like this.

@TheNeikos
Copy link
Contributor

I have also his this issue

@flosse
Copy link

flosse commented Jan 25, 2018

Are there any workarounds?

@kardeiz
Copy link
Author

kardeiz commented Jan 25, 2018

What I am doing to work around this is setting my project up as a workspace. I have all of my project in one main crate (in which I am using the proc_macro feature), except for my database stuff (since in my case Diesel is the only thing hitting this issue), which I am putting in a db subcrate (in which I do not use the proc_macro feature). This resolves the issue for me, and isn't too painful or clunky.

@flosse
Copy link

flosse commented Jan 25, 2018

@kardeiz thanks for your help! I'll try to do the same :)

barzamin added a commit to rustodon/rustodon that referenced this issue Jan 28, 2018
This is done so that we can use proc_macros in the main crate,
for Maud templating. Due to a rustc bug (rust-lang/rust#44925),
#![feature(proc_macros)] and custom derive attributes don't play
nice together, so switching on proc_macros breaks Diesel derives.

Thankfully, just splitting the Diesel code out into its own crate
and depending on it works fine, since crates are a feature boundary.
barzamin added a commit to rustodon/rustodon that referenced this issue Jan 30, 2018
This is done so that we can use proc_macros in the main crate,
for Maud templating. Due to a rustc bug (rust-lang/rust#44925),
#![feature(proc_macros)] and custom derive attributes don't play
nice together, so switching on proc_macros breaks Diesel derives.

Thankfully, just splitting the Diesel code out into its own crate
and depending on it works fine, since crates are a feature boundary.
@sgrif
Copy link
Contributor

sgrif commented Apr 21, 2018

This is still an issue on the most recent nightlies

@steveklabnik
Copy link
Member

I cannot reproduce this issue today, and it's been over a year since the last comment. I'm going to give this a close, but let me know if I'm missing something!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

6 participants