Skip to content

Commit 36b8502

Browse files
committed
Add an explanatory note when calling a closure via &
Closes #15506.
1 parent a8577be commit 36b8502

File tree

1 file changed

+9
-1
lines changed
  • src/librustc/middle/borrowck

1 file changed

+9
-1
lines changed

src/librustc/middle/borrowck/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
602602
span: Span,
603603
kind: AliasableViolationKind,
604604
cause: mc::AliasableReason) {
605+
let mut is_closure = false;
605606
let prefix = match kind {
606607
MutabilityViolation => {
607608
"cannot assign to data"
@@ -625,6 +626,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
625626
}
626627

627628
BorrowViolation(euv::ClosureInvocation) => {
629+
is_closure = true;
628630
"closure invocation"
629631
}
630632

@@ -649,14 +651,20 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
649651
mc::AliasableManaged => {
650652
self.tcx.sess.span_err(
651653
span,
652-
format!("{} in a `@` pointer", prefix).as_slice());
654+
format!("{} in a `Gc` pointer", prefix).as_slice());
653655
}
654656
mc::AliasableBorrowed => {
655657
self.tcx.sess.span_err(
656658
span,
657659
format!("{} in a `&` reference", prefix).as_slice());
658660
}
659661
}
662+
663+
if is_closure {
664+
self.tcx.sess.span_note(
665+
span,
666+
"closures behind references must be called via `&mut`");
667+
}
660668
}
661669

662670
pub fn note_and_explain_bckerr(&self, err: BckError) {

0 commit comments

Comments
 (0)