Skip to content

Commit 90357a9

Browse files
committed
internal: merge hir::BinaryOp and ast::BinOp
1 parent fe4f059 commit 90357a9

File tree

12 files changed

+160
-233
lines changed

12 files changed

+160
-233
lines changed

crates/hir_def/src/body/lower.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use crate::{
2727
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
2828
db::DefDatabase,
2929
expr::{
30-
dummy_expr_id, Array, BinaryOp, BindingAnnotation, Expr, ExprId, Label, LabelId, Literal,
31-
MatchArm, MatchGuard, Pat, PatId, RecordFieldPat, RecordLitField, Statement,
30+
dummy_expr_id, Array, BindingAnnotation, Expr, ExprId, Label, LabelId, Literal, MatchArm,
31+
MatchGuard, Pat, PatId, RecordFieldPat, RecordLitField, Statement,
3232
},
3333
intern::Interned,
3434
item_scope::BuiltinShadowMode,
@@ -508,7 +508,7 @@ impl ExprCollector<'_> {
508508
ast::Expr::BinExpr(e) => {
509509
let lhs = self.collect_expr_opt(e.lhs());
510510
let rhs = self.collect_expr_opt(e.rhs());
511-
let op = e.op_kind().map(BinaryOp::from);
511+
let op = e.op_kind();
512512
self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
513513
}
514514
ast::Expr::TupleExpr(e) => {

crates/ide_assists/src/handlers/apply_demorgan.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<(
2626
let expr = ctx.find_node_at_offset::<ast::BinExpr>()?;
2727
let op = expr.op_kind()?;
2828
let op_range = expr.op_token()?.text_range();
29-
let opposite_op = opposite_logic_op(op)?;
29+
30+
let opposite_op = match op {
31+
ast::BinaryOp::LogicOp(ast::LogicOp::And) => "||",
32+
ast::BinaryOp::LogicOp(ast::LogicOp::Or) => "&&",
33+
_ => return None,
34+
};
35+
3036
let cursor_in_range = op_range.contains_range(ctx.frange.range);
3137
if !cursor_in_range {
3238
return None;
@@ -136,15 +142,6 @@ pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<(
136142
)
137143
}
138144

139-
// Return the opposite text for a given logical operator, if it makes sense
140-
fn opposite_logic_op(kind: ast::BinOp) -> Option<&'static str> {
141-
match kind {
142-
ast::BinOp::BooleanOr => Some("&&"),
143-
ast::BinOp::BooleanAnd => Some("||"),
144-
_ => None,
145-
}
146-
}
147-
148145
#[cfg(test)]
149146
mod tests {
150147
use crate::tests::{check_assist, check_assist_not_applicable};

crates/ide_assists/src/handlers/extract_function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ fn expr_require_exclusive_access(ctx: &AssistContext, expr: &ast::Expr) -> Optio
892892
let parent = expr.syntax().parent()?;
893893

894894
if let Some(bin_expr) = ast::BinExpr::cast(parent.clone()) {
895-
if bin_expr.op_kind()?.is_assignment() {
895+
if matches!(bin_expr.op_kind()?, ast::BinaryOp::Assignment { .. }) {
896896
return Some(bin_expr.lhs()?.syntax() == expr.syntax());
897897
}
898898
return Some(false);

crates/ide_assists/src/handlers/flip_binexpr.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use syntax::ast::{AstNode, BinExpr, BinOp};
1+
use syntax::ast::{self, AstNode, BinExpr};
22

33
use crate::{AssistContext, AssistId, AssistKind, Assists};
44

@@ -56,14 +56,19 @@ enum FlipAction {
5656
DontFlip,
5757
}
5858

59-
impl From<BinOp> for FlipAction {
60-
fn from(op_kind: BinOp) -> Self {
59+
impl From<ast::BinaryOp> for FlipAction {
60+
fn from(op_kind: ast::BinaryOp) -> Self {
6161
match op_kind {
62-
kind if kind.is_assignment() => FlipAction::DontFlip,
63-
BinOp::GreaterTest => FlipAction::FlipAndReplaceOp("<"),
64-
BinOp::GreaterEqualTest => FlipAction::FlipAndReplaceOp("<="),
65-
BinOp::LesserTest => FlipAction::FlipAndReplaceOp(">"),
66-
BinOp::LesserEqualTest => FlipAction::FlipAndReplaceOp(">="),
62+
ast::BinaryOp::Assignment { .. } => FlipAction::DontFlip,
63+
ast::BinaryOp::CmpOp(ast::CmpOp::Ord { ordering, strict }) => {
64+
let rev_op = match (ordering, strict) {
65+
(ast::Ordering::Less, true) => ">",
66+
(ast::Ordering::Less, false) => ">=",
67+
(ast::Ordering::Greater, true) => "<",
68+
(ast::Ordering::Greater, false) => "<=",
69+
};
70+
FlipAction::FlipAndReplaceOp(rev_op)
71+
}
6772
_ => FlipAction::Flip,
6873
}
6974
}

crates/ide_assists/src/handlers/pull_assignment_up.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub(crate) fn pull_assignment_up(acc: &mut Assists, ctx: &AssistContext) -> Opti
3939
let assign_expr = ctx.find_node_at_offset::<ast::BinExpr>()?;
4040

4141
let op_kind = assign_expr.op_kind()?;
42-
if op_kind != ast::BinOp::Assignment {
42+
if op_kind != (ast::BinaryOp::Assignment { op: None }) {
4343
cov_mark::hit!(test_cant_pull_non_assignments);
4444
return None;
4545
}
@@ -143,7 +143,7 @@ impl<'a> AssignmentsCollector<'a> {
143143
}
144144

145145
fn collect_expr(&mut self, expr: &ast::BinExpr) -> Option<()> {
146-
if expr.op_kind()? == ast::BinOp::Assignment
146+
if expr.op_kind()? == (ast::BinaryOp::Assignment { op: None })
147147
&& is_equivalent(self.sema, &expr.lhs()?, &self.common_lhs)
148148
{
149149
self.assignments.push((expr.clone(), expr.rhs()?));

crates/ide_assists/src/utils.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,17 @@ pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr {
210210
fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
211211
match expr {
212212
ast::Expr::BinExpr(bin) => match bin.op_kind()? {
213-
ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()),
214-
ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()),
215-
ast::BinOp::LesserTest => bin.replace_op(T![>=]).map(|it| it.into()),
216-
ast::BinOp::LesserEqualTest => bin.replace_op(T![>]).map(|it| it.into()),
217-
ast::BinOp::GreaterTest => bin.replace_op(T![<=]).map(|it| it.into()),
218-
ast::BinOp::GreaterEqualTest => bin.replace_op(T![<]).map(|it| it.into()),
213+
ast::BinaryOp::CmpOp(op) => {
214+
let rev_op = match op {
215+
ast::CmpOp::Eq { negated: false } => T![!=],
216+
ast::CmpOp::Eq { negated: true } => T![==],
217+
ast::CmpOp::Ord { ordering: ast::Ordering::Less, strict: true } => T![>=],
218+
ast::CmpOp::Ord { ordering: ast::Ordering::Less, strict: false } => T![>],
219+
ast::CmpOp::Ord { ordering: ast::Ordering::Greater, strict: true } => T![<=],
220+
ast::CmpOp::Ord { ordering: ast::Ordering::Greater, strict: false } => T![<],
221+
};
222+
bin.replace_op(rev_op).map(ast::Expr::from)
223+
}
219224
// Parenthesize other expressions before prefixing `!`
220225
_ => Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))),
221226
},

crates/ide_assists/src/utils/gen_trait_fn_body.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This module contains functions to generate default trait impl function bodies where possible.
22
33
use syntax::{
4-
ast::{self, edit::AstNodeEdit, make, AstNode, NameOwner},
4+
ast::{self, edit::AstNodeEdit, make, AstNode, BinaryOp, CmpOp, LogicOp, NameOwner},
55
ted,
66
};
77

@@ -325,7 +325,7 @@ fn gen_hash_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
325325
fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
326326
fn gen_eq_chain(expr: Option<ast::Expr>, cmp: ast::Expr) -> Option<ast::Expr> {
327327
match expr {
328-
Some(expr) => Some(make::expr_op(ast::BinOp::BooleanAnd, expr, cmp)),
328+
Some(expr) => Some(make::expr_bin_op(expr, BinaryOp::LogicOp(LogicOp::And), cmp)),
329329
None => Some(cmp),
330330
}
331331
}
@@ -362,7 +362,8 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
362362
let lhs = make::expr_call(make_discriminant()?, make::arg_list(Some(lhs_name.clone())));
363363
let rhs_name = make::expr_path(make::ext::ident_path("other"));
364364
let rhs = make::expr_call(make_discriminant()?, make::arg_list(Some(rhs_name.clone())));
365-
let eq_check = make::expr_op(ast::BinOp::EqualityTest, lhs, rhs);
365+
let eq_check =
366+
make::expr_bin_op(lhs, BinaryOp::CmpOp(CmpOp::Eq { negated: false }), rhs);
366367

367368
let mut case_count = 0;
368369
let mut arms = vec![];
@@ -386,7 +387,11 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
386387

387388
let lhs = make::expr_path(make::ext::ident_path(l_name));
388389
let rhs = make::expr_path(make::ext::ident_path(r_name));
389-
let cmp = make::expr_op(ast::BinOp::EqualityTest, lhs, rhs);
390+
let cmp = make::expr_bin_op(
391+
lhs,
392+
BinaryOp::CmpOp(CmpOp::Eq { negated: false }),
393+
rhs,
394+
);
390395
expr = gen_eq_chain(expr, cmp);
391396
}
392397

@@ -415,7 +420,11 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
415420

416421
let lhs = make::expr_path(make::ext::ident_path(&l_name));
417422
let rhs = make::expr_path(make::ext::ident_path(&r_name));
418-
let cmp = make::expr_op(ast::BinOp::EqualityTest, lhs, rhs);
423+
let cmp = make::expr_bin_op(
424+
lhs,
425+
BinaryOp::CmpOp(CmpOp::Eq { negated: false }),
426+
rhs,
427+
);
419428
expr = gen_eq_chain(expr, cmp);
420429
}
421430

@@ -455,7 +464,8 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
455464
let lhs = make::expr_field(lhs, &field.name()?.to_string());
456465
let rhs = make::expr_path(make::ext::ident_path("other"));
457466
let rhs = make::expr_field(rhs, &field.name()?.to_string());
458-
let cmp = make::expr_op(ast::BinOp::EqualityTest, lhs, rhs);
467+
let cmp =
468+
make::expr_bin_op(lhs, BinaryOp::CmpOp(CmpOp::Eq { negated: false }), rhs);
459469
expr = gen_eq_chain(expr, cmp);
460470
}
461471
make::block_expr(None, expr).indent(ast::edit::IndentLevel(1))
@@ -469,7 +479,8 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
469479
let lhs = make::expr_field(lhs, &idx);
470480
let rhs = make::expr_path(make::ext::ident_path("other"));
471481
let rhs = make::expr_field(rhs, &idx);
472-
let cmp = make::expr_op(ast::BinOp::EqualityTest, lhs, rhs);
482+
let cmp =
483+
make::expr_bin_op(lhs, BinaryOp::CmpOp(CmpOp::Eq { negated: false }), rhs);
473484
expr = gen_eq_chain(expr, cmp);
474485
}
475486
make::block_expr(None, expr).indent(ast::edit::IndentLevel(1))

crates/ide_db/src/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ fn reference_access(def: &Definition, name_ref: &ast::NameRef) -> Option<Referen
655655
match_ast! {
656656
match (node) {
657657
ast::BinExpr(expr) => {
658-
if expr.op_kind()?.is_assignment() {
658+
if matches!(expr.op_kind()?, ast::BinaryOp::Assignment { .. }) {
659659
// If the variable or field ends on the LHS's end then it's a Write (covers fields and locals).
660660
// FIXME: This is not terribly accurate.
661661
if let Some(lhs) = expr.lhs() {

crates/syntax/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ use crate::{
1818
};
1919

2020
pub use self::{
21-
expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind},
21+
expr_ext::{ArrayExprKind, Effect, ElseBranch, LiteralKind},
2222
generated::{nodes::*, tokens::*},
2323
node_ext::{
2424
AttrKind, AttrsOwnerNode, FieldKind, Macro, NameLike, NameOrNameRef, PathSegmentKind,
2525
SelfParamKind, SlicePatComponents, StructKind, TypeBoundKind, VisibilityKind,
2626
},
27-
operators::{ArithOp, BinaryOp, CmpOp, LogicOp, RangeOp, UnaryOp, Ordering},
27+
operators::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp},
2828
token_ext::{
2929
CommentKind, CommentPlacement, CommentShape, FormatSpecifier, HasFormatSpecifier, IsString,
3030
QuoteOffsets, Radix,

0 commit comments

Comments
 (0)