File tree Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change 1+ // A version of `issue-70919-drop-in-loop`, but without
2+ // the necessary `drop` call.
3+ //
4+ // This should fail to compile, since the `Drop` impl
5+ // for `WrapperWithDrop` could observe the changed
6+ // `base` value.
7+
8+ struct WrapperWithDrop < ' a > ( & ' a mut bool ) ;
9+ impl < ' a > Drop for WrapperWithDrop < ' a > {
10+ fn drop ( & mut self ) {
11+ }
12+ }
13+
14+ fn drop_in_loop ( ) {
15+ let mut base = true ;
16+ let mut wrapper = WrapperWithDrop ( & mut base) ;
17+ loop {
18+ base = false ; //~ ERROR: cannot assign to `base`
19+ wrapper = WrapperWithDrop ( & mut base) ;
20+ }
21+ }
22+
23+ fn main ( ) {
24+ }
Original file line number Diff line number Diff line change 1+ error[E0506]: cannot assign to `base` because it is borrowed
2+ --> $DIR/drop-in-loop.rs:18:9
3+ |
4+ LL | let mut wrapper = WrapperWithDrop(&mut base);
5+ | --------- `base` is borrowed here
6+ LL | loop {
7+ LL | base = false;
8+ | ^^^^^^^^^^^^ `base` is assigned to here but it was already borrowed
9+ LL | wrapper = WrapperWithDrop(&mut base);
10+ | ------- borrow might be used here, when `wrapper` is dropped and runs the `Drop` code for type `WrapperWithDrop`
11+
12+ error: aborting due to previous error
13+
14+ For more information about this error, try `rustc --explain E0506`.
Original file line number Diff line number Diff line change 1+ // Regression test for issue #70919
2+ // Tests that we don't emit a spurious "borrow might be used" error
3+ // when we have an explicit `drop` in a loop
4+
5+ // check-pass
6+
7+ struct WrapperWithDrop < ' a > ( & ' a mut bool ) ;
8+ impl < ' a > Drop for WrapperWithDrop < ' a > {
9+ fn drop ( & mut self ) {
10+ }
11+ }
12+
13+ fn drop_in_loop ( ) {
14+ let mut base = true ;
15+ let mut wrapper = WrapperWithDrop ( & mut base) ;
16+ loop {
17+ drop ( wrapper) ;
18+
19+ base = false ;
20+ wrapper = WrapperWithDrop ( & mut base) ;
21+ }
22+ }
23+
24+ fn main ( ) {
25+ }
You can’t perform that action at this time.
0 commit comments