Skip to content

Commit

Permalink
Account for UnOps in borrowck message
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Mar 13, 2024
1 parent b367c25 commit 0953608
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_borrowck/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ borrowck_borrow_due_to_use_closure =
borrowck_borrow_due_to_use_coroutine =
borrow occurs due to use in coroutine
borrowck_calling_operator_moves =
calling this operator moves the value
borrowck_calling_operator_moves_lhs =
calling this operator moves the left-hand side
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
);
err.subdiagnostic(self.dcx(), CaptureReasonNote::FnOnceMoveInCall { var_span });
}
CallKind::Operator { self_arg, .. } => {
CallKind::Operator { self_arg, trait_id, .. } => {
let self_arg = self_arg.unwrap();
err.subdiagnostic(
self.dcx(),
Expand All @@ -1062,9 +1062,16 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
},
);
if self.fn_self_span_reported.insert(fn_span) {
let lang = self.infcx.tcx.lang_items();
err.subdiagnostic(
self.dcx(),
CaptureReasonNote::LhsMoveByOperator { span: self_arg.span },
if [lang.not_trait(), lang.deref_trait(), lang.neg_trait()]
.contains(&Some(trait_id))
{
CaptureReasonNote::UnOpMoveByOperator { span: self_arg.span }
} else {
CaptureReasonNote::LhsMoveByOperator { span: self_arg.span }
},
);
}
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_borrowck/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,11 @@ pub(crate) enum CaptureReasonNote {
#[primary_span]
var_span: Span,
},
#[note(borrowck_calling_operator_moves)]
UnOpMoveByOperator {
#[primary_span]
span: Span,
},
#[note(borrowck_calling_operator_moves_lhs)]
LhsMoveByOperator {
#[primary_span]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/unop-move-semantics.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LL |
LL | x.clone();
| ^ value borrowed here after move
|
note: calling this operator moves the left-hand side
note: calling this operator moves the value
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
help: consider cloning the value if the performance cost is acceptable
|
Expand Down Expand Up @@ -57,7 +57,7 @@ LL | !*m;
| |move occurs because `*m` has type `T`, which does not implement the `Copy` trait
| `*m` moved due to usage in operator
|
note: calling this operator moves the left-hand side
note: calling this operator moves the value
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL

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

0 comments on commit 0953608

Please sign in to comment.