]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/check/intrinsicck.rs
Auto merge of #102189 - davidtwco:translation-derive-enums, r=compiler-errors
[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
13 use super::FnCtxt;
14
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 };
19
20     if def.variants().len() == 2 && !def.repr().c() && def.repr().int.is_none() {
21         let data_idx;
22
23         let one = VariantIdx::new(1);
24         let zero = VariantIdx::new(0);
25
26         if def.variant(zero).fields.is_empty() {
27             data_idx = one;
28         } else if def.variant(one).fields.is_empty() {
29             data_idx = zero;
30         } else {
31             return ty;
32         }
33
34         if def.variant(data_idx).fields.len() == 1 {
35             return def.variant(data_idx).fields[0].ty(tcx, substs);
36         }
37     }
38
39     ty
40 }
41
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)
48         };
49         let (sk_from, from) = convert(from);
50         let (sk_to, to) = convert(to);
51
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) {
55                 return;
56             }
57
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")
66                     .emit();
67                 return;
68             }
69         }
70
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)) => {
76                 if bad == ty {
77                     "this type does not have a fixed size".to_owned()
78                 } else {
79                     format!("size can vary because of {bad}")
80                 }
81             }
82             Err(err) => err.to_string(),
83         };
84
85         let mut err = struct_span_err!(
86             self.tcx.sess,
87             span,
88             E0512,
89             "cannot transmute between types of different sizes, \
90                                         or dependently-sized types"
91         );
92         if from == to {
93             err.note(&format!("`{from}` does not have a fixed size"));
94         } else {
95             err.note(&format!("source type: `{}` ({})", from, skeleton_string(from, sk_from)))
96                 .note(&format!("target type: `{}` ({})", to, skeleton_string(to, sk_to)));
97         }
98         err.emit();
99     }
100 }
101
102 pub struct InlineAsmCtxt<'a, 'tcx> {
103     tcx: TyCtxt<'tcx>,
104     param_env: ty::ParamEnv<'tcx>,
105     get_operand_ty: Box<dyn Fn(&'tcx hir::Expr<'tcx>) -> Ty<'tcx> + 'a>,
106 }
107
108 impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
109     pub fn new_global_asm(tcx: TyCtxt<'tcx>) -> Self {
110         InlineAsmCtxt {
111             tcx,
112             param_env: ty::ParamEnv::empty(),
113             get_operand_ty: Box::new(|e| bug!("asm operand in global asm: {e:?}")),
114         }
115     }
116
117     pub fn new_in_fn(
118         tcx: TyCtxt<'tcx>,
119         param_env: ty::ParamEnv<'tcx>,
120         get_operand_ty: impl Fn(&'tcx hir::Expr<'tcx>) -> Ty<'tcx> + 'a,
121     ) -> Self {
122         InlineAsmCtxt { tcx, param_env, get_operand_ty: Box::new(get_operand_ty) }
123     }
124
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) {
130             return true;
131         }
132         if let ty::Foreign(..) = ty.kind() {
133             return true;
134         }
135         false
136     }
137
138     fn check_asm_operand_type(
139         &self,
140         idx: usize,
141         reg: InlineAsmRegOrRegClass,
142         expr: &'tcx hir::Expr<'tcx>,
143         template: &[InlineAsmTemplatePiece],
144         is_input: bool,
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);
151         }
152         let asm_ty_isize = match self.tcx.sess.target.pointer_width {
153             16 => InlineAsmType::I16,
154             32 => InlineAsmType::I32,
155             64 => InlineAsmType::I64,
156             _ => unreachable!(),
157         };
158
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) => {
173                 Some(asm_ty_isize)
174             }
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))
182                     }
183                     ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => {
184                         Some(InlineAsmType::VecI16(fields.len() as u64))
185                     }
186                     ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => {
187                         Some(InlineAsmType::VecI32(fields.len() as u64))
188                     }
189                     ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => {
190                         Some(InlineAsmType::VecI64(fields.len() as u64))
191                     }
192                     ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => {
193                         Some(InlineAsmType::VecI128(fields.len() as u64))
194                     }
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),
200                             _ => unreachable!(),
201                         })
202                     }
203                     ty::Float(FloatTy::F32) => Some(InlineAsmType::VecF32(fields.len() as u64)),
204                     ty::Float(FloatTy::F64) => Some(InlineAsmType::VecF64(fields.len() as u64)),
205                     _ => None,
206                 }
207             }
208             ty::Infer(_) => unreachable!(),
209             _ => None,
210         };
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);
214             err.note(
215                 "only integers, floats, SIMD vectors, pointers and function pointers \
216                  can be used as arguments for inline assembly",
217             );
218             err.emit();
219             return None;
220         };
221
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"));
228             err.emit();
229         }
230
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.
233         //
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.
236         //
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
239         // size).
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);
244
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}`"));
248                 err.note(
249                     "asm inout arguments must have the same type, \
250                     unless they are both pointers or integers of the same size",
251                 );
252                 err.emit();
253             }
254
255             // All of the later checks have already been done on the input, so
256             // let's not emit errors and warnings twice.
257             return Some(asm_ty);
258         }
259
260         // Check the type against the list of types supported by the selected
261         // register class.
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();
270             err.note(&format!(
271                 "register class `{}` supports these types: {}",
272                 reg_class.name(),
273                 supported_tys.join(", "),
274             ));
275             if let Some(suggest) = reg_class.suggest_class(asm_arch, asm_ty) {
276                 err.help(&format!(
277                     "consider using the `{}` register class instead",
278                     suggest.name()
279                 ));
280             }
281             err.emit();
282             return Some(asm_ty);
283         };
284
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.
291         //
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);
299                 err.note(&format!(
300                     "this is required to use type `{}` with register class `{}`",
301                     ty,
302                     reg_class.name(),
303                 ));
304                 err.emit();
305                 return Some(asm_ty);
306             }
307         }
308
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)
312         {
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
318                 {
319                     if operand_idx == idx && modifier.is_none() {
320                         spans.push(span);
321                     }
322                 }
323             }
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,
329                     expr.hir_id,
330                     spans,
331                     |lint| {
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");
335                         err.help(&format!(
336                             "use `{{{idx}:{suggested_modifier}}}` to have the register formatted as `{suggested_result}`",
337                         ));
338                         err.help(&format!(
339                             "or use `{{{idx}:{default_modifier}}}` to keep the default formatting of `{default_result}`",
340                         ));
341                         err.emit();
342                     },
343                 );
344             }
345         }
346
347         Some(asm_ty)
348     }
349
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");
356             return;
357         };
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.
362             //
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.
366             //
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.
376                         continue;
377                     }
378                     if let Err(msg) = reg.validate(
379                         asm_arch,
380                         self.tcx.sess.relocation_model(),
381                         &target_features,
382                         &self.tcx.sess.target,
383                         op.is_clobber(),
384                     ) {
385                         let msg = format!("cannot use register `{}`: {}", reg.name(), msg);
386                         self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
387                         continue;
388                     }
389                 }
390
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 {
395                         continue;
396                     }
397                     for &(_, feature) in reg_class.supported_types(asm_arch) {
398                         match feature {
399                             Some(feature) => {
400                                 if target_features.contains(&feature) {
401                                     missing_required_features.clear();
402                                     break;
403                                 } else {
404                                     missing_required_features.push(feature);
405                                 }
406                             }
407                             None => {
408                                 missing_required_features.clear();
409                                 break;
410                             }
411                         }
412                     }
413
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[..] {
418                         [] => {}
419                         [feature] => {
420                             let msg = format!(
421                                 "register class `{}` requires the `{}` target feature",
422                                 reg_class.name(),
423                                 feature
424                             );
425                             self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
426                             // register isn't enabled, don't do more checks
427                             continue;
428                         }
429                         features => {
430                             let msg = format!(
431                                 "register class `{}` requires at least one of the following target features: {}",
432                                 reg_class.name(),
433                                 features
434                                     .iter()
435                                     .map(|f| f.as_str())
436                                     .intersperse(", ")
437                                     .collect::<String>(),
438                             );
439                             self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
440                             // register isn't enabled, don't do more checks
441                             continue;
442                         }
443                     }
444                 }
445             }
446
447             match *op {
448                 hir::InlineAsmOperand::In { reg, ref expr } => {
449                     self.check_asm_operand_type(
450                         idx,
451                         reg,
452                         expr,
453                         asm.template,
454                         true,
455                         None,
456                         &target_features,
457                     );
458                 }
459                 hir::InlineAsmOperand::Out { reg, late: _, ref expr } => {
460                     if let Some(expr) = expr {
461                         self.check_asm_operand_type(
462                             idx,
463                             reg,
464                             expr,
465                             asm.template,
466                             false,
467                             None,
468                             &target_features,
469                         );
470                     }
471                 }
472                 hir::InlineAsmOperand::InOut { reg, late: _, ref expr } => {
473                     self.check_asm_operand_type(
474                         idx,
475                         reg,
476                         expr,
477                         asm.template,
478                         false,
479                         None,
480                         &target_features,
481                     );
482                 }
483                 hir::InlineAsmOperand::SplitInOut { reg, late: _, ref in_expr, ref out_expr } => {
484                     let in_ty = self.check_asm_operand_type(
485                         idx,
486                         reg,
487                         in_expr,
488                         asm.template,
489                         true,
490                         None,
491                         &target_features,
492                     );
493                     if let Some(out_expr) = out_expr {
494                         self.check_asm_operand_type(
495                             idx,
496                             reg,
497                             out_expr,
498                             asm.template,
499                             false,
500                             Some((in_expr, in_ty)),
501                             &target_features,
502                         );
503                     }
504                 }
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
510                 // depend on this.
511                 hir::InlineAsmOperand::SymFn { anon_const } => {
512                     let ty = self.tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id);
513                     match ty.kind() {
514                         ty::Never | ty::Error(_) => {}
515                         ty::FnDef(..) => {}
516                         _ => {
517                             let mut err =
518                                 self.tcx.sess.struct_span_err(*op_sp, "invalid `sym` operand");
519                             err.span_label(
520                                 self.tcx.hir().span(anon_const.body.hir_id),
521                                 &format!("is {} `{}`", ty.kind().article(), ty),
522                             );
523                             err.help("`sym` operands must refer to either a function or a static");
524                             err.emit();
525                         }
526                     };
527                 }
528             }
529         }
530     }
531 }