Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2bad485

Browse files
authored
Unrolled build for rust-lang#133424
Rollup merge of rust-lang#133424 - Nadrieril:guard-patterns-parsing, r=fee1-dead Parse guard patterns This implements the parsing of [RFC3637 Guard Patterns](https://rust-lang.github.io/rfcs/3637-guard-patterns.html) (see also [tracking issue](rust-lang#129967)). This PR is extracted from rust-lang#129996 with minor modifications. cc `@max-niederman`
2 parents 4d669fb + 2459dbb commit 2bad485

File tree

31 files changed

+328
-81
lines changed

31 files changed

+328
-81
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,11 @@ impl Pat {
627627
| PatKind::Or(s) => s.iter().for_each(|p| p.walk(it)),
628628

629629
// Trivial wrappers over inner patterns.
630-
PatKind::Box(s) | PatKind::Deref(s) | PatKind::Ref(s, _) | PatKind::Paren(s) => {
631-
s.walk(it)
632-
}
630+
PatKind::Box(s)
631+
| PatKind::Deref(s)
632+
| PatKind::Ref(s, _)
633+
| PatKind::Paren(s)
634+
| PatKind::Guard(s, _) => s.walk(it),
633635

634636
// These patterns do not contain subpatterns, skip.
635637
PatKind::Wild
@@ -839,6 +841,9 @@ pub enum PatKind {
839841
// A never pattern `!`.
840842
Never,
841843

844+
/// A guard pattern (e.g., `x if guard(x)`).
845+
Guard(P<Pat>, P<Expr>),
846+
842847
/// Parentheses in patterns used for grouping (i.e., `(PAT)`).
843848
Paren(P<Pat>),
844849

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,10 @@ pub fn walk_pat<T: MutVisitor>(vis: &mut T, pat: &mut P<Pat>) {
15251525
visit_opt(e2, |e| vis.visit_expr(e));
15261526
vis.visit_span(span);
15271527
}
1528+
PatKind::Guard(p, e) => {
1529+
vis.visit_pat(p);
1530+
vis.visit_expr(e);
1531+
}
15281532
PatKind::Tuple(elems) | PatKind::Slice(elems) | PatKind::Or(elems) => {
15291533
visit_thin_vec(elems, |elem| vis.visit_pat(elem))
15301534
}

compiler/rustc_ast/src/visit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,10 @@ pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) -> V::Res
682682
visit_opt!(visitor, visit_expr, lower_bound);
683683
visit_opt!(visitor, visit_expr, upper_bound);
684684
}
685+
PatKind::Guard(subpattern, guard_condition) => {
686+
try_visit!(visitor.visit_pat(subpattern));
687+
try_visit!(visitor.visit_expr(guard_condition));
688+
}
685689
PatKind::Wild | PatKind::Rest | PatKind::Never => {}
686690
PatKind::Err(_guar) => {}
687691
PatKind::Tuple(elems) | PatKind::Slice(elems) | PatKind::Or(elems) => {

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
114114
self.lower_range_end(end, e2.is_some()),
115115
);
116116
}
117+
// FIXME(guard_patterns): lower pattern guards to HIR
118+
PatKind::Guard(inner, _) => pattern = inner,
117119
PatKind::Slice(pats) => break self.lower_pat_slice(pats),
118120
PatKind::Rest => {
119121
// If we reach here the `..` pattern is not semantically allowed.

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
556556
gate_all!(builtin_syntax, "`builtin #` syntax is unstable");
557557
gate_all!(explicit_tail_calls, "`become` expression is experimental");
558558
gate_all!(generic_const_items, "generic const items are experimental");
559+
gate_all!(guard_patterns, "guard patterns are experimental", "consider using match arm guards");
559560
gate_all!(fn_delegation, "functions delegation is not yet fully implemented");
560561
gate_all!(postfix_match, "postfix match is experimental");
561562
gate_all!(mut_ref, "mutable by-reference bindings are experimental");

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,14 @@ impl<'a> State<'a> {
17091709
self.print_expr(e, FixupContext::default());
17101710
}
17111711
}
1712+
PatKind::Guard(subpat, condition) => {
1713+
self.popen();
1714+
self.print_pat(subpat);
1715+
self.space();
1716+
self.word_space("if");
1717+
self.print_expr(condition, FixupContext::default());
1718+
self.pclose();
1719+
}
17121720
PatKind::Slice(elts) => {
17131721
self.word("[");
17141722
self.commasep(Inconsistent, elts, |s, p| s.print_pat(p));

compiler/rustc_expand/src/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ pub fn parse_ast_fragment<'a>(
990990
}
991991
}
992992
AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
993-
AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_allow_top_alt(
993+
AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_allow_top_guard(
994994
None,
995995
RecoverComma::No,
996996
RecoverColon::Yes,

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,8 @@ declare_features! (
505505
(incomplete, generic_const_items, "1.73.0", Some(113521)),
506506
/// Allows registering static items globally, possibly across crates, to iterate over at runtime.
507507
(unstable, global_registration, "1.80.0", Some(125119)),
508+
/// Allows using guards in patterns.
509+
(incomplete, guard_patterns, "CURRENT_RUSTC_VERSION", Some(129967)),
508510
/// Allows using `..=X` as a patterns in slices.
509511
(unstable, half_open_range_patterns_in_slices, "1.66.0", Some(67264)),
510512
/// Allows `if let` guard in match arms.

compiler/rustc_lint/src/unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ impl EarlyLintPass for UnusedParens {
12351235
self.check_unused_parens_pat(cx, &f.pat, false, false, keep_space);
12361236
},
12371237
// Avoid linting on `i @ (p0 | .. | pn)` and `box (p0 | .. | pn)`, #64106.
1238-
Ident(.., Some(p)) | Box(p) | Deref(p) => self.check_unused_parens_pat(cx, p, true, false, keep_space),
1238+
Ident(.., Some(p)) | Box(p) | Deref(p) | Guard(p, _) => self.check_unused_parens_pat(cx, p, true, false, keep_space),
12391239
// Avoid linting on `&(mut x)` as `&mut x` has a different meaning, #55342.
12401240
// Also avoid linting on `& mut? (p0 | .. | pn)`, #64106.
12411241
Ref(p, m) => self.check_unused_parens_pat(cx, p, true, *m == Mutability::Not, keep_space),

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,7 +2631,7 @@ impl<'a> Parser<'a> {
26312631
};
26322632
self.bump(); // Eat `let` token
26332633
let lo = self.prev_token.span;
2634-
let pat = self.parse_pat_allow_top_alt(
2634+
let pat = self.parse_pat_no_top_guard(
26352635
None,
26362636
RecoverComma::Yes,
26372637
RecoverColon::Yes,
@@ -2778,7 +2778,7 @@ impl<'a> Parser<'a> {
27782778
};
27792779
// Try to parse the pattern `for ($PAT) in $EXPR`.
27802780
let pat = match (
2781-
self.parse_pat_allow_top_alt(
2781+
self.parse_pat_allow_top_guard(
27822782
None,
27832783
RecoverComma::Yes,
27842784
RecoverColon::Yes,
@@ -3241,7 +3241,7 @@ impl<'a> Parser<'a> {
32413241
// then we should recover.
32423242
let mut snapshot = this.create_snapshot_for_diagnostic();
32433243
let pattern_follows = snapshot
3244-
.parse_pat_allow_top_alt(
3244+
.parse_pat_no_top_guard(
32453245
None,
32463246
RecoverComma::Yes,
32473247
RecoverColon::Yes,
@@ -3315,43 +3315,37 @@ impl<'a> Parser<'a> {
33153315

33163316
fn parse_match_arm_pat_and_guard(&mut self) -> PResult<'a, (P<Pat>, Option<P<Expr>>)> {
33173317
if self.token == token::OpenDelim(Delimiter::Parenthesis) {
3318-
// Detect and recover from `($pat if $cond) => $arm`.
33193318
let left = self.token.span;
3320-
match self.parse_pat_allow_top_alt(
3319+
let pat = self.parse_pat_no_top_guard(
33213320
None,
33223321
RecoverComma::Yes,
33233322
RecoverColon::Yes,
33243323
CommaRecoveryMode::EitherTupleOrPipe,
3325-
) {
3326-
Ok(pat) => Ok((pat, self.parse_match_arm_guard()?)),
3327-
Err(err)
3328-
if let prev_sp = self.prev_token.span
3329-
&& let true = self.eat_keyword(kw::If) =>
3330-
{
3331-
// We know for certain we've found `($pat if` so far.
3332-
let mut cond = match self.parse_match_guard_condition() {
3333-
Ok(cond) => cond,
3334-
Err(cond_err) => {
3335-
cond_err.cancel();
3336-
return Err(err);
3337-
}
3338-
};
3339-
err.cancel();
3340-
CondChecker::new(self).visit_expr(&mut cond);
3341-
self.eat_to_tokens(&[&token::CloseDelim(Delimiter::Parenthesis)]);
3342-
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
3343-
let right = self.prev_token.span;
3344-
self.dcx().emit_err(errors::ParenthesesInMatchPat {
3345-
span: vec![left, right],
3346-
sugg: errors::ParenthesesInMatchPatSugg { left, right },
3347-
});
3348-
Ok((self.mk_pat(left.to(prev_sp), ast::PatKind::Wild), Some(cond)))
3349-
}
3350-
Err(err) => Err(err),
3324+
)?;
3325+
if let ast::PatKind::Paren(subpat) = &pat.kind
3326+
&& let ast::PatKind::Guard(..) = &subpat.kind
3327+
{
3328+
// Detect and recover from `($pat if $cond) => $arm`.
3329+
// FIXME(guard_patterns): convert this to a normal guard instead
3330+
let span = pat.span;
3331+
let ast::PatKind::Paren(subpat) = pat.into_inner().kind else { unreachable!() };
3332+
let ast::PatKind::Guard(_, mut cond) = subpat.into_inner().kind else {
3333+
unreachable!()
3334+
};
3335+
self.psess.gated_spans.ungate_last(sym::guard_patterns, cond.span);
3336+
CondChecker::new(self).visit_expr(&mut cond);
3337+
let right = self.prev_token.span;
3338+
self.dcx().emit_err(errors::ParenthesesInMatchPat {
3339+
span: vec![left, right],
3340+
sugg: errors::ParenthesesInMatchPatSugg { left, right },
3341+
});
3342+
Ok((self.mk_pat(span, ast::PatKind::Wild), Some(cond)))
3343+
} else {
3344+
Ok((pat, self.parse_match_arm_guard()?))
33513345
}
33523346
} else {
33533347
// Regular parser flow:
3354-
let pat = self.parse_pat_allow_top_alt(
3348+
let pat = self.parse_pat_no_top_guard(
33553349
None,
33563350
RecoverComma::Yes,
33573351
RecoverColon::Yes,

0 commit comments

Comments
 (0)