Skip to content

Commit b5383b8

Browse files
committed
Auto merge of #62075 - Centril:guardless-match-arms, r=petrochenkov
Remove `ast::Guard` With the introduction of `ast::ExprKind::Let` in #60861, the `ast::Guard` structure is now redundant in terms of representing [`if let` guards](#51114) in AST since it can be represented by `ExprKind::Let` syntactically. Therefore, we remove `ast::Guard` here. However, we keep `hir::Guard` because the semantic representation is a different matter and this story is more unclear right now (might involve `goto 'arm` in HIR or something...). r? @petrochenkov
2 parents a96ba96 + 4d53714 commit b5383b8

File tree

8 files changed

+14
-36
lines changed

8 files changed

+14
-36
lines changed

src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@ impl<'a> LoweringContext<'a> {
13821382
attrs: self.lower_attrs(&arm.attrs),
13831383
pats: arm.pats.iter().map(|x| self.lower_pat(x)).collect(),
13841384
guard: match arm.guard {
1385-
Some(Guard::If(ref x)) => Some(hir::Guard::If(P(self.lower_expr(x)))),
1385+
Some(ref x) => Some(hir::Guard::If(P(self.lower_expr(x)))),
13861386
_ => None,
13871387
},
13881388
body: P(self.lower_expr(&arm.body)),

src/librustc_resolve/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3065,7 +3065,7 @@ impl<'a> Resolver<'a> {
30653065
// This has to happen *after* we determine which pat_idents are variants.
30663066
self.check_consistent_bindings(&arm.pats);
30673067

3068-
if let Some(ast::Guard::If(ref expr)) = arm.guard {
3068+
if let Some(ref expr) = arm.guard {
30693069
self.visit_expr(expr)
30703070
}
30713071
self.visit_expr(&arm.body);

src/librustc_save_analysis/dump_visitor.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1617,9 +1617,8 @@ impl<'l, 'tcx, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tcx, '
16171617

16181618
fn visit_arm(&mut self, arm: &'l ast::Arm) {
16191619
self.process_var_decl_multi(&arm.pats);
1620-
match arm.guard {
1621-
Some(ast::Guard::If(ref expr)) => self.visit_expr(expr),
1622-
_ => {}
1620+
if let Some(expr) = &arm.guard {
1621+
self.visit_expr(expr);
16231622
}
16241623
self.visit_expr(&arm.body);
16251624
}

src/libsyntax/ast.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -893,16 +893,11 @@ pub struct Local {
893893
pub struct Arm {
894894
pub attrs: Vec<Attribute>,
895895
pub pats: Vec<P<Pat>>,
896-
pub guard: Option<Guard>,
896+
pub guard: Option<P<Expr>>,
897897
pub body: P<Expr>,
898898
pub span: Span,
899899
}
900900

901-
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
902-
pub enum Guard {
903-
If(P<Expr>),
904-
}
905-
906901
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
907902
pub struct Field {
908903
pub ident: Ident,

src/libsyntax/mut_visit.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,6 @@ pub trait MutVisitor: Sized {
131131
noop_visit_arm(a, self);
132132
}
133133

134-
fn visit_guard(&mut self, g: &mut Guard) {
135-
noop_visit_guard(g, self);
136-
}
137-
138134
fn visit_pat(&mut self, p: &mut P<Pat>) {
139135
noop_visit_pat(p, self);
140136
}
@@ -389,17 +385,11 @@ pub fn noop_visit_arm<T: MutVisitor>(
389385
) {
390386
visit_attrs(attrs, vis);
391387
visit_vec(pats, |pat| vis.visit_pat(pat));
392-
visit_opt(guard, |guard| vis.visit_guard(guard));
388+
visit_opt(guard, |guard| vis.visit_expr(guard));
393389
vis.visit_expr(body);
394390
vis.visit_span(span);
395391
}
396392

397-
pub fn noop_visit_guard<T: MutVisitor>(g: &mut Guard, vis: &mut T) {
398-
match g {
399-
Guard::If(e) => vis.visit_expr(e),
400-
}
401-
}
402-
403393
pub fn noop_visit_ty_constraint<T: MutVisitor>(
404394
AssocTyConstraint { id, ident, kind, span }: &mut AssocTyConstraint,
405395
vis: &mut T

src/libsyntax/parse/parser.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::ast::{AngleBracketedArgs, ParenthesizedArgs, AttrStyle, BareFnTy};
44
use crate::ast::{GenericBound, TraitBoundModifier};
55
use crate::ast::Unsafety;
6-
use crate::ast::{Mod, AnonConst, Arg, Arm, Guard, Attribute, BindingMode, TraitItemKind};
6+
use crate::ast::{Mod, AnonConst, Arg, Arm, Attribute, BindingMode, TraitItemKind};
77
use crate::ast::Block;
88
use crate::ast::{BlockCheckMode, CaptureBy, Movability};
99
use crate::ast::{Constness, Crate};
@@ -3413,7 +3413,7 @@ impl<'a> Parser<'a> {
34133413
let lo = self.token.span;
34143414
let pats = self.parse_pats()?;
34153415
let guard = if self.eat_keyword(kw::If) {
3416-
Some(Guard::If(self.parse_expr()?))
3416+
Some(self.parse_expr()?)
34173417
} else {
34183418
None
34193419
};

src/libsyntax/print/pprust.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -2663,14 +2663,10 @@ impl<'a> State<'a> {
26632663
self.print_outer_attributes(&arm.attrs)?;
26642664
self.print_pats(&arm.pats)?;
26652665
self.s.space()?;
2666-
if let Some(ref g) = arm.guard {
2667-
match g {
2668-
ast::Guard::If(ref e) => {
2669-
self.word_space("if")?;
2670-
self.print_expr(e)?;
2671-
self.s.space()?;
2672-
}
2673-
}
2666+
if let Some(ref e) = arm.guard {
2667+
self.word_space("if")?;
2668+
self.print_expr(e)?;
2669+
self.s.space()?;
26742670
}
26752671
self.word_space("=>")?;
26762672

src/libsyntax/visit.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -834,10 +834,8 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
834834

835835
pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
836836
walk_list!(visitor, visit_pat, &arm.pats);
837-
if let Some(ref g) = &arm.guard {
838-
match g {
839-
Guard::If(ref e) => visitor.visit_expr(e),
840-
}
837+
if let Some(ref e) = &arm.guard {
838+
visitor.visit_expr(e);
841839
}
842840
visitor.visit_expr(&arm.body);
843841
walk_list!(visitor, visit_attribute, &arm.attrs);

0 commit comments

Comments
 (0)