Skip to content

Commit a88ba94

Browse files
authored
Rollup merge of #160068 - qaijuang:fix-asm-register-alias-diagnostic, r=Amanieu
Preserve explicit register names in inline asm diagnostics This PR adds diagnostic-only metadata directly to each register-bearing HIR operand, and replaces #117912 AST lookup with the new HIR metadata. Fixes #159409
2 parents 0d38a84 + 5a9eb1e commit a88ba94

10 files changed

Lines changed: 195 additions & 41 deletions

File tree

‎compiler/rustc_ast_lowering/src/asm.rs‎

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
140140
.iter()
141141
.map(|(op, op_sp)| {
142142
let lower_reg = |&reg: &_| match reg {
143-
InlineAsmRegOrRegClass::Reg(reg) => {
144-
asm::InlineAsmRegOrRegClass::Reg(if let Some(asm_arch) = asm_arch {
143+
InlineAsmRegOrRegClass::Reg(reg) => hir::InlineAsmRegOrRegClass::Reg {
144+
reg: if let Some(asm_arch) = asm_arch {
145145
asm::InlineAsmReg::parse(asm_arch, reg).unwrap_or_else(|error| {
146146
self.dcx().emit_err(InvalidRegister {
147147
op_span: *op_sp,
@@ -152,10 +152,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
152152
})
153153
} else {
154154
asm::InlineAsmReg::Err
155-
})
156-
}
155+
},
156+
source_name: Some(reg),
157+
},
157158
InlineAsmRegOrRegClass::RegClass(reg_class) => {
158-
asm::InlineAsmRegOrRegClass::RegClass(if let Some(asm_arch) = asm_arch {
159+
hir::InlineAsmRegOrRegClass::RegClass(if let Some(asm_arch) = asm_arch {
159160
asm::InlineAsmRegClass::parse(asm_arch, reg_class).unwrap_or_else(
160161
|supported_register_classes| {
161162
self.dcx().emit_err(InvalidRegisterClass {
@@ -344,7 +345,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
344345
}
345346

346347
// Check for conflicts between explicit register operands.
347-
if let asm::InlineAsmRegOrRegClass::Reg(reg) = reg {
348+
if let hir::InlineAsmRegOrRegClass::Reg { reg, .. } = reg {
348349
let (input, output) = match op {
349350
hir::InlineAsmOperand::In { .. } => (true, false),
350351

@@ -393,24 +394,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
393394
}
394395
_ => None,
395396
};
396-
let reg_str = |idx| -> &str {
397-
// HIR asm doesn't preserve the original alias string of the explicit register,
398-
// so we have to retrieve it from AST
399-
let (op, _): &(InlineAsmOperand, Span) = &asm.operands[idx];
400-
if let Some(ast::InlineAsmRegOrRegClass::Reg(reg_sym)) =
401-
op.reg()
402-
{
403-
reg_sym.as_str()
404-
} else {
405-
unreachable!("{op:?} is not a register operand");
406-
}
407-
};
397+
let reg1_name = op
398+
.reg()
399+
.and_then(|reg| reg.source_name())
400+
.expect("{op:?} has no source register name");
401+
402+
let reg2_name = op2
403+
.reg()
404+
.and_then(|reg| reg.source_name())
405+
.expect("{op2:?} has no source register name");
408406

409407
self.dcx().emit_err(RegisterConflict {
410408
op_span1: op_sp,
411409
op_span2: op_sp2,
412-
reg1_name: reg_str(idx),
413-
reg2_name: reg_str(idx2),
410+
reg1_name: reg1_name.as_str(),
411+
reg2_name: reg2_name.as_str(),
414412
in_out,
415413
});
416414
}
@@ -457,7 +455,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
457455
if !output_used {
458456
operands.push((
459457
hir::InlineAsmOperand::Out {
460-
reg: asm::InlineAsmRegOrRegClass::Reg(clobber),
458+
reg: hir::InlineAsmRegOrRegClass::Reg {
459+
reg: clobber,
460+
source_name: None,
461+
},
461462
late: true,
462463
expr: None,
463464
},

‎compiler/rustc_hir/src/hir.rs‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_span::{
3030
BytePos, DUMMY_SP, DesugaringKind, ErrorGuaranteed, Ident, LocalExpnId, Span, Spanned, Symbol,
3131
kw, sym,
3232
};
33-
use rustc_target::asm::InlineAsmRegOrRegClass;
33+
use rustc_target::asm;
3434
use tracing::debug;
3535

3636
use crate::def::{CtorKind, DefKind, MacroKinds, PerNS, Res};
@@ -3611,6 +3611,44 @@ pub enum TyKind<'hir, Unambig = ()> {
36113611
Infer(Unambig),
36123612
}
36133613

3614+
/// Stores explicit register name from source
3615+
/// for diagnostics only.
3616+
#[derive(Debug, Clone, Copy, StableHash)]
3617+
pub enum InlineAsmRegOrRegClass {
3618+
Reg { reg: asm::InlineAsmReg, source_name: Option<Symbol> },
3619+
RegClass(asm::InlineAsmRegClass),
3620+
}
3621+
3622+
impl InlineAsmRegOrRegClass {
3623+
// For `rustc_mir_build` and `clippy_utils`
3624+
pub fn as_target(self) -> asm::InlineAsmRegOrRegClass {
3625+
match self {
3626+
Self::Reg { reg, .. } => asm::InlineAsmRegOrRegClass::Reg(reg),
3627+
Self::RegClass(reg_class) => asm::InlineAsmRegOrRegClass::RegClass(reg_class),
3628+
}
3629+
}
3630+
3631+
// For `rustc_ast_lowering`
3632+
pub fn reg_class(self) -> asm::InlineAsmRegClass {
3633+
self.as_target().reg_class()
3634+
}
3635+
3636+
// For `rustc_ast_lowering`
3637+
pub fn source_name(self) -> Option<Symbol> {
3638+
match self {
3639+
Self::Reg { source_name, .. } => source_name,
3640+
Self::RegClass(_) => None,
3641+
}
3642+
}
3643+
}
3644+
3645+
// For `rustc_hir_pretty`
3646+
impl fmt::Display for InlineAsmRegOrRegClass {
3647+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3648+
self.as_target().fmt(f)
3649+
}
3650+
}
3651+
36143652
#[derive(Debug, Clone, Copy, StableHash)]
36153653
pub enum InlineAsmOperand<'hir> {
36163654
In {
@@ -3665,7 +3703,7 @@ impl<'hir> InlineAsmOperand<'hir> {
36653703
pub fn is_clobber(&self) -> bool {
36663704
matches!(
36673705
self,
3668-
InlineAsmOperand::Out { reg: InlineAsmRegOrRegClass::Reg(_), late: _, expr: None }
3706+
InlineAsmOperand::Out { reg: InlineAsmRegOrRegClass::Reg { .. }, expr: None, .. }
36693707
)
36703708
}
36713709
}

‎compiler/rustc_hir_typeck/src/inline_asm.rs‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use rustc_middle::ty::{
1313
use rustc_span::def_id::LocalDefId;
1414
use rustc_span::{ErrorGuaranteed, Span, Symbol, bug, sym};
1515
use rustc_target::asm::{
16-
InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmSize, InlineAsmType,
17-
ModifierInfo,
16+
InlineAsmReg, InlineAsmRegClass, InlineAsmSize, InlineAsmType, ModifierInfo,
1817
};
1918
use rustc_trait_selection::infer::InferCtxtExt;
2019

@@ -191,7 +190,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
191190
fn check_asm_operand_type(
192191
&self,
193192
idx: usize,
194-
reg: InlineAsmRegOrRegClass,
193+
reg: hir::InlineAsmRegOrRegClass,
195194
expr: &'tcx hir::Expr<'tcx>,
196195
template: &[InlineAsmTemplatePiece],
197196
is_input: bool,
@@ -477,7 +476,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
477476
if let Some(reg) = op.reg() {
478477
// Some explicit registers cannot be used depending on the
479478
// target. Reject those here.
480-
if let InlineAsmRegOrRegClass::Reg(reg) = reg {
479+
if let hir::InlineAsmRegOrRegClass::Reg { reg, source_name } = reg {
481480
if let InlineAsmReg::Err = reg {
482481
// `validate` will panic on `Err`, as an error must
483482
// already have been reported.
@@ -490,7 +489,11 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
490489
&self.tcx().sess.target,
491490
op.is_clobber(),
492491
) {
493-
let msg = format!("cannot use register `{}`: {}", reg.name(), msg);
492+
let reg_name = match source_name {
493+
Some(ref reg_name) => reg_name.as_str(),
494+
None => &reg.name(),
495+
};
496+
let msg = format!("cannot use register `{reg_name}`: {msg}");
494497
self.fcx.dcx().span_err(op_sp, msg);
495498
continue;
496499
}

‎compiler/rustc_mir_build/src/thir/cx/expr.rs‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -827,22 +827,27 @@ impl<'tcx> ThirBuildCx<'tcx> {
827827
.operands
828828
.iter()
829829
.map(|(op, _op_sp)| match *op {
830-
hir::InlineAsmOperand::In { reg, expr } => {
831-
InlineAsmOperand::In { reg, expr: self.mirror_expr(expr) }
832-
}
830+
hir::InlineAsmOperand::In { reg, expr } => InlineAsmOperand::In {
831+
reg: reg.as_target(),
832+
expr: self.mirror_expr(expr),
833+
},
833834
hir::InlineAsmOperand::Out { reg, late, ref expr } => {
834835
InlineAsmOperand::Out {
835-
reg,
836+
reg: reg.as_target(),
836837
late,
837838
expr: expr.map(|expr| self.mirror_expr(expr)),
838839
}
839840
}
840841
hir::InlineAsmOperand::InOut { reg, late, expr } => {
841-
InlineAsmOperand::InOut { reg, late, expr: self.mirror_expr(expr) }
842+
InlineAsmOperand::InOut {
843+
reg: reg.as_target(),
844+
late,
845+
expr: self.mirror_expr(expr),
846+
}
842847
}
843848
hir::InlineAsmOperand::SplitInOut { reg, late, in_expr, ref out_expr } => {
844849
InlineAsmOperand::SplitInOut {
845-
reg,
850+
reg: reg.as_target(),
846851
late,
847852
in_expr: self.mirror_expr(in_expr),
848853
out_expr: out_expr.map(|expr| self.mirror_expr(expr)),

‎src/tools/clippy/clippy_utils/src/hir_utils.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,18 +1280,18 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
12801280
for (op, _op_sp) in asm.operands {
12811281
match op {
12821282
InlineAsmOperand::In { reg, expr } => {
1283-
reg.hash(&mut self.s);
1283+
reg.as_target().hash(&mut self.s);
12841284
self.hash_expr(expr);
12851285
},
12861286
InlineAsmOperand::Out { reg, late, expr } => {
1287-
reg.hash(&mut self.s);
1287+
reg.as_target().hash(&mut self.s);
12881288
late.hash(&mut self.s);
12891289
if let Some(expr) = expr {
12901290
self.hash_expr(expr);
12911291
}
12921292
},
12931293
InlineAsmOperand::InOut { reg, late, expr } => {
1294-
reg.hash(&mut self.s);
1294+
reg.as_target().hash(&mut self.s);
12951295
late.hash(&mut self.s);
12961296
self.hash_expr(expr);
12971297
},
@@ -1301,7 +1301,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
13011301
in_expr,
13021302
out_expr,
13031303
} => {
1304-
reg.hash(&mut self.s);
1304+
reg.as_target().hash(&mut self.s);
13051305
late.hash(&mut self.s);
13061306
self.hash_expr(in_expr);
13071307
if let Some(out_expr) = out_expr {

‎tests/ui/asm/sparc/bad-reg.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn f() {
2828
asm!("", out("g3") _);
2929
asm!("", out("g4") _);
3030
asm!("", out("g5") _);
31-
//[sparc,sparcv8plus]~^ ERROR cannot use register `r5`: g5 is reserved for system on SPARC32
31+
//[sparc,sparcv8plus]~^ ERROR cannot use register `g5`: g5 is reserved for system on SPARC32
3232
asm!("", out("g6") _);
3333
//~^ ERROR invalid register `g6`: reserved for system and cannot be used as an operand for inline asm
3434
asm!("", out("g7") _);

‎tests/ui/asm/sparc/bad-reg.sparc.stderr‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ error: register class `yreg` can only be used as a clobber, not as an input or o
6464
LL | asm!("/* {} */", out(yreg) _);
6565
| ^^^^^^^^^^^
6666

67-
error: cannot use register `r5`: g5 is reserved for system on SPARC32
67+
error: cannot use register `g5`: g5 is reserved for system on SPARC32
6868
--> $DIR/bad-reg.rs:30:18
6969
|
7070
LL | asm!("", out("g5") _);

‎tests/ui/asm/sparc/bad-reg.sparcv8plus.stderr‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ error: register class `yreg` can only be used as a clobber, not as an input or o
6464
LL | asm!("/* {} */", out(yreg) _);
6565
| ^^^^^^^^^^^
6666

67-
error: cannot use register `r5`: g5 is reserved for system on SPARC32
67+
error: cannot use register `g5`: g5 is reserved for system on SPARC32
6868
--> $DIR/bad-reg.rs:30:18
6969
|
7070
LL | asm!("", out("g5") _);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Check that inline assembly diagnostics use the explicit register name from the source.
2+
//
3+
// On 32-bit x86, vector registers 8 and higher are unavailable. Registers 16 through 31
4+
// share a canonical `zmm` representation, but diagnostics must still use the `xmm`, `ymm`,
5+
// or `zmm` name that the user wrote.
6+
//
7+
// Regression test for <https://github.com/rust-lang/rust/issues/159409>.
8+
9+
//@ add-minicore
10+
//@ revisions: x86 x86_64
11+
//@[x86] compile-flags: --target i686-unknown-linux-gnu
12+
//@[x86] needs-llvm-components: x86
13+
//@[x86] check-fail
14+
//@[x86_64] compile-flags: --target x86_64-unknown-linux-gnu
15+
//@[x86_64] needs-llvm-components: x86
16+
//@[x86_64] check-pass
17+
//@ ignore-backends: gcc
18+
19+
#![feature(no_core)]
20+
#![no_core]
21+
22+
extern crate minicore;
23+
use minicore::*;
24+
25+
fn main() {
26+
unsafe {
27+
// Register 15 is the last register with separate canonical representations.
28+
asm!("", lateout("xmm15") _);
29+
//[x86]~^ ERROR cannot use register `xmm15`
30+
asm!("", lateout("ymm15") _);
31+
//[x86]~^ ERROR cannot use register `ymm15`
32+
asm!("", lateout("zmm15") _);
33+
//[x86]~^ ERROR cannot use register `zmm15`
34+
35+
// Register 16 is the first register whose aliases share the canonical `zmm` representation.
36+
asm!("", lateout("xmm16") _);
37+
//[x86]~^ ERROR cannot use register `xmm16`
38+
asm!("", lateout("ymm16") _);
39+
//[x86]~^ ERROR cannot use register `ymm16`
40+
asm!("", lateout("zmm16") _);
41+
//[x86]~^ ERROR cannot use register `zmm16`
42+
43+
// Register 31 is the upper boundary of the affected register range.
44+
asm!("", lateout("xmm31") _);
45+
//[x86]~^ ERROR cannot use register `xmm31`
46+
asm!("", lateout("ymm31") _);
47+
//[x86]~^ ERROR cannot use register `ymm31`
48+
asm!("", lateout("zmm31") _);
49+
//[x86]~^ ERROR cannot use register `zmm31`
50+
}
51+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
error: cannot use register `xmm15`: register is only available on x86_64
2+
--> $DIR/x86-explicit-register-alias-diagnostic.rs:28:18
3+
|
4+
LL | asm!("", lateout("xmm15") _);
5+
| ^^^^^^^^^^^^^^^^^^
6+
7+
error: cannot use register `ymm15`: register is only available on x86_64
8+
--> $DIR/x86-explicit-register-alias-diagnostic.rs:30:18
9+
|
10+
LL | asm!("", lateout("ymm15") _);
11+
| ^^^^^^^^^^^^^^^^^^
12+
13+
error: cannot use register `zmm15`: register is only available on x86_64
14+
--> $DIR/x86-explicit-register-alias-diagnostic.rs:32:18
15+
|
16+
LL | asm!("", lateout("zmm15") _);
17+
| ^^^^^^^^^^^^^^^^^^
18+
19+
error: cannot use register `xmm16`: register is only available on x86_64
20+
--> $DIR/x86-explicit-register-alias-diagnostic.rs:36:18
21+
|
22+
LL | asm!("", lateout("xmm16") _);
23+
| ^^^^^^^^^^^^^^^^^^
24+
25+
error: cannot use register `ymm16`: register is only available on x86_64
26+
--> $DIR/x86-explicit-register-alias-diagnostic.rs:38:18
27+
|
28+
LL | asm!("", lateout("ymm16") _);
29+
| ^^^^^^^^^^^^^^^^^^
30+
31+
error: cannot use register `zmm16`: register is only available on x86_64
32+
--> $DIR/x86-explicit-register-alias-diagnostic.rs:40:18
33+
|
34+
LL | asm!("", lateout("zmm16") _);
35+
| ^^^^^^^^^^^^^^^^^^
36+
37+
error: cannot use register `xmm31`: register is only available on x86_64
38+
--> $DIR/x86-explicit-register-alias-diagnostic.rs:44:18
39+
|
40+
LL | asm!("", lateout("xmm31") _);
41+
| ^^^^^^^^^^^^^^^^^^
42+
43+
error: cannot use register `ymm31`: register is only available on x86_64
44+
--> $DIR/x86-explicit-register-alias-diagnostic.rs:46:18
45+
|
46+
LL | asm!("", lateout("ymm31") _);
47+
| ^^^^^^^^^^^^^^^^^^
48+
49+
error: cannot use register `zmm31`: register is only available on x86_64
50+
--> $DIR/x86-explicit-register-alias-diagnostic.rs:48:18
51+
|
52+
LL | asm!("", lateout("zmm31") _);
53+
| ^^^^^^^^^^^^^^^^^^
54+
55+
error: aborting due to 9 previous errors
56+

0 commit comments

Comments
 (0)