Skip to content

Allow unsafe_code in __thread_local_inner. #23579

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

Merged
merged 1 commit into from
Mar 24, 2015

Conversation

Ms2ger
Copy link
Contributor

@Ms2ger Ms2ger commented Mar 21, 2015

This fixes a build error when using thread_local!() in a deny(unsafe_code)
scope in Servo for Android.

@rust-highfive
Copy link
Contributor

r? @nikomatsakis

(rust_highfive has picked a reviewer for you, use r? to override)

@@ -205,6 +205,7 @@ macro_rules! __thread_local_inner {

#[cfg(any(not(any(target_os = "macos", target_os = "linux")), target_arch = "aarch64"))]
const _INIT: ::std::thread_local::__impl::KeyInner<$t> = {
#[allow(unsafe_code)]
unsafe extern fn __destroy(ptr: *mut u8) {
::std::thread_local::__impl::destroy_value::<$t>(ptr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the weird things I might have cleaned up in my post-const-fn branch - thread_local definitely took the longest out of all of libstd.

Would using ::std::thread_local::__impl::destroy_value::<$t> instead of __destroy work at all?
Is it perhaps an ABI mismatch? If it is, can the destroy_value definition be changed, instead?

Also, take great care in keeping consistency across the supported combinations (IMO they should go in separate files to avoid #[cfg] confusion).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would using ::std::thread_local::__impl::destroy_value::<$t> instead of __destroy work at all?

Yeah that worked locally for me. I think that I may have just been playing around with various strategies and never revisited this.

@Ms2ger could you update the PR with something like:

diff --git a/src/libstd/thread_local/mod.rs b/src/libstd/thread_local/mod.rs
index 0878029..15cc469 100644
--- a/src/libstd/thread_local/mod.rs
+++ b/src/libstd/thread_local/mod.rs
@@ -205,15 +205,14 @@ macro_rules! __thread_local_inner {

         #[cfg(any(not(any(target_os = "macos", target_os = "linux")), target_arch = "aarch64"))]
         const _INIT: ::std::thread_local::__impl::KeyInner<$t> = {
-            unsafe extern fn __destroy(ptr: *mut u8) {
-                ::std::thread_local::__impl::destroy_value::<$t>(ptr);
-            }
-
             ::std::thread_local::__impl::KeyInner {
                 inner: ::std::cell::UnsafeCell { value: $init },
                 os: ::std::thread_local::__impl::OsStaticKey {
                     inner: ::std::thread_local::__impl::OS_INIT_INNER,
-                    dtor: ::std::option::Option::Some(__destroy as unsafe extern fn(*mut u8)),
+                    dtor: ::std::option::Option::Some(
+                        ::std::thread_local::__impl::destroy_value::<$t>
+                            as unsafe extern fn(*mut u8)
+                    ),
                 },
             }
         };

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, but do you still need the cast? I'm mainly asking because if you do, that looks like a bug/limitation in the "coerce to expected type" logic in typeck.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice, it is no longer necessary as well!

This fixes a build error when using thread_local!() in a deny(unsafe_code)
scope in Servo for Android.
@Ms2ger Ms2ger force-pushed the thread_local-unsafe branch from 86e649a to 29aca83 Compare March 22, 2015 19:28
@Ms2ger
Copy link
Contributor Author

Ms2ger commented Mar 22, 2015

r? @alexcrichton

@alexcrichton
Copy link
Member

@bors: r+ 29aca83

Thanks!

@bors
Copy link
Collaborator

bors commented Mar 23, 2015

⌛ Testing commit 29aca83 with merge 8692e1c...

@bors
Copy link
Collaborator

bors commented Mar 23, 2015

💔 Test failed - auto-win-64-opt

@Manishearth
Copy link
Member

@bors: retry

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Mar 23, 2015
Conflicts:
	src/libstd/thread/local.rs
@bors bors merged commit 29aca83 into rust-lang:master Mar 24, 2015
@Ms2ger Ms2ger deleted the thread_local-unsafe branch March 25, 2015 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants