Skip to content

Commit 62e735c

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 20f23ab commit 62e735c

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

compiler/rustc_hir_analysis/src/check/intrinsicck.rs

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

compiler/rustc_hir_typeck/src/lib.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,10 @@ fn infer_type_if_missing<'tcx>(fcx: &FnCtxt<'_, 'tcx>, node: Node<'tcx>) -> Opti
264264
Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(asm), span, .. })
265265
| Node::Item(&hir::Item { kind: hir::ItemKind::GlobalAsm(asm), span, .. }) => {
266266
asm.operands.iter().find_map(|(op, _op_sp)| match op {
267-
hir::InlineAsmOperand::Const { anon_const } if anon_const.hir_id == id => {
268-
// Inline assembly constants must be integers.
269-
Some(fcx.next_int_var())
270-
}
271-
hir::InlineAsmOperand::SymFn { anon_const } if anon_const.hir_id == id => {
267+
hir::InlineAsmOperand::Const { anon_const }
268+
| hir::InlineAsmOperand::SymFn { anon_const }
269+
if anon_const.hir_id == id =>
270+
{
272271
Some(fcx.next_ty_var(span))
273272
}
274273
_ => None,

0 commit comments

Comments
 (0)