-
Notifications
You must be signed in to change notification settings - Fork 13k
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 #45025 - pnkfelix:mir-borrowck-moves-of-supporting-pref…
…ixes-invalidate-uses-too, r=arielb1 MIR-borrowck: moves of prefixes invalidate uses too I overlooked the fact that when we check if a path is moved, we need to check for interference between the (shallow) prefixes and the use in question. ~~Long term, we may want to revise how this computation is done. For example, it might be better to represent the set of invalidated prefixes in the dataflow computation (the `maybe_uninitialized` dataflow), and thus avoid one of the loops in the code here.~~ * Update: I was wrong in my original recollection of the dataflow code, which actually does the right thing, in terms of precisely tracking substructure initialization and movement. Fix #44833 ---- Update: The initial version of this PR's description (and the code as well) erroneously focused on supporting prefixes. ~~But the two main cases of interest are: 1. the *shallow* prefixes, and 2. the deref-free prefix built off a local (if the lvalue is indeed built off a local)~~ Update 2: The main cases of interest are in fact: 1. the nearest prefix with a MovePath, and 2. the suffixes.
- Loading branch information
Showing
7 changed files
with
276 additions
and
17 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
49 changes: 49 additions & 0 deletions
49
src/test/compile-fail/borrowck/borrowck-uninit-field-access.rs
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,49 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// revisions: ast mir | ||
//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir | ||
|
||
// Check that do not allow access to fields of uninitialized or moved | ||
// structs. | ||
|
||
#[derive(Default)] | ||
struct Point { | ||
x: isize, | ||
y: isize, | ||
} | ||
|
||
#[derive(Default)] | ||
struct Line { | ||
origin: Point, | ||
middle: Point, | ||
target: Point, | ||
} | ||
|
||
impl Line { fn consume(self) { } } | ||
|
||
fn main() { | ||
let mut a: Point; | ||
let _ = a.x + 1; //[ast]~ ERROR use of possibly uninitialized variable: `a.x` | ||
//[mir]~^ ERROR [E0381] | ||
//[mir]~| ERROR (Mir) [E0381] | ||
|
||
let mut line1 = Line::default(); | ||
let _moved = line1.origin; | ||
let _ = line1.origin.x + 1; //[ast]~ ERROR use of collaterally moved value: `line1.origin.x` | ||
//[mir]~^ [E0382] | ||
//[mir]~| (Mir) [E0381] | ||
|
||
let mut line2 = Line::default(); | ||
let _moved = (line2.origin, line2.middle); | ||
line2.consume(); //[ast]~ ERROR use of partially moved value: `line2` [E0382] | ||
//[mir]~^ [E0382] | ||
//[mir]~| (Mir) [E0381] | ||
} |
60 changes: 60 additions & 0 deletions
60
src/test/compile-fail/borrowck/borrowck-uninit-ref-chain.rs
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,60 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// revisions: ast mir | ||
//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir | ||
|
||
struct S<X, Y> { | ||
x: X, | ||
y: Y, | ||
} | ||
|
||
fn main() { | ||
let x: &&Box<i32>; | ||
let _y = &**x; //[ast]~ ERROR use of possibly uninitialized variable: `**x` [E0381] | ||
//[mir]~^ (Ast) [E0381] | ||
//[mir]~| (Mir) [E0381] | ||
|
||
let x: &&S<i32, i32>; | ||
let _y = &**x; //[ast]~ ERROR use of possibly uninitialized variable: `**x` [E0381] | ||
//[mir]~^ (Ast) [E0381] | ||
//[mir]~| (Mir) [E0381] | ||
|
||
let x: &&i32; | ||
let _y = &**x; //[ast]~ ERROR use of possibly uninitialized variable: `**x` [E0381] | ||
//[mir]~^ (Ast) [E0381] | ||
//[mir]~| (Mir) [E0381] | ||
|
||
|
||
let mut a: S<i32, i32>; | ||
a.x = 0; | ||
let _b = &a.x; //[ast]~ ERROR use of possibly uninitialized variable: `a.x` [E0381] | ||
//[mir]~^ ERROR (Ast) [E0381] | ||
// (deliberately *not* an error under MIR-borrowck) | ||
|
||
let mut a: S<&&i32, &&i32>; | ||
a.x = &&0; | ||
let _b = &**a.x; //[ast]~ ERROR use of possibly uninitialized variable: `**a.x` [E0381] | ||
//[mir]~^ ERROR (Ast) [E0381] | ||
// (deliberately *not* an error under MIR-borrowck) | ||
|
||
|
||
let mut a: S<i32, i32>; | ||
a.x = 0; | ||
let _b = &a.y; //[ast]~ ERROR use of possibly uninitialized variable: `a.y` [E0381] | ||
//[mir]~^ ERROR (Ast) [E0381] | ||
//[mir]~| ERROR (Mir) [E0381] | ||
|
||
let mut a: S<&&i32, &&i32>; | ||
a.x = &&0; | ||
let _b = &**a.y; //[ast]~ ERROR use of possibly uninitialized variable: `**a.y` [E0381] | ||
//[mir]~^ ERROR (Ast) [E0381] | ||
//[mir]~| ERROR (Mir) [E0381] | ||
} |
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
Oops, something went wrong.