-
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.
Check #[thread_local] statics correctly in the compiler.
- Loading branch information
Showing
12 changed files
with
229 additions
and
41 deletions.
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
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,42 @@ | ||
// Copyright 2017 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. | ||
|
||
#![feature(cfg_target_thread_local, thread_local)] | ||
|
||
#[cfg_attr(target_thread_local, thread_local)] | ||
static FOO: u8 = 3; | ||
|
||
// On platforms *without* `#[thread_local]`, create | ||
// a reference to a temporary to fake the same error. | ||
#[cfg(not(target_thread_local))] | ||
macro_rules! let_ { | ||
($name:ident = $tls:expr) => { | ||
let dummy = *$tls; | ||
let $name = &dummy; | ||
} | ||
} | ||
|
||
#[cfg(target_thread_local)] | ||
macro_rules! let_ { | ||
($name:ident = $tls:expr) => { | ||
let $name = $tls; | ||
} | ||
} | ||
|
||
fn main() { | ||
let_!(a = &FOO); | ||
//~^ ERROR borrowed value does not live long enough | ||
//~| does not live long enough | ||
//~| NOTE borrowed value must be valid for the static lifetime | ||
|
||
std::thread::spawn(move || { | ||
println!("{}", a); | ||
}); | ||
} //~ temporary value only lives until here |
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,39 @@ | ||
// Copyright 2017 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. | ||
|
||
#![feature(const_fn, drop_types_in_const)] | ||
#![feature(cfg_target_thread_local, thread_local_internals)] | ||
|
||
// On platforms *without* `#[thread_local]`, use | ||
// a custom non-`Sync` type to fake the same error. | ||
#[cfg(not(target_thread_local))] | ||
struct Key<T> { | ||
_data: std::cell::UnsafeCell<Option<T>>, | ||
_flag: std::cell::Cell<bool>, | ||
} | ||
|
||
#[cfg(not(target_thread_local))] | ||
impl<T> Key<T> { | ||
const fn new() -> Self { | ||
Key { | ||
_data: std::cell::UnsafeCell::new(None), | ||
_flag: std::cell::Cell::new(false), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(target_thread_local)] | ||
use std::thread::__FastLocalKeyInner as Key; | ||
|
||
static __KEY: Key<()> = Key::new(); | ||
//~^ ERROR `std::cell::UnsafeCell<std::option::Option<()>>: std::marker::Sync` is not satisfied | ||
//~| ERROR `std::cell::Cell<bool>: std::marker::Sync` is not satisfied | ||
|
||
fn main() {} |
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,41 @@ | ||
// Copyright 2017 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. | ||
|
||
#![feature(const_fn, drop_types_in_const)] | ||
#![feature(cfg_target_thread_local, thread_local_internals)] | ||
|
||
type Foo = std::cell::RefCell<String>; | ||
|
||
#[cfg(target_thread_local)] | ||
static __KEY: std::thread::__FastLocalKeyInner<Foo> = | ||
std::thread::__FastLocalKeyInner::new(); | ||
|
||
#[cfg(not(target_thread_local))] | ||
static __KEY: std::thread::__OsLocalKeyInner<Foo> = | ||
std::thread::__OsLocalKeyInner::new(); | ||
|
||
fn __getit() -> std::option::Option< | ||
&'static std::cell::UnsafeCell< | ||
std::option::Option<Foo>>> | ||
{ | ||
__KEY.get() //~ ERROR invocation of unsafe method requires unsafe | ||
} | ||
|
||
static FOO: std::thread::LocalKey<Foo> = | ||
std::thread::LocalKey::new(__getit, Default::default); | ||
//~^ ERROR call to unsafe function requires unsafe | ||
|
||
fn main() { | ||
FOO.with(|foo| println!("{}", foo.borrow())); | ||
std::thread::spawn(|| { | ||
FOO.with(|foo| *foo.borrow_mut() += "foo"); | ||
}).join().unwrap(); | ||
FOO.with(|foo| println!("{}", foo.borrow())); | ||
} |
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,38 @@ | ||
// Copyright 2017 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. | ||
|
||
#![feature(const_fn, thread_local)] | ||
|
||
#[thread_local] | ||
static A: u32 = 1; | ||
|
||
static B: u32 = A; | ||
//~^ ERROR thread-local statics cannot be accessed at compile-time | ||
//~| ERROR cannot refer to other statics by value | ||
//~| WARN non-constant path in constant expression | ||
|
||
static C: &u32 = &A; | ||
//~^ ERROR thread-local statics cannot be accessed at compile-time | ||
|
||
const D: u32 = A; | ||
//~^ ERROR thread-local statics cannot be accessed at compile-time | ||
//~| ERROR cannot refer to statics by value | ||
//~| WARN non-constant path in constant expression | ||
|
||
const E: &u32 = &A; | ||
//~^ ERROR thread-local statics cannot be accessed at compile-time | ||
|
||
const fn f() -> u32 { | ||
A | ||
//~^ ERROR thread-local statics cannot be accessed at compile-time | ||
//~| ERROR cannot refer to statics by value | ||
} | ||
|
||
fn main() {} |
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