Skip to content

Allow "moves" out of immutable static (turn into memcpy) #13245

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
15 changes: 14 additions & 1 deletion src/librustc/middle/borrowck/gather_loans/gather_moves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,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_static_item |
mc::cat_upvar(..) |
mc::cat_copied_upvar(mc::CopiedUpvar { onceness: ast::Many, .. }) => {
bccx.span_err(
cmt0.span,
Expand All @@ -126,6 +126,19 @@ fn check_is_legal_to_move_from(bccx: &BorrowckCtxt,
false
}

mc::cat_static_item => {
match cmt.mutbl {
// "Moves" out of static items end-up calling memcpy, so they are allowed.
mc::McImmutable => {
true
}
_ => {
bccx.span_err( cmt0.span, "cannot move out of mutable static items");
false
}
}
}

// Can move out of captured upvars only if the destination closure
// type is 'once'. 1-shot stack closures emit the copied_upvar form
// (see mem_categorization.rs).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Verifies that static items can't be moved
// Ensure that moves out of mutable static items is disallowed

use std::kinds::marker;

Expand All @@ -17,13 +17,11 @@ struct Foo {
nocopy: marker::NoCopy
}

static BAR: Foo = Foo{foo: 5, nocopy: marker::NoCopy};


fn test(f: Foo) {
let _f = Foo{foo: 4, ..f};
}
static mut BAR: Foo = Foo{foo: 5, nocopy: marker::NoCopy};

fn main() {
test(BAR); //~ ERROR cannot move out of static item
unsafe {
let _f = BAR; //~ ERROR: cannot move out of mutable static item
}
}

6 changes: 3 additions & 3 deletions src/test/compile-fail/std-uncopyable-atomics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ use std::sync::atomics::*;
use std::ptr;

fn main() {
let x = INIT_ATOMIC_BOOL; //~ ERROR cannot move out of static item
let x = INIT_ATOMIC_BOOL;
let x = *&x; //~ ERROR: cannot move out of dereference
let x = INIT_ATOMIC_INT; //~ ERROR cannot move out of static item
let x = INIT_ATOMIC_INT;
let x = *&x; //~ ERROR: cannot move out of dereference
let x = INIT_ATOMIC_UINT; //~ ERROR cannot move out of static item
let x = INIT_ATOMIC_UINT;
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
37 changes: 37 additions & 0 deletions src/test/run-pass/borrowck-ensure-static-items-moves-copy.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.

// Ensure that moves out of statics don't null the static

use std::kinds::marker;

struct Foo {
foo: int,
nocopy: marker::NoCopy
}

impl Eq for Foo {
fn eq(&self, other: &Foo) -> bool {
self.foo == other.foo
}
}

impl std::fmt::Show for Foo {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f.buf, "Foo({})", self.foo)
}
}

static BAR: Foo = Foo{foo: 5, nocopy: marker::NoCopy};

fn main() {
let x = BAR;
assert_eq!(x, BAR);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Ensure that moves out of static items is forbidden
// Ensure that moves out of immutable static items is allowed

use std::kinds::marker;

Expand All @@ -25,5 +25,5 @@ fn test(f: Foo) {
}

fn main() {
test(BAR); //~ ERROR cannot move out of static item
test(BAR);
}