Skip to content
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

There seems to be some parameters that has not been used in the __thread_local_inner macro of the unstabilized standard library. #65993

Closed
3442853561 opened this issue Oct 31, 2019 · 1 comment · Fixed by #66146
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@3442853561
Copy link
Contributor

3442853561 commented Oct 31, 2019

When I tried to learn std::thread_local by reading the implementation in the standard library, I found such a piece of code. There seems to be some parameters that has not been used in the __thread_local_inner macro of the unstabilized standard library.

#[doc(hidden)]
#[unstable(feature = "thread_local_internals",
           reason = "should not be necessary",
           issue = "0")]
#[macro_export]
#[allow_internal_unstable(thread_local_internals, cfg_target_thread_local, thread_local)]
#[allow_internal_unsafe]
macro_rules! __thread_local_inner {
    (@key $(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
        {
            #[inline]
            fn __init() -> $t { $init }

            unsafe fn __getit() -> $crate::option::Option<&'static $t> {
                #[cfg(all(target_arch = "wasm32", not(target_feature = "atomics")))]
                static __KEY: $crate::thread::__StaticLocalKeyInner<$t> =
                    $crate::thread::__StaticLocalKeyInner::new();

                #[thread_local]
                #[cfg(all(
                    target_thread_local,
                    not(all(target_arch = "wasm32", not(target_feature = "atomics"))),
                ))]
                static __KEY: $crate::thread::__FastLocalKeyInner<$t> =
                    $crate::thread::__FastLocalKeyInner::new();

                #[cfg(all(
                    not(target_thread_local),
                    not(all(target_arch = "wasm32", not(target_feature = "atomics"))),
                ))]
                static __KEY: $crate::thread::__OsLocalKeyInner<$t> =
                    $crate::thread::__OsLocalKeyInner::new();

                __KEY.get(__init)
            }

            unsafe {
                $crate::thread::LocalKey::new(__getit)
            }
        }
    };
    ($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
        $(#[$attr])* $vis const $name: $crate::thread::LocalKey<$t> =
            $crate::__thread_local_inner!(@key $(#[$attr])* $vis $name, $t, $init);
    }
}

$(#[$attr:meta])*, $vis and $name in (@key $(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) seems to be useless.

Since this is a macro without stability, why don't we remove these parameters? Like

macro_rules! __thread_local_inner {
    (@key $t:ty, $init:expr) => {
        // blablabla
    };
    ($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
        $(#[$attr])* $vis const $name: $crate::thread::LocalKey<$t> =
            $crate::__thread_local_inner!(@key $t, $init);
    }
}
@3442853561 3442853561 changed the title There seems to be a parameter that has not been used in the __thread_local_inner macro of the unstabilized standard library. There seems to be some parameters that has not been used in the __thread_local_inner macro of the unstabilized standard library. Oct 31, 2019
@3442853561
Copy link
Contributor Author

There is an example to prove that these parameters are not useful.

#![feature(thread_local_internals)]
use std::cell::RefCell;

pub const FOO: std::thread::LocalKey<RefCell<u32>> = __thread_local_inner!(@key pub BAR, RefCell<u32>, RefCell::new(1));

fn main() {
    FOO.with(|f| {
        assert_eq!(*f.borrow(), 1);
        *f.borrow_mut() = 2;
    });
    
    // BAR.with(|b| {
    //     assert_eq!(*b.borrow(), 1);
    //     *b.borrow_mut() = 2;
    // });
}

pietroalbini added a commit to pietroalbini/rust that referenced this issue Nov 6, 2019
Remove unused parameters in `__thread_local_inner`

Fixes rust-lang#65993.
@Alexendoo Alexendoo added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Nov 6, 2019
JohnTitor added a commit to JohnTitor/rust that referenced this issue Nov 6, 2019
Remove unused parameters in `__thread_local_inner`

Fixes rust-lang#65993.
@bors bors closed this as completed in de40190 Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants