Skip to content

Remove Sized bound from scoped_thread_local! and ScopedKey #25232

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
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
23 changes: 12 additions & 11 deletions src/libstd/thread/scoped_tls.rs
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ pub mod __impl {
#[unstable(feature = "scoped_tls",
reason = "scoped TLS has yet to have wide enough use to fully consider \
stabilizing its interface")]
pub struct ScopedKey<T> { #[doc(hidden)] pub inner: __impl::KeyInner<T> }
pub struct ScopedKey<T: ?Sized> { #[doc(hidden)] pub inner: __impl::KeyInner<*const T> }

/// Declare a new scoped thread local storage key.
///
@@ -123,7 +123,7 @@ macro_rules! __scoped_thread_local_inner {
const _INIT: __Key<$t> = __Key {
inner: ::std::thread::__scoped::KeyInner {
inner: ::std::thread::__scoped::OS_INIT,
marker: ::std::marker::PhantomData::<::std::cell::Cell<$t>>,
marker: ::std::marker::PhantomData::<::std::cell::UnsafeCell<*const $t>>,
}
};

@@ -134,7 +134,7 @@ macro_rules! __scoped_thread_local_inner {
#[unstable(feature = "scoped_tls",
reason = "scoped TLS has yet to have wide enough use to fully consider \
stabilizing its interface")]
impl<T> ScopedKey<T> {
impl<T: ?Sized> ScopedKey<T> {
/// Inserts a value into this scoped thread local storage slot for a
/// duration of a closure.
///
@@ -167,19 +167,20 @@ impl<T> ScopedKey<T> {
pub fn set<R, F>(&'static self, t: &T, cb: F) -> R where
F: FnOnce() -> R,
{
struct Reset<'a, T: 'a> {
key: &'a __impl::KeyInner<T>,
val: *mut T,
struct Reset<'a, T: 'a + ?Sized> {
key: &'a __impl::KeyInner<*const T>,
val: *mut *const T,
}
impl<'a, T> Drop for Reset<'a, T> {
impl<'a, T: ?Sized> Drop for Reset<'a, T> {
fn drop(&mut self) {
unsafe { self.key.set(self.val) }
}
}

let mut pt = t as *const _;
let prev = unsafe {
let prev = self.inner.get();
self.inner.set(t as *const T as *mut T);
self.inner.set(&mut pt);
prev
};

@@ -213,7 +214,7 @@ impl<T> ScopedKey<T> {
let ptr = self.inner.get();
assert!(!ptr.is_null(), "cannot access a scoped thread local \
variable without calling `set` first");
cb(&*ptr)
cb(&**ptr)
}
}

@@ -252,13 +253,13 @@ mod imp {
target_arch = "aarch64"))]
mod imp {
use marker;
use std::cell::Cell;
use std::cell::UnsafeCell;
use sys_common::thread_local::StaticKey as OsStaticKey;

#[doc(hidden)]
pub struct KeyInner<T> {
pub inner: OsStaticKey,
pub marker: marker::PhantomData<Cell<T>>,
pub marker: marker::PhantomData<UnsafeCell<T>>,
}

unsafe impl<T> ::marker::Sync for KeyInner<T> { }
39 changes: 39 additions & 0 deletions src/test/run-pass/issue-25193.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2015 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(scoped_tls)]

trait TestTrait {
fn test(&self) -> bool;
}

struct TestStruct;

impl TestTrait for TestStruct {
fn test(&self) -> bool {
true
}
}

scoped_thread_local!(static TEST_SLICE: [u32]);
scoped_thread_local!(static TEST_TRAIT: TestTrait);

pub fn main() {
TEST_SLICE.set(&[0; 10], || {
TEST_SLICE.with(|slice| {
assert_eq!(slice, [0; 10]);
});
});
TEST_TRAIT.set(&TestStruct, || {
TEST_TRAIT.with(|traitObj| {
assert!(traitObj.test());
});
});
}