Skip to content

librustc: Forbid partial reinitialization of uninitialized structures or enumerations that implement the Drop trait. #18963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
50 changes: 48 additions & 2 deletions src/librustc/middle/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,13 +748,26 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
if mode != euv::Init {
check_for_assignment_to_borrowed_path(
self, assignment_id, assignment_span, assignee_cmt.clone());
mark_variable_as_used_mut(self, assignee_cmt);
mark_variable_as_used_mut(self, assignee_cmt.clone());
}
}

// Check for partial reinitialization of a fully uninitialized structure.
match assignee_cmt.cat {
mc::cat_interior(ref container_cmt, _) | mc::cat_downcast(ref container_cmt) => {
check_for_illegal_initialization(self,
assignment_id,
assignment_span,
container_cmt);
}
mc::cat_rvalue(_) | mc::cat_static_item | mc::cat_upvar(_) | mc::cat_local(_) |
mc::cat_deref(_, _, _) => {}
}
return;
}

// Initializations are OK.
// Initializations are OK if and only if they aren't partial
// reinitialization of a partially-uninitialized structure.
if mode == euv::Init {
return
}
Expand Down Expand Up @@ -929,6 +942,39 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
false
});
}

fn check_for_illegal_initialization(this: &CheckLoanCtxt,
assignment_id: ast::NodeId,
span: Span,
assignee_cmt: &mc::cmt) {
let local_id = match assignee_cmt.cat {
mc::cat_interior(ref container_cmt, _) | mc::cat_downcast(ref container_cmt) => {
return check_for_illegal_initialization(this,
assignment_id,
span,
container_cmt)
}
mc::cat_local(local_id) => local_id,
mc::cat_rvalue(_) | mc::cat_static_item | mc::cat_upvar(_) |
mc::cat_deref(..) => return,
};

let struct_id = match ty::get(assignee_cmt.ty).sty {
ty::ty_struct(def_id, _) => def_id,
_ => return,
};
if !ty::has_dtor(this.tcx(), struct_id) {
return
}

let loan_path = Rc::new(LpVar(local_id));
this.move_data.each_move_of(assignment_id, &loan_path, |_, _| {
this.bccx.report_partial_reinitialization_of_uninitialized_structure(
span,
&*loan_path);
false
});
}
}

pub fn report_illegal_mutation(&self,
Expand Down
12 changes: 12 additions & 0 deletions src/librustc/middle/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,18 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
}
}

pub fn report_partial_reinitialization_of_uninitialized_structure(
&self,
span: Span,
lp: &LoanPath) {
self.tcx
.sess
.span_err(span,
(format!("partial reinitialization of uninitialized \
structure `{}`",
self.loan_path_to_string(lp))).as_slice());
}

pub fn report_reassigned_immutable_variable(&self,
span: Span,
lp: &LoanPath,
Expand Down
37 changes: 37 additions & 0 deletions src/test/compile-fail/borrowck-partial-reinit-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2014 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>,
}

impl Drop for Test {
fn drop(&mut self) {
println!("dropping!");
}
}

impl Drop for Test2 {
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
}

fn main() {
stuff()
}
34 changes: 34 additions & 0 deletions src/test/compile-fail/borrowck-partial-reinit-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2014 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: int,
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 t)};
t.b = Some(box u);
//~^ ERROR partial reinitialization of uninitialized structure
println!("done");
}

fn main() {
stuff();
println!("Hello, world!")
}