Skip to content

Commit 3cc3486

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 85ed21e + 4d53714 commit 3cc3486

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
@@ -3055,7 +3055,7 @@ impl<'a> Resolver<'a> {
30553055

30563056
self.resolve_pats(&arm.pats, PatternSource::Match);
30573057

3058-
if let Some(ast::Guard::If(ref expr)) = arm.guard {
3058+
if let Some(ref expr) = arm.guard {
30593059
self.visit_expr(expr)
30603060
}
30613061
self.visit_expr(&arm.body);

src/librustc_save_analysis/dump_visitor.rs

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

16101610
fn visit_arm(&mut self, arm: &'l ast::Arm) {
16111611
self.process_var_decl_multi(&arm.pats);
1612-
match arm.guard {
1613-
Some(ast::Guard::If(ref expr)) => self.visit_expr(expr),
1614-
_ => {}
1612+
if let Some(expr) = &arm.guard {
1613+
self.visit_expr(expr);
16151614
}
16161615
self.visit_expr(&arm.body);
16171616
}

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};
@@ -3396,7 +3396,7 @@ impl<'a> Parser<'a> {
33963396
let lo = self.token.span;
33973397
let pats = self.parse_pats()?;
33983398
let guard = if self.eat_keyword(kw::If) {
3399-
Some(Guard::If(self.parse_expr()?))
3399+
Some(self.parse_expr()?)
34003400
} else {
34013401
None
34023402
};

src/libsyntax/print/pprust.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -2653,14 +2653,10 @@ impl<'a> State<'a> {
26532653
self.print_outer_attributes(&arm.attrs)?;
26542654
self.print_pats(&arm.pats)?;
26552655
self.s.space()?;
2656-
if let Some(ref g) = arm.guard {
2657-
match g {
2658-
ast::Guard::If(ref e) => {
2659-
self.word_space("if")?;
2660-
self.print_expr(e)?;
2661-
self.s.space()?;
2662-
}
2663-
}
2656+
if let Some(ref e) = arm.guard {
2657+
self.word_space("if")?;
2658+
self.print_expr(e)?;
2659+
self.s.space()?;
26642660
}
26652661
self.word_space("=>")?;
26662662

src/libsyntax/visit.rs

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

827827
pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
828828
walk_list!(visitor, visit_pat, &arm.pats);
829-
if let Some(ref g) = &arm.guard {
830-
match g {
831-
Guard::If(ref e) => visitor.visit_expr(e),
832-
}
829+
if let Some(ref e) = &arm.guard {
830+
visitor.visit_expr(e);
833831
}
834832
visitor.visit_expr(&arm.body);
835833
walk_list!(visitor, visit_attribute, &arm.attrs);

0 commit comments

Comments
 (0)