-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #87998 - nneonneo:master, r=oli-obk
Avoid spurious "previous iteration of loop" errors Only follow backwards edges during `get_moved_indexes` if the move path is definitely initialized at loop entry. Otherwise, the error occurred prior to the loop, so we ignore the backwards edges to avoid generating misleading "value moved here, in previous iteration of loop" errors. This patch also slightly improves the analysis of inits, including `NonPanicPathOnly` initializations (which are ignored by `drop_flag_effects::for_location_inits`). This is required for the definite initialization analysis, but may also help find certain skipped reinits in rare cases. Patch passes all non-ignored src/test/ui testcases. Fixes #72649.
- Loading branch information
Showing
3 changed files
with
212 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Regression test for issue #72649 | ||
// Tests that we don't emit spurious | ||
// 'value moved in previous iteration of loop' message | ||
|
||
struct NonCopy; | ||
|
||
fn good() { | ||
loop { | ||
let value = NonCopy{}; | ||
let _used = value; | ||
} | ||
} | ||
|
||
fn moved_here_1() { | ||
loop { | ||
let value = NonCopy{}; | ||
//~^ NOTE move occurs because `value` has type `NonCopy`, which does not implement the `Copy` trait | ||
let _used = value; | ||
//~^ NOTE value moved here | ||
let _used2 = value; //~ ERROR use of moved value: `value` | ||
//~^ NOTE value used here after move | ||
} | ||
} | ||
|
||
fn moved_here_2() { | ||
let value = NonCopy{}; | ||
//~^ NOTE move occurs because `value` has type `NonCopy`, which does not implement the `Copy` trait | ||
loop { | ||
let _used = value; | ||
//~^ NOTE value moved here | ||
loop { | ||
let _used2 = value; //~ ERROR use of moved value: `value` | ||
//~^ NOTE value used here after move | ||
} | ||
} | ||
} | ||
|
||
fn moved_loop_1() { | ||
let value = NonCopy{}; | ||
//~^ NOTE move occurs because `value` has type `NonCopy`, which does not implement the `Copy` trait | ||
loop { | ||
let _used = value; //~ ERROR use of moved value: `value` | ||
//~^ NOTE value moved here, in previous iteration of loop | ||
} | ||
} | ||
|
||
fn moved_loop_2() { | ||
let mut value = NonCopy{}; | ||
//~^ NOTE move occurs because `value` has type `NonCopy`, which does not implement the `Copy` trait | ||
let _used = value; | ||
value = NonCopy{}; | ||
loop { | ||
let _used2 = value; //~ ERROR use of moved value: `value` | ||
//~^ NOTE value moved here, in previous iteration of loop | ||
} | ||
} | ||
|
||
fn uninit_1() { | ||
loop { | ||
let value: NonCopy; | ||
let _used = value; //~ ERROR use of possibly-uninitialized variable: `value` | ||
//~^ NOTE use of possibly-uninitialized `value` | ||
} | ||
} | ||
|
||
fn uninit_2() { | ||
let mut value: NonCopy; | ||
loop { | ||
let _used = value; //~ ERROR use of possibly-uninitialized variable: `value` | ||
//~^ NOTE use of possibly-uninitialized `value` | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
error[E0382]: use of moved value: `value` | ||
--> $DIR/issue-72649-uninit-in-loop.rs:20:22 | ||
| | ||
LL | let value = NonCopy{}; | ||
| ----- move occurs because `value` has type `NonCopy`, which does not implement the `Copy` trait | ||
LL | | ||
LL | let _used = value; | ||
| ----- value moved here | ||
LL | | ||
LL | let _used2 = value; | ||
| ^^^^^ value used here after move | ||
|
||
error[E0382]: use of moved value: `value` | ||
--> $DIR/issue-72649-uninit-in-loop.rs:32:26 | ||
| | ||
LL | let value = NonCopy{}; | ||
| ----- move occurs because `value` has type `NonCopy`, which does not implement the `Copy` trait | ||
... | ||
LL | let _used = value; | ||
| ----- value moved here | ||
... | ||
LL | let _used2 = value; | ||
| ^^^^^ value used here after move | ||
|
||
error[E0382]: use of moved value: `value` | ||
--> $DIR/issue-72649-uninit-in-loop.rs:42:21 | ||
| | ||
LL | let value = NonCopy{}; | ||
| ----- move occurs because `value` has type `NonCopy`, which does not implement the `Copy` trait | ||
... | ||
LL | let _used = value; | ||
| ^^^^^ value moved here, in previous iteration of loop | ||
|
||
error[E0382]: use of moved value: `value` | ||
--> $DIR/issue-72649-uninit-in-loop.rs:53:22 | ||
| | ||
LL | let mut value = NonCopy{}; | ||
| --------- move occurs because `value` has type `NonCopy`, which does not implement the `Copy` trait | ||
... | ||
LL | let _used2 = value; | ||
| ^^^^^ value moved here, in previous iteration of loop | ||
|
||
error[E0381]: use of possibly-uninitialized variable: `value` | ||
--> $DIR/issue-72649-uninit-in-loop.rs:61:21 | ||
| | ||
LL | let _used = value; | ||
| ^^^^^ use of possibly-uninitialized `value` | ||
|
||
error[E0381]: use of possibly-uninitialized variable: `value` | ||
--> $DIR/issue-72649-uninit-in-loop.rs:69:21 | ||
| | ||
LL | let _used = value; | ||
| ^^^^^ use of possibly-uninitialized `value` | ||
|
||
error: aborting due to 6 previous errors | ||
|
||
Some errors have detailed explanations: E0381, E0382. | ||
For more information about an error, try `rustc --explain E0381`. |