Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b43aa96
Print failure message on all tests that should panic, but don't
johanngan Jan 10, 2021
679f6f3
Add `unwrap_unchecked()` methods for `Option` and `Result`
ojeda Jan 10, 2021
76299b3
Add `SAFETY` annotations
ojeda Jan 10, 2021
63a1eee
Reset LateContext enclosing body in nested items
camsteffen Jan 18, 2021
21fb586
Query for TypeckResults in LateContext::qpath_res
camsteffen Jan 18, 2021
def0e9b
Fix ICE with `ReadPointerAsBytes` validation error
camelid Jan 11, 2021
a7b7a43
Move test to `src/test/ui/consts/`
camelid Jan 11, 2021
eaba3da
Remove qpath_res util function
camsteffen Jan 18, 2021
e25959b
Make more traits of the From/Into family diagnostic items
flip1995 Jan 22, 2021
3a4786d
Use UFCS instead of method calls in `derive(Debug)`. See issue 81211 …
pnkfelix Jan 23, 2021
78e57d3
Regression tests for issue 81211.
pnkfelix Jan 23, 2021
d78f0a1
Test exploring the interactions between all of the different kinds of…
pnkfelix Jan 23, 2021
aa8fdad
placate tidy.
pnkfelix Jan 23, 2021
6d4e03a
codegen: assume constants cannot fail to evaluate
RalfJung Jan 24, 2021
2be1993
Ignore test on 32-bit architectures
camelid Jan 25, 2021
26b4baf
Point to span of upvar making closure FnMut
sledgehammervampire Jan 18, 2021
088c89d
Account for generics when suggesting bound
estebank Jan 19, 2021
042facb
Fix some bugs reported by eslint
GuillaumeGomez Jan 23, 2021
0140dac
Link the reference about undefined behavior
ojeda Jan 25, 2021
01250fc
Add tracking issue
ojeda Jan 25, 2021
24149d7
Blessed change to output of flaky test.
pnkfelix Jan 25, 2021
1c0a52d
rustdoc: Document CommonMark extensions.
ehuss Jan 25, 2021
17ba456
Rollup merge of #80868 - johanngan:should-panic-msg-with-expected, r=…
jonas-schievink Jan 25, 2021
0e416cc
Rollup merge of #80876 - ojeda:option-result-unwrap_unchecked, r=m-ou-se
jonas-schievink Jan 25, 2021
8ee1696
Rollup merge of #80900 - camelid:readpointerasbytes-ice, r=oli-obk
jonas-schievink Jan 25, 2021
e4e79fa
Rollup merge of #81158 - 1000teslas:issue-80313-fix, r=Aaron1011
jonas-schievink Jan 25, 2021
0652507
Rollup merge of #81176 - camsteffen:qpath-res, r=oli-obk
jonas-schievink Jan 25, 2021
00188e0
Rollup merge of #81195 - estebank:suggest-bound-on-trait-with-params,…
jonas-schievink Jan 25, 2021
e94b0b7
Rollup merge of #81277 - flip1995:from_diag_items, r=matthewjasper
jonas-schievink Jan 25, 2021
21998ab
Rollup merge of #81294 - pnkfelix:issue-81211-use-ufcs-in-derive-debu…
jonas-schievink Jan 25, 2021
b1d6d12
Rollup merge of #81299 - GuillaumeGomez:fix-eslint-detected-bugs, r=N…
jonas-schievink Jan 25, 2021
cc7b2ce
Rollup merge of #81327 - RalfJung:codegen-no-const-fail, r=oli-obk
jonas-schievink Jan 25, 2021
c8a112d
Rollup merge of #81389 - ehuss:rustdoc-cmark-extensions, r=GuillaumeG…
jonas-schievink Jan 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Point to span of upvar making closure FnMut
Add expected error

Add comment

Tweak comment wording

Fix after rebase to updated master

Fix after rebase to updated master

Distinguish mutation in normal and move closures

Tweak error message

Fix error message for nested closures

Refactor code showing mutated upvar in closure

Remove debug assert

B
  • Loading branch information
sledgehammervampire committed Jan 25, 2021
commit 26b4baf46ea66d3651281c8b66d76853e6362c65
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use rustc_hir as hir;
use rustc_hir::Node;
use rustc_index::vec::Idx;
use rustc_middle::mir::{self, ClearCrossCrate, Local, LocalDecl, LocalInfo, Location};
use rustc_middle::mir::{Mutability, Place, PlaceRef, ProjectionElem};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::{
hir::place::PlaceBase,
mir::{self, ClearCrossCrate, Local, LocalDecl, LocalInfo, Location},
};
use rustc_span::source_map::DesugaringKind;
use rustc_span::symbol::{kw, Symbol};
use rustc_span::Span;
Expand Down Expand Up @@ -241,6 +244,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
format!("mut {}", self.local_names[local].unwrap()),
Applicability::MachineApplicable,
);
let tcx = self.infcx.tcx;
if let ty::Closure(id, _) = the_place_err.ty(self.body, tcx).ty.kind() {
self.show_mutating_upvar(tcx, id, the_place_err, &mut err);
}
}

// Also suggest adding mut for upvars
Expand Down Expand Up @@ -271,6 +278,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
);
}
}

let tcx = self.infcx.tcx;
if let ty::Ref(_, ty, Mutability::Mut) = the_place_err.ty(self.body, tcx).ty.kind()
{
if let ty::Closure(id, _) = ty.kind() {
self.show_mutating_upvar(tcx, id, the_place_err, &mut err);
}
}
}

// complete hack to approximate old AST-borrowck
Expand Down Expand Up @@ -463,6 +478,45 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
err.buffer(&mut self.errors_buffer);
}

// point to span of upvar making closure call require mutable borrow
fn show_mutating_upvar(
&self,
tcx: TyCtxt<'_>,
id: &hir::def_id::DefId,
the_place_err: PlaceRef<'tcx>,
err: &mut DiagnosticBuilder<'_>,
) {
let id = id.expect_local();
let tables = tcx.typeck(id);
let hir_id = tcx.hir().local_def_id_to_hir_id(id);
let (span, place) = &tables.closure_kind_origins()[hir_id];
let reason = if let PlaceBase::Upvar(upvar_id) = place.base {
let upvar = ty::place_to_string_for_capture(tcx, place);
match tables.upvar_capture(upvar_id) {
ty::UpvarCapture::ByRef(ty::UpvarBorrow {
kind: ty::BorrowKind::MutBorrow,
..
}) => {
format!("mutable borrow of `{}`", upvar)
}
ty::UpvarCapture::ByValue(_) => {
format!("possible mutation of `{}`", upvar)
}
_ => bug!("upvar `{}` borrowed, but not mutably", upvar),
}
} else {
bug!("not an upvar")
};
err.span_label(
*span,
format!(
"calling `{}` requires mutable binding due to {}",
self.describe_place(the_place_err).unwrap(),
reason
),
);
}

/// Targeted error when encountering an `FnMut` closure where an `Fn` closure was expected.
fn expected_fn_found_fn_mut_call(&self, err: &mut DiagnosticBuilder<'_>, sp: Span, act: &str) {
err.span_label(sp, format!("cannot {}", act));
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/borrowck/borrow-raw-address-of-mutability.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable
|
LL | let f = || {
| - help: consider changing this to be mutable: `mut f`
...
LL | let y = &raw mut x;
| - calling `f` requires mutable binding due to mutable borrow of `x`
LL | };
LL | f();
| ^ cannot borrow as mutable

Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/closures/issue-80313-mutable-borrow-in-closure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
let mut my_var = false;
let callback = || {
&mut my_var;
};
callback(); //~ ERROR E0596
}
14 changes: 14 additions & 0 deletions src/test/ui/closures/issue-80313-mutable-borrow-in-closure.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0596]: cannot borrow `callback` as mutable, as it is not declared as mutable
--> $DIR/issue-80313-mutable-borrow-in-closure.rs:6:5
|
LL | let callback = || {
| -------- help: consider changing this to be mutable: `mut callback`
LL | &mut my_var;
| ------ calling `callback` requires mutable binding due to mutable borrow of `my_var`
LL | };
LL | callback();
| ^^^^^^^^ cannot borrow as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
let mut my_var = false;
let callback = move || {
&mut my_var;
};
callback(); //~ ERROR E0596
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0596]: cannot borrow `callback` as mutable, as it is not declared as mutable
--> $DIR/issue-80313-mutable-borrow-in-move-closure.rs:6:5
|
LL | let callback = move || {
| -------- help: consider changing this to be mutable: `mut callback`
LL | &mut my_var;
| ------ calling `callback` requires mutable binding due to possible mutation of `my_var`
LL | };
LL | callback();
| ^^^^^^^^ cannot borrow as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
7 changes: 7 additions & 0 deletions src/test/ui/closures/issue-80313-mutation-in-closure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
let mut my_var = false;
let callback = || {
my_var = true;
};
callback(); //~ ERROR E0596
}
14 changes: 14 additions & 0 deletions src/test/ui/closures/issue-80313-mutation-in-closure.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0596]: cannot borrow `callback` as mutable, as it is not declared as mutable
--> $DIR/issue-80313-mutation-in-closure.rs:6:5
|
LL | let callback = || {
| -------- help: consider changing this to be mutable: `mut callback`
LL | my_var = true;
| ------ calling `callback` requires mutable binding due to mutable borrow of `my_var`
LL | };
LL | callback();
| ^^^^^^^^ cannot borrow as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
7 changes: 7 additions & 0 deletions src/test/ui/closures/issue-80313-mutation-in-move-closure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
let mut my_var = false;
let callback = move || {
my_var = true;
};
callback(); //~ ERROR E0596
}
14 changes: 14 additions & 0 deletions src/test/ui/closures/issue-80313-mutation-in-move-closure.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0596]: cannot borrow `callback` as mutable, as it is not declared as mutable
--> $DIR/issue-80313-mutation-in-move-closure.rs:6:5
|
LL | let callback = move || {
| -------- help: consider changing this to be mutable: `mut callback`
LL | my_var = true;
| ------ calling `callback` requires mutable binding due to possible mutation of `my_var`
LL | };
LL | callback();
| ^^^^^^^^ cannot borrow as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0596]: cannot borrow `tick1` as mutable, as it is not declared as mutable
|
LL | let tick1 = || {
| ----- help: consider changing this to be mutable: `mut tick1`
LL | counter += 1;
| ------- calling `tick1` requires mutable binding due to mutable borrow of `counter`
...
LL | tick1();
| ^^^^^ cannot borrow as mutable
Expand All @@ -12,6 +14,8 @@ error[E0596]: cannot borrow `tick2` as mutable, as it is not declared as mutable
|
LL | let tick2 = || {
| ----- help: consider changing this to be mutable: `mut tick2`
LL | tick1();
| ----- calling `tick2` requires mutable binding due to mutable borrow of `tick1`
...
LL | tick2();
| ^^^^^ cannot borrow as mutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error[E0596]: cannot borrow `tick` as mutable, as it is not declared as mutable
--> $DIR/unboxed-closures-infer-fnmut-missing-mut.rs:7:5
|
LL | let tick = || counter += 1;
| ---- help: consider changing this to be mutable: `mut tick`
| ---- ------- calling `tick` requires mutable binding due to mutable borrow of `counter`
| |
| help: consider changing this to be mutable: `mut tick`
LL | tick();
| ^^^^ cannot borrow as mutable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error[E0596]: cannot borrow `tick` as mutable, as it is not declared as mutable
--> $DIR/unboxed-closures-infer-fnmut-move-missing-mut.rs:7:5
|
LL | let tick = move || counter += 1;
| ---- help: consider changing this to be mutable: `mut tick`
| ---- ------- calling `tick` requires mutable binding due to possible mutation of `counter`
| |
| help: consider changing this to be mutable: `mut tick`
LL | tick();
| ^^^^ cannot borrow as mutable

Expand Down