Skip to content

Commit 59de833

Browse files
committed
Suggest dereferencing &T == T
1 parent fec80b4 commit 59de833

File tree

4 files changed

+173
-117
lines changed

4 files changed

+173
-117
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2371,7 +2371,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23712371
}
23722372
};
23732373

2374-
// Suggest dereferencing the lhs for expressions such as `&T == T`
2374+
// Suggest dereferencing the lhs for expressions such as `&T <= T`
23752375
if let Some(hir::Node::Expr(hir::Expr {
23762376
kind: hir::ExprKind::Binary(_, lhs, ..),
23772377
..

compiler/rustc_hir_typeck/src/op.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4747
if let Some(lhs_deref_ty) = self.deref_once_mutably_for_diagnostic(lhs_ty) {
4848
if self
4949
.lookup_op_method(
50-
lhs_deref_ty,
50+
(lhs, lhs_deref_ty),
5151
Some((rhs, rhs_ty)),
5252
Op::Binary(op, IsAssign::Yes),
5353
expected,
@@ -58,7 +58,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5858
// emitted a better suggestion during error handling in check_overloaded_binop.
5959
if self
6060
.lookup_op_method(
61-
lhs_ty,
61+
(lhs, lhs_ty),
6262
Some((rhs, rhs_ty)),
6363
Op::Binary(op, IsAssign::Yes),
6464
expected,
@@ -246,7 +246,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
246246
});
247247

248248
let result = self.lookup_op_method(
249-
lhs_ty,
249+
(lhs_expr, lhs_ty),
250250
Some((rhs_expr, rhs_ty_var)),
251251
Op::Binary(op, is_assign),
252252
expected,
@@ -391,7 +391,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
391391
|err: &mut DiagnosticBuilder<'_, _>, lhs_deref_ty: Ty<'tcx>| {
392392
if self
393393
.lookup_op_method(
394-
lhs_deref_ty,
394+
(lhs_expr, lhs_deref_ty),
395395
Some((rhs_expr, rhs_ty)),
396396
Op::Binary(op, is_assign),
397397
expected,
@@ -424,7 +424,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
424424
rhs_new_mutbl: Option<ast::Mutability>| {
425425
if self
426426
.lookup_op_method(
427-
lhs_adjusted_ty,
427+
(lhs_expr, lhs_adjusted_ty),
428428
Some((rhs_expr, rhs_adjusted_ty)),
429429
Op::Binary(op, is_assign),
430430
expected,
@@ -479,7 +479,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
479479

480480
let is_compatible_after_call = |lhs_ty, rhs_ty| {
481481
self.lookup_op_method(
482-
lhs_ty,
482+
(lhs_expr, lhs_ty),
483483
Some((rhs_expr, rhs_ty)),
484484
Op::Binary(op, is_assign),
485485
expected,
@@ -578,7 +578,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
578578
// suggestion for the user.
579579
let errors = self
580580
.lookup_op_method(
581-
lhs_ty,
581+
(lhs_expr, lhs_ty),
582582
Some((rhs_expr, rhs_ty)),
583583
Op::Binary(op, is_assign),
584584
expected,
@@ -779,7 +779,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
779779
expected: Expectation<'tcx>,
780780
) -> Ty<'tcx> {
781781
assert!(op.is_by_value());
782-
match self.lookup_op_method(operand_ty, None, Op::Unary(op, ex.span), expected) {
782+
match self.lookup_op_method((ex, operand_ty), None, Op::Unary(op, ex.span), expected) {
783783
Ok(method) => {
784784
self.write_method_call(ex.hir_id, method);
785785
method.sig.output()
@@ -865,11 +865,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
865865

866866
fn lookup_op_method(
867867
&self,
868-
lhs_ty: Ty<'tcx>,
868+
lhs: (&'tcx hir::Expr<'tcx>, Ty<'tcx>),
869869
opt_rhs: Option<(&'tcx hir::Expr<'tcx>, Ty<'tcx>)>,
870870
op: Op,
871871
expected: Expectation<'tcx>,
872872
) -> Result<MethodCallee<'tcx>, Vec<FulfillmentError<'tcx>>> {
873+
let (lhs_expr, lhs_ty) = lhs;
873874
let span = match op {
874875
Op::Binary(op, _) => op.span,
875876
Op::Unary(_, span) => span,
@@ -910,8 +911,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
910911
let cause = self.cause(
911912
span,
912913
traits::BinOp {
914+
lhs_hir_id: lhs_expr.hir_id,
915+
rhs_hir_id: opt_rhs_expr.map(|expr| expr.hir_id),
913916
rhs_span: opt_rhs_expr.map(|expr| expr.span),
914-
is_lit: opt_rhs_expr.is_some_and(|expr| matches!(expr.kind, hir::ExprKind::Lit(_))),
917+
rhs_is_lit: opt_rhs_expr
918+
.is_some_and(|expr| matches!(expr.kind, hir::ExprKind::Lit(_))),
915919
output_ty: expected.only_has_type(self),
916920
},
917921
);

compiler/rustc_middle/src/traits/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,10 @@ pub enum ObligationCauseCode<'tcx> {
440440
MatchImpl(ObligationCause<'tcx>, DefId),
441441

442442
BinOp {
443+
lhs_hir_id: hir::HirId,
444+
rhs_hir_id: Option<hir::HirId>,
443445
rhs_span: Option<Span>,
444-
is_lit: bool,
446+
rhs_is_lit: bool,
445447
output_ty: Option<Ty<'tcx>>,
446448
},
447449

0 commit comments

Comments
 (0)