diff options
| author | Amanieu d'Antras <amanieu@gmail.com> | 2024-05-26 00:11:32 +0100 |
|---|---|---|
| committer | Amanieu d'Antras <amanieu@gmail.com> | 2024-07-25 20:12:14 +0100 |
| commit | 4d747128eb1e1c0da7432b071107d29721e19a04 (patch) | |
| tree | 167077aec13eccf565b49a3b12b00945e6bb985c /compiler/rustc_hir_analysis/src/check/intrinsicck.rs | |
| parent | eb10639928a2781cf0a12440007fbcc1e3a6888f (diff) | |
| download | rust-4d747128eb1e1c0da7432b071107d29721e19a04.tar.gz rust-4d747128eb1e1c0da7432b071107d29721e19a04.zip | |
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.
Diffstat (limited to 'compiler/rustc_hir_analysis/src/check/intrinsicck.rs')
| -rw-r--r-- | compiler/rustc_hir_analysis/src/check/intrinsicck.rs | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/compiler/rustc_hir_analysis/src/check/intrinsicck.rs b/compiler/rustc_hir_analysis/src/check/intrinsicck.rs index 2e965c59ebb..9286d3d83bf 100644 --- a/compiler/rustc_hir_analysis/src/check/intrinsicck.rs +++ b/compiler/rustc_hir_analysis/src/check/intrinsicck.rs @@ -455,10 +455,26 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> { ); } } - // No special checking is needed for these: - // - Typeck has checked that Const operands are integers. - // - AST lowering guarantees that SymStatic points to a static. - hir::InlineAsmOperand::Const { .. } | hir::InlineAsmOperand::SymStatic { .. } => {} + hir::InlineAsmOperand::Const { anon_const } => { + let ty = self.tcx.type_of(anon_const.def_id).instantiate_identity(); + match ty.kind() { + ty::Error(_) => {} + ty::Int(_) | ty::Uint(_) => {} + _ => { + self.tcx + .dcx() + .struct_span_err(*op_sp, "invalid type for `const` operand") + .with_span_label( + self.tcx.def_span(anon_const.def_id), + format!("is {} `{}`", ty.kind().article(), ty), + ) + .with_help("`const` operands must be of an integer type") + .emit(); + } + }; + } + // AST lowering guarantees that SymStatic points to a static. + hir::InlineAsmOperand::SymStatic { .. } => {} // Check that sym actually points to a function. Later passes // depend on this. hir::InlineAsmOperand::SymFn { anon_const } => { |
