Skip to content

Commit

Permalink
Forbid moves out of static items Closes #10577
Browse files Browse the repository at this point in the history
  • Loading branch information
flaper87 committed Feb 27, 2014
1 parent c75dd78 commit 8784d2f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
15 changes: 1 addition & 14 deletions src/librustc/middle/borrowck/gather_loans/gather_moves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn check_is_legal_to_move_from(bccx: &BorrowckCtxt,
mc::cat_deref(_, _, mc::BorrowedPtr(..)) |
mc::cat_deref(_, _, mc::GcPtr) |
mc::cat_deref(_, _, mc::UnsafePtr(..)) |
mc::cat_upvar(..) |
mc::cat_upvar(..) | mc::cat_static_item |
mc::cat_copied_upvar(mc::CopiedUpvar { onceness: ast::Many, .. }) => {
bccx.span_err(
cmt0.span,
Expand All @@ -120,19 +120,6 @@ fn check_is_legal_to_move_from(bccx: &BorrowckCtxt,
true
}

// It seems strange to allow a move out of a static item,
// but what happens in practice is that you have a
// reference to a constant with a type that should be
// moved, like `None::<~int>`. The type of this constant
// is technically `Option<~int>`, which moves, but we know
// that the content of static items will never actually
// contain allocated pointers, so we can just memcpy it.
// Since static items can never have allocated memory,
// this is ok. For now anyhow.
mc::cat_static_item => {
true
}

mc::cat_rvalue(..) |
mc::cat_local(..) |
mc::cat_arg(..) => {
Expand Down
29 changes: 29 additions & 0 deletions src/test/compile-fail/borrowck-move-out-of-static-item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.

// Ensure that moves out of static items is forbidden

use std::kinds::marker;

struct Foo {
foo: int,
nopod: marker::NoPod
}

static BAR: Foo = Foo{foo: 5, nopod: marker::NoPod};


fn test(f: Foo) {
let _f = Foo{foo: 4, ..f};
}

fn main() {
test(BAR); //~ ERROR cannot move out of static item
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/static-items-cant-move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ fn test(f: Foo) {
}

fn main() {
test(BAR);
test(BAR); //~ ERROR cannot move out of static item
}
8 changes: 4 additions & 4 deletions src/test/compile-fail/std-uncopyable-atomics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ use std::sync::atomics::*;
use std::ptr;

fn main() {
let x = INIT_ATOMIC_FLAG;
let x = INIT_ATOMIC_FLAG; //~ ERROR cannot move out of static item
let x = *&x; //~ ERROR: cannot move out of dereference
let x = INIT_ATOMIC_BOOL;
let x = INIT_ATOMIC_BOOL; //~ ERROR cannot move out of static item
let x = *&x; //~ ERROR: cannot move out of dereference
let x = INIT_ATOMIC_INT;
let x = INIT_ATOMIC_INT; //~ ERROR cannot move out of static item
let x = *&x; //~ ERROR: cannot move out of dereference
let x = INIT_ATOMIC_UINT;
let x = INIT_ATOMIC_UINT; //~ ERROR cannot move out of static item
let x = *&x; //~ ERROR: cannot move out of dereference
let x: AtomicPtr<uint> = AtomicPtr::new(ptr::mut_null());
let x = *&x; //~ ERROR: cannot move out of dereference
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/issue-6919.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
extern crate issue6919_3;

pub fn main() {
issue6919_3::D.k;
let _ = issue6919_3::D.k;
}

0 comments on commit 8784d2f

Please sign in to comment.