Skip to content

Commit a0d4497

Browse files
committed
Do not suggest to make mut binding external to Fn closure
1 parent bb345a0 commit a0d4497

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

src/librustc_borrowck/borrowck/mod.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -842,10 +842,32 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
842842
if let mc::NoteClosureEnv(upvar_id) = err.cmt.note {
843843
let node_id = self.tcx.hir.hir_to_node_id(upvar_id.var_id);
844844
let sp = self.tcx.hir.span(node_id);
845-
match self.tcx.sess.codemap().span_to_snippet(sp) {
846-
Ok(snippet) => {
845+
let fn_closure_msg = "`Fn` closures cannot capture their enclosing \
846+
environment for modifications";
847+
match (self.tcx.sess.codemap().span_to_snippet(sp), &err.cmt.cat) {
848+
(_, &Categorization::Upvar(mc::Upvar {
849+
kind: ty::ClosureKind::Fn, ..
850+
})) => {
851+
db.note(fn_closure_msg);
852+
// we should point at the cause for this closure being
853+
// identified as `Fn` (like in signature of method this
854+
// closure was passed into)
855+
}
856+
(Ok(ref snippet), ref cat) => {
847857
let msg = &format!("consider making `{}` mutable", snippet);
848-
db.span_suggestion(sp, msg, format!("mut {}", snippet));
858+
let suggestion = format!("mut {}", snippet);
859+
860+
if let &Categorization::Deref(ref cmt, _) = cat {
861+
if let Categorization::Upvar(mc::Upvar {
862+
kind: ty::ClosureKind::Fn, ..
863+
}) = cmt.cat {
864+
db.note(fn_closure_msg);
865+
} else {
866+
db.span_suggestion(sp, msg, suggestion);
867+
}
868+
} else {
869+
db.span_suggestion(sp, msg, suggestion);
870+
}
849871
}
850872
_ => {
851873
db.span_help(sp, "consider making this binding mutable");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn bar<F: Fn()>(_f: F) {}
12+
13+
pub fn foo() {
14+
let mut x = 0;
15+
bar(move || x = 1);
16+
//~^ ERROR cannot assign to captured outer variable in an `Fn` closure
17+
//~| NOTE `Fn` closures cannot capture their enclosing environment for modifications
18+
}
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0594]: cannot assign to captured outer variable in an `Fn` closure
2+
--> $DIR/fn-closure-mutable-capture.rs:15:17
3+
|
4+
15 | bar(move || x = 1);
5+
| ^^^^^
6+
|
7+
= note: `Fn` closures cannot capture their enclosing environment for modifications
8+
help: consider changing this closure to take self by mutable reference
9+
--> $DIR/fn-closure-mutable-capture.rs:15:9
10+
|
11+
15 | bar(move || x = 1);
12+
| ^^^^^^^^^^^^^
13+
14+
error: aborting due to previous error
15+

0 commit comments

Comments
 (0)