rust-lang / rust-lang/rust

False `error: unknown x86 target feature: avx2` when passed to `is_x86_feature_detected!` as a `literal` instead of `tt`.

Open
#128,586 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-target-feature C-bug O-x86_64 T-compiler T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Minimal reproduction:

macro_rules! x86_feature_detect {
    ($name:ident -> $feature:literal) => {
        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        thread_local! {
            pub static $name: core::cell::LazyCell<bool> = core::cell::LazyCell::new(|| is_x86_feature_detected!($feature));
        }

    };
}

x86_feature_detect!(SUPPORTS_AVX -> "avx2");

playground permalink

"avx2" is a feature for x86 documented at https://doc.rust-lang.org/std/macro.is_x86_feature_detected.html

The error:

   Compiling playground v0.0.1 (/playground)
error: unknown x86 target feature: avx2
  --> src/lib.rs:11:1
   |
11 | x86_feature_detect!(SUPPORTS_AVX -> "avx2");
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: this error originates in the macro `is_x86_feature_detected` which comes from the expansion of the macro `x86_feature_detect` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `playground` (lib) due to 1 previous error

This error is unclear -- and hard to reason about/diagnose since it comes from within a macro.

Based on the docs at https://doc.rust-lang.org/std/macro.is_x86_feature_detected.html,
I changed the type from literal to tt and it compiled as expected ("fixed" playground permalink).

This behavior appears consistent on stable and nightly.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the minimal macro example from the issue, comparing the $feature:literal and $feature:tt forms in the linked Playground. Read the is_x86_feature_detected! macro expansion and verify that the documented x86 feature such as avx2 produces an accurate result or diagnostic when passed through the wrapper macro.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.