Skip to content

Commit 03c307a

Browse files
committed
Tweak type inference for const operands in inline asm
Previously these would be treated like integer literals and default to `i32` if a type could not be determined. To allow for forward-compatibility with `str` constants in the future, this PR changes type inference to use an unbound type variable instead. The actual type checking is deferred until after typeck where we still ensure that the final type for the `const` operand is an integer type.
1 parent 5065123 commit 03c307a

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

compiler/rustc_hir_analysis/src/check/intrinsicck.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,26 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
451451
);
452452
}
453453
}
454-
// No special checking is needed for these:
455-
// - Typeck has checked that Const operands are integers.
456-
// - AST lowering guarantees that SymStatic points to a static.
457-
hir::InlineAsmOperand::Const { .. } | hir::InlineAsmOperand::SymStatic { .. } => {}
454+
hir::InlineAsmOperand::Const { anon_const } => {
455+
let ty = self.tcx.type_of(anon_const.def_id).instantiate_identity();
456+
match ty.kind() {
457+
ty::Error(_) => {}
458+
ty::Int(_) | ty::Uint(_) => {}
459+
_ => {
460+
self.tcx
461+
.dcx()
462+
.struct_span_err(*op_sp, "invalid type for `const` operand")
463+
.with_span_label(
464+
self.tcx.def_span(anon_const.def_id),
465+
format!("is {} `{}`", ty.kind().article(), ty),
466+
)
467+
.with_help("`const` operands must be of an integer type")
468+
.emit();
469+
}
470+
};
471+
}
472+
// AST lowering guarantees that SymStatic points to a static.
473+
hir::InlineAsmOperand::SymStatic { .. } => {}
458474
// Check that sym actually points to a function. Later passes
459475
// depend on this.
460476
hir::InlineAsmOperand::SymFn { anon_const } => {

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,10 @@ fn infer_type_if_missing<'tcx>(fcx: &FnCtxt<'_, 'tcx>, node: Node<'tcx>) -> Opti
254254
Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(asm), span, .. })
255255
| Node::Item(&hir::Item { kind: hir::ItemKind::GlobalAsm(asm), span, .. }) => {
256256
asm.operands.iter().find_map(|(op, _op_sp)| match op {
257-
hir::InlineAsmOperand::Const { anon_const } if anon_const.hir_id == id => {
258-
// Inline assembly constants must be integers.
259-
Some(fcx.next_int_var())
260-
}
261-
hir::InlineAsmOperand::SymFn { anon_const } if anon_const.hir_id == id => {
257+
hir::InlineAsmOperand::Const { anon_const }
258+
| hir::InlineAsmOperand::SymFn { anon_const }
259+
if anon_const.hir_id == id =>
260+
{
262261
Some(fcx.next_ty_var(span))
263262
}
264263
_ => None,

0 commit comments

Comments
 (0)