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

Move ast::Item::ident into ast::ItemKind #138740

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

Conversation

nnethercote
Copy link
Contributor

@nnethercote nnethercote commented Mar 20, 2025

The follow-up to #138384, which did the same thing for hir::ItemKind.

r? @fmease

`global_allocator_spans` and `alloc_error_handler_span` are identical
except for `name`.
@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. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Mar 20, 2025
@nnethercote
Copy link
Contributor Author

@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 Mar 20, 2025
@bors
Copy link
Contributor

bors commented Mar 20, 2025

⌛ Trying commit 87677af with merge 960f748...

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 20, 2025
…try>

Move `ast::Item::ident` into `ast::ItemKind`

r? `@ghost`
@bors
Copy link
Contributor

bors commented Mar 20, 2025

☀️ Try build successful - checks-actions
Build commit: 960f748 (960f7488ddd0a883d61daf2c4e954ec575bd58aa)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (960f748): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

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-perf +perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.3% [0.3%, 0.3%] 1
Improvements ✅
(primary)
-0.3% [-0.3%, -0.2%] 4
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.3% [-0.3%, -0.2%] 4

Max RSS (memory usage)

Results (primary -0.7%, secondary -1.1%)

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)
1.1% [0.6%, 1.5%] 2
Regressions ❌
(secondary)
2.7% [2.7%, 2.7%] 1
Improvements ✅
(primary)
-1.6% [-2.0%, -1.4%] 4
Improvements ✅
(secondary)
-2.4% [-2.8%, -1.8%] 3
All ❌✅ (primary) -0.7% [-2.0%, 1.5%] 6

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

Results (primary -0.2%)

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.1% [0.0%, 0.1%] 33
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.6% [-1.0%, -0.0%] 19
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.2% [-1.0%, 0.1%] 52

Bootstrap: 773.468s -> 775.556s (0.27%)
Artifact size: 365.48 MiB -> 365.41 MiB (-0.02%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Mar 20, 2025
@nnethercote
Copy link
Contributor Author

Performance is basically neutral. If anything, the improvements slightly outweigh the regressions, but all changes are very small.

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Mar 20, 2025
Instead of putting the item inside it, just pass the ident and
visibility (the only things needed) alongside it where necessary.

This helps with the next commit, which will move the ident's location.
Specifically, it gets rid of the `match visitor_kind` in
`rewrite_type_alias`.
`FmtVisitor::visit_mac` has an `Option<Ident>` arg which is always
either `None` or `Some(kw::Empty)`, because `ItemKind::MacCall` always
has an empty ident. This value is passed through various functions until
it reaches `rewrite_macro_name`, which treats `None` and
`Some(kw::Empty)` the same.

In other words, the argument is useless. This commit removes it. There
is no change in behaviour. The commit also changes a few `symbol::Ident`
occurrences to `Ident` in `macros.rs`; `Symbol` is imported in that file
so `Ident` might as well be, too.

(This is a good example of why it's a bad idea for `Itemt` to have an
`ident` field when various item kinds don't have an identifier. It's
easy to get confused when "empty identifier" is used to mean "no
identifier". This will be fixed in a subsequent commit.)
`expand_test_case` looks for any item with a `#[test_case]` attribute
and adds a `test_path_symbol` attribute to it while also fiddling with
the item's ident's span.

This is pretty weird, because `#[test_case]` is only valid on
`fn`/`const`/`static` items, as far as I can tell. But you don't
currently get an error or warning if you use it on other kinds of items.

This commit changes things so that a `#[test_case]` item is modified
only if it is `fn`/`const`/`static`. This is relevant for moving idents
from `Item` to `ItemKind`, because some item kinds don't have an ident,
e.g. `impl` blocks.

The commit also does the following.
- Renames a local variable `test_id` as `test_ident`.
- Changes a `const` to `static` in
  `tests/ui/custom_test_frameworks/full.rs` to give the `static` case
  some test coverage.
- Adds a `struct` and `impl` to the same test to give some test coverage
  to the non-affected item kinds. These have a `FIXME` comment
  identifying the weirdness here. Hopefully this will be useful
  breadcrumbs for somebody else in the future.
`ast::Item` has an `ident` field.

- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
  `Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
  `Trait`, `TraitAlias`, `MacroDef`, `Delegation`.

- It's always empty for these item kinds: `Use`, `ForeignMod`,
  `GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.

There is a similar story for `AssocItemKind` and `ForeignItemKind`.

Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.

The commit is large but it's mostly obvious plumbing work. Some notable
things.

- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
  fields within some of the `ast::ItemKind` variants (specifically:
  `Struct`, `Union`, `Enum`). I might do that in a follow-up; this
  commit is big enough already.

- For the visitors: `FnKind` no longer needs an `ident` field because
  the `Fn` within how has one.

- In the parser, the `ItemInfo` typedef is no longer needed. It was used
  in various places to return an `Ident` alongside an `ItemKind`, but
  now the `Ident` (if present) is within the `ItemKind`.

- In a few places I renamed identifier variables called `name` (or
  `foo_name`) as `ident` (or `foo_ident`), to better match the type, and
  because `name` is normally used for `Symbol`s. It's confusing to see
  something like `foo_name.name`.
@nnethercote nnethercote force-pushed the ast-ItemKind-idents branch from 87677af to c977a9d Compare March 21, 2025 05:24
@nnethercote nnethercote marked this pull request as ready for review March 21, 2025 05:24
@rustbot
Copy link
Collaborator

rustbot commented Mar 21, 2025

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@nnethercote
Copy link
Contributor Author

@fmease: thanks for reviewing, and apologies for the size of the final commit. I can move the four preliminary commits into a separate PR if that would be helpful.

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-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.

None yet

5 participants