Store else token together with else branch
diff --git a/src/expr.rs b/src/expr.rs
index 76c666f..e50cf3a 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -104,9 +104,8 @@
pub attrs: Vec<Attribute>,
pub if_token: Token![if],
pub cond: Box<Expr>,
- pub if_true: Block,
- pub else_token: Option<Token![else]>,
- pub if_false: Option<Box<Expr>>,
+ pub then_branch: Block,
+ pub else_branch: Option<(Token![else], Box<Expr>)>,
}),
/// An `if let` expression with an optional else block
@@ -121,9 +120,8 @@
pub pat: Box<Pat>,
pub eq_token: Token![=],
pub expr: Box<Expr>,
- pub if_true: Block,
- pub else_token: Option<Token![else]>,
- pub if_false: Option<Box<Expr>>,
+ pub then_branch: Block,
+ pub else_branch: Option<(Token![else], Box<Expr>)>,
}),
/// A while loop, with an optional label
@@ -748,7 +746,9 @@
#[cfg(feature = "parsing")]
pub mod parsing {
use super::*;
- use ty::parsing::{qpath, ty_no_eq_after};
+ use ty::parsing::qpath;
+ #[cfg(feature = "full")]
+ use ty::parsing::ty_no_eq_after;
#[cfg(feature = "full")]
use proc_macro2::{Delimiter, Span, TokenNode, TokenStream};
@@ -1476,13 +1476,12 @@
let_token: let_,
eq_token: eq,
expr: Box::new(cond),
- if_true: Block {
+ then_branch: Block {
stmts: then_block.0,
brace_token: then_block.1,
},
if_token: if_,
- else_token: else_block.as_ref().map(|p| Token.0)),
- if_false: else_block.map(|p| Box::new(p.1.into())),
+ else_branch: else_block,
})
));
}
@@ -1497,19 +1496,18 @@
(ExprIf {
attrs: Vec::new(),
cond: Box::new(cond),
- if_true: Block {
+ then_branch: Block {
stmts: then_block.0,
brace_token: then_block.1,
},
if_token: if_,
- else_token: else_block.as_ref().map(|p| Token.0)),
- if_false: else_block.map(|p| Box::new(p.1.into())),
+ else_branch: else_block,
})
));
}
#[cfg(feature = "full")]
- named!(else_block -> (Token![else], Expr), do_parse!(
+ named!(else_block -> (Token![else], Box<Expr>), do_parse!(
else_: keyword!(else) >>
expr: alt!(
syn!(ExprIf) => { Expr::If }
@@ -1527,7 +1525,7 @@
}))
)
) >>
- (else_, expr)
+ (else_, Box::new(expr))
));
#[cfg(feature = "full")]
@@ -2564,21 +2562,20 @@
#[cfg(feature = "full")]
fn maybe_wrap_else(
tokens: &mut Tokens,
- else_token: &Option<Token![else]>,
- if_false: &Option<Box<Expr>>,
+ else_: &Option<(Token![else], Box<Expr>)>,
) {
- if let Some(ref if_false) = *if_false {
- TokensOrDefault(else_token).to_tokens(tokens);
+ if let Some((ref else_token, ref else_)) = *else_ {
+ else_token.to_tokens(tokens);
// If we are not one of the valid expressions to exist in an else
// clause, wrap ourselves in a block.
- match **if_false {
+ match **else_ {
Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
- if_false.to_tokens(tokens);
+ else_.to_tokens(tokens);
}
_ => {
token::Brace::default().surround(tokens, |tokens| {
- if_false.to_tokens(tokens);
+ else_.to_tokens(tokens);
});
}
}
@@ -2591,8 +2588,8 @@
tokens.append_all(self.attrs.outer());
self.if_token.to_tokens(tokens);
wrap_bare_struct(tokens, &self.cond);
- self.if_true.to_tokens(tokens);
- maybe_wrap_else(tokens, &self.else_token, &self.if_false);
+ self.then_branch.to_tokens(tokens);
+ maybe_wrap_else(tokens, &self.else_branch);
}
}
@@ -2605,8 +2602,8 @@
self.pat.to_tokens(tokens);
self.eq_token.to_tokens(tokens);
wrap_bare_struct(tokens, &self.expr);
- self.if_true.to_tokens(tokens);
- maybe_wrap_else(tokens, &self.else_token, &self.if_false);
+ self.then_branch.to_tokens(tokens);
+ maybe_wrap_else(tokens, &self.else_branch);
}
}
diff --git a/src/gen/fold.rs b/src/gen/fold.rs
index 25ddbc6..90a3393 100644
--- a/src/gen/fold.rs
+++ b/src/gen/fold.rs
@@ -1147,9 +1147,11 @@
attrs: FoldHelper::lift(_i . attrs, |it| { _visitor.fold_attribute(it) }),
if_token: Token ! [ if ](tokens_helper(_visitor, &(_i . if_token).0)),
cond: Box::new(_visitor.fold_expr(* _i . cond)),
- if_true: _visitor.fold_block(_i . if_true),
- else_token: (_i . else_token).map(|it| { Token ! [ else ](tokens_helper(_visitor, &(it).0)) }),
- if_false: (_i . if_false).map(|it| { Box::new(_visitor.fold_expr(* it)) }),
+ then_branch: _visitor.fold_block(_i . then_branch),
+ else_branch: (_i . else_branch).map(|it| { (
+ Token ! [ else ](tokens_helper(_visitor, &(( it ) . 0).0)),
+ Box::new(_visitor.fold_expr(* ( it ) . 1)),
+ ) }),
}
}
# [ cfg ( feature = "full" ) ]
@@ -1161,9 +1163,11 @@
pat: Box::new(_visitor.fold_pat(* _i . pat)),
eq_token: Token ! [ = ](tokens_helper(_visitor, &(_i . eq_token).0)),
expr: Box::new(_visitor.fold_expr(* _i . expr)),
- if_true: _visitor.fold_block(_i . if_true),
- else_token: (_i . else_token).map(|it| { Token ! [ else ](tokens_helper(_visitor, &(it).0)) }),
- if_false: (_i . if_false).map(|it| { Box::new(_visitor.fold_expr(* it)) }),
+ then_branch: _visitor.fold_block(_i . then_branch),
+ else_branch: (_i . else_branch).map(|it| { (
+ Token ! [ else ](tokens_helper(_visitor, &(( it ) . 0).0)),
+ Box::new(_visitor.fold_expr(* ( it ) . 1)),
+ ) }),
}
}
# [ cfg ( feature = "full" ) ]
diff --git a/src/gen/visit.rs b/src/gen/visit.rs
index d00b2b2..1a9609e 100644
--- a/src/gen/visit.rs
+++ b/src/gen/visit.rs
@@ -924,9 +924,11 @@
for it in & _i . attrs { _visitor.visit_attribute(it) };
tokens_helper(_visitor, &(& _i . if_token).0);
_visitor.visit_expr(& * _i . cond);
- _visitor.visit_block(& _i . if_true);
- if let Some(ref it) = _i . else_token { tokens_helper(_visitor, &(it).0) };
- if let Some(ref it) = _i . if_false { _visitor.visit_expr(& * * it) };
+ _visitor.visit_block(& _i . then_branch);
+ if let Some(ref it) = _i . else_branch {
+ tokens_helper(_visitor, &(& ( it ) . 0).0);
+ _visitor.visit_expr(& * ( it ) . 1);
+ };
}
# [ cfg ( feature = "full" ) ]
pub fn visit_expr_if_let<'ast, V: Visitor<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast ExprIfLet) {
@@ -936,9 +938,11 @@
_visitor.visit_pat(& * _i . pat);
tokens_helper(_visitor, &(& _i . eq_token).0);
_visitor.visit_expr(& * _i . expr);
- _visitor.visit_block(& _i . if_true);
- if let Some(ref it) = _i . else_token { tokens_helper(_visitor, &(it).0) };
- if let Some(ref it) = _i . if_false { _visitor.visit_expr(& * * it) };
+ _visitor.visit_block(& _i . then_branch);
+ if let Some(ref it) = _i . else_branch {
+ tokens_helper(_visitor, &(& ( it ) . 0).0);
+ _visitor.visit_expr(& * ( it ) . 1);
+ };
}
# [ cfg ( feature = "full" ) ]
pub fn visit_expr_in_place<'ast, V: Visitor<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast ExprInPlace) {
diff --git a/src/gen/visit_mut.rs b/src/gen/visit_mut.rs
index 353c246..a82f430 100644
--- a/src/gen/visit_mut.rs
+++ b/src/gen/visit_mut.rs
@@ -924,9 +924,11 @@
for it in & mut _i . attrs { _visitor.visit_attribute_mut(it) };
tokens_helper(_visitor, &mut (& mut _i . if_token).0);
_visitor.visit_expr_mut(& mut * _i . cond);
- _visitor.visit_block_mut(& mut _i . if_true);
- if let Some(ref mut it) = _i . else_token { tokens_helper(_visitor, &mut (it).0) };
- if let Some(ref mut it) = _i . if_false { _visitor.visit_expr_mut(& mut * * it) };
+ _visitor.visit_block_mut(& mut _i . then_branch);
+ if let Some(ref mut it) = _i . else_branch {
+ tokens_helper(_visitor, &mut (& mut ( it ) . 0).0);
+ _visitor.visit_expr_mut(& mut * ( it ) . 1);
+ };
}
# [ cfg ( feature = "full" ) ]
pub fn visit_expr_if_let_mut<V: VisitorMut + ?Sized>(_visitor: &mut V, _i: &mut ExprIfLet) {
@@ -936,9 +938,11 @@
_visitor.visit_pat_mut(& mut * _i . pat);
tokens_helper(_visitor, &mut (& mut _i . eq_token).0);
_visitor.visit_expr_mut(& mut * _i . expr);
- _visitor.visit_block_mut(& mut _i . if_true);
- if let Some(ref mut it) = _i . else_token { tokens_helper(_visitor, &mut (it).0) };
- if let Some(ref mut it) = _i . if_false { _visitor.visit_expr_mut(& mut * * it) };
+ _visitor.visit_block_mut(& mut _i . then_branch);
+ if let Some(ref mut it) = _i . else_branch {
+ tokens_helper(_visitor, &mut (& mut ( it ) . 0).0);
+ _visitor.visit_expr_mut(& mut * ( it ) . 1);
+ };
}
# [ cfg ( feature = "full" ) ]
pub fn visit_expr_in_place_mut<V: VisitorMut + ?Sized>(_visitor: &mut V, _i: &mut ExprInPlace) {