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

Add new PatKind::Missing variants #139035

Merged
merged 2 commits into from
Apr 7, 2025
Merged

Conversation

nnethercote
Copy link
Contributor

@nnethercote nnethercote commented Mar 27, 2025

To avoid some ugly uses of kw::Empty when handling "missing" patterns, e.g. in bare fn tys. Helps with #137978. Details in the individual commits.

r? @oli-obk

"Missing" patterns are possible in bare fn types (`fn f(u32)`) and
similar places. Currently these are represented in the AST with
`ast::PatKind::Ident` with no `by_ref`, no `mut`, an empty ident, and no
sub-pattern. This flows through to `{hir,thir}::PatKind::Binding` for
HIR and THIR.

This is a bit nasty. It's very non-obvious, and easy to forget to check
for the exceptional empty identifier case.

This commit adds a new variant, `PatKind::Missing`, to do it properly.

The process I followed:
- Add a `Missing` variant to `{ast,hir,thir}::PatKind`.
- Chang `parse_param_general` to produce `ast::PatKind::Missing`
  instead of `ast::PatKind::Missing`.
- Look through `kw::Empty` occurrences to find functions where an
  existing empty ident check needs replacing with a `PatKind::Missing`
  check: `print_param`, `check_trait_item`, `is_named_param`.
- Add a `PatKind::Missing => unreachable!(),` arm to every exhaustive
  match identified by the compiler.
- Find which arms are actually reachable by running the test suite,
  changing them to something appropriate, usually by looking at what
  would happen to a `PatKind::Ident`/`PatKind::Binding` with no ref, no
  `mut`, an empty ident, and no subpattern.

Quite a few of the `unreachable!()` arms were never reached. This makes
sense because `PatKind::Missing` can't happen in every pattern, only
in places like bare fn tys and trait fn decls.

I also tried an alternative approach: modifying `ast::Param::pat` to
hold an `Option<P<Pat>>` instead of a `P<Pat>`, but that quickly turned
into a very large and painful change. Adding `PatKind::Missing` is much
easier.
Thanks to the introduction of `PatKind::Missing`.
@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 27, 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 28, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 28, 2025
Add new `PatKind::Missing` variants

To avoid some ugly uses of `kw::Empty` when handling "missing" patterns, e.g. in bare fn tys. Helps with rust-lang#137978. Details in the individual commits.

r? `@ghost`
@bors
Copy link
Collaborator

bors commented Mar 28, 2025

⌛ Trying commit 909f449 with merge b1bc47e...

@bors
Copy link
Collaborator

bors commented Mar 28, 2025

☀️ Try build successful - checks-actions
Build commit: b1bc47e (b1bc47effce412da5d78921d97b88415d6cbc1d4)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (b1bc47e): comparison URL.

Overall result: no relevant changes - no 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.

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

Instruction count

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

Max RSS (memory usage)

Results (primary -0.9%)

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)
- - 0
Improvements ✅
(primary)
-0.9% [-0.9%, -0.9%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.9% [-0.9%, -0.9%] 1

Cycles

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

Binary size

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

Bootstrap: missing data
Artifact size: 365.91 MiB -> 365.92 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 28, 2025
@nnethercote
Copy link
Contributor Author

Perf-neutral, which is good.

@bors rollup=maybe

@nnethercote nnethercote marked this pull request as ready for review March 28, 2025 09:06
@rustbot
Copy link
Collaborator

rustbot commented Mar 28, 2025

Some changes occurred in match lowering

cc @Nadrieril

Some changes occurred in exhaustiveness checking

cc @Nadrieril

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in match checking

cc @Nadrieril

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@oli-obk
Copy link
Contributor

oli-obk commented Mar 28, 2025

modifying ast::Param::pat to
hold an Option<P<Pat>> instead of a P<Pat>, but that quickly turned
into a very large and painful change. Adding PatKind::Missing is much
easier.

I want to try making FnDecl generic over the Param or Ty before committing to this PR. If you want to give this a try, I will likely start doing this mid-next week otherwise.

@nnethercote
Copy link
Contributor Author

I want to try making FnDecl generic over the Param or Ty before committing to this PR

I tried that too, it got ugly pretty quickly, but maybe you can find a way to do it that's nice.

@@ -42,6 +42,7 @@ pub(crate) fn is_short_pattern(

fn is_short_pattern_inner(context: &RewriteContext<'_>, pat: &ast::Pat) -> bool {
match &pat.kind {
ast::PatKind::Missing => unreachable!(),
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 briefly explain how we know this is unreachable.

Copy link
Contributor

Choose a reason for hiding this comment

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

Gonna assume "no tests were hit" 😆

@oli-obk
Copy link
Contributor

oli-obk commented Apr 7, 2025

Ok turns out we actually support parameter names in function pointer types!?!

type Foo = fn(foo: ());

fn foo(f: fn(foo: ())) {}

we just do not support patterns. what the heck. Can we break that. Is like anyone actually writing that

@nnethercote
Copy link
Contributor Author

Ok turns out we actually support parameter names in function pointer types!?!
we just do not support patterns.

Same is true for fn decls in extern blocks, and maybe bodyless fn decls in trait blocks (not certain about that last one).

@oli-obk
Copy link
Contributor

oli-obk commented Apr 7, 2025

Same is true for fn decls in extern blocks, and maybe bodyless fn decls in trait blocks (not certain about that last one).

those make sense. But function pointers are types, the information is useless even to callers as they never see it across crates.

@oli-obk
Copy link
Contributor

oli-obk commented Apr 7, 2025

Ok, finally convinced

@bors r+

it got ugly pretty quickly

it was nice (very subjectively), but obviously not feasible

@bors
Copy link
Collaborator

bors commented Apr 7, 2025

📌 Commit 909f449 has been approved by oli-obk

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-review Status: Awaiting review from the assignee but also interested parties. labels Apr 7, 2025
Zalathar added a commit to Zalathar/rust that referenced this pull request Apr 7, 2025
…-obk

Add new `PatKind::Missing` variants

To avoid some ugly uses of `kw::Empty` when handling "missing" patterns, e.g. in bare fn tys. Helps with rust-lang#137978. Details in the individual commits.

r? `@oli-obk`
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 7, 2025
Rollup of 8 pull requests

Successful merges:

 - rust-lang#138603 (Report line number of test when should_panic test failed)
 - rust-lang#139035 (Add new `PatKind::Missing` variants)
 - rust-lang#139112 (Implement `super let`)
 - rust-lang#139365 (Default auto traits: fix perf)
 - rust-lang#139397 (coverage: Build the CGU's global file table as late as possible)
 - rust-lang#139455 ( Remove support for `extern "rust-intrinsic"` blocks)
 - rust-lang#139461 (Stop calling `source_span` query in significant drop order code)
 - rust-lang#139466 (Trivial tweaks to stop tracking source span directly)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 7, 2025
Rollup of 9 pull requests

Successful merges:

 - rust-lang#139035 (Add new `PatKind::Missing` variants)
 - rust-lang#139108 (Simplify `thir::PatKind::ExpandedConstant`)
 - rust-lang#139112 (Implement `super let`)
 - rust-lang#139365 (Default auto traits: fix perf)
 - rust-lang#139397 (coverage: Build the CGU's global file table as late as possible)
 - rust-lang#139455 ( Remove support for `extern "rust-intrinsic"` blocks)
 - rust-lang#139461 (Stop calling `source_span` query in significant drop order code)
 - rust-lang#139465 (add sret handling for scalar autodiff)
 - rust-lang#139466 (Trivial tweaks to stop tracking source span directly)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 82df622 into rust-lang:master Apr 7, 2025
7 checks passed
@rustbot rustbot added this to the 1.88.0 milestone Apr 7, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 7, 2025
Rollup merge of rust-lang#139035 - nnethercote:PatKind-Missing, r=oli-obk

Add new `PatKind::Missing` variants

To avoid some ugly uses of `kw::Empty` when handling "missing" patterns, e.g. in bare fn tys. Helps with rust-lang#137978. Details in the individual commits.

r? ``@oli-obk``
@nnethercote nnethercote deleted the PatKind-Missing branch April 7, 2025 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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. 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