Skip to content

Commit 2b3bceb

Browse files
committed
Add {ast,hir,thir}::PatKind::Missing variants.
"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.
1 parent c8609d9 commit 2b3bceb

2 files changed

Lines changed: 3 additions & 5 deletions

File tree

src/items.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,11 +2433,7 @@ pub(crate) fn span_hi_for_param(context: &RewriteContext<'_>, param: &ast::Param
24332433
}
24342434

24352435
pub(crate) fn is_named_param(param: &ast::Param) -> bool {
2436-
if let ast::PatKind::Ident(_, ident, _) = param.pat.kind {
2437-
ident.name != symbol::kw::Empty
2438-
} else {
2439-
true
2440-
}
2436+
!matches!(param.pat.kind, ast::PatKind::Missing)
24412437
}
24422438

24432439
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

src/patterns.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub(crate) fn is_short_pattern(
4242

4343
fn is_short_pattern_inner(context: &RewriteContext<'_>, pat: &ast::Pat) -> bool {
4444
match &pat.kind {
45+
ast::PatKind::Missing => unreachable!(),
4546
ast::PatKind::Rest | ast::PatKind::Never | ast::PatKind::Wild | ast::PatKind::Err(_) => {
4647
true
4748
}
@@ -100,6 +101,7 @@ impl Rewrite for Pat {
100101

101102
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
102103
match self.kind {
104+
PatKind::Missing => unreachable!(),
103105
PatKind::Or(ref pats) => {
104106
let pat_strs = pats
105107
.iter()

0 commit comments

Comments
 (0)