Skip to content

Commit ed70120

Browse files
authored
Rollup merge of rust-lang#117912 - GeorgeWort:master, r=petrochenkov
Name explicit registers in conflict register errors for inline assembly
2 parents b1e56de + e0bfb61 commit ed70120

File tree

6 files changed

+43
-22
lines changed

6 files changed

+43
-22
lines changed

compiler/rustc_ast/src/ast.rs

+12
Original file line numberDiff line numberDiff line change
@@ -2236,6 +2236,18 @@ pub enum InlineAsmOperand {
22362236
},
22372237
}
22382238

2239+
impl InlineAsmOperand {
2240+
pub fn reg(&self) -> Option<&InlineAsmRegOrRegClass> {
2241+
match self {
2242+
Self::In { reg, .. }
2243+
| Self::Out { reg, .. }
2244+
| Self::InOut { reg, .. }
2245+
| Self::SplitInOut { reg, .. } => Some(reg),
2246+
Self::Const { .. } | Self::Sym { .. } => None,
2247+
}
2248+
}
2249+
}
2250+
22392251
/// Inline assembly.
22402252
///
22412253
/// E.g., `asm!("NOP");`.

compiler/rustc_ast_lowering/src/asm.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
354354

355355
let idx2 = *o.get();
356356
let (ref op2, op_sp2) = operands[idx2];
357-
let Some(asm::InlineAsmRegOrRegClass::Reg(reg2)) = op2.reg()
358-
else {
359-
unreachable!();
360-
};
361357

362358
let in_out = match (op, op2) {
363359
(
@@ -375,11 +371,24 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
375371
_ => None,
376372
};
377373

374+
let reg_str = |idx| -> &str {
375+
// HIR asm doesn't preserve the original alias string of the explicit register,
376+
// so we have to retrieve it from AST
377+
let (op, _): &(InlineAsmOperand, Span) = &asm.operands[idx];
378+
if let Some(ast::InlineAsmRegOrRegClass::Reg(reg_sym)) =
379+
op.reg()
380+
{
381+
reg_sym.as_str()
382+
} else {
383+
unreachable!();
384+
}
385+
};
386+
378387
sess.emit_err(RegisterConflict {
379388
op_span1: op_sp,
380389
op_span2: op_sp2,
381-
reg1_name: reg.name(),
382-
reg2_name: reg2.name(),
390+
reg1_name: reg_str(idx),
391+
reg2_name: reg_str(idx2),
383392
in_out,
384393
});
385394
}

tests/ui/asm/aarch64/bad-reg.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ fn main() {
4848
// (except in/lateout which don't conflict)
4949

5050
asm!("", in("x0") foo, in("w0") bar);
51-
//~^ ERROR register `x0` conflicts with register `x0`
51+
//~^ ERROR register `w0` conflicts with register `x0`
5252
asm!("", in("x0") foo, out("x0") bar);
5353
//~^ ERROR register `x0` conflicts with register `x0`
5454
asm!("", in("w0") foo, lateout("w0") bar);
5555
asm!("", in("v0") foo, in("q0") bar);
56-
//~^ ERROR register `v0` conflicts with register `v0`
56+
//~^ ERROR register `q0` conflicts with register `v0`
5757
asm!("", in("v0") foo, out("q0") bar);
58-
//~^ ERROR register `v0` conflicts with register `v0`
58+
//~^ ERROR register `q0` conflicts with register `v0`
5959
asm!("", in("v0") foo, lateout("q0") bar);
6060
}
6161
}

tests/ui/asm/aarch64/bad-reg.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ error: register class `preg` can only be used as a clobber, not as an input or o
9898
LL | asm!("{}", out(preg) _);
9999
| ^^^^^^^^^^^
100100

101-
error: register `x0` conflicts with register `x0`
101+
error: register `w0` conflicts with register `x0`
102102
--> $DIR/bad-reg.rs:50:32
103103
|
104104
LL | asm!("", in("x0") foo, in("w0") bar);
105-
| ------------ ^^^^^^^^^^^^ register `x0`
105+
| ------------ ^^^^^^^^^^^^ register `w0`
106106
| |
107107
| register `x0`
108108

@@ -120,19 +120,19 @@ help: use `lateout` instead of `out` to avoid conflict
120120
LL | asm!("", in("x0") foo, out("x0") bar);
121121
| ^^^^^^^^^^^^
122122

123-
error: register `v0` conflicts with register `v0`
123+
error: register `q0` conflicts with register `v0`
124124
--> $DIR/bad-reg.rs:55:32
125125
|
126126
LL | asm!("", in("v0") foo, in("q0") bar);
127-
| ------------ ^^^^^^^^^^^^ register `v0`
127+
| ------------ ^^^^^^^^^^^^ register `q0`
128128
| |
129129
| register `v0`
130130

131-
error: register `v0` conflicts with register `v0`
131+
error: register `q0` conflicts with register `v0`
132132
--> $DIR/bad-reg.rs:57:32
133133
|
134134
LL | asm!("", in("v0") foo, out("q0") bar);
135-
| ------------ ^^^^^^^^^^^^^ register `v0`
135+
| ------------ ^^^^^^^^^^^^^ register `q0`
136136
| |
137137
| register `v0`
138138
|

tests/ui/asm/x86_64/bad-reg.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ fn main() {
5656
// (except in/lateout which don't conflict)
5757

5858
asm!("", in("eax") foo, in("al") bar);
59-
//~^ ERROR register `al` conflicts with register `ax`
59+
//~^ ERROR register `al` conflicts with register `eax`
6060
//~| ERROR `i32` cannot be used with this register class
6161
asm!("", in("rax") foo, out("rax") bar);
62-
//~^ ERROR register `ax` conflicts with register `ax`
62+
//~^ ERROR register `rax` conflicts with register `rax`
6363
asm!("", in("al") foo, lateout("al") bar);
6464
//~^ ERROR `i32` cannot be used with this register class
6565
//~| ERROR `i32` cannot be used with this register class

tests/ui/asm/x86_64/bad-reg.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,21 @@ error: register class `mmx_reg` can only be used as a clobber, not as an input o
106106
LL | asm!("{}", out(mmx_reg) _);
107107
| ^^^^^^^^^^^^^^
108108

109-
error: register `al` conflicts with register `ax`
109+
error: register `al` conflicts with register `eax`
110110
--> $DIR/bad-reg.rs:58:33
111111
|
112112
LL | asm!("", in("eax") foo, in("al") bar);
113113
| ------------- ^^^^^^^^^^^^ register `al`
114114
| |
115-
| register `ax`
115+
| register `eax`
116116

117-
error: register `ax` conflicts with register `ax`
117+
error: register `rax` conflicts with register `rax`
118118
--> $DIR/bad-reg.rs:61:33
119119
|
120120
LL | asm!("", in("rax") foo, out("rax") bar);
121-
| ------------- ^^^^^^^^^^^^^^ register `ax`
121+
| ------------- ^^^^^^^^^^^^^^ register `rax`
122122
| |
123-
| register `ax`
123+
| register `rax`
124124
|
125125
help: use `lateout` instead of `out` to avoid conflict
126126
--> $DIR/bad-reg.rs:61:18

0 commit comments

Comments
 (0)