Skip to content

confusing error message when target feature is missing #121496

Open
@folkertdev

Description

@folkertdev

Code

// compile with `cargo run --target aarch64-unknown-linux-gnu` 

fn main() {
    unsafe { dbg!(__crc32b(13, 42)) };
}

unsafe fn __crc32b(mut crc: u32, data: u8) -> u32 {
    std::arch::asm!("crc32b {crc:w}, {crc:w}, {data:w}", crc = inout(reg) crc, data = in(reg) data);
    crc
}

Current output

error: instruction requires: crc
 --> src/main.rs:8:22
  |
8 |     std::arch::asm!("crc32b {crc:w}, {crc:w}, {data:w}", crc = inout(reg) crc, data = in(reg) data);
  |                      ^
  |
note: instantiated into assembly here
 --> <inline asm>:1:2
  |
1 |     crc32b w8, w8, w1
  |     ^

Desired output

error: missing target feature "crc"
 --> src/main.rs:8:22
  |
8 |     std::arch::asm!("crc32b {crc:w}, {crc:w}, {data:w}", crc = inout(reg) crc, data = in(reg) data);
  |                      ^ this inline assembly requires the "crc" target feature to be enabled
  |
???: add the target feature to the surrounding function
  + #[cfg(target_feature(enable = "crc")]
7 | unsafe fn __crc32b(mut crc: u32, data: u8) -> u32 {
note: instantiated into assembly here
 --> <inline asm>:1:2
  |
1 |     crc32b w8, w8, w1
  |     ^

Rationale and extra context

it would be nice for the error to mention that a target feature is required.

this error only occurs on arm. I've not been able to get this error message on x86 (I tried with various unstable attributes like avx512 or tbm (trailing bit manipulation).with avx512 you already need the target feature because otherwise you cannot in/out avx512 registers to the assembly, so this issue is unlikely to come up there.

with tbm i would expect this error, but apparently it doesn't come up there, maybe the linker does not report it?

Other cases

No response

Rust Version

rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6

Anything else?

I'd be interested in implementing this. I've poked around a bit already, but would like to settle on a desired error message first.

One thing I'm not sure about is whether the span of the surrounding function is available when the error is generated here https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_ssa/src/back/write.rs#L1898. is that a hint that we can give there?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inline-assemblyArea: Inline assembly (`asm!(…)`)D-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions