Skip to content

Commit 04ab648

Browse files
Amanieufolkertdev
authored andcommitted
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 a886938 commit 04ab648

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
@@ -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

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

0 commit comments

Comments
 (0)