rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
99.13k stars 12.8k forks source link

confusing error message when target feature is missing #121496

Open folkertdev opened 9 months ago

folkertdev commented 9 months ago

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?

asquared31415 commented 9 months ago

We intentionally do almost no validation of the asm contents in rustc itself and I think that this decision is unlikely to change. LLVM's assembler reports an error on the expanded asm, but it only gives a message and a character position, and rustc re-wraps that into an error with the same format as other errors.

folkertdev commented 9 months ago

right, there is not a lot of structure to go on in the error message (and no guarantee of any consistency between platforms). But I think some information about the source of the error (it's the assembler then? I thought it was the linker) would be helpful. Just something to search for