Skip to content

Commit 3e3e69b

Browse files
committed
Use BinOpKind instead of BinOp for function args where possible.
Because it's nice to avoid passing in unnecessary data.
1 parent ef6e4d7 commit 3e3e69b

File tree

7 files changed

+77
-78
lines changed

7 files changed

+77
-78
lines changed

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -274,22 +274,22 @@ impl<'a> State<'a> {
274274

275275
fn print_expr_binary(
276276
&mut self,
277-
op: ast::BinOp,
277+
op: ast::BinOpKind,
278278
lhs: &ast::Expr,
279279
rhs: &ast::Expr,
280280
fixup: FixupContext,
281281
) {
282-
let binop_prec = op.node.precedence();
282+
let binop_prec = op.precedence();
283283
let left_prec = lhs.precedence();
284284
let right_prec = rhs.precedence();
285285

286-
let (mut left_needs_paren, right_needs_paren) = match op.node.fixity() {
286+
let (mut left_needs_paren, right_needs_paren) = match op.fixity() {
287287
Fixity::Left => (left_prec < binop_prec, right_prec <= binop_prec),
288288
Fixity::Right => (left_prec <= binop_prec, right_prec < binop_prec),
289289
Fixity::None => (left_prec <= binop_prec, right_prec <= binop_prec),
290290
};
291291

292-
match (&lhs.kind, op.node) {
292+
match (&lhs.kind, op) {
293293
// These cases need parens: `x as i32 < y` has the parser thinking that `i32 < y` is
294294
// the beginning of a path type. It starts trying to parse `x as (i32 < y ...` instead
295295
// of `(x as i32) < ...`. We need to convince it _not_ to do that.
@@ -312,7 +312,7 @@ impl<'a> State<'a> {
312312

313313
self.print_expr_cond_paren(lhs, left_needs_paren, fixup.leftmost_subexpression());
314314
self.space();
315-
self.word_space(op.node.as_str());
315+
self.word_space(op.as_str());
316316
self.print_expr_cond_paren(rhs, right_needs_paren, fixup.subsequent_subexpression());
317317
}
318318

@@ -410,7 +410,7 @@ impl<'a> State<'a> {
410410
self.print_expr_method_call(seg, receiver, args, fixup);
411411
}
412412
ast::ExprKind::Binary(op, lhs, rhs) => {
413-
self.print_expr_binary(*op, lhs, rhs, fixup);
413+
self.print_expr_binary(op.node, lhs, rhs, fixup);
414414
}
415415
ast::ExprKind::Unary(op, expr) => {
416416
self.print_expr_unary(*op, expr, fixup);

compiler/rustc_hir_pretty/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1276,18 +1276,18 @@ impl<'a> State<'a> {
12761276
self.print_call_post(base_args)
12771277
}
12781278

1279-
fn print_expr_binary(&mut self, op: hir::BinOp, lhs: &hir::Expr<'_>, rhs: &hir::Expr<'_>) {
1280-
let binop_prec = op.node.precedence();
1279+
fn print_expr_binary(&mut self, op: hir::BinOpKind, lhs: &hir::Expr<'_>, rhs: &hir::Expr<'_>) {
1280+
let binop_prec = op.precedence();
12811281
let left_prec = lhs.precedence();
12821282
let right_prec = rhs.precedence();
12831283

1284-
let (mut left_needs_paren, right_needs_paren) = match op.node.fixity() {
1284+
let (mut left_needs_paren, right_needs_paren) = match op.fixity() {
12851285
Fixity::Left => (left_prec < binop_prec, right_prec <= binop_prec),
12861286
Fixity::Right => (left_prec <= binop_prec, right_prec < binop_prec),
12871287
Fixity::None => (left_prec <= binop_prec, right_prec <= binop_prec),
12881288
};
12891289

1290-
match (&lhs.kind, op.node) {
1290+
match (&lhs.kind, op) {
12911291
// These cases need parens: `x as i32 < y` has the parser thinking that `i32 < y` is
12921292
// the beginning of a path type. It starts trying to parse `x as (i32 < y ...` instead
12931293
// of `(x as i32) < ...`. We need to convince it _not_ to do that.
@@ -1302,7 +1302,7 @@ impl<'a> State<'a> {
13021302

13031303
self.print_expr_cond_paren(lhs, left_needs_paren);
13041304
self.space();
1305-
self.word_space(op.node.as_str());
1305+
self.word_space(op.as_str());
13061306
self.print_expr_cond_paren(rhs, right_needs_paren);
13071307
}
13081308

@@ -1456,7 +1456,7 @@ impl<'a> State<'a> {
14561456
self.word(".use");
14571457
}
14581458
hir::ExprKind::Binary(op, lhs, rhs) => {
1459-
self.print_expr_binary(op, lhs, rhs);
1459+
self.print_expr_binary(op.node, lhs, rhs);
14601460
}
14611461
hir::ExprKind::Unary(op, expr) => {
14621462
self.print_expr_unary(op, expr);

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3478,9 +3478,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
34783478
lhs_ty: Ty<'tcx>,
34793479
rhs_expr: &'tcx hir::Expr<'tcx>,
34803480
lhs_expr: &'tcx hir::Expr<'tcx>,
3481-
op: hir::BinOp,
3481+
op: hir::BinOpKind,
34823482
) {
3483-
match op.node {
3483+
match op {
34843484
hir::BinOpKind::Eq => {
34853485
if let Some(partial_eq_def_id) = self.infcx.tcx.lang_items().eq_trait()
34863486
&& self

compiler/rustc_hir_typeck/src/op.rs

+29-27
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3535
let (lhs_ty, rhs_ty, return_ty) =
3636
self.check_overloaded_binop(expr, lhs, rhs, op, IsAssign::Yes, expected);
3737

38-
let ty =
39-
if !lhs_ty.is_ty_var() && !rhs_ty.is_ty_var() && is_builtin_binop(lhs_ty, rhs_ty, op) {
40-
self.enforce_builtin_binop_types(lhs.span, lhs_ty, rhs.span, rhs_ty, op);
41-
self.tcx.types.unit
42-
} else {
43-
return_ty
44-
};
38+
let ty = if !lhs_ty.is_ty_var()
39+
&& !rhs_ty.is_ty_var()
40+
&& is_builtin_binop(lhs_ty, rhs_ty, op.node)
41+
{
42+
self.enforce_builtin_binop_types(lhs.span, lhs_ty, rhs.span, rhs_ty, op.node);
43+
self.tcx.types.unit
44+
} else {
45+
return_ty
46+
};
4547

4648
self.check_lhs_assignable(lhs, E0067, op.span, |err| {
4749
if let Some(lhs_deref_ty) = self.deref_once_mutably_for_diagnostic(lhs_ty) {
4850
if self
4951
.lookup_op_method(
5052
(lhs, lhs_deref_ty),
5153
Some((rhs, rhs_ty)),
52-
lang_item_for_binop(self.tcx, op, IsAssign::Yes),
54+
lang_item_for_binop(self.tcx, op.node, IsAssign::Yes),
5355
op.span,
5456
expected,
5557
)
@@ -61,7 +63,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6163
.lookup_op_method(
6264
(lhs, lhs_ty),
6365
Some((rhs, rhs_ty)),
64-
lang_item_for_binop(self.tcx, op, IsAssign::Yes),
66+
lang_item_for_binop(self.tcx, op.node, IsAssign::Yes),
6567
op.span,
6668
expected,
6769
)
@@ -100,7 +102,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
100102
expr.hir_id, expr, op, lhs_expr, rhs_expr
101103
);
102104

103-
match BinOpCategory::from(op) {
105+
match BinOpCategory::from(op.node) {
104106
BinOpCategory::Shortcircuit => {
105107
// && and || are a simple case.
106108
self.check_expr_coercible_to_type(lhs_expr, tcx.types.bool, None);
@@ -139,14 +141,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
139141
// can't pin this down to a specific impl.
140142
if !lhs_ty.is_ty_var()
141143
&& !rhs_ty.is_ty_var()
142-
&& is_builtin_binop(lhs_ty, rhs_ty, op)
144+
&& is_builtin_binop(lhs_ty, rhs_ty, op.node)
143145
{
144146
let builtin_return_ty = self.enforce_builtin_binop_types(
145147
lhs_expr.span,
146148
lhs_ty,
147149
rhs_expr.span,
148150
rhs_ty,
149-
op,
151+
op.node,
150152
);
151153
self.demand_eqtype(expr.span, builtin_return_ty, return_ty);
152154
builtin_return_ty
@@ -163,7 +165,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
163165
lhs_ty: Ty<'tcx>,
164166
rhs_span: Span,
165167
rhs_ty: Ty<'tcx>,
166-
op: hir::BinOp,
168+
op: hir::BinOpKind,
167169
) -> Ty<'tcx> {
168170
debug_assert!(is_builtin_binop(lhs_ty, rhs_ty, op));
169171

@@ -244,7 +246,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
244246
let result = self.lookup_op_method(
245247
(lhs_expr, lhs_ty),
246248
Some((rhs_expr, rhs_ty_var)),
247-
lang_item_for_binop(self.tcx, op, is_assign),
249+
lang_item_for_binop(self.tcx, op.node, is_assign),
248250
op.span,
249251
expected,
250252
);
@@ -255,7 +257,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
255257
rhs_ty_var,
256258
Some(lhs_expr),
257259
|err, ty| {
258-
self.suggest_swapping_lhs_and_rhs(err, ty, lhs_ty, rhs_expr, lhs_expr, op);
260+
self.suggest_swapping_lhs_and_rhs(err, ty, lhs_ty, rhs_expr, lhs_expr, op.node);
259261
},
260262
);
261263
let rhs_ty = self.resolve_vars_with_obligations(rhs_ty);
@@ -304,7 +306,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
304306
Ty::new_misc_error(self.tcx)
305307
}
306308
Err(errors) => {
307-
let (_, trait_def_id) = lang_item_for_binop(self.tcx, op, is_assign);
309+
let (_, trait_def_id) = lang_item_for_binop(self.tcx, op.node, is_assign);
308310
let missing_trait = trait_def_id
309311
.map(|def_id| with_no_trimmed_paths!(self.tcx.def_path_str(def_id)));
310312
let mut path = None;
@@ -411,7 +413,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
411413
.lookup_op_method(
412414
(lhs_expr, lhs_deref_ty),
413415
Some((rhs_expr, rhs_ty)),
414-
lang_item_for_binop(self.tcx, op, is_assign),
416+
lang_item_for_binop(self.tcx, op.node, is_assign),
415417
op.span,
416418
expected,
417419
)
@@ -445,7 +447,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
445447
.lookup_op_method(
446448
(lhs_expr, lhs_adjusted_ty),
447449
Some((rhs_expr, rhs_adjusted_ty)),
448-
lang_item_for_binop(self.tcx, op, is_assign),
450+
lang_item_for_binop(self.tcx, op.node, is_assign),
449451
op.span,
450452
expected,
451453
)
@@ -503,7 +505,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
503505
self.lookup_op_method(
504506
(lhs_expr, lhs_ty),
505507
Some((rhs_expr, rhs_ty)),
506-
lang_item_for_binop(self.tcx, op, is_assign),
508+
lang_item_for_binop(self.tcx, op.node, is_assign),
507509
op.span,
508510
expected,
509511
)
@@ -597,7 +599,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
597599
.lookup_op_method(
598600
(lhs_expr, lhs_ty),
599601
Some((rhs_expr, rhs_ty)),
600-
lang_item_for_binop(self.tcx, op, is_assign),
602+
lang_item_for_binop(self.tcx, op.node, is_assign),
601603
op.span,
602604
expected,
603605
)
@@ -991,12 +993,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
991993

992994
fn lang_item_for_binop(
993995
tcx: TyCtxt<'_>,
994-
op: hir::BinOp,
996+
op: hir::BinOpKind,
995997
is_assign: IsAssign,
996998
) -> (Symbol, Option<hir::def_id::DefId>) {
997999
let lang = tcx.lang_items();
9981000
if is_assign == IsAssign::Yes {
999-
match op.node {
1001+
match op {
10001002
hir::BinOpKind::Add => (sym::add_assign, lang.add_assign_trait()),
10011003
hir::BinOpKind::Sub => (sym::sub_assign, lang.sub_assign_trait()),
10021004
hir::BinOpKind::Mul => (sym::mul_assign, lang.mul_assign_trait()),
@@ -1015,11 +1017,11 @@ fn lang_item_for_binop(
10151017
| hir::BinOpKind::Ne
10161018
| hir::BinOpKind::And
10171019
| hir::BinOpKind::Or => {
1018-
bug!("impossible assignment operation: {}=", op.node.as_str())
1020+
bug!("impossible assignment operation: {}=", op.as_str())
10191021
}
10201022
}
10211023
} else {
1022-
match op.node {
1024+
match op {
10231025
hir::BinOpKind::Add => (sym::add, lang.add_trait()),
10241026
hir::BinOpKind::Sub => (sym::sub, lang.sub_trait()),
10251027
hir::BinOpKind::Mul => (sym::mul, lang.mul_trait()),
@@ -1076,8 +1078,8 @@ enum BinOpCategory {
10761078
}
10771079

10781080
impl BinOpCategory {
1079-
fn from(op: hir::BinOp) -> BinOpCategory {
1080-
match op.node {
1081+
fn from(op: hir::BinOpKind) -> BinOpCategory {
1082+
match op {
10811083
hir::BinOpKind::Shl | hir::BinOpKind::Shr => BinOpCategory::Shift,
10821084

10831085
hir::BinOpKind::Add
@@ -1133,7 +1135,7 @@ fn deref_ty_if_possible(ty: Ty<'_>) -> Ty<'_> {
11331135
/// Reason #2 is the killer. I tried for a while to always use
11341136
/// overloaded logic and just check the types in constants/codegen after
11351137
/// the fact, and it worked fine, except for SIMD types. -nmatsakis
1136-
fn is_builtin_binop<'tcx>(lhs: Ty<'tcx>, rhs: Ty<'tcx>, op: hir::BinOp) -> bool {
1138+
fn is_builtin_binop<'tcx>(lhs: Ty<'tcx>, rhs: Ty<'tcx>, op: hir::BinOpKind) -> bool {
11371139
// Special-case a single layer of referencing, so that things like `5.0 + &6.0f32` work.
11381140
// (See https://github.com/rust-lang/rust/issues/57447.)
11391141
let (lhs, rhs) = (deref_ty_if_possible(lhs), deref_ty_if_possible(rhs));

compiler/rustc_lint/src/types.rs

+21-24
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::ty::{
1414
};
1515
use rustc_session::{declare_lint, declare_lint_pass, impl_lint_pass};
1616
use rustc_span::def_id::LocalDefId;
17-
use rustc_span::{Span, Symbol, source_map, sym};
17+
use rustc_span::{Span, Symbol, sym};
1818
use tracing::debug;
1919
use {rustc_ast as ast, rustc_hir as hir};
2020

@@ -223,7 +223,7 @@ impl TypeLimits {
223223
fn lint_nan<'tcx>(
224224
cx: &LateContext<'tcx>,
225225
e: &'tcx hir::Expr<'tcx>,
226-
binop: hir::BinOp,
226+
binop: hir::BinOpKind,
227227
l: &'tcx hir::Expr<'tcx>,
228228
r: &'tcx hir::Expr<'tcx>,
229229
) {
@@ -262,19 +262,19 @@ fn lint_nan<'tcx>(
262262
InvalidNanComparisons::EqNe { suggestion }
263263
}
264264

265-
let lint = match binop.node {
265+
let lint = match binop {
266266
hir::BinOpKind::Eq | hir::BinOpKind::Ne if is_nan(cx, l) => {
267267
eq_ne(e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful {
268268
nan_plus_binop: l_span.until(r_span),
269269
float: r_span.shrink_to_hi(),
270-
neg: (binop.node == hir::BinOpKind::Ne).then(|| r_span.shrink_to_lo()),
270+
neg: (binop == hir::BinOpKind::Ne).then(|| r_span.shrink_to_lo()),
271271
})
272272
}
273273
hir::BinOpKind::Eq | hir::BinOpKind::Ne if is_nan(cx, r) => {
274274
eq_ne(e, l, r, |l_span, r_span| InvalidNanComparisonsSuggestion::Spanful {
275275
nan_plus_binop: l_span.shrink_to_hi().to(r_span),
276276
float: l_span.shrink_to_hi(),
277-
neg: (binop.node == hir::BinOpKind::Ne).then(|| l_span.shrink_to_lo()),
277+
neg: (binop == hir::BinOpKind::Ne).then(|| l_span.shrink_to_lo()),
278278
})
279279
}
280280
hir::BinOpKind::Lt | hir::BinOpKind::Le | hir::BinOpKind::Gt | hir::BinOpKind::Ge
@@ -560,11 +560,11 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
560560
}
561561
}
562562
hir::ExprKind::Binary(binop, ref l, ref r) => {
563-
if is_comparison(binop) {
564-
if !check_limits(cx, binop, l, r) {
563+
if is_comparison(binop.node) {
564+
if !check_limits(cx, binop.node, l, r) {
565565
cx.emit_span_lint(UNUSED_COMPARISONS, e.span, UnusedComparisons);
566566
} else {
567-
lint_nan(cx, e, binop, l, r);
567+
lint_nan(cx, e, binop.node, l, r);
568568
let cmpop = ComparisonOp::BinOp(binop.node);
569569
lint_wide_pointer(cx, e, cmpop, l, r);
570570
lint_fn_pointer(cx, e, cmpop, l, r);
@@ -591,8 +591,8 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
591591
_ => {}
592592
};
593593

594-
fn is_valid<T: PartialOrd>(binop: hir::BinOp, v: T, min: T, max: T) -> bool {
595-
match binop.node {
594+
fn is_valid<T: PartialOrd>(binop: hir::BinOpKind, v: T, min: T, max: T) -> bool {
595+
match binop {
596596
hir::BinOpKind::Lt => v > min && v <= max,
597597
hir::BinOpKind::Le => v >= min && v < max,
598598
hir::BinOpKind::Gt => v >= min && v < max,
@@ -602,22 +602,19 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
602602
}
603603
}
604604

605-
fn rev_binop(binop: hir::BinOp) -> hir::BinOp {
606-
source_map::respan(
607-
binop.span,
608-
match binop.node {
609-
hir::BinOpKind::Lt => hir::BinOpKind::Gt,
610-
hir::BinOpKind::Le => hir::BinOpKind::Ge,
611-
hir::BinOpKind::Gt => hir::BinOpKind::Lt,
612-
hir::BinOpKind::Ge => hir::BinOpKind::Le,
613-
_ => return binop,
614-
},
615-
)
605+
fn rev_binop(binop: hir::BinOpKind) -> hir::BinOpKind {
606+
match binop {
607+
hir::BinOpKind::Lt => hir::BinOpKind::Gt,
608+
hir::BinOpKind::Le => hir::BinOpKind::Ge,
609+
hir::BinOpKind::Gt => hir::BinOpKind::Lt,
610+
hir::BinOpKind::Ge => hir::BinOpKind::Le,
611+
_ => binop,
612+
}
616613
}
617614

618615
fn check_limits(
619616
cx: &LateContext<'_>,
620-
binop: hir::BinOp,
617+
binop: hir::BinOpKind,
621618
l: &hir::Expr<'_>,
622619
r: &hir::Expr<'_>,
623620
) -> bool {
@@ -659,9 +656,9 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
659656
}
660657
}
661658

662-
fn is_comparison(binop: hir::BinOp) -> bool {
659+
fn is_comparison(binop: hir::BinOpKind) -> bool {
663660
matches!(
664-
binop.node,
661+
binop,
665662
hir::BinOpKind::Eq
666663
| hir::BinOpKind::Lt
667664
| hir::BinOpKind::Le

0 commit comments

Comments
 (0)