Skip to content

Commit 9c1ef7a

Browse files
committed
---
yaml --- r: 156938 b: refs/heads/auto c: 7129f17 h: refs/heads/master v: v3
1 parent 319aff8 commit 9c1ef7a

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1313
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1414
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1515
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
16-
refs/heads/auto: 1062955b46950114ed3a0a71c043e9644afdce48
16+
refs/heads/auto: 7129f172aee6d5143ca36d81defd47dd0c113ce3
1717
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1818
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1919
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/librustc/middle/borrowck/check_loans.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -777,13 +777,28 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
777777
// Otherwise, just a plain error.
778778
match assignee_cmt.note {
779779
mc::NoteClosureEnv(upvar_id) => {
780-
self.bccx.span_err(
781-
assignment_span,
782-
format!("cannot assign to {}",
783-
self.bccx.cmt_to_string(&*assignee_cmt)).as_slice());
784-
self.bccx.span_note(
785-
self.tcx().map.span(upvar_id.closure_expr_id),
786-
"consider changing this closure to take self by mutable reference");
780+
// If this is an `Fn` closure, it simply can't mutate upvars.
781+
// If it's an `FnMut` closure, the original variable was declared immutable.
782+
// We need to determine which is the case here.
783+
let kind = match assignee_cmt.upvar().unwrap().cat {
784+
mc::cat_upvar(mc::Upvar { kind, .. }) => kind,
785+
_ => unreachable!()
786+
};
787+
if kind == ty::FnUnboxedClosureKind {
788+
self.bccx.span_err(
789+
assignment_span,
790+
format!("cannot assign to {}",
791+
self.bccx.cmt_to_string(&*assignee_cmt)).as_slice());
792+
self.bccx.span_note(
793+
self.tcx().map.span(upvar_id.closure_expr_id),
794+
"consider changing this closure to take self by mutable reference");
795+
} else {
796+
self.bccx.span_err(
797+
assignment_span,
798+
format!("cannot assign to {} {}",
799+
assignee_cmt.mutbl.to_user_str(),
800+
self.bccx.cmt_to_string(&*assignee_cmt)).as_slice());
801+
}
787802
}
788803
_ => match opt_loan_path(&assignee_cmt) {
789804
Some(lp) => {

branches/auto/src/librustc/middle/borrowck/mod.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
626626
match err.code {
627627
err_mutbl => {
628628
let descr = match err.cmt.note {
629-
mc::NoteClosureEnv(_) => {
629+
mc::NoteClosureEnv(_) | mc::NoteUpvarRef(_) => {
630630
self.cmt_to_string(&*err.cmt)
631631
}
632632
_ => match opt_loan_path(&err.cmt) {
@@ -762,11 +762,20 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
762762
match code {
763763
err_mutbl(..) => {
764764
match err.cmt.note {
765-
mc::NoteClosureEnv(upvar_id) => {
766-
self.tcx.sess.span_note(
767-
self.tcx.map.span(upvar_id.closure_expr_id),
768-
"consider changing this closure to take \
769-
self by mutable reference");
765+
mc::NoteClosureEnv(upvar_id) | mc::NoteUpvarRef(upvar_id) => {
766+
// If this is an `Fn` closure, it simply can't mutate upvars.
767+
// If it's an `FnMut` closure, the original variable was declared immutable.
768+
// We need to determine which is the case here.
769+
let kind = match err.cmt.upvar().unwrap().cat {
770+
mc::cat_upvar(mc::Upvar { kind, .. }) => kind,
771+
_ => unreachable!()
772+
};
773+
if kind == ty::FnUnboxedClosureKind {
774+
self.tcx.sess.span_note(
775+
self.tcx.map.span(upvar_id.closure_expr_id),
776+
"consider changing this closure to take \
777+
self by mutable reference");
778+
}
770779
}
771780
_ => {}
772781
}

0 commit comments

Comments
 (0)