Skip to content

Commit

Permalink
pprust: increase precedence of block-like exprs
Browse files Browse the repository at this point in the history
  • Loading branch information
spernsteiner committed Sep 7, 2017
1 parent 347db06 commit 3454d99
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
11 changes: 5 additions & 6 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2292,12 +2292,6 @@ fn expr_precedence(expr: &hir::Expr) -> i8 {
hir::ExprRet(..) |
hir::ExprYield(..) => PREC_JUMP,

hir::ExprIf(..) |
hir::ExprWhile(..) |
hir::ExprLoop(..) |
hir::ExprMatch(..) |
hir::ExprBlock(..) => PREC_BLOCK,

// Binop-like expr kinds, handled by `AssocOp`.
hir::ExprBinary(op, _, _) => bin_op_to_assoc_op(op.node).precedence() as i8,

Expand Down Expand Up @@ -2326,6 +2320,11 @@ fn expr_precedence(expr: &hir::Expr) -> i8 {
hir::ExprTup(..) |
hir::ExprLit(..) |
hir::ExprPath(..) |
hir::ExprIf(..) |
hir::ExprWhile(..) |
hir::ExprLoop(..) |
hir::ExprMatch(..) |
hir::ExprBlock(..) |
hir::ExprStruct(..) => PREC_PAREN,
}
}
Expand Down
20 changes: 9 additions & 11 deletions src/libsyntax/util/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ impl AssocOp {
pub const PREC_RESET: i8 = -100;
pub const PREC_CLOSURE: i8 = -40;
pub const PREC_JUMP: i8 = -30;
pub const PREC_BLOCK: i8 = -20;
pub const PREC_RANGE: i8 = -10;
// The range 2 ... 14 is reserved for AssocOp binary operator precedences.
pub const PREC_PREFIX: i8 = 50;
Expand All @@ -236,16 +235,6 @@ pub fn expr_precedence(expr: &ast::Expr) -> i8 {
ExprKind::Ret(..) |
ExprKind::Yield(..) => PREC_JUMP,

ExprKind::If(..) |
ExprKind::IfLet(..) |
ExprKind::While(..) |
ExprKind::WhileLet(..) |
ExprKind::ForLoop(..) |
ExprKind::Loop(..) |
ExprKind::Match(..) |
ExprKind::Block(..) |
ExprKind::Catch(..) => PREC_BLOCK,

// `Range` claims to have higher precedence than `Assign`, but `x .. x = x` fails to parse,
// instead of parsing as `(x .. x) = x`. Giving `Range` a lower precedence ensures that
// `pprust` will add parentheses in the right places to get the desired parse.
Expand Down Expand Up @@ -284,6 +273,15 @@ pub fn expr_precedence(expr: &ast::Expr) -> i8 {
ExprKind::Lit(..) |
ExprKind::Path(..) |
ExprKind::Paren(..) |
ExprKind::If(..) |
ExprKind::IfLet(..) |
ExprKind::While(..) |
ExprKind::WhileLet(..) |
ExprKind::ForLoop(..) |
ExprKind::Loop(..) |
ExprKind::Match(..) |
ExprKind::Block(..) |
ExprKind::Catch(..) |
ExprKind::Struct(..) => PREC_PAREN,
}
}
Expand Down

0 comments on commit 3454d99

Please sign in to comment.