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

std::thread::LocalKey not found #16486

Closed
A4-Tacks opened this issue Feb 4, 2024 · 4 comments
Closed

std::thread::LocalKey not found #16486

A4-Tacks opened this issue Feb 4, 2024 · 4 comments
Labels
C-bug Category: bug

Comments

@A4-Tacks
Copy link

A4-Tacks commented Feb 4, 2024

src/main.rs

fn main() {
    let _key: std::thread::LocalKey<i32> = todo!();
}

completion list

3       std::thread::
  Thread factory,      Builder
~ which can be used    JoinHandle
~ in order to          Parker
~ configure the        Result
~ properties of        Scope
~ a new thread.        ScopedJoinHandle
~                      Thread
~ Methods can be       ThreadId
~ chained on it in     available_parallelism() (alias available_concurrency, hardware_concurrency, num_cpus)~ fn() ->
~ order to             current()~ fn() -> Thread
~ configure it.        imp
~                      panicking()~ fn() -> bool
~ The two              park()~ fn()
~ configurations       park_timeout(…)~ fn(Duration)
~ available are:       park_timeout_ms(…)~ fn(u32)
~                      scope(…)~ fn(F) -> T
~   * name:            sleep(…)~ fn(Duration)
~ specifies an         sleep_ms(…)~ fn(u32)
~ associated name      spawn(…)~ fn(F) -> JoinHandle<T>
~ for the thread       yield_now()~ fn()

run cargo check 0 warnings, 0 errors

patch ~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/mod.rs

196,203c196,197
< cfg_if::cfg_if! {
<     if #[cfg(test)] {
<         // Avoid duplicating the global state associated with thread-locals between this crate and
<         // realstd. Miri relies on this.
<         pub use realstd::thread::{local_impl, AccessError, LocalKey};
<     } else {
<         #[stable(feature = "rust1", since = "1.0.0")]
<         pub use self::local::{AccessError, LocalKey};
---
> #[stable(feature = "rust1", since = "1.0.0")]
> pub use self::local::{AccessError, LocalKey};
205,211c199,203
<         // Implementation details used by the thread_local!{} macro.
<         #[doc(hidden)]
<         #[unstable(feature = "thread_local_internals", issue = "none")]
<         pub mod local_impl {
<             pub use crate::sys::common::thread_local::{thread_local_inner, Key, abort_on_dtor_unwind};
<         }
<     }
---
> // Implementation details used by the thread_local!{} macro.
> #[doc(hidden)]
> #[unstable(feature = "thread_local_internals", issue = "none")]
> pub mod local_impl {
>     pub use crate::sys::common::thread_local::{thread_local_inner, Key, abort_on_dtor_unwind};

use rust-analyzer Expand macro

// Recursive expansion of cfg_if!! macro
// ======================================

#[stable(feature = "rust1", since = "1.0.0")]
pub use self::local::{AccessError, LocalKey};
pub use realstd::thread::{local_impl, AccessError, LocalKey};
#[doc(hidden)]
#[unstable(feature = "thread_local_internals", issue = "none")]
pub mod local_impl {
    pub use crate::sys::common::thread_local::{abort_on_dtor_unwind, thread_local_inner, Key};
}

After the patch, the behavior was as expected, but it seems that this issue cannot be reproduced well

OS: Linux Acer-Lrne 6.5.2-arch1-1 #1 SMP PREEMPT_DYNAMIC Wed, 06 Sep 2023 21:01:01 +0000 x86_64 GNU/Linux

rust-analyzer version: rust-analyzer 1.75.0 (82e1608 2023-12-21) and rust-analyzer 1.78.0-nightly (b11fbfb 2024-02-03)

rustc version: rustc 1.75.0 (82e1608df 2023-12-21) and rustc 1.78.0-nightly (b11fbfbf3 2024-02-03)

relevant settings: NONE

@A4-Tacks A4-Tacks added the C-bug Category: bug label Feb 4, 2024
@Veykril
Copy link
Member

Veykril commented Feb 4, 2024

This is a known issue as rust-analyzer can't analyze the standard libraries crates-io dependencies by default, so the cfg-if macro is unresolved and hence not expanded. You can fix this by either somehow introducing cfg-if into your dependency graph which will make r-a hackily inject it into the standard library dependencies or enable the experimental rust-analyzer.cargo.sysrootQueryMetadata config which allows r-a to analyze said dependencies (though it will write and delete a lock file in your sysroot)

@lnicola
Copy link
Member

lnicola commented Feb 5, 2024

#7637

If rust-analyzer.cargo.sysrootQueryMetadata works for you, we can probably close this.

@A4-Tacks
Copy link
Author

A4-Tacks commented Feb 5, 2024

#7637

If rust-analyzer.cargo.sysrootQueryMetadata works for you, we can probably close this.

It seems that this doesn't work. I need to introduce cfg-if dependencies in my own project to solve all of this

@lnicola
Copy link
Member

lnicola commented Feb 6, 2024

Oh, right, see #16387. We try to run cargo metadata in the sysroot, but fail because of a nightly feature, you can see that in the logs.

Something like this will fix it, for better or worse:

    "rust-analyzer.server.extraEnv": {
        "__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS": "nightly"
    }

Closing as a duplicate of #7637.

@lnicola lnicola closed this as completed Feb 6, 2024
bors added a commit that referenced this issue Feb 14, 2024
internal: Set channel override when querying the sysroot metadata

This is pretty hard to discover, and makes the setting useless, we should probably enable it for now.

CC #16486
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

3 participants