1
Fork 0

Eliminate magic numbers from expression precedence

This commit is contained in:
David Tolnay 2024-11-28 12:47:18 -08:00
parent 539c863eaf
commit 7ced18f329
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
18 changed files with 162 additions and 138 deletions

View file

@ -10,7 +10,7 @@ use std::cell::Cell;
use std::vec;
use rustc_abi::ExternAbi;
use rustc_ast::util::parser::{self, AssocOp, Fixity};
use rustc_ast::util::parser::{self, AssocOp, ExprPrecedence, Fixity};
use rustc_ast_pretty::pp::Breaks::{Consistent, Inconsistent};
use rustc_ast_pretty::pp::{self, Breaks};
use rustc_ast_pretty::pprust::{Comments, PrintState};
@ -1134,7 +1134,7 @@ impl<'a> State<'a> {
fn print_expr_call(&mut self, func: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
let needs_paren = match func.kind {
hir::ExprKind::Field(..) => true,
_ => func.precedence() < parser::PREC_UNAMBIGUOUS,
_ => func.precedence() < ExprPrecedence::Unambiguous,
};
self.print_expr_cond_paren(func, needs_paren);
@ -1148,7 +1148,7 @@ impl<'a> State<'a> {
args: &[hir::Expr<'_>],
) {
let base_args = args;
self.print_expr_cond_paren(receiver, receiver.precedence() < parser::PREC_UNAMBIGUOUS);
self.print_expr_cond_paren(receiver, receiver.precedence() < ExprPrecedence::Unambiguous);
self.word(".");
self.print_ident(segment.ident);
@ -1162,7 +1162,7 @@ impl<'a> State<'a> {
fn print_expr_binary(&mut self, op: hir::BinOp, lhs: &hir::Expr<'_>, rhs: &hir::Expr<'_>) {
let assoc_op = AssocOp::from_ast_binop(op.node);
let binop_prec = assoc_op.precedence() as i8;
let binop_prec = assoc_op.precedence();
let left_prec = lhs.precedence();
let right_prec = rhs.precedence();
@ -1193,7 +1193,7 @@ impl<'a> State<'a> {
fn print_expr_unary(&mut self, op: hir::UnOp, expr: &hir::Expr<'_>) {
self.word(op.as_str());
self.print_expr_cond_paren(expr, expr.precedence() < parser::PREC_PREFIX)
self.print_expr_cond_paren(expr, expr.precedence() < ExprPrecedence::Prefix);
}
fn print_expr_addr_of(
@ -1210,7 +1210,7 @@ impl<'a> State<'a> {
self.print_mutability(mutability, true);
}
}
self.print_expr_cond_paren(expr, expr.precedence() < parser::PREC_PREFIX)
self.print_expr_cond_paren(expr, expr.precedence() < ExprPrecedence::Prefix);
}
fn print_literal(&mut self, lit: &hir::Lit) {
@ -1348,8 +1348,7 @@ impl<'a> State<'a> {
self.print_literal(lit);
}
hir::ExprKind::Cast(expr, ty) => {
let prec = AssocOp::As.precedence() as i8;
self.print_expr_cond_paren(expr, expr.precedence() < prec);
self.print_expr_cond_paren(expr, expr.precedence() < ExprPrecedence::Cast);
self.space();
self.word_space("as");
self.print_type(ty);
@ -1450,27 +1449,25 @@ impl<'a> State<'a> {
self.print_block(blk);
}
hir::ExprKind::Assign(lhs, rhs, _) => {
let prec = AssocOp::Assign.precedence() as i8;
self.print_expr_cond_paren(lhs, lhs.precedence() <= prec);
self.print_expr_cond_paren(lhs, lhs.precedence() <= ExprPrecedence::Assign);
self.space();
self.word_space("=");
self.print_expr_cond_paren(rhs, rhs.precedence() < prec);
self.print_expr_cond_paren(rhs, rhs.precedence() < ExprPrecedence::Assign);
}
hir::ExprKind::AssignOp(op, lhs, rhs) => {
let prec = AssocOp::Assign.precedence() as i8;
self.print_expr_cond_paren(lhs, lhs.precedence() <= prec);
self.print_expr_cond_paren(lhs, lhs.precedence() <= ExprPrecedence::Assign);
self.space();
self.word(op.node.as_str());
self.word_space("=");
self.print_expr_cond_paren(rhs, rhs.precedence() < prec);
self.print_expr_cond_paren(rhs, rhs.precedence() < ExprPrecedence::Assign);
}
hir::ExprKind::Field(expr, ident) => {
self.print_expr_cond_paren(expr, expr.precedence() < parser::PREC_UNAMBIGUOUS);
self.print_expr_cond_paren(expr, expr.precedence() < ExprPrecedence::Unambiguous);
self.word(".");
self.print_ident(ident);
}
hir::ExprKind::Index(expr, index, _) => {
self.print_expr_cond_paren(expr, expr.precedence() < parser::PREC_UNAMBIGUOUS);
self.print_expr_cond_paren(expr, expr.precedence() < ExprPrecedence::Unambiguous);
self.word("[");
self.print_expr(index);
self.word("]");
@ -1484,7 +1481,7 @@ impl<'a> State<'a> {
}
if let Some(expr) = opt_expr {
self.space();
self.print_expr_cond_paren(expr, expr.precedence() < parser::PREC_JUMP);
self.print_expr_cond_paren(expr, expr.precedence() < ExprPrecedence::Jump);
}
}
hir::ExprKind::Continue(destination) => {
@ -1498,13 +1495,13 @@ impl<'a> State<'a> {
self.word("return");
if let Some(expr) = result {
self.word(" ");
self.print_expr_cond_paren(expr, expr.precedence() < parser::PREC_JUMP);
self.print_expr_cond_paren(expr, expr.precedence() < ExprPrecedence::Jump);
}
}
hir::ExprKind::Become(result) => {
self.word("become");
self.word(" ");
self.print_expr_cond_paren(result, result.precedence() < parser::PREC_JUMP);
self.print_expr_cond_paren(result, result.precedence() < ExprPrecedence::Jump);
}
hir::ExprKind::InlineAsm(asm) => {
self.word("asm!");
@ -1529,7 +1526,7 @@ impl<'a> State<'a> {
}
hir::ExprKind::Yield(expr, _) => {
self.word_space("yield");
self.print_expr_cond_paren(expr, expr.precedence() < parser::PREC_JUMP);
self.print_expr_cond_paren(expr, expr.precedence() < ExprPrecedence::Jump);
}
hir::ExprKind::Err(_) => {
self.popen();