Skip to content

Commit 0329c34

Browse files
committed
Account for UnOps in borrowck message
1 parent 899a6b3 commit 0329c34

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

compiler/rustc_borrowck/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ borrowck_borrow_due_to_use_closure =
1616
borrowck_borrow_due_to_use_coroutine =
1717
borrow occurs due to use in coroutine
1818
19+
borrowck_calling_operator_moves =
20+
calling this operator moves the value
21+
1922
borrowck_calling_operator_moves_lhs =
2023
calling this operator moves the left-hand side
2124

compiler/rustc_borrowck/src/diagnostics/mod.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10501050
);
10511051
err.subdiagnostic(self.dcx(), CaptureReasonNote::FnOnceMoveInCall { var_span });
10521052
}
1053-
CallKind::Operator { self_arg, .. } => {
1053+
CallKind::Operator { self_arg, trait_id, .. } => {
10541054
let self_arg = self_arg.unwrap();
10551055
err.subdiagnostic(
10561056
self.dcx(),
@@ -1062,9 +1062,16 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10621062
},
10631063
);
10641064
if self.fn_self_span_reported.insert(fn_span) {
1065+
let lang = self.infcx.tcx.lang_items();
10651066
err.subdiagnostic(
10661067
self.dcx(),
1067-
CaptureReasonNote::LhsMoveByOperator { span: self_arg.span },
1068+
if [lang.not_trait(), lang.deref_trait(), lang.neg_trait()]
1069+
.contains(&Some(trait_id))
1070+
{
1071+
CaptureReasonNote::UnOpMoveByOperator { span: self_arg.span }
1072+
} else {
1073+
CaptureReasonNote::LhsMoveByOperator { span: self_arg.span }
1074+
},
10681075
);
10691076
}
10701077
}

compiler/rustc_borrowck/src/session_diagnostics.rs

+5
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,11 @@ pub(crate) enum CaptureReasonNote {
368368
#[primary_span]
369369
var_span: Span,
370370
},
371+
#[note(borrowck_calling_operator_moves)]
372+
UnOpMoveByOperator {
373+
#[primary_span]
374+
span: Span,
375+
},
371376
#[note(borrowck_calling_operator_moves_lhs)]
372377
LhsMoveByOperator {
373378
#[primary_span]

tests/ui/unop-move-semantics.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL |
99
LL | x.clone();
1010
| ^ value borrowed here after move
1111
|
12-
note: calling this operator moves the left-hand side
12+
note: calling this operator moves the value
1313
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
1414
help: consider cloning the value if the performance cost is acceptable
1515
|
@@ -57,7 +57,7 @@ LL | !*m;
5757
| |move occurs because `*m` has type `T`, which does not implement the `Copy` trait
5858
| `*m` moved due to usage in operator
5959
|
60-
note: calling this operator moves the left-hand side
60+
note: calling this operator moves the value
6161
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
6262

6363
error[E0507]: cannot move out of `*n` which is behind a shared reference

0 commit comments

Comments
 (0)