-
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 #22219 - pnkfelix:partial-reinit, r=pnkfelix
borrowck: Prevent partial reinitialization of uninitialized structures This is a pnkfelix-swiped squash of #22079, which was a rebase and revision of #18963 Fixes #18571.
- Loading branch information
Showing
6 changed files
with
181 additions
and
1 deletion.
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
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 2014-2015 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. | ||
|
||
struct Test; | ||
|
||
struct Test2 { | ||
b: Option<Test>, | ||
} | ||
|
||
struct Test3(Option<Test>); | ||
|
||
impl Drop for Test { | ||
fn drop(&mut self) { | ||
println!("dropping!"); | ||
} | ||
} | ||
|
||
impl Drop for Test2 { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
impl Drop for Test3 { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
fn stuff() { | ||
let mut t = Test2 { b: None }; | ||
let u = Test; | ||
drop(t); | ||
t.b = Some(u); | ||
//~^ ERROR partial reinitialization of uninitialized structure `t` | ||
|
||
let mut t = Test3(None); | ||
let u = Test; | ||
drop(t); | ||
t.0 = Some(u); | ||
//~^ ERROR partial reinitialization of uninitialized structure `t` | ||
} | ||
|
||
fn main() { | ||
stuff() | ||
} |
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,34 @@ | ||
// Copyright 2014-2015 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. | ||
|
||
struct Test { | ||
a: isize, | ||
b: Option<Box<Test>>, | ||
} | ||
|
||
impl Drop for Test { | ||
fn drop(&mut self) { | ||
println!("Dropping {}", self.a); | ||
} | ||
} | ||
|
||
fn stuff() { | ||
let mut t = Test { a: 1, b: None}; | ||
let mut u = Test { a: 2, b: Some(Box::new(t))}; | ||
t.b = Some(Box::new(u)); | ||
//~^ ERROR partial reinitialization of uninitialized structure `t` | ||
println!("done"); | ||
} | ||
|
||
fn main() { | ||
stuff(); | ||
println!("Hello, world!") | ||
} | ||
|
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,22 @@ | ||
// Copyright 2015 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. | ||
use std::mem; | ||
|
||
struct Test { f: usize } | ||
impl Drop for Test { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
fn main() { | ||
let mut x = (Test { f: 2 }, Test { f: 4 }); | ||
mem::drop(x.0); | ||
x.0.f = 3; | ||
//~^ ERROR partial reinitialization of uninitialized structure `x.0` | ||
} |
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,33 @@ | ||
// Copyright 2015 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. | ||
|
||
struct Test; | ||
|
||
struct Test2(Option<Test>); | ||
|
||
impl Drop for Test { | ||
fn drop(&mut self) { | ||
println!("dropping!"); | ||
} | ||
} | ||
|
||
impl Drop for Test2 { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
fn stuff() { | ||
let mut x : (Test2, Test2); | ||
(x.0).0 = Some(Test); | ||
//~^ ERROR partial reinitialization of uninitialized structure `x.0` | ||
} | ||
|
||
fn main() { | ||
stuff() | ||
} |