diff options
| author | bors <bors@rust-lang.org> | 2018-09-01 20:31:29 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-09-01 20:31:29 +0000 |
| commit | 28bcffead74d5e17c6cb1f7de432e37f93a6b50c (patch) | |
| tree | 1abf601a008600ac3a23c22c1ce003c571dee816 /src/libsyntax | |
| parent | f39f218ec33d93e8a1b0ac4282f62ee35e02c18a (diff) | |
| parent | 7a083ca25f14833d704d2efba5ca9b431f6c65ad (diff) | |
| download | rust-28bcffead74d5e17c6cb1f7de432e37f93a6b50c.tar.gz rust-28bcffead74d5e17c6cb1f7de432e37f93a6b50c.zip | |
Auto merge of #53815 - F001:if-let-guard, r=petrochenkov
refactor match guard This is the first step to implement RFC 2294: if-let-guard. Tracking issue: https://github.com/rust-lang/rust/issues/51114 The second step should be introducing another variant `IfLet` in the Guard enum. I separated them into 2 PRs for the convenience of reviewers. r? @petrochenkov
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 7 | ||||
| -rw-r--r-- | src/libsyntax/fold.rs | 12 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 12 | ||||
| -rw-r--r-- | src/libsyntax/visit.rs | 6 |
5 files changed, 32 insertions, 9 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index bd0e0d277ee..72f1791ef7c 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -857,11 +857,16 @@ pub struct Local { pub struct Arm { pub attrs: Vec<Attribute>, pub pats: Vec<P<Pat>>, - pub guard: Option<P<Expr>>, + pub guard: Option<Guard>, pub body: P<Expr>, } #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] +pub enum Guard { + If(P<Expr>), +} + +#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub struct Field { pub ident: Ident, pub expr: P<Expr>, diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 660056e15e0..dff408d2339 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -118,6 +118,10 @@ pub trait Folder : Sized { noop_fold_arm(a, self) } + fn fold_guard(&mut self, g: Guard) -> Guard { + noop_fold_guard(g, self) + } + fn fold_pat(&mut self, p: P<Pat>) -> P<Pat> { noop_fold_pat(p, self) } @@ -354,11 +358,17 @@ pub fn noop_fold_arm<T: Folder>(Arm {attrs, pats, guard, body}: Arm, Arm { attrs: fold_attrs(attrs, fld), pats: pats.move_map(|x| fld.fold_pat(x)), - guard: guard.map(|x| fld.fold_expr(x)), + guard: guard.map(|x| fld.fold_guard(x)), body: fld.fold_expr(body), } } +pub fn noop_fold_guard<T: Folder>(g: Guard, fld: &mut T) -> Guard { + match g { + Guard::If(e) => Guard::If(fld.fold_expr(e)), + } +} + pub fn noop_fold_ty_binding<T: Folder>(b: TypeBinding, fld: &mut T) -> TypeBinding { TypeBinding { id: fld.new_id(b.id), diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 6c50b9c82f2..c741bde7c5f 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -12,7 +12,7 @@ use rustc_target::spec::abi::{self, Abi}; use ast::{AngleBracketedArgs, ParenthesisedArgs, AttrStyle, BareFnTy}; use ast::{GenericBound, TraitBoundModifier}; use ast::Unsafety; -use ast::{Mod, AnonConst, Arg, Arm, Attribute, BindingMode, TraitItemKind}; +use ast::{Mod, AnonConst, Arg, Arm, Guard, Attribute, BindingMode, TraitItemKind}; use ast::Block; use ast::{BlockCheckMode, CaptureBy, Movability}; use ast::{Constness, Crate}; @@ -3533,7 +3533,7 @@ impl<'a> Parser<'a> { self.eat(&token::BinOp(token::Or)); let pats = self.parse_pats()?; let guard = if self.eat_keyword(keywords::If) { - Some(self.parse_expr()?) + Some(Guard::If(self.parse_expr()?)) } else { None }; diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index e78e1afe3a4..85d29a5be89 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -2702,10 +2702,14 @@ impl<'a> State<'a> { self.print_outer_attributes(&arm.attrs)?; self.print_pats(&arm.pats)?; self.s.space()?; - if let Some(ref e) = arm.guard { - self.word_space("if")?; - self.print_expr(e)?; - self.s.space()?; + if let Some(ref g) = arm.guard { + match g { + ast::Guard::If(ref e) => { + self.word_space("if")?; + self.print_expr(e)?; + self.s.space()?; + } + } } self.word_space("=>")?; diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index e57d692faae..77311bf53fd 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -819,7 +819,11 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) { pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) { walk_list!(visitor, visit_pat, &arm.pats); - walk_list!(visitor, visit_expr, &arm.guard); + if let Some(ref g) = &arm.guard { + match g { + Guard::If(ref e) => visitor.visit_expr(e), + } + } visitor.visit_expr(&arm.body); walk_list!(visitor, visit_attribute, &arm.attrs); } |
