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

Derive macro configuration does not support cfg_attr that expands to multiple attributes #16818

Closed
Veykril opened this issue Mar 12, 2024 · 5 comments
Labels
A-proc-macro proc macro C-bug Category: bug

Comments

@Veykril
Copy link
Member

Veykril commented Mar 12, 2024

#16789

@Veykril Veykril added C-bug Category: bug A-proc-macro proc macro labels Mar 12, 2024
@Dessix
Copy link

Dessix commented Aug 15, 2024

I believe I may have just run into this with sea-orm in its latest version.

Rust-Analyzer is reporting the following error when rustc accepts the code when using multi-predicate cfg_attrs:

Missing macro attribute \`rs_type\`

For anyone hitting this on conditional code like the following:

#[cfg_attr(
  feature = "sea-query",
  derive(EnumIter, DeriveActiveEnum),
  sea_orm(
    db_type = "String(StringLen::N(64))",
    rs_type = "String",
  ),
)]
pub enum MyEnum {
  // ...
}

Manually expanding to two separate predicative attributes resolves the issue:

#[cfg_attr(feature = "sea-query", derive(EnumIter, DeriveActiveEnum))]
#[cfg_attr(feature = "sea-query", sea_orm(rs_type = "String", db_type = "String(StringLen::N(64))"))]
pub enum MyEnum {
  // ...
}

I am unsure why this started cropping up only in the most recent version of SeaORM or rust-analyzer for me, but the Rust reference documentation shows that this attribute accepts multiple attributes in its Attrs grammar rule. I hope this is what this issue was about, but the linked thread didn't go into much detail about what "handl[ing] multiple attributes" entailed. I'm posting this for searchability, as I spent a while digging through sea-orm instead of RA trying to track this one down. Being that I commonly use this syntax for 6+ serde attributes, I'm surprised I haven't run into this sooner.

@Veykril
Copy link
Member Author

Veykril commented Aug 15, 2024

Ah, you are running into something differently here. I believe the case where cfg_attr expands to multiple things is fixed already (what this issue was about). What you are running into is a very special case that we haven't considered, namely where the derive is within a cfg_attr that expands to more things. When we expand the derive, we have to strip the derive invocation before passing the item as the attribute, but we do that pre-cfg_attr expansion so we strip the entire cfg_attr in your first example and which is why the second snippet works fine.

@Veykril
Copy link
Member Author

Veykril commented Aug 15, 2024

#8434 has some more context

@Dessix
Copy link

Dessix commented Aug 15, 2024

My apologies for posting it to the wrong issue then- but I'm glad I helped catch a subtle bug ^^

@wyatt-herkamp
Copy link
Contributor

@Veykril and @Dessix

This issue already exists here. #10110

I meant to fix it. However, I have not had time recently.

@Veykril Veykril closed this as not planned Won't fix, can't repro, duplicate, stale Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-proc-macro proc macro C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

3 participants