Skip to content

Commit 8784d2f

Browse files
committed
Forbid moves out of static items Closes #10577
1 parent c75dd78 commit 8784d2f

File tree

5 files changed

+36
-20
lines changed

5 files changed

+36
-20
lines changed

src/librustc/middle/borrowck/gather_loans/gather_moves.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fn check_is_legal_to_move_from(bccx: &BorrowckCtxt,
104104
mc::cat_deref(_, _, mc::BorrowedPtr(..)) |
105105
mc::cat_deref(_, _, mc::GcPtr) |
106106
mc::cat_deref(_, _, mc::UnsafePtr(..)) |
107-
mc::cat_upvar(..) |
107+
mc::cat_upvar(..) | mc::cat_static_item |
108108
mc::cat_copied_upvar(mc::CopiedUpvar { onceness: ast::Many, .. }) => {
109109
bccx.span_err(
110110
cmt0.span,
@@ -120,19 +120,6 @@ fn check_is_legal_to_move_from(bccx: &BorrowckCtxt,
120120
true
121121
}
122122

123-
// It seems strange to allow a move out of a static item,
124-
// but what happens in practice is that you have a
125-
// reference to a constant with a type that should be
126-
// moved, like `None::<~int>`. The type of this constant
127-
// is technically `Option<~int>`, which moves, but we know
128-
// that the content of static items will never actually
129-
// contain allocated pointers, so we can just memcpy it.
130-
// Since static items can never have allocated memory,
131-
// this is ok. For now anyhow.
132-
mc::cat_static_item => {
133-
true
134-
}
135-
136123
mc::cat_rvalue(..) |
137124
mc::cat_local(..) |
138125
mc::cat_arg(..) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Ensure that moves out of static items is forbidden
12+
13+
use std::kinds::marker;
14+
15+
struct Foo {
16+
foo: int,
17+
nopod: marker::NoPod
18+
}
19+
20+
static BAR: Foo = Foo{foo: 5, nopod: marker::NoPod};
21+
22+
23+
fn test(f: Foo) {
24+
let _f = Foo{foo: 4, ..f};
25+
}
26+
27+
fn main() {
28+
test(BAR); //~ ERROR cannot move out of static item
29+
}

src/test/compile-fail/static-items-cant-move.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ fn test(f: Foo) {
2525
}
2626

2727
fn main() {
28-
test(BAR);
28+
test(BAR); //~ ERROR cannot move out of static item
2929
}

src/test/compile-fail/std-uncopyable-atomics.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ use std::sync::atomics::*;
1616
use std::ptr;
1717

1818
fn main() {
19-
let x = INIT_ATOMIC_FLAG;
19+
let x = INIT_ATOMIC_FLAG; //~ ERROR cannot move out of static item
2020
let x = *&x; //~ ERROR: cannot move out of dereference
21-
let x = INIT_ATOMIC_BOOL;
21+
let x = INIT_ATOMIC_BOOL; //~ ERROR cannot move out of static item
2222
let x = *&x; //~ ERROR: cannot move out of dereference
23-
let x = INIT_ATOMIC_INT;
23+
let x = INIT_ATOMIC_INT; //~ ERROR cannot move out of static item
2424
let x = *&x; //~ ERROR: cannot move out of dereference
25-
let x = INIT_ATOMIC_UINT;
25+
let x = INIT_ATOMIC_UINT; //~ ERROR cannot move out of static item
2626
let x = *&x; //~ ERROR: cannot move out of dereference
2727
let x: AtomicPtr<uint> = AtomicPtr::new(ptr::mut_null());
2828
let x = *&x; //~ ERROR: cannot move out of dereference

src/test/run-pass/issue-6919.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
extern crate issue6919_3;
1616

1717
pub fn main() {
18-
issue6919_3::D.k;
18+
let _ = issue6919_3::D.k;
1919
}
2020

0 commit comments

Comments
 (0)