Rollup merge of #72625 - Amanieu:asm-srcloc, r=petrochenkov

Improve inline asm error diagnostics

Previously we were just using the raw LLVM error output (with line, caret, etc) as the diagnostic message, which ends up looking rather out of place with our existing diagnostics.

The new diagnostics properly format the diagnostics and also take advantage of LLVM's per-line `srcloc` attribute to map an error in inline assembly directly to the relevant line of source code.

Incidentally also fixes #71639 by disabling `srcloc` metadata during LTO builds since we don't know what crate it might have come from. We can only resolve `srcloc`s from the currently crate since it indexes into the source map for the current crate.

Fixes #72664
Fixes #71639

r? @petrochenkov

### Old style

```rust
#![feature(llvm_asm)]

fn main() {
    unsafe {
        let _x: i32;
        llvm_asm!(
            "mov $0, $1
             invalid_instruction $0, $1
             mov $0, $1"
             : "=&r" (_x)
             : "r" (0)
             :: "intel"
        );
    }
}
```

```
error: <inline asm>:3:14: error: invalid instruction mnemonic 'invalid_instruction'
             invalid_instruction ecx, eax
             ^~~~~~~~~~~~~~~~~~~

  --> src/main.rs:6:9
   |
6  | /         llvm_asm!(
7  | |             "mov $0, $1
8  | |              invalid_instruction $0, $1
9  | |              mov $0, $1"
...  |
12 | |              :: "intel"
13 | |         );
   | |__________^
```

### New style

```rust
#![feature(asm)]

fn main() {
    unsafe {
        asm!(
            "mov {0}, {1}
             invalid_instruction {0}, {1}
             mov {0}, {1}",
            out(reg) _,
            in(reg) 0i64,
        );
    }
}
```

```
error: invalid instruction mnemonic 'invalid_instruction'
 --> test.rs:7:14
  |
7 |              invalid_instruction {0}, {1}
  |              ^
  |
note: instantiated into assembly here
 --> <inline asm>:3:14
  |
3 |              invalid_instruction rax, rcx
  |              ^^^^^^^^^^^^^^^^^^^
```
This commit is contained in:
Ralf Jung 2020-05-30 23:08:44 +02:00 committed by GitHub
commit fadfcb644e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 367 additions and 57 deletions

View file

@ -2,16 +2,19 @@ error: invalid operand in inline asm: 'int $3'
--> $DIR/issue-23458.rs:8:9
|
LL | llvm_asm!("int $3");
| ^^^^^^^^^^^^^^^^^^^^
error: <inline asm>:1:2: error: too few operands for instruction
int
^
| ^
error: too few operands for instruction
--> $DIR/issue-23458.rs:8:9
|
LL | llvm_asm!("int $3");
| ^^^^^^^^^^^^^^^^^^^^
| ^
|
note: instantiated into assembly here
--> <inline asm>:1:2
|
LL | int
| ^
error: aborting due to 2 previous errors