1
Fork 0

Introduce AssocOp::Binary.

It mirrors `ExprKind::Binary`, and contains a `BinOpKind`. This makes
`AssocOp` more like `ExprKind`. Note that the variants removed from
`AssocOp` are all named differently to `BinOpToken`, e.g. `Multiply`
instead of `Mul`, so that's an inconsistency removed.

The commit adds `precedence` and `fixity` methods to `BinOpKind`, and
calls them from the corresponding methods in `AssocOp`. This avoids the
need to create an `AssocOp` from a `BinOpKind` in a bunch of places, and
`AssocOp::from_ast_binop` is removed.

`AssocOp::to_ast_binop` is also no longer needed.

Overall things are shorter and nicer.
This commit is contained in:
Nicholas Nethercote 2024-12-19 18:24:07 +11:00
parent a8364f3b2a
commit ceafbad81f
10 changed files with 152 additions and 256 deletions

View file

@ -1516,10 +1516,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
ty::ExprKind::Binop(op) => {
let (_, _, c1, c2) = expr.binop_args();
let precedence = |binop: crate::mir::BinOp| {
use rustc_ast::util::parser::AssocOp;
AssocOp::from_ast_binop(binop.to_hir_binop()).precedence()
};
let precedence = |binop: crate::mir::BinOp| binop.to_hir_binop().precedence();
let op_precedence = precedence(op);
let formatted_op = op.to_hir_binop().as_str();
let (lhs_parenthesized, rhs_parenthesized) = match (c1.kind(), c2.kind()) {