-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Conversation
"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`.
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
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`
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (b1bc47e): comparison URL. Overall result: no relevant changes - no action neededBenchmarking 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 Instruction countThis 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.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: missing data |
Perf-neutral, which is good. @bors rollup=maybe |
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 |
I want to try making |
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!(), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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" 😆
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 |
Same is true for fn decls in |
those make sense. But function pointers are types, the information is useless even to callers as they never see it across crates. |
Ok, finally convinced @bors r+
it was nice (very subjectively), but obviously not feasible |
…-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`
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
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
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``
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