1 use rustc_ast::InlineAsmTemplatePiece;
2 use rustc_data_structures::fx::FxHashSet;
3 use rustc_errors::struct_span_err;
5 use rustc_index::vec::Idx;
6 use rustc_middle::ty::layout::{LayoutError, SizeSkeleton};
7 use rustc_middle::ty::{self, Article, FloatTy, IntTy, Ty, TyCtxt, TypeVisitable, UintTy};
8 use rustc_session::lint;
9 use rustc_span::{Span, Symbol, DUMMY_SP};
10 use rustc_target::abi::{Pointer, VariantIdx};
11 use rustc_target::asm::{InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType};
15 /// If the type is `Option<T>`, it will return `T`, otherwise
16 /// the type itself. Works on most `Option`-like types.
17 fn unpack_option_like<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
18 let ty::Adt(def, substs) = *ty.kind() else { return ty };
20 if def.variants().len() == 2 && !def.repr().c() && def.repr().int.is_none() {
23 let one = VariantIdx::new(1);
24 let zero = VariantIdx::new(0);
26 if def.variant(zero).fields.is_empty() {
28 } else if def.variant(one).fields.is_empty() {
34 if def.variant(data_idx).fields.len() == 1 {
35 return def.variant(data_idx).fields[0].ty(tcx, substs);
42 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
43 pub fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>) {
44 let convert = |ty: Ty<'tcx>| {
45 let ty = self.resolve_vars_if_possible(ty);
46 let ty = self.tcx.normalize_erasing_regions(self.param_env, ty);
47 (SizeSkeleton::compute(ty, self.tcx, self.param_env), ty)
49 let (sk_from, from) = convert(from);
50 let (sk_to, to) = convert(to);
52 // Check for same size using the skeletons.
53 if let (Ok(sk_from), Ok(sk_to)) = (sk_from, sk_to) {
54 if sk_from.same_size(sk_to) {
58 // Special-case transmuting from `typeof(function)` and
59 // `Option<typeof(function)>` to present a clearer error.
60 let from = unpack_option_like(self.tcx, from);
61 if let (&ty::FnDef(..), SizeSkeleton::Known(size_to)) = (from.kind(), sk_to) && size_to == Pointer.size(&self.tcx) {
62 struct_span_err!(self.tcx.sess, span, E0591, "can't transmute zero-sized type")
63 .note(&format!("source type: {from}"))
64 .note(&format!("target type: {to}"))
65 .help("cast with `as` to a pointer instead")
71 // Try to display a sensible error with as much information as possible.
72 let skeleton_string = |ty: Ty<'tcx>, sk| match sk {
73 Ok(SizeSkeleton::Known(size)) => format!("{} bits", size.bits()),
74 Ok(SizeSkeleton::Pointer { tail, .. }) => format!("pointer to `{tail}`"),
75 Err(LayoutError::Unknown(bad)) => {
77 "this type does not have a fixed size".to_owned()
79 format!("size can vary because of {bad}")
82 Err(err) => err.to_string(),
85 let mut err = struct_span_err!(
89 "cannot transmute between types of different sizes, \
90 or dependently-sized types"
93 err.note(&format!("`{from}` does not have a fixed size"));
95 err.note(&format!("source type: `{}` ({})", from, skeleton_string(from, sk_from)))
96 .note(&format!("target type: `{}` ({})", to, skeleton_string(to, sk_to)));
102 pub struct InlineAsmCtxt<'a, 'tcx> {
104 param_env: ty::ParamEnv<'tcx>,
105 get_operand_ty: Box<dyn Fn(&'tcx hir::Expr<'tcx>) -> Ty<'tcx> + 'a>,
108 impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
109 pub fn new_global_asm(tcx: TyCtxt<'tcx>) -> Self {
112 param_env: ty::ParamEnv::empty(),
113 get_operand_ty: Box::new(|e| bug!("asm operand in global asm: {e:?}")),
119 param_env: ty::ParamEnv<'tcx>,
120 get_operand_ty: impl Fn(&'tcx hir::Expr<'tcx>) -> Ty<'tcx> + 'a,
122 InlineAsmCtxt { tcx, param_env, get_operand_ty: Box::new(get_operand_ty) }
125 // FIXME(compiler-errors): This could use `<$ty as Pointee>::Metadata == ()`
126 fn is_thin_ptr_ty(&self, ty: Ty<'tcx>) -> bool {
127 // Type still may have region variables, but `Sized` does not depend
128 // on those, so just erase them before querying.
129 if ty.is_sized(self.tcx.at(DUMMY_SP), self.param_env) {
132 if let ty::Foreign(..) = ty.kind() {
138 fn check_asm_operand_type(
141 reg: InlineAsmRegOrRegClass,
142 expr: &'tcx hir::Expr<'tcx>,
143 template: &[InlineAsmTemplatePiece],
145 tied_input: Option<(&'tcx hir::Expr<'tcx>, Option<InlineAsmType>)>,
146 target_features: &FxHashSet<Symbol>,
147 ) -> Option<InlineAsmType> {
148 let ty = (self.get_operand_ty)(expr);
149 if ty.has_infer_types_or_consts() {
150 bug!("inference variable in asm operand ty: {:?} {:?}", expr, ty);
152 let asm_ty_isize = match self.tcx.sess.target.pointer_width {
153 16 => InlineAsmType::I16,
154 32 => InlineAsmType::I32,
155 64 => InlineAsmType::I64,
159 let asm_ty = match *ty.kind() {
160 // `!` is allowed for input but not for output (issue #87802)
161 ty::Never if is_input => return None,
162 ty::Error(_) => return None,
163 ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => Some(InlineAsmType::I8),
164 ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => Some(InlineAsmType::I16),
165 ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => Some(InlineAsmType::I32),
166 ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => Some(InlineAsmType::I64),
167 ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => Some(InlineAsmType::I128),
168 ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => Some(asm_ty_isize),
169 ty::Float(FloatTy::F32) => Some(InlineAsmType::F32),
170 ty::Float(FloatTy::F64) => Some(InlineAsmType::F64),
171 ty::FnPtr(_) => Some(asm_ty_isize),
172 ty::RawPtr(ty::TypeAndMut { ty, mutbl: _ }) if self.is_thin_ptr_ty(ty) => {
175 ty::Adt(adt, substs) if adt.repr().simd() => {
176 let fields = &adt.non_enum_variant().fields;
177 let elem_ty = fields[0].ty(self.tcx, substs);
178 match elem_ty.kind() {
179 ty::Never | ty::Error(_) => return None,
180 ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => {
181 Some(InlineAsmType::VecI8(fields.len() as u64))
183 ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => {
184 Some(InlineAsmType::VecI16(fields.len() as u64))
186 ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => {
187 Some(InlineAsmType::VecI32(fields.len() as u64))
189 ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => {
190 Some(InlineAsmType::VecI64(fields.len() as u64))
192 ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => {
193 Some(InlineAsmType::VecI128(fields.len() as u64))
195 ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => {
196 Some(match self.tcx.sess.target.pointer_width {
197 16 => InlineAsmType::VecI16(fields.len() as u64),
198 32 => InlineAsmType::VecI32(fields.len() as u64),
199 64 => InlineAsmType::VecI64(fields.len() as u64),
203 ty::Float(FloatTy::F32) => Some(InlineAsmType::VecF32(fields.len() as u64)),
204 ty::Float(FloatTy::F64) => Some(InlineAsmType::VecF64(fields.len() as u64)),
208 ty::Infer(_) => unreachable!(),
211 let Some(asm_ty) = asm_ty else {
212 let msg = &format!("cannot use value of type `{ty}` for inline assembly");
213 let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
215 "only integers, floats, SIMD vectors, pointers and function pointers \
216 can be used as arguments for inline assembly",
222 // Check that the type implements Copy. The only case where this can
223 // possibly fail is for SIMD types which don't #[derive(Copy)].
224 if !ty.is_copy_modulo_regions(self.tcx.at(expr.span), self.param_env) {
225 let msg = "arguments for inline assembly must be copyable";
226 let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
227 err.note(&format!("`{ty}` does not implement the Copy trait"));
231 // Ideally we wouldn't need to do this, but LLVM's register allocator
232 // really doesn't like it when tied operands have different types.
234 // This is purely an LLVM limitation, but we have to live with it since
235 // there is no way to hide this with implicit conversions.
237 // For the purposes of this check we only look at the `InlineAsmType`,
238 // which means that pointers and integers are treated as identical (modulo
240 if let Some((in_expr, Some(in_asm_ty))) = tied_input {
241 if in_asm_ty != asm_ty {
242 let msg = "incompatible types for asm inout argument";
243 let mut err = self.tcx.sess.struct_span_err(vec![in_expr.span, expr.span], msg);
245 let in_expr_ty = (self.get_operand_ty)(in_expr);
246 err.span_label(in_expr.span, &format!("type `{in_expr_ty}`"));
247 err.span_label(expr.span, &format!("type `{ty}`"));
249 "asm inout arguments must have the same type, \
250 unless they are both pointers or integers of the same size",
255 // All of the later checks have already been done on the input, so
256 // let's not emit errors and warnings twice.
260 // Check the type against the list of types supported by the selected
262 let asm_arch = self.tcx.sess.asm_arch.unwrap();
263 let reg_class = reg.reg_class();
264 let supported_tys = reg_class.supported_types(asm_arch);
265 let Some((_, feature)) = supported_tys.iter().find(|&&(t, _)| t == asm_ty) else {
266 let msg = &format!("type `{ty}` cannot be used with this register class");
267 let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
268 let supported_tys: Vec<_> =
269 supported_tys.iter().map(|(t, _)| t.to_string()).collect();
271 "register class `{}` supports these types: {}",
273 supported_tys.join(", "),
275 if let Some(suggest) = reg_class.suggest_class(asm_arch, asm_ty) {
277 "consider using the `{}` register class instead",
285 // Check whether the selected type requires a target feature. Note that
286 // this is different from the feature check we did earlier. While the
287 // previous check checked that this register class is usable at all
288 // with the currently enabled features, some types may only be usable
289 // with a register class when a certain feature is enabled. We check
290 // this here since it depends on the results of typeck.
292 // Also note that this check isn't run when the operand type is never
293 // (!). In that case we still need the earlier check to verify that the
294 // register class is usable at all.
295 if let Some(feature) = feature {
296 if !target_features.contains(&feature) {
297 let msg = &format!("`{}` target feature is not enabled", feature);
298 let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
300 "this is required to use type `{}` with register class `{}`",
309 // Check whether a modifier is suggested for using this type.
310 if let Some((suggested_modifier, suggested_result)) =
311 reg_class.suggest_modifier(asm_arch, asm_ty)
313 // Search for any use of this operand without a modifier and emit
314 // the suggestion for them.
315 let mut spans = vec![];
316 for piece in template {
317 if let &InlineAsmTemplatePiece::Placeholder { operand_idx, modifier, span } = piece
319 if operand_idx == idx && modifier.is_none() {
324 if !spans.is_empty() {
325 let (default_modifier, default_result) =
326 reg_class.default_modifier(asm_arch).unwrap();
327 self.tcx.struct_span_lint_hir(
328 lint::builtin::ASM_SUB_REGISTER,
332 let msg = "formatting may not be suitable for sub-register argument";
333 let mut err = lint.build(msg);
334 err.span_label(expr.span, "for this argument");
336 "use the `{suggested_modifier}` modifier to have the register formatted as `{suggested_result}`",
339 "or use the `{default_modifier}` modifier to keep the default formatting of `{default_result}`",
350 pub fn check_asm(&self, asm: &hir::InlineAsm<'tcx>, enclosing_id: hir::HirId) {
351 let hir = self.tcx.hir();
352 let enclosing_def_id = hir.local_def_id(enclosing_id).to_def_id();
353 let target_features = self.tcx.asm_target_features(enclosing_def_id);
354 let Some(asm_arch) = self.tcx.sess.asm_arch else {
355 self.tcx.sess.delay_span_bug(DUMMY_SP, "target architecture does not support asm");
358 for (idx, (op, op_sp)) in asm.operands.iter().enumerate() {
359 // Validate register classes against currently enabled target
360 // features. We check that at least one type is available for
361 // the enabled features.
363 // We ignore target feature requirements for clobbers: if the
364 // feature is disabled then the compiler doesn't care what we
365 // do with the registers.
367 // Note that this is only possible for explicit register
368 // operands, which cannot be used in the asm string.
369 if let Some(reg) = op.reg() {
370 // Some explicit registers cannot be used depending on the
371 // target. Reject those here.
372 if let InlineAsmRegOrRegClass::Reg(reg) = reg {
373 if let InlineAsmReg::Err = reg {
374 // `validate` will panic on `Err`, as an error must
375 // already have been reported.
378 if let Err(msg) = reg.validate(
380 self.tcx.sess.relocation_model(),
382 &self.tcx.sess.target,
385 let msg = format!("cannot use register `{}`: {}", reg.name(), msg);
386 self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
391 if !op.is_clobber() {
392 let mut missing_required_features = vec![];
393 let reg_class = reg.reg_class();
394 if let InlineAsmRegClass::Err = reg_class {
397 for &(_, feature) in reg_class.supported_types(asm_arch) {
400 if target_features.contains(&feature) {
401 missing_required_features.clear();
404 missing_required_features.push(feature);
408 missing_required_features.clear();
414 // We are sorting primitive strs here and can use unstable sort here
415 missing_required_features.sort_unstable();
416 missing_required_features.dedup();
417 match &missing_required_features[..] {
421 "register class `{}` requires the `{}` target feature",
425 self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
426 // register isn't enabled, don't do more checks
431 "register class `{}` requires at least one of the following target features: {}",
437 .collect::<String>(),
439 self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
440 // register isn't enabled, don't do more checks
448 hir::InlineAsmOperand::In { reg, ref expr } => {
449 self.check_asm_operand_type(
459 hir::InlineAsmOperand::Out { reg, late: _, ref expr } => {
460 if let Some(expr) = expr {
461 self.check_asm_operand_type(
472 hir::InlineAsmOperand::InOut { reg, late: _, ref expr } => {
473 self.check_asm_operand_type(
483 hir::InlineAsmOperand::SplitInOut { reg, late: _, ref in_expr, ref out_expr } => {
484 let in_ty = self.check_asm_operand_type(
493 if let Some(out_expr) = out_expr {
494 self.check_asm_operand_type(
500 Some((in_expr, in_ty)),
505 // No special checking is needed for these:
506 // - Typeck has checked that Const operands are integers.
507 // - AST lowering guarantees that SymStatic points to a static.
508 hir::InlineAsmOperand::Const { .. } | hir::InlineAsmOperand::SymStatic { .. } => {}
509 // Check that sym actually points to a function. Later passes
511 hir::InlineAsmOperand::SymFn { anon_const } => {
512 let ty = self.tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id);
514 ty::Never | ty::Error(_) => {}
518 self.tcx.sess.struct_span_err(*op_sp, "invalid `sym` operand");
520 self.tcx.hir().span(anon_const.body.hir_id),
521 &format!("is {} `{}`", ty.kind().article(), ty),
523 err.help("`sym` operands must refer to either a function or a static");