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

Split MacArgs in two. #104559

Merged
merged 1 commit into from
Nov 22, 2022
Merged

Split MacArgs in two. #104559

merged 1 commit into from
Nov 22, 2022

Conversation

nnethercote
Copy link
Contributor

MacArgs is an enum with three variants: Empty, Delimited, and Eq. It's used in two ways:

  • For representing attribute macro arguments (e.g. in AttrItem), where all three variants are used.
  • For representing function-like macros (e.g. in MacCall and MacroDef), where only the Delimited variant is used.

In other words, MacArgs is used in two quite different places due to them having partial overlap. I find this makes the code hard to read. It also leads to various unreachable code paths, and allows invalid values (such as accidentally using MacArgs::Empty in a MacCall).

This commit splits MacArgs in two:

  • DelimArgs is a new struct just for the "delimited arguments" case. It is now used in MacCall and MacroDef.
  • AttrArgs is a renaming of the old MacArgs enum for the attribute macro case. Its Delimited variant now contains a DelimArgs.

Various other related things are renamed as well.

These changes make the code clearer, avoids several unreachable paths, and disallows the invalid values.

r? @petrochenkov

@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 Nov 18, 2022
@rustbot
Copy link
Collaborator

rustbot commented Nov 18, 2022

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@nnethercote
Copy link
Contributor Author

This shouldn't affect perf, but let's check.

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 18, 2022
@bors
Copy link
Contributor

bors commented Nov 18, 2022

⌛ Trying commit d12ca878a74b0a5115f6d2437aee163d8536ed3a with merge aeff0eacdd9049f2167cbec2bc220d77283e20f2...

@bors

This comment was marked as resolved.

@bors bors 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 Nov 18, 2022
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@nnethercote
Copy link
Contributor Author

That's weird... the commit was fine when applied to the earlier revision I was working against, and only broke when applied to a new revision. I thought the tests would have run against the earlier revision? Huh.

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Nov 18, 2022

⌛ Trying commit 539fe25b517ed1a10b8a3f7e321bf5ec3678abf2 with merge 5c1b781397ab288bb9524bba03b0f8844b4b636c...

@bors
Copy link
Contributor

bors commented Nov 18, 2022

☀️ Try build successful - checks-actions
Build commit: 5c1b781397ab288bb9524bba03b0f8844b4b636c (5c1b781397ab288bb9524bba03b0f8844b4b636c)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (5c1b781397ab288bb9524bba03b0f8844b4b636c): comparison URL.

Overall result: ❌ regressions - ACTION NEEDED

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf +perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.4% [1.4%, 1.4%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
5.3% [2.3%, 8.2%] 2
Improvements ✅
(primary)
-1.1% [-1.1%, -1.1%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.1% [-1.1%, -1.1%] 1

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.4% [2.1%, 2.9%] 3
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.3% [-3.7%, -2.9%] 2
All ❌✅ (primary) 2.4% [2.1%, 2.9%] 3

@rustbot rustbot added perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 18, 2022
@petrochenkov petrochenkov removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Nov 18, 2022
@nnethercote
Copy link
Contributor Author

The regressions are small enough to be ignored.

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Nov 18, 2022
@petrochenkov
Copy link
Contributor

From the description it looks similar to closed #96209, I'll check what are the differences.

compiler/rustc_ast/src/ast.rs Outdated Show resolved Hide resolved
use crate::ast::{
self, AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, Attribute, DelimArgs, Lit,
LitKind, MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem, Path, PathSegment,
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you keep the single-line style for imports, here and in other files?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The great thing about rustfmt is that it mostly avoids the need to make arbitrary formatting decisions. But here:

use crate::ast::{AttrId, AttrItem, AttrKind, AttrStyle, Attribute};
use crate::ast::{Lit, LitKind};
use crate::ast::{MacArgs, MacArgsEq, MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem};

I have multiple choices: add to the end of the first line, add to the start of the second line, or even combine the first and second line. And this is a relatively clean example where everything is in alphabetical order. We have lots of examples like this one:

use rustc_ast::{attr, BindingAnnotation, ByRef, Term};
use rustc_ast::{GenericArg, MacArgs, MacArgsEq};
use rustc_ast::{GenericBound, SelfKind, TraitBoundModifier};

where alphabetical order is not maintained across lines. Where should I insert an identifier like DelimArgs here? It could be on any of the lines. That's exactly the kind of arbitrary choice I don't like having to make. The multi-line style avoids this, because rustfmt does it all for you. I think that's why it gets used for cases like this.

Having said that, I'll revert if it's necessary to get r+.

Copy link
Contributor

Choose a reason for hiding this comment

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

Where should I insert an identifier like DelimArgs here?

Same as with multi-line imports, into its alphabetic order position.

I think that's why it gets used for cases like this.

For cases like that I'd personally use a glob import.

Copy link
Contributor

Choose a reason for hiding this comment

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

Single-line imports are easily greppable, and that's something I do quite regularly, that's why I keep this style in crates that I review and that already use this style.

compiler/rustc_ast_lowering/src/lib.rs Outdated Show resolved Hide resolved
compiler/rustc_parse/src/parser/item.rs Outdated Show resolved Hide resolved
@petrochenkov
Copy link
Contributor

Ok, it indeed looks like #96209 light, with MacDelimiter renaming removed and DelimArgs shared.

I'm still somewhat skeptical about this and suspect that we'll need to revert the change at some point, but let's land this for now if you are insisting.
@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 Nov 18, 2022
@nnethercote
Copy link
Contributor Author

Thanks for being flexible. I still find the AttrArgs vs. MetaItem split confusing, but this helps.

And I think this PR is better than #96209 because the DelimSpan/MacDelimiter/TokenStream trio isn't repeated across both types, thanks to the fact that AttrArgs::Delimited now contains a DelimArgs.

@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 Nov 21, 2022
@bors
Copy link
Contributor

bors commented Nov 21, 2022

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

@petrochenkov
Copy link
Contributor

r=me after fixing the last multi-line import and squashing commits.
@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 Nov 21, 2022
`MacArgs` is an enum with three variants: `Empty`, `Delimited`, and `Eq`. It's
used in two ways:
- For representing attribute macro arguments (e.g. in `AttrItem`), where all
  three variants are used.
- For representing function-like macros (e.g. in `MacCall` and `MacroDef`),
  where only the `Delimited` variant is used.

In other words, `MacArgs` is used in two quite different places due to them
having partial overlap. I find this makes the code hard to read. It also leads
to various unreachable code paths, and allows invalid values (such as
accidentally using `MacArgs::Empty` in a `MacCall`).

This commit splits `MacArgs` in two:
- `DelimArgs` is a new struct just for the "delimited arguments" case. It is
  now used in `MacCall` and `MacroDef`.
- `AttrArgs` is a renaming of the old `MacArgs` enum for the attribute macro
  case. Its `Delimited` variant now contains a `DelimArgs`.

Various other related things are renamed as well.

These changes make the code clearer, avoids several unreachable paths, and
disallows the invalid values.
@nnethercote
Copy link
Contributor Author

@bors r=petrochenkov rollup

@bors
Copy link
Contributor

bors commented Nov 21, 2022

📌 Commit 3e3a419 has been approved by petrochenkov

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 21, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 22, 2022
…iaskrgr

Rollup of 11 pull requests

Successful merges:

 - rust-lang#103396 (Pin::new_unchecked: discuss pinning closure captures)
 - rust-lang#104416 (Fix using `include_bytes` in pattern position)
 - rust-lang#104557 (Add a test case for async dyn* traits)
 - rust-lang#104559 (Split `MacArgs` in two.)
 - rust-lang#104597 (Probe + better error messsage for `need_migrate_deref_output_trait_object`)
 - rust-lang#104656 (Move tests)
 - rust-lang#104657 (Do not check transmute if has non region infer)
 - rust-lang#104663 (rustdoc: factor out common button CSS)
 - rust-lang#104666 (Migrate alias search result to CSS variables)
 - rust-lang#104674 (Make negative_impl and negative_impl_exists take the right types)
 - rust-lang#104692 (Update test's cfg-if dependency to 1.0)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 589d843 into rust-lang:master Nov 22, 2022
@rustbot rustbot added this to the 1.67.0 milestone Nov 22, 2022
@nnethercote nnethercote deleted the split-MacArgs branch November 22, 2022 22:08
flip1995 pushed a commit to flip1995/rust that referenced this pull request Dec 1, 2022
…iaskrgr

Rollup of 11 pull requests

Successful merges:

 - rust-lang#103396 (Pin::new_unchecked: discuss pinning closure captures)
 - rust-lang#104416 (Fix using `include_bytes` in pattern position)
 - rust-lang#104557 (Add a test case for async dyn* traits)
 - rust-lang#104559 (Split `MacArgs` in two.)
 - rust-lang#104597 (Probe + better error messsage for `need_migrate_deref_output_trait_object`)
 - rust-lang#104656 (Move tests)
 - rust-lang#104657 (Do not check transmute if has non region infer)
 - rust-lang#104663 (rustdoc: factor out common button CSS)
 - rust-lang#104666 (Migrate alias search result to CSS variables)
 - rust-lang#104674 (Make negative_impl and negative_impl_exists take the right types)
 - rust-lang#104692 (Update test's cfg-if dependency to 1.0)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
trevyn added a commit to trevyn/rustfmt that referenced this pull request Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

6 participants