Skip to content

Commit

Permalink
Merge pull request #260 from mystor/separate-unsafe
Browse files Browse the repository at this point in the history
Separate unsafe blocks into a ExprKind variant
  • Loading branch information
dtolnay authored Dec 5, 2017
2 parents 302afd2 + 3aa0dc7 commit d1efb85
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
40 changes: 32 additions & 8 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,14 @@ ast_enum_of_structs! {
pub or2_token: Token![|],
}),

/// A block (`{ ... }` or `unsafe { ... }`)
/// An unsafe block (`unsafe { ... }`)
pub Unsafe(ExprUnsafe #full {
pub unsafe_token: Token![unsafe],
pub block: Block,
}),

/// A block (`{ ... }`)
pub Block(ExprBlock #full {
pub unsafety: Unsafety,
pub block: Block,
}),

Expand Down Expand Up @@ -628,6 +633,7 @@ fn arm_expr_requires_comma(expr: &Expr) -> bool {
// see https://github.com/rust-lang/rust/blob/eb8f2586e
// /src/libsyntax/parse/classify.rs#L17-L37
match expr.node {
ExprKind::Unsafe(..) |
ExprKind::Block(..) |
ExprKind::If(..) |
ExprKind::IfLet(..) |
Expand Down Expand Up @@ -1192,6 +1198,8 @@ pub mod parsing {
|
syn!(ExprYield) => { ExprKind::Yield }
|
syn!(ExprUnsafe) => { ExprKind::Unsafe }
|
call!(expr_closure, allow_struct)
|
cond_reduce!(allow_block, map!(syn!(ExprBlock), ExprKind::Block))
Expand Down Expand Up @@ -1238,6 +1246,8 @@ pub mod parsing {
|
syn!(ExprYield) => { ExprKind::Yield }
|
syn!(ExprUnsafe) => { ExprKind::Unsafe }
|
syn!(ExprBlock) => { ExprKind::Block }
), Expr::from));

Expand Down Expand Up @@ -1272,7 +1282,6 @@ pub mod parsing {
kind: InPlaceKind::In(in_),
value: Box::new(Expr {
node: ExprBlock {
unsafety: Unsafety::Normal,
block: Block {
stmts: value.0,
brace_token: value.1,
Expand Down Expand Up @@ -1403,7 +1412,6 @@ pub mod parsing {
do_parse!(
else_block: braces!(call!(Block::parse_within)) >>
(ExprKind::Block(ExprBlock {
unsafety: Unsafety::Normal,
block: Block {
stmts: else_block.0,
brace_token: else_block.1,
Expand Down Expand Up @@ -1537,7 +1545,6 @@ pub mod parsing {
body: syn!(Block) >>
(ReturnType::Type(ty, arrow),
ExprKind::Block(ExprBlock {
unsafety: Unsafety::Normal,
block: body,
}).into())
)
Expand Down Expand Up @@ -1738,13 +1745,23 @@ pub mod parsing {
));
}

#[cfg(feature = "full")]
impl Synom for ExprUnsafe {
named!(parse -> Self, do_parse!(
unsafe_: keyword!(unsafe) >>
b: syn!(Block) >>
(ExprUnsafe {
unsafe_token: unsafe_,
block: b,
})
));
}

#[cfg(feature = "full")]
impl Synom for ExprBlock {
named!(parse -> Self, do_parse!(
rules: syn!(Unsafety) >>
b: syn!(Block) >>
(ExprBlock {
unsafety: rules,
block: b,
})
));
Expand Down Expand Up @@ -2567,10 +2584,17 @@ mod printing {
}
}

#[cfg(feature = "full")]
impl ToTokens for ExprUnsafe {
fn to_tokens(&self, tokens: &mut Tokens) {
self.unsafe_token.to_tokens(tokens);
self.block.to_tokens(tokens);
}
}

#[cfg(feature = "full")]
impl ToTokens for ExprBlock {
fn to_tokens(&self, tokens: &mut Tokens) {
self.unsafety.to_tokens(tokens);
self.block.to_tokens(tokens);
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/gen/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ fn fold_expr_type(&mut self, i: ExprType) -> ExprType { fold_expr_type(self, i)

fn fold_expr_unary(&mut self, i: ExprUnary) -> ExprUnary { fold_expr_unary(self, i) }
# [ cfg ( feature = "full" ) ]
fn fold_expr_unsafe(&mut self, i: ExprUnsafe) -> ExprUnsafe { fold_expr_unsafe(self, i) }
# [ cfg ( feature = "full" ) ]
fn fold_expr_while(&mut self, i: ExprWhile) -> ExprWhile { fold_expr_while(self, i) }
# [ cfg ( feature = "full" ) ]
fn fold_expr_while_let(&mut self, i: ExprWhileLet) -> ExprWhileLet { fold_expr_while_let(self, i) }
Expand Down Expand Up @@ -833,7 +835,6 @@ pub fn fold_expr_binary<V: Folder + ?Sized>(_visitor: &mut V, _i: ExprBinary) ->
# [ cfg ( feature = "full" ) ]
pub fn fold_expr_block<V: Folder + ?Sized>(_visitor: &mut V, _i: ExprBlock) -> ExprBlock {
ExprBlock {
unsafety: _visitor.fold_unsafety(_i . unsafety),
block: _visitor.fold_block(_i . block),
}
}
Expand Down Expand Up @@ -1058,6 +1059,11 @@ pub fn fold_expr_kind<V: Folder + ?Sized>(_visitor: &mut V, _i: ExprKind) -> Exp
full!(_visitor.fold_expr_closure(_binding_0)),
)
}
Unsafe(_binding_0, ) => {
Unsafe (
full!(_visitor.fold_expr_unsafe(_binding_0)),
)
}
Block(_binding_0, ) => {
Block (
full!(_visitor.fold_expr_block(_binding_0)),
Expand Down Expand Up @@ -1279,6 +1285,13 @@ pub fn fold_expr_unary<V: Folder + ?Sized>(_visitor: &mut V, _i: ExprUnary) -> E
}
}
# [ cfg ( feature = "full" ) ]
pub fn fold_expr_unsafe<V: Folder + ?Sized>(_visitor: &mut V, _i: ExprUnsafe) -> ExprUnsafe {
ExprUnsafe {
unsafe_token: _i . unsafe_token,
block: _visitor.fold_block(_i . block),
}
}
# [ cfg ( feature = "full" ) ]
pub fn fold_expr_while<V: Folder + ?Sized>(_visitor: &mut V, _i: ExprWhile) -> ExprWhile {
ExprWhile {
cond: Box::new(_visitor.fold_expr(* _i . cond)),
Expand Down
11 changes: 10 additions & 1 deletion src/gen/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ fn visit_expr_type(&mut self, i: &'ast ExprType) { visit_expr_type(self, i) }

fn visit_expr_unary(&mut self, i: &'ast ExprUnary) { visit_expr_unary(self, i) }
# [ cfg ( feature = "full" ) ]
fn visit_expr_unsafe(&mut self, i: &'ast ExprUnsafe) { visit_expr_unsafe(self, i) }
# [ cfg ( feature = "full" ) ]
fn visit_expr_while(&mut self, i: &'ast ExprWhile) { visit_expr_while(self, i) }
# [ cfg ( feature = "full" ) ]
fn visit_expr_while_let(&mut self, i: &'ast ExprWhileLet) { visit_expr_while_let(self, i) }
Expand Down Expand Up @@ -687,7 +689,6 @@ pub fn visit_expr_binary<'ast, V: Visitor<'ast> + ?Sized>(_visitor: &mut V, _i:
}
# [ cfg ( feature = "full" ) ]
pub fn visit_expr_block<'ast, V: Visitor<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast ExprBlock) {
_visitor.visit_unsafety(&_i . unsafety);
_visitor.visit_block(&_i . block);
}
# [ cfg ( feature = "full" ) ]
Expand Down Expand Up @@ -845,6 +846,9 @@ pub fn visit_expr_kind<'ast, V: Visitor<'ast> + ?Sized>(_visitor: &mut V, _i: &'
Closure(ref _binding_0, ) => {
full!(_visitor.visit_expr_closure(&* _binding_0));
}
Unsafe(ref _binding_0, ) => {
full!(_visitor.visit_expr_unsafe(&* _binding_0));
}
Block(ref _binding_0, ) => {
full!(_visitor.visit_expr_block(&* _binding_0));
}
Expand Down Expand Up @@ -998,6 +1002,11 @@ pub fn visit_expr_unary<'ast, V: Visitor<'ast> + ?Sized>(_visitor: &mut V, _i: &
_visitor.visit_expr(&_i . expr);
}
# [ cfg ( feature = "full" ) ]
pub fn visit_expr_unsafe<'ast, V: Visitor<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast ExprUnsafe) {
// Skipped field _i . unsafe_token;
_visitor.visit_block(&_i . block);
}
# [ cfg ( feature = "full" ) ]
pub fn visit_expr_while<'ast, V: Visitor<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast ExprWhile) {
_visitor.visit_expr(&_i . cond);
_visitor.visit_block(&_i . body);
Expand Down
11 changes: 10 additions & 1 deletion src/gen/visit_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ fn visit_expr_type_mut(&mut self, i: &mut ExprType) { visit_expr_type_mut(self,

fn visit_expr_unary_mut(&mut self, i: &mut ExprUnary) { visit_expr_unary_mut(self, i) }
# [ cfg ( feature = "full" ) ]
fn visit_expr_unsafe_mut(&mut self, i: &mut ExprUnsafe) { visit_expr_unsafe_mut(self, i) }
# [ cfg ( feature = "full" ) ]
fn visit_expr_while_mut(&mut self, i: &mut ExprWhile) { visit_expr_while_mut(self, i) }
# [ cfg ( feature = "full" ) ]
fn visit_expr_while_let_mut(&mut self, i: &mut ExprWhileLet) { visit_expr_while_let_mut(self, i) }
Expand Down Expand Up @@ -687,7 +689,6 @@ pub fn visit_expr_binary_mut<V: VisitorMut + ?Sized>(_visitor: &mut V, _i: &mut
}
# [ cfg ( feature = "full" ) ]
pub fn visit_expr_block_mut<V: VisitorMut + ?Sized>(_visitor: &mut V, _i: &mut ExprBlock) {
_visitor.visit_unsafety_mut(&mut _i . unsafety);
_visitor.visit_block_mut(&mut _i . block);
}
# [ cfg ( feature = "full" ) ]
Expand Down Expand Up @@ -845,6 +846,9 @@ pub fn visit_expr_kind_mut<V: VisitorMut + ?Sized>(_visitor: &mut V, _i: &mut Ex
Closure(ref mut _binding_0, ) => {
full!(_visitor.visit_expr_closure_mut(&mut * _binding_0));
}
Unsafe(ref mut _binding_0, ) => {
full!(_visitor.visit_expr_unsafe_mut(&mut * _binding_0));
}
Block(ref mut _binding_0, ) => {
full!(_visitor.visit_expr_block_mut(&mut * _binding_0));
}
Expand Down Expand Up @@ -998,6 +1002,11 @@ pub fn visit_expr_unary_mut<V: VisitorMut + ?Sized>(_visitor: &mut V, _i: &mut E
_visitor.visit_expr_mut(&mut _i . expr);
}
# [ cfg ( feature = "full" ) ]
pub fn visit_expr_unsafe_mut<V: VisitorMut + ?Sized>(_visitor: &mut V, _i: &mut ExprUnsafe) {
// Skipped field _i . unsafe_token;
_visitor.visit_block_mut(&mut _i . block);
}
# [ cfg ( feature = "full" ) ]
pub fn visit_expr_while_mut<V: VisitorMut + ?Sized>(_visitor: &mut V, _i: &mut ExprWhile) {
_visitor.visit_expr_mut(&mut _i . cond);
_visitor.visit_block_mut(&mut _i . body);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub use expr::{Expr, ExprKind, ExprBox, ExprInPlace, ExprArray, ExprCall,
ExprAssign, ExprAssignOp, ExprField, ExprTupField, ExprIndex,
ExprRange, ExprPath, ExprAddrOf, ExprBreak, ExprContinue,
ExprRet, ExprStruct, ExprRepeat, ExprParen, ExprTry, ExprCatch,
ExprGroup, ExprYield};
ExprGroup, ExprYield, ExprUnsafe};

#[cfg(feature = "full")]
pub use expr::{Arm, BindingMode, Block, CaptureBy, FieldPat, FieldValue, Local,
Expand Down
2 changes: 2 additions & 0 deletions tests/test_precedence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ fn test_simple_precedence() {
"{ for i in r { } *some_ptr += 1; }",
"{ loop { break 5; } }",
"{ if true { () }.mthd() }",
"{ for i in unsafe { 20 } { } }",
];

let mut failed = 0;
Expand Down Expand Up @@ -322,6 +323,7 @@ fn syn_brackets(syn_expr: syn::Expr) -> syn::Expr {
ExprKind::Group(_) => unreachable!(),
ExprKind::Paren(p) => paren(self, p.expr.node),
ExprKind::If(..) |
ExprKind::Unsafe(..) |
ExprKind::Block(..) |
ExprKind::IfLet(..) => {
return fold_expr(self, expr);
Expand Down

0 comments on commit d1efb85

Please sign in to comment.