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

Implement #[cfg] in where clauses #132388

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

frank-king
Copy link
Contributor

This PR implements #115590, which supports #[cfg] attributes in where clauses.

The biggest change is, that it adds AttrsVec and NodeId to the ast::WherePredicate and HirId to the hir::WherePredicate.

@rustbot
Copy link
Collaborator

rustbot commented Oct 31, 2024

r? @cjgillot

rustbot has assigned @cjgillot.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added 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. labels Oct 31, 2024
@rustbot
Copy link
Collaborator

rustbot commented Oct 31, 2024

HIR ty lowering was modified

cc @fmease

Changes to the size of AST and/or HIR nodes.

cc @nnethercote

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Oct 31, 2024

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Copy link
Contributor

@cjgillot cjgillot left a comment

Choose a reason for hiding this comment

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

Thanks @frank-king for the work. I haven't looked deeply into the parser code yet, I need to familiarize myself with it first.

@tgross35 tgross35 changed the title Implment #[cfg] in where clauses Implement #[cfg] in where clauses Oct 31, 2024
@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as resolved.

@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as resolved.

@rust-log-analyzer

This comment has been minimized.

@petrochenkov
Copy link
Contributor

I've opened #132894 for that refactoring.

Blocked on #132894.
@rustbot blocked

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 16, 2024
@petrochenkov petrochenkov self-assigned this Nov 16, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 22, 2024
…=<try>

Refactor `where` predicates, and reserve for attributes support

Refactor `WherePredicate` to `WherePredicateKind`, and reserve for attributes support in `where` predicates.

This is a part of rust-lang#115590 and is split from rust-lang#132388.

r? petrochenkov
@frank-king
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Nov 26, 2024
@petrochenkov
Copy link
Contributor

(I didn't finish the full review yet.)

@cjgillot cjgillot removed their assignment Dec 2, 2024
@bors

This comment was marked as resolved.

@bors

This comment was marked as resolved.

@bors

This comment was marked as resolved.

@bors

This comment was marked as resolved.

@@ -52,6 +52,7 @@ pub enum Annotatable {
FieldDef(ast::FieldDef),
Variant(ast::Variant),
Crate(ast::Crate),
WherePredicate(ast::WherePredicate),
Copy link
Contributor

Choose a reason for hiding this comment

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

Style nit: could you consistently put the logic for WherePredicate immediately after the logic for Variants (in annotatables, ast fragments, etc)?
The new fragment is most similar to Variant, and it's good to have things in the same order everywhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

Still need to reorder a few things in expand.rs.

span,
kind: ast::WherePredicateKind::BoundPredicate(ast::WhereBoundPredicate {
bound_generic_params: Default::default(),
bounded_ty: ty(),
Copy link
Contributor

Choose a reason for hiding this comment

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

So you put a macro here to recognize placeholders, instead of adding the is_placeholder flag like in variants.
This is a bit tricky and at least needs a documentation, but I'd probably recommend to just add the is_placeholder flag instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Note that you never check whether a where-predicate node is a placeholder, because this PR misses the attribute resolution/expansion logic in

  • compiler\rustc_resolve\src\def_collector.rs
  • compiler\rustc_resolve\src\build_reduced_graph.rs
  • compiler\rustc_expand\src\placeholders.rs

Search for the .is_placeholder there and you can also do the same things as Variants do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After gaining more knowledge of placeholders, I started to realize that here we need is_placeholder because we don't have something like ast::ExprKind::MacCall or ast::ItemKind::ItemCall but we still need to repesent a macro call in the Variant and WherePredicate positions, is that correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it better to add a new variant ast::WherePredicateKind::MacCall? Then the size of ast::WherePredicate can be smaller that adding an is_placeholder: bool field. That also makes the codes more readable, and reserves the posibility that we can support macro calls in where clauses in the future.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it better to add a new variant ast::WherePredicateKind::MacCall?

Nah, MacCall is only for function-like macro calls, if they are not currently supported in some position, then it's better to not introduce it, and just use is_placeholder.

@@ -784,6 +784,10 @@ passes_unstable_attr_for_already_stable_feature =
.item = the stability attribute annotates this item
.help = consider removing the attribute
passes_unsupported_attributes_in_where =
attributes in `where` clauses are not supported
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
attributes in `where` clauses are not supported
most attributes are not supported in `where` clauses

Copy link
Contributor

Choose a reason for hiding this comment

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

"in where clauses" in the end is a bit better grammatically.

@petrochenkov
Copy link
Contributor

Sorry for the long delay, I've finished the review now.
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 20, 2025
@rustbot rustbot added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Feb 24, 2025
@rustbot
Copy link
Collaborator

rustbot commented Feb 24, 2025

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

cc @jdonszelmann

@rust-log-analyzer
Copy link
Collaborator

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
    Checking walkdir v2.5.0
    Checking filetime v0.2.25
    Checking if_chain v1.0.2
    Checking rinja v0.3.5
error[E0004]: non-exhaustive patterns: `&rustc_ast::WherePredicateKind::MacCall(_)` not covered
    |
    |
54  |                 match &clause.kind {
    |                       ^^^^^^^^^^^^ pattern `&rustc_ast::WherePredicateKind::MacCall(_)` not covered
note: `rustc_ast::WherePredicateKind` defined here
   --> /checkout/compiler/rustc_ast/src/ast.rs:428:1
    |
428 | pub enum WherePredicateKind {
428 | pub enum WherePredicateKind {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
436 |     MacCall(P<MacCall>),
    = note: the matched value is of type `&rustc_ast::WherePredicateKind`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
    |
    |
72  ~                     WherePredicateKind::EqPredicate(_) => {},
73  ~                     &rustc_ast::WherePredicateKind::MacCall(_) => todo!(),

For more information about this error, try `rustc --explain E0004`.
error: could not compile `clippy_lints` (lib) due to 1 previous error
Build completed unsuccessfully in 0:03:23

@petrochenkov petrochenkov 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 Feb 24, 2025
#[derive_const(Clone)] ():,
//~^ ERROR most attributes in `where` clauses are not supported
//~| ERROR expected non-macro attribute, found attribute macro `derive_const`
#[test_case] ():,
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to test all the macros exhaustively, it makes the tests harder to read, just one macro would be enough.

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

bors commented Feb 25, 2025

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

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-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants