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

feat: apply #[cfg] to proc macro inputs #16789

Merged
merged 5 commits into from
Mar 12, 2024
Merged

Conversation

wyatt-herkamp
Copy link
Contributor

@wyatt-herkamp wyatt-herkamp commented Mar 8, 2024

This will attempt to process cfg attributes and cfg_attr attributes for proc macro expansion.
image

Closes #8434 , #11657, and #13904

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 8, 2024
@wyatt-herkamp
Copy link
Contributor Author

I do have a few questions for the team that works on Rust Analyzer.

  1. Do you want me to throw this behind a feature flag as an unstable feature while it is tested?
  2. The changes to fixup.rs I changed the type for remove from SyntaxNode to SyntaxElement because I need to be able to remove the comma as well. Is this the best choice. Or can I remove the comma another way?
  3. Are there edge cases when I need to pass the cfg and cfg_attr attribute to the actual proc macro?
  4. Can this match statement be simplified down?

@wyatt-herkamp
Copy link
Contributor Author

image
#[cfg_attr(..,..)] Is parsing

@wyatt-herkamp wyatt-herkamp marked this pull request as ready for review March 9, 2024 18:03
@rustbot rustbot added has-merge-commits S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 9, 2024
@rustbot
Copy link
Collaborator

rustbot commented Mar 9, 2024

There are merge commits (commits with multiple parents) in your changes. We have a no merge policy so these commits will need to be removed for this pull request to be merged.

You can start a rebase with the following commands:

$ # rebase
$ git rebase -i master
$ # delete any merge commits in the editor that appears
$ git push --force-with-lease

The following commits are merge commits:

@wyatt-herkamp wyatt-herkamp changed the title [WIP] cfg Attribute Stripping for Proc Macro Expansion cfg Attribute Stripping for Proc Macro Expansion Mar 9, 2024
@rustbot rustbot removed has-merge-commits S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 9, 2024
Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annoying we have to do this on the syntax tree but there is no way around that. note that

struct S {
    field: fn(#[cfg(FALSE)] ()) -> ()
}

here we also need to configure the param away. It's fine to not support this right now though, I think we should probably have this infra here traverse the token tree in an untyped manner and just look for attributes and then handle things accordingly? Unsure.

Anyways with the few things I've raised I'm fine with merging this for now, since you said you were planning on tackling full cfg_attr supports, cleaning up and what not can be done afterwards (since the changes are all tied closely to a single module).

crates/hir-expand/src/db.rs Show resolved Hide resolved
crates/cfg/src/cfg_expr.rs Outdated Show resolved Hide resolved
crates/hir-expand/src/cfg_process.rs Outdated Show resolved Hide resolved
crates/hir-expand/src/cfg_process.rs Show resolved Hide resolved
@Veykril
Copy link
Member

Veykril commented Mar 11, 2024

Forgot to answer your questions:

Do you want me to throw this behind a feature flag as an unstable feature while it is tested?

If this is easily doable (which it looks like it should be), then yes let's do that for now so we can test it a bit.

The changes to fixup.rs I changed the type for remove from SyntaxNode to SyntaxElement because I need to be able to remove the comma as well. Is this the best choice. Or can I remove the comma another way?

Seems right to do this

Are there edge cases when I need to pass the cfg and cfg_attr attribute to the actual proc macro?

I don't think so

Can this match statement be simplified down?

Not really

@wyatt-herkamp
Copy link
Contributor Author

wyatt-herkamp commented Mar 11, 2024

If this is easily doable (which it looks like it should be), then yes let's do that for now so we can test it a bit.

How do I get the settings from the expand db?

I don't think so

Actually you already provided one
field: fn(#[cfg(FALSE)] ()) -> ()

@Veykril
Copy link
Member

Veykril commented Mar 11, 2024

How do I get the settings from the expand db?

We'd have to thread that through the database which is kind of annoying actually. Should be fine to not put this behind a config then. It shouldn't really break anything anyways (or rather make it worse than the status quo).

Also would be nice to add a test that this works as expected for our builtin derives in crates\hir-def\src\macro_expansion_tests\builtin_derive_macro.rs

@Veykril
Copy link
Member

Veykril commented Mar 11, 2024

Are there edge cases when I need to pass the cfg and cfg_attr attribute to the actual proc macro?

Actually there is another one, #[cfg_attr(foo, cfg_attr(bar, baz))], that is we need to recurse on this

@wyatt-herkamp
Copy link
Contributor Author

Are there edge cases when I need to pass the cfg and cfg_attr attribute to the actual proc macro?

Actually there is another one, #[cfg_attr(foo, cfg_attr(bar, baz))], that is we need to recurse on this

Do you want that here? Or should that be done when we handle multiple attributes?

@Veykril
Copy link
Member

Veykril commented Mar 11, 2024

that is fine to skip for now, its a very rare occurence

@wyatt-herkamp
Copy link
Contributor Author

wyatt-herkamp commented Mar 11, 2024

that is fine to skip for now, its a very rare occurence

Awesome. I think it should be good now.

Comparing against output from cargo expand is good.
Code_TtasZpkNeL

Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, lgtm 👍 (a few things I'd clean up slightly, but I think that can wait until your follow up and I might just do that myself, don't wanna nitpick and block a PR that way)

@Veykril
Copy link
Member

Veykril commented Mar 12, 2024

Very nice to see this, this has been a pain point for a lot of folks, especially diesel I think cc @weiznich :)
Thanks!
@bors r+

@bors
Copy link
Contributor

bors commented Mar 12, 2024

📌 Commit 447de3d has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Mar 12, 2024

⌛ Testing commit 447de3d with merge d3e6fea...

@Veykril Veykril changed the title cfg Attribute Stripping for Proc Macro Expansion feat: Configure derive macro inputs Mar 12, 2024
@bors
Copy link
Contributor

bors commented Mar 12, 2024

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing d3e6fea to master...

@bors bors merged commit d3e6fea into rust-lang:master Mar 12, 2024
11 checks passed
@wyatt-herkamp wyatt-herkamp deleted the cfg_attr branch March 12, 2024 10:31
@lnicola lnicola changed the title feat: Configure derive macro inputs feat: apply #[cfg] to proc macro inputs Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom derive attribute stripping is insufficient
4 participants