2 use rustc_ast::InlineAsmTemplatePiece;
3 use rustc_data_structures::fx::FxHashSet;
4 use rustc_errors::struct_span_err;
6 use rustc_index::vec::Idx;
7 use rustc_middle::ty::layout::{LayoutError, SizeSkeleton};
8 use rustc_middle::ty::{self, Article, FloatTy, IntTy, Ty, TyCtxt, TypeVisitable, UintTy};
9 use rustc_session::lint;
10 use rustc_span::{Symbol, DUMMY_SP};
11 use rustc_target::abi::{Pointer, VariantIdx};
12 use rustc_target::asm::{InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType};
16 /// If the type is `Option<T>`, it will return `T`, otherwise
17 /// the type itself. Works on most `Option`-like types.
18 fn unpack_option_like<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
19 let ty::Adt(def, substs) = *ty.kind() else { return ty };
21 if def.variants().len() == 2 && !def.repr().c() && def.repr().int.is_none() {
24 let one = VariantIdx::new(1);
25 let zero = VariantIdx::new(0);
27 if def.variant(zero).fields.is_empty() {
29 } else if def.variant(one).fields.is_empty() {
35 if def.variant(data_idx).fields.len() == 1 {
36 return def.variant(data_idx).fields[0].ty(tcx, substs);
43 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
44 pub fn check_transmute(&self, from: Ty<'tcx>, to: Ty<'tcx>, hir_id: HirId) {
46 let span = tcx.hir().span(hir_id);
47 let normalize = |ty| {
48 let ty = self.resolve_vars_if_possible(ty);
49 self.tcx.normalize_erasing_regions(self.param_env, ty)
51 let from = normalize(from);
52 let to = normalize(to);
55 // Transmutes that are only changing lifetimes are always ok.
60 let skel = |ty| SizeSkeleton::compute(ty, tcx, self.param_env);
61 let sk_from = skel(from);
63 trace!(?sk_from, ?sk_to);
65 // Check for same size using the skeletons.
66 if let (Ok(sk_from), Ok(sk_to)) = (sk_from, sk_to) {
67 if sk_from.same_size(sk_to) {
71 // Special-case transmuting from `typeof(function)` and
72 // `Option<typeof(function)>` to present a clearer error.
73 let from = unpack_option_like(tcx, from);
74 if let (&ty::FnDef(..), SizeSkeleton::Known(size_to)) = (from.kind(), sk_to) && size_to == Pointer.size(&tcx) {
75 struct_span_err!(tcx.sess, span, E0591, "can't transmute zero-sized type")
76 .note(&format!("source type: {from}"))
77 .note(&format!("target type: {to}"))
78 .help("cast with `as` to a pointer instead")
84 // Try to display a sensible error with as much information as possible.
85 let skeleton_string = |ty: Ty<'tcx>, sk| match sk {
86 Ok(SizeSkeleton::Known(size)) => format!("{} bits", size.bits()),
87 Ok(SizeSkeleton::Pointer { tail, .. }) => format!("pointer to `{tail}`"),
88 Err(LayoutError::Unknown(bad)) => {
90 "this type does not have a fixed size".to_owned()
92 format!("size can vary because of {bad}")
95 Err(err) => err.to_string(),
98 let mut err = struct_span_err!(
102 "cannot transmute between types of different sizes, \
103 or dependently-sized types"
106 err.note(&format!("`{from}` does not have a fixed size"));
108 err.note(&format!("source type: `{}` ({})", from, skeleton_string(from, sk_from)))
109 .note(&format!("target type: `{}` ({})", to, skeleton_string(to, sk_to)));
115 pub struct InlineAsmCtxt<'a, 'tcx> {
117 param_env: ty::ParamEnv<'tcx>,
118 get_operand_ty: Box<dyn Fn(&'tcx hir::Expr<'tcx>) -> Ty<'tcx> + 'a>,
121 impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
122 pub fn new_global_asm(tcx: TyCtxt<'tcx>) -> Self {
125 param_env: ty::ParamEnv::empty(),
126 get_operand_ty: Box::new(|e| bug!("asm operand in global asm: {e:?}")),
132 param_env: ty::ParamEnv<'tcx>,
133 get_operand_ty: impl Fn(&'tcx hir::Expr<'tcx>) -> Ty<'tcx> + 'a,
135 InlineAsmCtxt { tcx, param_env, get_operand_ty: Box::new(get_operand_ty) }
138 // FIXME(compiler-errors): This could use `<$ty as Pointee>::Metadata == ()`
139 fn is_thin_ptr_ty(&self, ty: Ty<'tcx>) -> bool {
140 // Type still may have region variables, but `Sized` does not depend
141 // on those, so just erase them before querying.
142 if ty.is_sized(self.tcx.at(DUMMY_SP), self.param_env) {
145 if let ty::Foreign(..) = ty.kind() {
151 fn check_asm_operand_type(
154 reg: InlineAsmRegOrRegClass,
155 expr: &'tcx hir::Expr<'tcx>,
156 template: &[InlineAsmTemplatePiece],
158 tied_input: Option<(&'tcx hir::Expr<'tcx>, Option<InlineAsmType>)>,
159 target_features: &FxHashSet<Symbol>,
160 ) -> Option<InlineAsmType> {
161 let ty = (self.get_operand_ty)(expr);
162 if ty.has_non_region_infer() {
163 bug!("inference variable in asm operand ty: {:?} {:?}", expr, ty);
165 let asm_ty_isize = match self.tcx.sess.target.pointer_width {
166 16 => InlineAsmType::I16,
167 32 => InlineAsmType::I32,
168 64 => InlineAsmType::I64,
172 let asm_ty = match *ty.kind() {
173 // `!` is allowed for input but not for output (issue #87802)
174 ty::Never if is_input => return None,
175 ty::Error(_) => return None,
176 ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => Some(InlineAsmType::I8),
177 ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => Some(InlineAsmType::I16),
178 ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => Some(InlineAsmType::I32),
179 ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => Some(InlineAsmType::I64),
180 ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => Some(InlineAsmType::I128),
181 ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => Some(asm_ty_isize),
182 ty::Float(FloatTy::F32) => Some(InlineAsmType::F32),
183 ty::Float(FloatTy::F64) => Some(InlineAsmType::F64),
184 ty::FnPtr(_) => Some(asm_ty_isize),
185 ty::RawPtr(ty::TypeAndMut { ty, mutbl: _ }) if self.is_thin_ptr_ty(ty) => {
188 ty::Adt(adt, substs) if adt.repr().simd() => {
189 let fields = &adt.non_enum_variant().fields;
190 let elem_ty = fields[0].ty(self.tcx, substs);
191 match elem_ty.kind() {
192 ty::Never | ty::Error(_) => return None,
193 ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => {
194 Some(InlineAsmType::VecI8(fields.len() as u64))
196 ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => {
197 Some(InlineAsmType::VecI16(fields.len() as u64))
199 ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => {
200 Some(InlineAsmType::VecI32(fields.len() as u64))
202 ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => {
203 Some(InlineAsmType::VecI64(fields.len() as u64))
205 ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => {
206 Some(InlineAsmType::VecI128(fields.len() as u64))
208 ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => {
209 Some(match self.tcx.sess.target.pointer_width {
210 16 => InlineAsmType::VecI16(fields.len() as u64),
211 32 => InlineAsmType::VecI32(fields.len() as u64),
212 64 => InlineAsmType::VecI64(fields.len() as u64),
216 ty::Float(FloatTy::F32) => Some(InlineAsmType::VecF32(fields.len() as u64)),
217 ty::Float(FloatTy::F64) => Some(InlineAsmType::VecF64(fields.len() as u64)),
221 ty::Infer(_) => unreachable!(),
224 let Some(asm_ty) = asm_ty else {
225 let msg = &format!("cannot use value of type `{ty}` for inline assembly");
226 let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
228 "only integers, floats, SIMD vectors, pointers and function pointers \
229 can be used as arguments for inline assembly",
235 // Check that the type implements Copy. The only case where this can
236 // possibly fail is for SIMD types which don't #[derive(Copy)].
237 if !ty.is_copy_modulo_regions(self.tcx.at(expr.span), self.param_env) {
238 let msg = "arguments for inline assembly must be copyable";
239 let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
240 err.note(&format!("`{ty}` does not implement the Copy trait"));
244 // Ideally we wouldn't need to do this, but LLVM's register allocator
245 // really doesn't like it when tied operands have different types.
247 // This is purely an LLVM limitation, but we have to live with it since
248 // there is no way to hide this with implicit conversions.
250 // For the purposes of this check we only look at the `InlineAsmType`,
251 // which means that pointers and integers are treated as identical (modulo
253 if let Some((in_expr, Some(in_asm_ty))) = tied_input {
254 if in_asm_ty != asm_ty {
255 let msg = "incompatible types for asm inout argument";
256 let mut err = self.tcx.sess.struct_span_err(vec![in_expr.span, expr.span], msg);
258 let in_expr_ty = (self.get_operand_ty)(in_expr);
259 err.span_label(in_expr.span, &format!("type `{in_expr_ty}`"));
260 err.span_label(expr.span, &format!("type `{ty}`"));
262 "asm inout arguments must have the same type, \
263 unless they are both pointers or integers of the same size",
268 // All of the later checks have already been done on the input, so
269 // let's not emit errors and warnings twice.
273 // Check the type against the list of types supported by the selected
275 let asm_arch = self.tcx.sess.asm_arch.unwrap();
276 let reg_class = reg.reg_class();
277 let supported_tys = reg_class.supported_types(asm_arch);
278 let Some((_, feature)) = supported_tys.iter().find(|&&(t, _)| t == asm_ty) else {
279 let msg = &format!("type `{ty}` cannot be used with this register class");
280 let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
281 let supported_tys: Vec<_> =
282 supported_tys.iter().map(|(t, _)| t.to_string()).collect();
284 "register class `{}` supports these types: {}",
286 supported_tys.join(", "),
288 if let Some(suggest) = reg_class.suggest_class(asm_arch, asm_ty) {
290 "consider using the `{}` register class instead",
298 // Check whether the selected type requires a target feature. Note that
299 // this is different from the feature check we did earlier. While the
300 // previous check checked that this register class is usable at all
301 // with the currently enabled features, some types may only be usable
302 // with a register class when a certain feature is enabled. We check
303 // this here since it depends on the results of typeck.
305 // Also note that this check isn't run when the operand type is never
306 // (!). In that case we still need the earlier check to verify that the
307 // register class is usable at all.
308 if let Some(feature) = feature {
309 if !target_features.contains(&feature) {
310 let msg = &format!("`{}` target feature is not enabled", feature);
311 let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
313 "this is required to use type `{}` with register class `{}`",
322 // Check whether a modifier is suggested for using this type.
323 if let Some((suggested_modifier, suggested_result)) =
324 reg_class.suggest_modifier(asm_arch, asm_ty)
326 // Search for any use of this operand without a modifier and emit
327 // the suggestion for them.
328 let mut spans = vec![];
329 for piece in template {
330 if let &InlineAsmTemplatePiece::Placeholder { operand_idx, modifier, span } = piece
332 if operand_idx == idx && modifier.is_none() {
337 if !spans.is_empty() {
338 let (default_modifier, default_result) =
339 reg_class.default_modifier(asm_arch).unwrap();
340 self.tcx.struct_span_lint_hir(
341 lint::builtin::ASM_SUB_REGISTER,
344 "formatting may not be suitable for sub-register argument",
346 lint.span_label(expr.span, "for this argument");
348 "use `{{{idx}:{suggested_modifier}}}` to have the register formatted as `{suggested_result}`",
351 "or use `{{{idx}:{default_modifier}}}` to keep the default formatting of `{default_result}`",
362 pub fn check_asm(&self, asm: &hir::InlineAsm<'tcx>, enclosing_id: hir::HirId) {
363 let hir = self.tcx.hir();
364 let enclosing_def_id = hir.local_def_id(enclosing_id).to_def_id();
365 let target_features = self.tcx.asm_target_features(enclosing_def_id);
366 let Some(asm_arch) = self.tcx.sess.asm_arch else {
367 self.tcx.sess.delay_span_bug(DUMMY_SP, "target architecture does not support asm");
370 for (idx, (op, op_sp)) in asm.operands.iter().enumerate() {
371 // Validate register classes against currently enabled target
372 // features. We check that at least one type is available for
373 // the enabled features.
375 // We ignore target feature requirements for clobbers: if the
376 // feature is disabled then the compiler doesn't care what we
377 // do with the registers.
379 // Note that this is only possible for explicit register
380 // operands, which cannot be used in the asm string.
381 if let Some(reg) = op.reg() {
382 // Some explicit registers cannot be used depending on the
383 // target. Reject those here.
384 if let InlineAsmRegOrRegClass::Reg(reg) = reg {
385 if let InlineAsmReg::Err = reg {
386 // `validate` will panic on `Err`, as an error must
387 // already have been reported.
390 if let Err(msg) = reg.validate(
392 self.tcx.sess.relocation_model(),
394 &self.tcx.sess.target,
397 let msg = format!("cannot use register `{}`: {}", reg.name(), msg);
398 self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
403 if !op.is_clobber() {
404 let mut missing_required_features = vec![];
405 let reg_class = reg.reg_class();
406 if let InlineAsmRegClass::Err = reg_class {
409 for &(_, feature) in reg_class.supported_types(asm_arch) {
412 if target_features.contains(&feature) {
413 missing_required_features.clear();
416 missing_required_features.push(feature);
420 missing_required_features.clear();
426 // We are sorting primitive strs here and can use unstable sort here
427 missing_required_features.sort_unstable();
428 missing_required_features.dedup();
429 match &missing_required_features[..] {
433 "register class `{}` requires the `{}` target feature",
437 self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
438 // register isn't enabled, don't do more checks
443 "register class `{}` requires at least one of the following target features: {}",
449 .collect::<String>(),
451 self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
452 // register isn't enabled, don't do more checks
460 hir::InlineAsmOperand::In { reg, ref expr } => {
461 self.check_asm_operand_type(
471 hir::InlineAsmOperand::Out { reg, late: _, ref expr } => {
472 if let Some(expr) = expr {
473 self.check_asm_operand_type(
484 hir::InlineAsmOperand::InOut { reg, late: _, ref expr } => {
485 self.check_asm_operand_type(
495 hir::InlineAsmOperand::SplitInOut { reg, late: _, ref in_expr, ref out_expr } => {
496 let in_ty = self.check_asm_operand_type(
505 if let Some(out_expr) = out_expr {
506 self.check_asm_operand_type(
512 Some((in_expr, in_ty)),
517 // No special checking is needed for these:
518 // - Typeck has checked that Const operands are integers.
519 // - AST lowering guarantees that SymStatic points to a static.
520 hir::InlineAsmOperand::Const { .. } | hir::InlineAsmOperand::SymStatic { .. } => {}
521 // Check that sym actually points to a function. Later passes
523 hir::InlineAsmOperand::SymFn { anon_const } => {
524 let ty = self.tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id);
526 ty::Never | ty::Error(_) => {}
530 self.tcx.sess.struct_span_err(*op_sp, "invalid `sym` operand");
532 self.tcx.hir().span(anon_const.body.hir_id),
533 &format!("is {} `{}`", ty.kind().article(), ty),
535 err.help("`sym` operands must refer to either a function or a static");