Skip to content

Port #[cfg] to the new attribute parsing infrastructure #143460

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

JonathanBrouwer
Copy link
Contributor

@JonathanBrouwer JonathanBrouwer commented Jul 4, 2025

Ports #[cfg] to the new attribute parsing infrastructure for #131229 (comment)

I've split this PR into commits for reviewability, and left some comments to clarify things

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Jul 4, 2025
@petrochenkov
Copy link
Contributor

I don't understand what is the point of this, at high level.
cfg and cfg_attr are a sort of macro attributes, they are not lowered into anything and do not appear in HIR.
I didn't yet hear about any plans to extend the new infra to something beyond inert attributes.

@petrochenkov
Copy link
Contributor

petrochenkov commented Jul 5, 2025

Or is this is going to be only about cfg predicates that live inside cfg or cfg_attr?
Those can indeed occur in some inert attributes too, for example link, in that case they need to be lowered and encoded in metadata.

@JonathanBrouwer
Copy link
Contributor Author

JonathanBrouwer commented Jul 5, 2025

@petrochenkov

@jdonszelmann might be able to explain better, she has been supporting me, but I'll try my best.

We already have some infrastructure for parsing attributes in the early stage, before the HIR, and this is already used for the repr attribute. The long term plan is to completely get rid of rustc_attr/attr/mod.rs, and the cfg attribute uses this so it also needs to be converted to a parsed attribute. I hope the change will be clearer when this PR is ready, it is still very much work in progress. There's a nice amount of code which I should be able to delete when this PR is ready.

Furthermore as you mentioned some attributes like link also need this change, I currently have a PR open for porting link which is blocked on this change.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 6, 2025

☔ The latest upstream changes (presumably #143526) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 6, 2025
Copy link
Contributor Author

@JonathanBrouwer JonathanBrouwer left a comment

Choose a reason for hiding this comment

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

@@ -0,0 +1,247 @@
use rustc_ast::{LitKind, MetaItem, MetaItemInner, MetaItemKind, MetaItemLit, NodeId};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This file is temporarily still needed for the following things: #[doc], #[link], cfg!(), #[cfg_attr].
In the future this file, as well as a few others, should no longer be needed, but making these attributes use the new cfg parser is sadly not easily possible without parsing the entire attributes.

As well as this file, there is also the Cfg type in rustdoc and all errors associated with this file. All in all this PR could have a significantly negative diff (all of this is like ~600 lines of code) if not for this

pub(crate) fn cfg_true(&self, attr: &Attribute, node: NodeId) -> EvalConfigResult {
// We need to run this to do basic validation of the attribute, such as that lits are valid, etc
// FIXME(jdonszelmann) this should not be necessary in the future
match validate_attr::parse_meta(&self.sess.psess, attr) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sadly this is needed, @jdonszelmann are you aware of this / do you have any plans to fix this?
We're also still parsing all the other attributes using both the old and new parser for this, probably not performance sensitive but a bit weird

pub fn main() {}
pub fn main() {
if cfg!(not()) { }
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This error code will not be needed in the future (as wel as many others), but needed a new example for now

/// Whether to emit errors or delay them as a bug
/// For most attributes, the attribute will be parsed again in the `Late` stage and in this case the errors should be delayed
/// But for some, such as `cfg`, the attribute will be removed before the `Late` stage so errors must be emitted
pub emit_errors: bool,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is needed because #[cfg] will not be parsed in the Late stage, so we need to emit all errors/warnings now

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 7, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 7, 2025

jdonszelmann is not on the review rotation at the moment.
They may take a while to respond.

@JonathanBrouwer JonathanBrouwer marked this pull request as ready for review July 7, 2025 07:26
@rustbot
Copy link
Collaborator

rustbot commented Jul 7, 2025

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

Some changes occurred in compiler/rustc_attr_data_structures

cc @jdonszelmann

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

|
LL | #[cfg(false)]
| ^^^^^^^^^^^^^
| ^^^^^
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made a small change here, this error will now always point to the exact part of the cfg attribute that makes the attribute false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants