]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/check/intrinsicck.rs
df94abbafb1bd7e9cb726c2935a074bd650c6a84
[rust.git] / compiler / rustc_typeck / src / check / intrinsicck.rs
1 use rustc_ast::InlineAsmTemplatePiece;
2 use rustc_data_structures::fx::FxHashSet;
3 use rustc_errors::struct_span_err;
4 use rustc_hir as hir;
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};
12 use rustc_trait_selection::infer::InferCtxtExt;
13
14 use super::FnCtxt;
15
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 };
20
21     if def.variants().len() == 2 && !def.repr().c() && def.repr().int.is_none() {
22         let data_idx;
23
24         let one = VariantIdx::new(1);
25         let zero = VariantIdx::new(0);
26
27         if def.variant(zero).fields.is_empty() {
28             data_idx = one;
29         } else if def.variant(one).fields.is_empty() {
30             data_idx = zero;
31         } else {
32             return ty;
33         }
34
35         if def.variant(data_idx).fields.len() == 1 {
36             return def.variant(data_idx).fields[0].ty(tcx, substs);
37         }
38     }
39
40     ty
41 }
42
43 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
44     pub fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>) {
45         let convert = |ty: Ty<'tcx>| {
46             let ty = self.resolve_vars_if_possible(ty);
47             let ty = self.tcx.normalize_erasing_regions(self.param_env, ty);
48             (SizeSkeleton::compute(ty, self.tcx, self.param_env), ty)
49         };
50         let (sk_from, from) = convert(from);
51         let (sk_to, to) = convert(to);
52
53         // Check for same size using the skeletons.
54         if let (Ok(sk_from), Ok(sk_to)) = (sk_from, sk_to) {
55             if sk_from.same_size(sk_to) {
56                 return;
57             }
58
59             // Special-case transmuting from `typeof(function)` and
60             // `Option<typeof(function)>` to present a clearer error.
61             let from = unpack_option_like(self.tcx, from);
62             if let (&ty::FnDef(..), SizeSkeleton::Known(size_to)) = (from.kind(), sk_to) && size_to == Pointer.size(&self.tcx) {
63                 struct_span_err!(self.tcx.sess, span, E0591, "can't transmute zero-sized type")
64                     .note(&format!("source type: {from}"))
65                     .note(&format!("target type: {to}"))
66                     .help("cast with `as` to a pointer instead")
67                     .emit();
68                 return;
69             }
70         }
71
72         // Try to display a sensible error with as much information as possible.
73         let skeleton_string = |ty: Ty<'tcx>, sk| match sk {
74             Ok(SizeSkeleton::Known(size)) => format!("{} bits", size.bits()),
75             Ok(SizeSkeleton::Pointer { tail, .. }) => format!("pointer to `{tail}`"),
76             Err(LayoutError::Unknown(bad)) => {
77                 if bad == ty {
78                     "this type does not have a fixed size".to_owned()
79                 } else {
80                     format!("size can vary because of {bad}")
81                 }
82             }
83             Err(err) => err.to_string(),
84         };
85
86         let mut err = struct_span_err!(
87             self.tcx.sess,
88             span,
89             E0512,
90             "cannot transmute between types of different sizes, \
91                                         or dependently-sized types"
92         );
93         if from == to {
94             err.note(&format!("`{from}` does not have a fixed size"));
95         } else {
96             err.note(&format!("source type: `{}` ({})", from, skeleton_string(from, sk_from)))
97                 .note(&format!("target type: `{}` ({})", to, skeleton_string(to, sk_to)));
98         }
99         err.emit();
100     }
101
102     // FIXME(compiler-errors): This could use `<$ty as Pointee>::Metadata == ()`
103     fn is_thin_ptr_ty(&self, ty: Ty<'tcx>) -> bool {
104         // Type still may have region variables, but `Sized` does not depend
105         // on those, so just erase them before querying.
106         if self.tcx.erase_regions(ty).is_sized(self.tcx.at(DUMMY_SP), self.param_env) {
107             return true;
108         }
109         if let ty::Foreign(..) = ty.kind() {
110             return true;
111         }
112         false
113     }
114 }
115
116 pub struct InlineAsmCtxt<'a, 'tcx> {
117     tcx: TyCtxt<'tcx>,
118     fcx: Option<&'a FnCtxt<'a, 'tcx>>,
119 }
120
121 impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
122     pub fn new_global_asm(tcx: TyCtxt<'tcx>) -> Self {
123         InlineAsmCtxt { tcx, fcx: None }
124     }
125
126     pub fn new_in_fn(fcx: &'a FnCtxt<'a, 'tcx>) -> Self {
127         InlineAsmCtxt { tcx: fcx.tcx, fcx: Some(fcx) }
128     }
129
130     fn check_asm_operand_type(
131         &self,
132         idx: usize,
133         reg: InlineAsmRegOrRegClass,
134         expr: &hir::Expr<'tcx>,
135         template: &[InlineAsmTemplatePiece],
136         is_input: bool,
137         tied_input: Option<(&hir::Expr<'tcx>, Option<InlineAsmType>)>,
138         target_features: &FxHashSet<Symbol>,
139     ) -> Option<InlineAsmType> {
140         let fcx = self.fcx.unwrap_or_else(|| span_bug!(expr.span, "asm operand for global asm"));
141         // Check the type against the allowed types for inline asm.
142         let ty = fcx.typeck_results.borrow().expr_ty_adjusted(expr);
143         let ty = fcx.resolve_vars_if_possible(ty);
144         let asm_ty_isize = match self.tcx.sess.target.pointer_width {
145             16 => InlineAsmType::I16,
146             32 => InlineAsmType::I32,
147             64 => InlineAsmType::I64,
148             _ => unreachable!(),
149         };
150
151         // Expect types to be fully resolved, no const or type variables.
152         if ty.has_infer_types_or_consts() {
153             assert!(fcx.is_tainted_by_errors());
154             return None;
155         }
156
157         let asm_ty = match *ty.kind() {
158             // `!` is allowed for input but not for output (issue #87802)
159             ty::Never if is_input => return None,
160             ty::Error(_) => return None,
161             ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => Some(InlineAsmType::I8),
162             ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => Some(InlineAsmType::I16),
163             ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => Some(InlineAsmType::I32),
164             ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => Some(InlineAsmType::I64),
165             ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => Some(InlineAsmType::I128),
166             ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => Some(asm_ty_isize),
167             ty::Float(FloatTy::F32) => Some(InlineAsmType::F32),
168             ty::Float(FloatTy::F64) => Some(InlineAsmType::F64),
169             ty::FnPtr(_) => Some(asm_ty_isize),
170             ty::RawPtr(ty::TypeAndMut { ty, mutbl: _ }) if fcx.is_thin_ptr_ty(ty) => {
171                 Some(asm_ty_isize)
172             }
173             ty::Adt(adt, substs) if adt.repr().simd() => {
174                 let fields = &adt.non_enum_variant().fields;
175                 let elem_ty = fields[0].ty(self.tcx, substs);
176                 match elem_ty.kind() {
177                     ty::Never | ty::Error(_) => return None,
178                     ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => {
179                         Some(InlineAsmType::VecI8(fields.len() as u64))
180                     }
181                     ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => {
182                         Some(InlineAsmType::VecI16(fields.len() as u64))
183                     }
184                     ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => {
185                         Some(InlineAsmType::VecI32(fields.len() as u64))
186                     }
187                     ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => {
188                         Some(InlineAsmType::VecI64(fields.len() as u64))
189                     }
190                     ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => {
191                         Some(InlineAsmType::VecI128(fields.len() as u64))
192                     }
193                     ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => {
194                         Some(match self.tcx.sess.target.pointer_width {
195                             16 => InlineAsmType::VecI16(fields.len() as u64),
196                             32 => InlineAsmType::VecI32(fields.len() as u64),
197                             64 => InlineAsmType::VecI64(fields.len() as u64),
198                             _ => unreachable!(),
199                         })
200                     }
201                     ty::Float(FloatTy::F32) => Some(InlineAsmType::VecF32(fields.len() as u64)),
202                     ty::Float(FloatTy::F64) => Some(InlineAsmType::VecF64(fields.len() as u64)),
203                     _ => None,
204                 }
205             }
206             ty::Infer(_) => unreachable!(),
207             _ => None,
208         };
209         let Some(asm_ty) = asm_ty else {
210             let msg = &format!("cannot use value of type `{ty}` for inline assembly");
211             let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
212             err.note(
213                 "only integers, floats, SIMD vectors, pointers and function pointers \
214                  can be used as arguments for inline assembly",
215             );
216             err.emit();
217             return None;
218         };
219
220         // Check that the type implements Copy. The only case where this can
221         // possibly fail is for SIMD types which don't #[derive(Copy)].
222         if !fcx.infcx.type_is_copy_modulo_regions(fcx.param_env, ty, DUMMY_SP) {
223             let msg = "arguments for inline assembly must be copyable";
224             let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
225             err.note(&format!("`{ty}` does not implement the Copy trait"));
226             err.emit();
227         }
228
229         // Ideally we wouldn't need to do this, but LLVM's register allocator
230         // really doesn't like it when tied operands have different types.
231         //
232         // This is purely an LLVM limitation, but we have to live with it since
233         // there is no way to hide this with implicit conversions.
234         //
235         // For the purposes of this check we only look at the `InlineAsmType`,
236         // which means that pointers and integers are treated as identical (modulo
237         // size).
238         if let Some((in_expr, Some(in_asm_ty))) = tied_input {
239             if in_asm_ty != asm_ty {
240                 let msg = "incompatible types for asm inout argument";
241                 let mut err = self.tcx.sess.struct_span_err(vec![in_expr.span, expr.span], msg);
242
243                 let in_expr_ty = fcx.typeck_results.borrow().expr_ty_adjusted(in_expr);
244                 let in_expr_ty = fcx.resolve_vars_if_possible(in_expr_ty);
245                 err.span_label(in_expr.span, &format!("type `{in_expr_ty}`"));
246                 err.span_label(expr.span, &format!("type `{ty}`"));
247                 err.note(
248                     "asm inout arguments must have the same type, \
249                     unless they are both pointers or integers of the same size",
250                 );
251                 err.emit();
252             }
253
254             // All of the later checks have already been done on the input, so
255             // let's not emit errors and warnings twice.
256             return Some(asm_ty);
257         }
258
259         // Check the type against the list of types supported by the selected
260         // register class.
261         let asm_arch = self.tcx.sess.asm_arch.unwrap();
262         let reg_class = reg.reg_class();
263         let supported_tys = reg_class.supported_types(asm_arch);
264         let Some((_, feature)) = supported_tys.iter().find(|&&(t, _)| t == asm_ty) else {
265             let msg = &format!("type `{ty}` cannot be used with this register class");
266             let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
267             let supported_tys: Vec<_> =
268                 supported_tys.iter().map(|(t, _)| t.to_string()).collect();
269             err.note(&format!(
270                 "register class `{}` supports these types: {}",
271                 reg_class.name(),
272                 supported_tys.join(", "),
273             ));
274             if let Some(suggest) = reg_class.suggest_class(asm_arch, asm_ty) {
275                 err.help(&format!(
276                     "consider using the `{}` register class instead",
277                     suggest.name()
278                 ));
279             }
280             err.emit();
281             return Some(asm_ty);
282         };
283
284         // Check whether the selected type requires a target feature. Note that
285         // this is different from the feature check we did earlier. While the
286         // previous check checked that this register class is usable at all
287         // with the currently enabled features, some types may only be usable
288         // with a register class when a certain feature is enabled. We check
289         // this here since it depends on the results of typeck.
290         //
291         // Also note that this check isn't run when the operand type is never
292         // (!). In that case we still need the earlier check to verify that the
293         // register class is usable at all.
294         if let Some(feature) = feature {
295             if !target_features.contains(&feature) {
296                 let msg = &format!("`{}` target feature is not enabled", feature);
297                 let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
298                 err.note(&format!(
299                     "this is required to use type `{}` with register class `{}`",
300                     ty,
301                     reg_class.name(),
302                 ));
303                 err.emit();
304                 return Some(asm_ty);
305             }
306         }
307
308         // Check whether a modifier is suggested for using this type.
309         if let Some((suggested_modifier, suggested_result)) =
310             reg_class.suggest_modifier(asm_arch, asm_ty)
311         {
312             // Search for any use of this operand without a modifier and emit
313             // the suggestion for them.
314             let mut spans = vec![];
315             for piece in template {
316                 if let &InlineAsmTemplatePiece::Placeholder { operand_idx, modifier, span } = piece
317                 {
318                     if operand_idx == idx && modifier.is_none() {
319                         spans.push(span);
320                     }
321                 }
322             }
323             if !spans.is_empty() {
324                 let (default_modifier, default_result) =
325                     reg_class.default_modifier(asm_arch).unwrap();
326                 self.tcx.struct_span_lint_hir(
327                     lint::builtin::ASM_SUB_REGISTER,
328                     expr.hir_id,
329                     spans,
330                     |lint| {
331                         let msg = "formatting may not be suitable for sub-register argument";
332                         let mut err = lint.build(msg);
333                         err.span_label(expr.span, "for this argument");
334                         err.help(&format!(
335                             "use the `{suggested_modifier}` modifier to have the register formatted as `{suggested_result}`",
336                         ));
337                         err.help(&format!(
338                             "or use the `{default_modifier}` modifier to keep the default formatting of `{default_result}`",
339                         ));
340                         err.emit();
341                     },
342                 );
343             }
344         }
345
346         Some(asm_ty)
347     }
348
349     pub fn check_asm(&self, asm: &hir::InlineAsm<'tcx>, enclosing_id: hir::HirId) {
350         let hir = self.tcx.hir();
351         let enclosing_def_id = hir.local_def_id(enclosing_id).to_def_id();
352         let target_features = self.tcx.asm_target_features(enclosing_def_id);
353         let Some(asm_arch) = self.tcx.sess.asm_arch else {
354             self.tcx.sess.delay_span_bug(DUMMY_SP, "target architecture does not support asm");
355             return;
356         };
357         for (idx, (op, op_sp)) in asm.operands.iter().enumerate() {
358             // Validate register classes against currently enabled target
359             // features. We check that at least one type is available for
360             // the enabled features.
361             //
362             // We ignore target feature requirements for clobbers: if the
363             // feature is disabled then the compiler doesn't care what we
364             // do with the registers.
365             //
366             // Note that this is only possible for explicit register
367             // operands, which cannot be used in the asm string.
368             if let Some(reg) = op.reg() {
369                 // Some explicit registers cannot be used depending on the
370                 // target. Reject those here.
371                 if let InlineAsmRegOrRegClass::Reg(reg) = reg {
372                     if let InlineAsmReg::Err = reg {
373                         // `validate` will panic on `Err`, as an error must
374                         // already have been reported.
375                         continue;
376                     }
377                     if let Err(msg) = reg.validate(
378                         asm_arch,
379                         self.tcx.sess.relocation_model(),
380                         &target_features,
381                         &self.tcx.sess.target,
382                         op.is_clobber(),
383                     ) {
384                         let msg = format!("cannot use register `{}`: {}", reg.name(), msg);
385                         self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
386                         continue;
387                     }
388                 }
389
390                 if !op.is_clobber() {
391                     let mut missing_required_features = vec![];
392                     let reg_class = reg.reg_class();
393                     if let InlineAsmRegClass::Err = reg_class {
394                         continue;
395                     }
396                     for &(_, feature) in reg_class.supported_types(asm_arch) {
397                         match feature {
398                             Some(feature) => {
399                                 if target_features.contains(&feature) {
400                                     missing_required_features.clear();
401                                     break;
402                                 } else {
403                                     missing_required_features.push(feature);
404                                 }
405                             }
406                             None => {
407                                 missing_required_features.clear();
408                                 break;
409                             }
410                         }
411                     }
412
413                     // We are sorting primitive strs here and can use unstable sort here
414                     missing_required_features.sort_unstable();
415                     missing_required_features.dedup();
416                     match &missing_required_features[..] {
417                         [] => {}
418                         [feature] => {
419                             let msg = format!(
420                                 "register class `{}` requires the `{}` target feature",
421                                 reg_class.name(),
422                                 feature
423                             );
424                             self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
425                             // register isn't enabled, don't do more checks
426                             continue;
427                         }
428                         features => {
429                             let msg = format!(
430                                 "register class `{}` requires at least one of the following target features: {}",
431                                 reg_class.name(),
432                                 features
433                                     .iter()
434                                     .map(|f| f.as_str())
435                                     .intersperse(", ")
436                                     .collect::<String>(),
437                             );
438                             self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
439                             // register isn't enabled, don't do more checks
440                             continue;
441                         }
442                     }
443                 }
444             }
445
446             match *op {
447                 hir::InlineAsmOperand::In { reg, ref expr } => {
448                     self.check_asm_operand_type(
449                         idx,
450                         reg,
451                         expr,
452                         asm.template,
453                         true,
454                         None,
455                         &target_features,
456                     );
457                 }
458                 hir::InlineAsmOperand::Out { reg, late: _, ref expr } => {
459                     if let Some(expr) = expr {
460                         self.check_asm_operand_type(
461                             idx,
462                             reg,
463                             expr,
464                             asm.template,
465                             false,
466                             None,
467                             &target_features,
468                         );
469                     }
470                 }
471                 hir::InlineAsmOperand::InOut { reg, late: _, ref expr } => {
472                     self.check_asm_operand_type(
473                         idx,
474                         reg,
475                         expr,
476                         asm.template,
477                         false,
478                         None,
479                         &target_features,
480                     );
481                 }
482                 hir::InlineAsmOperand::SplitInOut { reg, late: _, ref in_expr, ref out_expr } => {
483                     let in_ty = self.check_asm_operand_type(
484                         idx,
485                         reg,
486                         in_expr,
487                         asm.template,
488                         true,
489                         None,
490                         &target_features,
491                     );
492                     if let Some(out_expr) = out_expr {
493                         self.check_asm_operand_type(
494                             idx,
495                             reg,
496                             out_expr,
497                             asm.template,
498                             false,
499                             Some((in_expr, in_ty)),
500                             &target_features,
501                         );
502                     }
503                 }
504                 // No special checking is needed for these:
505                 // - Typeck has checked that Const operands are integers.
506                 // - AST lowering guarantees that SymStatic points to a static.
507                 hir::InlineAsmOperand::Const { .. } | hir::InlineAsmOperand::SymStatic { .. } => {}
508                 // Check that sym actually points to a function. Later passes
509                 // depend on this.
510                 hir::InlineAsmOperand::SymFn { anon_const } => {
511                     let ty = self.tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id);
512                     match ty.kind() {
513                         ty::Never | ty::Error(_) => {}
514                         ty::FnDef(..) => {}
515                         _ => {
516                             let mut err =
517                                 self.tcx.sess.struct_span_err(*op_sp, "invalid `sym` operand");
518                             err.span_label(
519                                 self.tcx.hir().span(anon_const.body.hir_id),
520                                 &format!("is {} `{}`", ty.kind().article(), ty),
521                             );
522                             err.help("`sym` operands must refer to either a function or a static");
523                             err.emit();
524                         }
525                     };
526                 }
527             }
528         }
529     }
530 }