Skip to content

Commit 82df622

Browse files
authored
Rollup merge of #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 #137978. Details in the individual commits. r? ``@oli-obk``
2 parents 8fb32ab + 909f449 commit 82df622

File tree

42 files changed

+92
-66
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+92
-66
lines changed

compiler/rustc_ast/src/ast.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ impl Pat {
563563
/// This is intended for use by diagnostics.
564564
pub fn to_ty(&self) -> Option<P<Ty>> {
565565
let kind = match &self.kind {
566+
PatKind::Missing => unreachable!(),
566567
// In a type expression `_` is an inference variable.
567568
PatKind::Wild => TyKind::Infer,
568569
// An IDENT pattern with no binding mode would be valid as path to a type. E.g. `u32`.
@@ -625,7 +626,8 @@ impl Pat {
625626
| PatKind::Guard(s, _) => s.walk(it),
626627

627628
// These patterns do not contain subpatterns, skip.
628-
PatKind::Wild
629+
PatKind::Missing
630+
| PatKind::Wild
629631
| PatKind::Rest
630632
| PatKind::Never
631633
| PatKind::Expr(_)
@@ -676,6 +678,7 @@ impl Pat {
676678
/// Return a name suitable for diagnostics.
677679
pub fn descr(&self) -> Option<String> {
678680
match &self.kind {
681+
PatKind::Missing => unreachable!(),
679682
PatKind::Wild => Some("_".to_string()),
680683
PatKind::Ident(BindingMode::NONE, ident, None) => Some(format!("{ident}")),
681684
PatKind::Ref(pat, mutbl) => pat.descr().map(|d| format!("&{}{d}", mutbl.prefix_str())),
@@ -769,6 +772,9 @@ pub enum RangeSyntax {
769772
// Adding a new variant? Please update `test_pat` in `tests/ui/macros/stringify.rs`.
770773
#[derive(Clone, Encodable, Decodable, Debug)]
771774
pub enum PatKind {
775+
/// A missing pattern, e.g. for an anonymous param in a bare fn like `fn f(u32)`.
776+
Missing,
777+
772778
/// Represents a wildcard pattern (`_`).
773779
Wild,
774780

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ pub fn walk_pat<T: MutVisitor>(vis: &mut T, pat: &mut P<Pat>) {
15871587
vis.visit_id(id);
15881588
match kind {
15891589
PatKind::Err(_guar) => {}
1590-
PatKind::Wild | PatKind::Rest | PatKind::Never => {}
1590+
PatKind::Missing | PatKind::Wild | PatKind::Rest | PatKind::Never => {}
15911591
PatKind::Ident(_binding_mode, ident, sub) => {
15921592
vis.visit_ident(ident);
15931593
visit_opt(sub, |sub| vis.visit_pat(sub));

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) -> V::Res
750750
try_visit!(visitor.visit_pat(subpattern));
751751
try_visit!(visitor.visit_expr(guard_condition));
752752
}
753-
PatKind::Wild | PatKind::Rest | PatKind::Never => {}
753+
PatKind::Missing | PatKind::Wild | PatKind::Rest | PatKind::Never => {}
754754
PatKind::Err(_guar) => {}
755755
PatKind::Tuple(elems) | PatKind::Slice(elems) | PatKind::Or(elems) => {
756756
walk_list!(visitor, visit_pat, elems);

compiler/rustc_ast_lowering/src/lib.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -1496,18 +1496,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14961496

14971497
fn lower_fn_params_to_names(&mut self, decl: &FnDecl) -> &'hir [Option<Ident>] {
14981498
self.arena.alloc_from_iter(decl.inputs.iter().map(|param| match param.pat.kind {
1499-
PatKind::Ident(_, ident, _) => {
1500-
if ident.name != kw::Empty {
1501-
Some(self.lower_ident(ident))
1502-
} else {
1503-
None
1504-
}
1505-
}
1499+
PatKind::Missing => None,
1500+
PatKind::Ident(_, ident, _) => Some(self.lower_ident(ident)),
15061501
PatKind::Wild => Some(Ident::new(kw::Underscore, self.lower_span(param.pat.span))),
15071502
_ => {
15081503
self.dcx().span_delayed_bug(
15091504
param.pat.span,
1510-
"non-ident/wild param pat must trigger an error",
1505+
"non-missing/ident/wild param pat must trigger an error",
15111506
);
15121507
None
15131508
}

compiler/rustc_ast_lowering/src/pat.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2626
let pat_hir_id = self.lower_node_id(pattern.id);
2727
let node = loop {
2828
match &pattern.kind {
29+
PatKind::Missing => break hir::PatKind::Missing,
2930
PatKind::Wild => break hir::PatKind::Wild,
3031
PatKind::Never => break hir::PatKind::Never,
3132
PatKind::Ident(binding_mode, ident, sub) => {

compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl<'a> AstValidator<'a> {
244244
fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, Option<Ident>, bool)) {
245245
for Param { pat, .. } in &decl.inputs {
246246
match pat.kind {
247-
PatKind::Ident(BindingMode::NONE, _, None) | PatKind::Wild => {}
247+
PatKind::Missing | PatKind::Ident(BindingMode::NONE, _, None) | PatKind::Wild => {}
248248
PatKind::Ident(BindingMode::MUT, ident, None) => {
249249
report_err(pat.span, Some(ident), true)
250250
}

compiler/rustc_ast_pretty/src/pprust/state.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -1622,9 +1622,9 @@ impl<'a> State<'a> {
16221622
fn print_pat(&mut self, pat: &ast::Pat) {
16231623
self.maybe_print_comment(pat.span.lo());
16241624
self.ann.pre(self, AnnNode::Pat(pat));
1625-
/* Pat isn't normalized, but the beauty of it
1626-
is that it doesn't matter */
1625+
/* Pat isn't normalized, but the beauty of it is that it doesn't matter */
16271626
match &pat.kind {
1627+
PatKind::Missing => unreachable!(),
16281628
PatKind::Wild => self.word("_"),
16291629
PatKind::Never => self.word("!"),
16301630
PatKind::Ident(BindingMode(by_ref, mutbl), ident, sub) => {
@@ -1946,12 +1946,7 @@ impl<'a> State<'a> {
19461946
if let Some(eself) = input.to_self() {
19471947
self.print_explicit_self(&eself);
19481948
} else {
1949-
let invalid = if let PatKind::Ident(_, ident, _) = input.pat.kind {
1950-
ident.name == kw::Empty
1951-
} else {
1952-
false
1953-
};
1954-
if !invalid {
1949+
if !matches!(input.pat.kind, PatKind::Missing) {
19551950
self.print_pat(&input.pat);
19561951
self.word(":");
19571952
self.space();

compiler/rustc_hir/src/hir.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,7 @@ impl<'hir> Pat<'hir> {
15551555

15561556
use PatKind::*;
15571557
match self.kind {
1558+
Missing => unreachable!(),
15581559
Wild | Never | Expr(_) | Range(..) | Binding(.., None) | Err(_) => true,
15591560
Box(s) | Deref(s) | Ref(s, _) | Binding(.., Some(s)) | Guard(s, _) => s.walk_short_(it),
15601561
Struct(_, fields, _) => fields.iter().all(|field| field.pat.walk_short_(it)),
@@ -1582,7 +1583,7 @@ impl<'hir> Pat<'hir> {
15821583

15831584
use PatKind::*;
15841585
match self.kind {
1585-
Wild | Never | Expr(_) | Range(..) | Binding(.., None) | Err(_) => {}
1586+
Missing | Wild | Never | Expr(_) | Range(..) | Binding(.., None) | Err(_) => {}
15861587
Box(s) | Deref(s) | Ref(s, _) | Binding(.., Some(s)) | Guard(s, _) => s.walk_(it),
15871588
Struct(_, fields, _) => fields.iter().for_each(|field| field.pat.walk_(it)),
15881589
TupleStruct(_, s, _) | Tuple(s, _) | Or(s) => s.iter().for_each(|p| p.walk_(it)),
@@ -1720,6 +1721,9 @@ pub enum TyPatKind<'hir> {
17201721

17211722
#[derive(Debug, Clone, Copy, HashStable_Generic)]
17221723
pub enum PatKind<'hir> {
1724+
/// A missing pattern, e.g. for an anonymous param in a bare fn like `fn f(u32)`.
1725+
Missing,
1726+
17231727
/// Represents a wildcard pattern (i.e., `_`).
17241728
Wild,
17251729

compiler/rustc_hir/src/intravisit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) -> V:
744744
visit_opt!(visitor, visit_pat_expr, lower_bound);
745745
visit_opt!(visitor, visit_pat_expr, upper_bound);
746746
}
747-
PatKind::Never | PatKind::Wild | PatKind::Err(_) => (),
747+
PatKind::Missing | PatKind::Never | PatKind::Wild | PatKind::Err(_) => (),
748748
PatKind::Slice(prepatterns, ref slice_pattern, postpatterns) => {
749749
walk_list!(visitor, visit_pat, prepatterns);
750750
visit_opt!(visitor, visit_pat, slice_pattern);

compiler/rustc_hir_analysis/src/check/region.rs

+1
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ fn resolve_local<'tcx>(
623623

624624
PatKind::Ref(_, _)
625625
| PatKind::Binding(hir::BindingMode(hir::ByRef::No, _), ..)
626+
| PatKind::Missing
626627
| PatKind::Wild
627628
| PatKind::Never
628629
| PatKind::Expr(_)

compiler/rustc_hir_pretty/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1868,6 +1868,7 @@ impl<'a> State<'a> {
18681868
// Pat isn't normalized, but the beauty of it
18691869
// is that it doesn't matter
18701870
match pat.kind {
1871+
PatKind::Missing => unreachable!(),
18711872
PatKind::Wild => self.word("_"),
18721873
PatKind::Never => self.word("!"),
18731874
PatKind::Binding(BindingMode(by_ref, mutbl), _, ident, sub) => {

compiler/rustc_hir_typeck/src/expr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
482482

483483
// All of these constitute a read, or match on something that isn't `!`,
484484
// which would require a `NeverToAny` coercion.
485-
hir::PatKind::Binding(_, _, _, _)
485+
hir::PatKind::Missing
486+
| hir::PatKind::Binding(_, _, _, _)
486487
| hir::PatKind::Struct(_, _, _)
487488
| hir::PatKind::TupleStruct(_, _, _)
488489
| hir::PatKind::Tuple(_, _)

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

+2
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
611611
for pat in pats {
612612
self.cat_pattern(discr_place.clone(), pat, &mut |place, pat| {
613613
match &pat.kind {
614+
PatKind::Missing => unreachable!(),
614615
PatKind::Binding(.., opt_sub_pat) => {
615616
// If the opt_sub_pat is None, then the binding does not count as
616617
// a wildcard for the purpose of borrowing discr.
@@ -1832,6 +1833,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
18321833
| PatKind::Expr(..)
18331834
| PatKind::Range(..)
18341835
| PatKind::Never
1836+
| PatKind::Missing
18351837
| PatKind::Wild
18361838
| PatKind::Err(_) => {
18371839
// always ok

compiler/rustc_hir_typeck/src/pat.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
341341
};
342342

343343
let ty = match pat.kind {
344-
PatKind::Wild | PatKind::Err(_) => expected,
344+
PatKind::Missing | PatKind::Wild | PatKind::Err(_) => expected,
345345
// We allow any type here; we ensure that the type is uninhabited during match checking.
346346
PatKind::Never => expected,
347347
PatKind::Expr(PatExpr { kind: PatExprKind::Path(qpath), hir_id, span }) => {
@@ -505,9 +505,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
505505
},
506506

507507
// Ref patterns are complicated, we handle them in `check_pat_ref`.
508-
PatKind::Ref(..) => AdjustMode::Pass,
508+
PatKind::Ref(..)
509+
// No need to do anything on a missing pattern.
510+
| PatKind::Missing
509511
// A `_` pattern works with any expected type, so there's no need to do anything.
510-
PatKind::Wild
512+
| PatKind::Wild
511513
// A malformed pattern doesn't have an expected type, so let's just accept any type.
512514
| PatKind::Err(_)
513515
// Bindings also work with whatever the expected type is,
@@ -1032,7 +1034,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10321034
| PatKind::Tuple(..)
10331035
| PatKind::Slice(..) => "binding",
10341036

1035-
PatKind::Wild
1037+
PatKind::Missing
1038+
| PatKind::Wild
10361039
| PatKind::Never
10371040
| PatKind::Binding(..)
10381041
| PatKind::Box(..)

compiler/rustc_lint/src/builtin.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -779,21 +779,19 @@ impl EarlyLintPass for AnonymousParameters {
779779
}
780780
if let ast::AssocItemKind::Fn(box Fn { ref sig, .. }) = it.kind {
781781
for arg in sig.decl.inputs.iter() {
782-
if let ast::PatKind::Ident(_, ident, None) = arg.pat.kind {
783-
if ident.name == kw::Empty {
784-
let ty_snip = cx.sess().source_map().span_to_snippet(arg.ty.span);
782+
if let ast::PatKind::Missing = arg.pat.kind {
783+
let ty_snip = cx.sess().source_map().span_to_snippet(arg.ty.span);
785784

786-
let (ty_snip, appl) = if let Ok(ref snip) = ty_snip {
787-
(snip.as_str(), Applicability::MachineApplicable)
788-
} else {
789-
("<type>", Applicability::HasPlaceholders)
790-
};
791-
cx.emit_span_lint(
792-
ANONYMOUS_PARAMETERS,
793-
arg.pat.span,
794-
BuiltinAnonymousParams { suggestion: (arg.pat.span, appl), ty_snip },
795-
);
796-
}
785+
let (ty_snip, appl) = if let Ok(ref snip) = ty_snip {
786+
(snip.as_str(), Applicability::MachineApplicable)
787+
} else {
788+
("<type>", Applicability::HasPlaceholders)
789+
};
790+
cx.emit_span_lint(
791+
ANONYMOUS_PARAMETERS,
792+
arg.pat.span,
793+
BuiltinAnonymousParams { suggestion: (arg.pat.span, appl), ty_snip },
794+
);
797795
}
798796
}
799797
}

compiler/rustc_lint/src/unused.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,8 @@ impl EarlyLintPass for UnusedParens {
12011201
// Do not lint on `(..)` as that will result in the other arms being useless.
12021202
Paren(_)
12031203
// The other cases do not contain sub-patterns.
1204-
| Wild | Never | Rest | Expr(..) | MacCall(..) | Range(..) | Ident(.., None) | Path(..) | Err(_) => {},
1204+
| Missing | Wild | Never | Rest | Expr(..) | MacCall(..) | Range(..) | Ident(.., None)
1205+
| Path(..) | Err(_) => {},
12051206
// These are list-like patterns; parens can always be removed.
12061207
TupleStruct(_, _, ps) | Tuple(ps) | Slice(ps) | Or(ps) => for p in ps {
12071208
self.check_unused_parens_pat(cx, p, false, false, keep_space);

compiler/rustc_middle/src/thir.rs

+3
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,9 @@ pub struct Ascription<'tcx> {
747747

748748
#[derive(Clone, Debug, HashStable, TypeVisitable)]
749749
pub enum PatKind<'tcx> {
750+
/// A missing pattern, e.g. for an anonymous param in a bare fn like `fn f(u32)`.
751+
Missing,
752+
750753
/// A wildcard pattern: `_`.
751754
Wild,
752755

compiler/rustc_middle/src/thir/visit.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ pub(crate) fn for_each_immediate_subpat<'a, 'tcx>(
250250
mut callback: impl FnMut(&'a Pat<'tcx>),
251251
) {
252252
match &pat.kind {
253-
PatKind::Wild
253+
PatKind::Missing
254+
| PatKind::Wild
254255
| PatKind::Binding { subpattern: None, .. }
255256
| PatKind::Constant { value: _ }
256257
| PatKind::Range(_)

compiler/rustc_mir_build/src/builder/matches/match_pair.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'tcx> MatchPairTree<'tcx> {
118118
let place = place_builder.try_to_place(cx);
119119
let mut subpairs = Vec::new();
120120
let test_case = match pattern.kind {
121-
PatKind::Wild | PatKind::Error(_) => None,
121+
PatKind::Missing | PatKind::Wild | PatKind::Error(_) => None,
122122

123123
PatKind::Or { ref pats } => Some(TestCase::Or {
124124
pats: pats.iter().map(|pat| FlatPat::new(place_builder.clone(), pat, cx)).collect(),

compiler/rustc_mir_build/src/builder/matches/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
920920

921921
PatKind::Constant { .. }
922922
| PatKind::Range { .. }
923+
| PatKind::Missing
923924
| PatKind::Wild
924925
| PatKind::Never
925926
| PatKind::Error(_) => {}

compiler/rustc_mir_build/src/check_unsafety.rs

+1
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
315315
fn visit_pat(&mut self, pat: &'a Pat<'tcx>) {
316316
if self.in_union_destructure {
317317
match pat.kind {
318+
PatKind::Missing => unreachable!(),
318319
// binding to a variable allows getting stuff out of variable
319320
PatKind::Binding { .. }
320321
// match is conditional on having this value

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
290290
let mut span = pat.span;
291291

292292
let kind = match pat.kind {
293+
hir::PatKind::Missing => PatKind::Missing,
294+
293295
hir::PatKind::Wild => PatKind::Wild,
294296

295297
hir::PatKind::Never => PatKind::Never,

compiler/rustc_mir_build/src/thir/print.rs

+1
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
664664
print_indented!(self, "kind: PatKind {", depth_lvl);
665665

666666
match pat_kind {
667+
PatKind::Missing => unreachable!(),
667668
PatKind::Wild => {
668669
print_indented!(self, "Wild", depth_lvl + 1);
669670
}

compiler/rustc_parse/src/parser/item.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2987,9 +2987,7 @@ impl<'a> Parser<'a> {
29872987
}
29882988
match ty {
29892989
Ok(ty) => {
2990-
let ident = Ident::new(kw::Empty, this.prev_token.span);
2991-
let bm = BindingMode::NONE;
2992-
let pat = this.mk_pat_ident(ty.span, bm, ident);
2990+
let pat = this.mk_pat(ty.span, PatKind::Missing);
29932991
(pat, ty)
29942992
}
29952993
// If this is a C-variadic argument and we hit an error, return the error.

compiler/rustc_passes/src/input_stats.rs

+2
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
295295
record_variants!(
296296
(self, p, p.kind, Some(p.hir_id), hir, Pat, PatKind),
297297
[
298+
Missing,
298299
Wild,
299300
Binding,
300301
Struct,
@@ -597,6 +598,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
597598
record_variants!(
598599
(self, p, p.kind, None, ast, Pat, PatKind),
599600
[
601+
Missing,
600602
Wild,
601603
Ident,
602604
Struct,

compiler/rustc_passes/src/liveness.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ use rustc_middle::query::Providers;
9696
use rustc_middle::span_bug;
9797
use rustc_middle::ty::{self, RootVariableMinCaptureList, Ty, TyCtxt};
9898
use rustc_session::lint;
99-
use rustc_span::{BytePos, Span, Symbol, kw, sym};
99+
use rustc_span::{BytePos, Span, Symbol, sym};
100100
use tracing::{debug, instrument};
101101

102102
use self::LiveNodeKind::*;
@@ -1481,9 +1481,6 @@ impl<'tcx> Liveness<'_, 'tcx> {
14811481

14821482
fn should_warn(&self, var: Variable) -> Option<String> {
14831483
let name = self.ir.variable_name(var);
1484-
if name == kw::Empty {
1485-
return None;
1486-
}
14871484
let name = name.as_str();
14881485
if name.as_bytes()[0] == b'_' {
14891486
return None;

compiler/rustc_pattern_analysis/src/rustc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
460460
PatKind::AscribeUserType { subpattern, .. }
461461
| PatKind::ExpandedConstant { subpattern, .. } => return self.lower_pat(subpattern),
462462
PatKind::Binding { subpattern: Some(subpat), .. } => return self.lower_pat(subpat),
463-
PatKind::Binding { subpattern: None, .. } | PatKind::Wild => {
463+
PatKind::Missing | PatKind::Binding { subpattern: None, .. } | PatKind::Wild => {
464464
ctor = Wildcard;
465465
fields = vec![];
466466
arity = 0;

0 commit comments

Comments
 (0)