Skip to content

Commit cf0d322

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 d75d3df commit cf0d322

File tree

7 files changed

+73
-71
lines changed

7 files changed

+73
-71
lines changed

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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,18 +1290,18 @@ impl<'a> State<'a> {
12901290
self.print_call_post(base_args)
12911291
}
12921292

1293-
fn print_expr_binary(&mut self, op: hir::BinOp, lhs: &hir::Expr<'_>, rhs: &hir::Expr<'_>) {
1294-
let binop_prec = op.node.precedence();
1293+
fn print_expr_binary(&mut self, op: hir::BinOpKind, lhs: &hir::Expr<'_>, rhs: &hir::Expr<'_>) {
1294+
let binop_prec = op.precedence();
12951295
let left_prec = lhs.precedence();
12961296
let right_prec = rhs.precedence();
12971297

1298-
let (mut left_needs_paren, right_needs_paren) = match op.node.fixity() {
1298+
let (mut left_needs_paren, right_needs_paren) = match op.fixity() {
12991299
Fixity::Left => (left_prec < binop_prec, right_prec <= binop_prec),
13001300
Fixity::Right => (left_prec <= binop_prec, right_prec < binop_prec),
13011301
Fixity::None => (left_prec <= binop_prec, right_prec <= binop_prec),
13021302
};
13031303

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

13171317
self.print_expr_cond_paren(lhs, left_needs_paren);
13181318
self.space();
1319-
self.word_space(op.node.as_str());
1319+
self.word_space(op.as_str());
13201320
self.print_expr_cond_paren(rhs, right_needs_paren);
13211321
}
13221322

@@ -1465,7 +1465,7 @@ impl<'a> State<'a> {
14651465
self.print_expr_method_call(segment, receiver, args);
14661466
}
14671467
hir::ExprKind::Binary(op, lhs, rhs) => {
1468-
self.print_expr_binary(op, lhs, rhs);
1468+
self.print_expr_binary(op.node, lhs, rhs);
14691469
}
14701470
hir::ExprKind::Unary(op, expr) => {
14711471
self.print_expr_unary(op, expr);

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3483,9 +3483,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
34833483
lhs_ty: Ty<'tcx>,
34843484
rhs_expr: &'tcx hir::Expr<'tcx>,
34853485
lhs_expr: &'tcx hir::Expr<'tcx>,
3486-
op: hir::BinOp,
3486+
op: hir::BinOpKind,
34873487
) {
3488-
match op.node {
3488+
match op {
34893489
hir::BinOpKind::Eq => {
34903490
if let Some(partial_eq_def_id) = self.infcx.tcx.lang_items().eq_trait()
34913491
&& self

compiler/rustc_hir_typeck/src/op.rs

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

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

4749
self.check_lhs_assignable(lhs, E0067, op.span, |err| {
4850
if let Some(lhs_deref_ty) = self.deref_once_mutably_for_diagnostic(lhs_ty) {
4951
if self
5052
.lookup_op_method(
5153
(lhs, lhs_deref_ty),
5254
Some((rhs, rhs_ty)),
53-
lang_item_for_binop(self.tcx, op, IsAssign::Yes),
55+
lang_item_for_binop(self.tcx, op.node, IsAssign::Yes),
5456
op.span,
5557
expected,
5658
)
@@ -62,7 +64,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6264
.lookup_op_method(
6365
(lhs, lhs_ty),
6466
Some((rhs, rhs_ty)),
65-
lang_item_for_binop(self.tcx, op, IsAssign::Yes),
67+
lang_item_for_binop(self.tcx, op.node, IsAssign::Yes),
6668
op.span,
6769
expected,
6870
)
@@ -101,7 +103,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
101103
expr.hir_id, expr, op, lhs_expr, rhs_expr
102104
);
103105

104-
match BinOpCategory::from(op) {
106+
match BinOpCategory::from(op.node) {
105107
BinOpCategory::Shortcircuit => {
106108
// && and || are a simple case.
107109
self.check_expr_coercible_to_type(lhs_expr, tcx.types.bool, None);
@@ -140,14 +142,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
140142
// can't pin this down to a specific impl.
141143
if !lhs_ty.is_ty_var()
142144
&& !rhs_ty.is_ty_var()
143-
&& is_builtin_binop(lhs_ty, rhs_ty, op)
145+
&& is_builtin_binop(lhs_ty, rhs_ty, op.node)
144146
{
145147
let builtin_return_ty = self.enforce_builtin_binop_types(
146148
lhs_expr.span,
147149
lhs_ty,
148150
rhs_expr.span,
149151
rhs_ty,
150-
op,
152+
op.node,
151153
);
152154
self.demand_eqtype(expr.span, builtin_return_ty, return_ty);
153155
builtin_return_ty
@@ -164,7 +166,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
164166
lhs_ty: Ty<'tcx>,
165167
rhs_span: Span,
166168
rhs_ty: Ty<'tcx>,
167-
op: hir::BinOp,
169+
op: hir::BinOpKind,
168170
) -> Ty<'tcx> {
169171
debug_assert!(is_builtin_binop(lhs_ty, rhs_ty, op));
170172

@@ -245,7 +247,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
245247
let result = self.lookup_op_method(
246248
(lhs_expr, lhs_ty),
247249
Some((rhs_expr, rhs_ty_var)),
248-
lang_item_for_binop(self.tcx, op, is_assign),
250+
lang_item_for_binop(self.tcx, op.node, is_assign),
249251
op.span,
250252
expected,
251253
);
@@ -256,7 +258,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
256258
rhs_ty_var,
257259
Some(lhs_expr),
258260
|err, ty| {
259-
self.suggest_swapping_lhs_and_rhs(err, ty, lhs_ty, rhs_expr, lhs_expr, op);
261+
self.suggest_swapping_lhs_and_rhs(err, ty, lhs_ty, rhs_expr, lhs_expr, op.node);
260262
},
261263
);
262264
let rhs_ty = self.resolve_vars_with_obligations(rhs_ty);
@@ -305,7 +307,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
305307
Ty::new_misc_error(self.tcx)
306308
}
307309
Err(errors) => {
308-
let (_, trait_def_id) = lang_item_for_binop(self.tcx, op, is_assign);
310+
let (_, trait_def_id) = lang_item_for_binop(self.tcx, op.node, is_assign);
309311
let missing_trait = trait_def_id
310312
.map(|def_id| with_no_trimmed_paths!(self.tcx.def_path_str(def_id)));
311313
let (mut err, output_def_id) = match is_assign {
@@ -407,7 +409,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
407409
.lookup_op_method(
408410
(lhs_expr, lhs_deref_ty),
409411
Some((rhs_expr, rhs_ty)),
410-
lang_item_for_binop(self.tcx, op, is_assign),
412+
lang_item_for_binop(self.tcx, op.node, is_assign),
411413
op.span,
412414
expected,
413415
)
@@ -441,7 +443,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
441443
.lookup_op_method(
442444
(lhs_expr, lhs_adjusted_ty),
443445
Some((rhs_expr, rhs_adjusted_ty)),
444-
lang_item_for_binop(self.tcx, op, is_assign),
446+
lang_item_for_binop(self.tcx, op.node, is_assign),
445447
op.span,
446448
expected,
447449
)
@@ -497,7 +499,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
497499
self.lookup_op_method(
498500
(lhs_expr, lhs_ty),
499501
Some((rhs_expr, rhs_ty)),
500-
lang_item_for_binop(self.tcx, op, is_assign),
502+
lang_item_for_binop(self.tcx, op.node, is_assign),
501503
op.span,
502504
expected,
503505
)
@@ -591,7 +593,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
591593
.lookup_op_method(
592594
(lhs_expr, lhs_ty),
593595
Some((rhs_expr, rhs_ty)),
594-
lang_item_for_binop(self.tcx, op, is_assign),
596+
lang_item_for_binop(self.tcx, op.node, is_assign),
595597
op.span,
596598
expected,
597599
)
@@ -971,12 +973,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
971973

972974
fn lang_item_for_binop(
973975
tcx: TyCtxt<'_>,
974-
op: hir::BinOp,
976+
op: hir::BinOpKind,
975977
is_assign: IsAssign,
976978
) -> (Symbol, Option<hir::def_id::DefId>) {
977979
let lang = tcx.lang_items();
978980
if is_assign == IsAssign::Yes {
979-
match op.node {
981+
match op {
980982
hir::BinOpKind::Add => (sym::add_assign, lang.add_assign_trait()),
981983
hir::BinOpKind::Sub => (sym::sub_assign, lang.sub_assign_trait()),
982984
hir::BinOpKind::Mul => (sym::mul_assign, lang.mul_assign_trait()),
@@ -995,11 +997,11 @@ fn lang_item_for_binop(
995997
| hir::BinOpKind::Ne
996998
| hir::BinOpKind::And
997999
| hir::BinOpKind::Or => {
998-
bug!("impossible assignment operation: {}=", op.node.as_str())
1000+
bug!("impossible assignment operation: {}=", op.as_str())
9991001
}
10001002
}
10011003
} else {
1002-
match op.node {
1004+
match op {
10031005
hir::BinOpKind::Add => (sym::add, lang.add_trait()),
10041006
hir::BinOpKind::Sub => (sym::sub, lang.sub_trait()),
10051007
hir::BinOpKind::Mul => (sym::mul, lang.mul_trait()),
@@ -1056,8 +1058,8 @@ enum BinOpCategory {
10561058
}
10571059

10581060
impl BinOpCategory {
1059-
fn from(op: hir::BinOp) -> BinOpCategory {
1060-
match op.node {
1061+
fn from(op: hir::BinOpKind) -> BinOpCategory {
1062+
match op {
10611063
hir::BinOpKind::Shl | hir::BinOpKind::Shr => BinOpCategory::Shift,
10621064

10631065
hir::BinOpKind::Add
@@ -1113,7 +1115,7 @@ fn deref_ty_if_possible(ty: Ty<'_>) -> Ty<'_> {
11131115
/// Reason #2 is the killer. I tried for a while to always use
11141116
/// overloaded logic and just check the types in constants/codegen after
11151117
/// the fact, and it worked fine, except for SIMD types. -nmatsakis
1116-
fn is_builtin_binop<'tcx>(lhs: Ty<'tcx>, rhs: Ty<'tcx>, op: hir::BinOp) -> bool {
1118+
fn is_builtin_binop<'tcx>(lhs: Ty<'tcx>, rhs: Ty<'tcx>, op: hir::BinOpKind) -> bool {
11171119
// Special-case a single layer of referencing, so that things like `5.0 + &6.0f32` work.
11181120
// (See https://github.com/rust-lang/rust/issues/57447.)
11191121
let (lhs, rhs) = (deref_ty_if_possible(lhs), deref_ty_if_possible(rhs));

compiler/rustc_lint/src/types.rs

Lines changed: 17 additions & 17 deletions
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
@@ -546,11 +546,11 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
546546
}
547547
}
548548
hir::ExprKind::Binary(binop, ref l, ref r) => {
549-
if is_comparison(binop) {
550-
if !check_limits(cx, binop, l, r) {
549+
if is_comparison(binop.node) {
550+
if !check_limits(cx, binop.node, l, r) {
551551
cx.emit_span_lint(UNUSED_COMPARISONS, e.span, UnusedComparisons);
552552
} else {
553-
lint_nan(cx, e, binop, l, r);
553+
lint_nan(cx, e, binop.node, l, r);
554554
let cmpop = ComparisonOp::BinOp(binop.node);
555555
lint_wide_pointer(cx, e, cmpop, l, r);
556556
lint_fn_pointer(cx, e, cmpop, l, r);
@@ -578,8 +578,8 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
578578
_ => {}
579579
};
580580

581-
fn is_valid<T: PartialOrd>(binop: hir::BinOp, v: T, min: T, max: T) -> bool {
582-
match binop.node {
581+
fn is_valid<T: PartialOrd>(binop: hir::BinOpKind, v: T, min: T, max: T) -> bool {
582+
match binop {
583583
hir::BinOpKind::Lt => v > min && v <= max,
584584
hir::BinOpKind::Le => v >= min && v < max,
585585
hir::BinOpKind::Gt => v >= min && v < max,
@@ -589,19 +589,19 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
589589
}
590590
}
591591

592-
fn rev_binop(binop: hir::BinOp) -> hir::BinOp {
593-
source_map::respan(binop.span, match binop.node {
592+
fn rev_binop(binop: hir::BinOpKind) -> hir::BinOpKind {
593+
match binop {
594594
hir::BinOpKind::Lt => hir::BinOpKind::Gt,
595595
hir::BinOpKind::Le => hir::BinOpKind::Ge,
596596
hir::BinOpKind::Gt => hir::BinOpKind::Lt,
597597
hir::BinOpKind::Ge => hir::BinOpKind::Le,
598-
_ => return binop,
599-
})
598+
_ => binop,
599+
}
600600
}
601601

602602
fn check_limits(
603603
cx: &LateContext<'_>,
604-
binop: hir::BinOp,
604+
binop: hir::BinOpKind,
605605
l: &hir::Expr<'_>,
606606
r: &hir::Expr<'_>,
607607
) -> bool {
@@ -643,9 +643,9 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
643643
}
644644
}
645645

646-
fn is_comparison(binop: hir::BinOp) -> bool {
646+
fn is_comparison(binop: hir::BinOpKind) -> bool {
647647
matches!(
648-
binop.node,
648+
binop,
649649
hir::BinOpKind::Eq
650650
| hir::BinOpKind::Lt
651651
| hir::BinOpKind::Le

0 commit comments

Comments
 (0)