Skip to content

in asm!() using a local numeric label made of all 0's and 1's gives a confusing error #94426

Closed
@oconnor663

Description

@oconnor663
rustc 1.59.0 (9d1b2106e 2022-02-23)

Here's a function that jumps to a local numberic label inside of asm!:

use std::arch::asm;

#[cfg(target_arch = "x86_64")]
fn main() {
    let mut counter: u64 = 0;
    unsafe {
        asm!(
            "2:",
            "inc {counter}",
            "cmp {counter}, 10",
            "jnz 2b",
            counter = inout(reg) counter,
        );
    }
    dbg!(counter);  // prints 10
}

As per Rust By Example, we use 2: as a local label there to work around an LLVM bug that mistakes labels like 0:, 1:, or 10: for binary. This example works. However, if we don't know about this rule, and we try to use 0: instead, the error we get is confusing:

error: invalid operand for instruction
  --> src/main.rs:11:14
   |
11 |             "jnz 0b",
   |              ^
   |
note: instantiated into assembly here
  --> <inline asm>:5:1
   |
5  | jnz 0b
   | ^

error: could not compile `scratch` due to previous error

Using 1: is the same:

error: invalid operand for instruction
  --> src/main.rs:11:14
   |
11 |             "jnz 1b",
   |              ^
   |
note: instantiated into assembly here
  --> <inline asm>:5:1
   |
5  | jnz 1b
   | ^

error: could not compile `scratch` due to previous error

It would be pretty great to have a hint here like "0: and 1: aren't valid local labels unfortunately, so just start with 2:".

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-inline-assemblyArea: Inline assembly (`asm!(…)`)D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.L-binary_asm_labelsLint: LLVM parses labels like 0, 1, 10, 11, etc. oddlyT-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