]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/check/intrinsicck.rs
Auto merge of #96046 - oli-obk:const_typeck, r=cjgillot
[rust.git] / compiler / rustc_typeck / src / check / intrinsicck.rs
1 use rustc_ast::InlineAsmTemplatePiece;
2 use rustc_data_structures::stable_set::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, FloatTy, InferTy, IntTy, Ty, TyCtxt, TypeFoldable, 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     fn is_thin_ptr_ty(&self, ty: Ty<'tcx>) -> bool {
102         if ty.is_sized(self.tcx.at(DUMMY_SP), self.param_env) {
103             return true;
104         }
105         if let ty::Foreign(..) = ty.kind() {
106             return true;
107         }
108         false
109     }
110
111     fn check_asm_operand_type(
112         &self,
113         idx: usize,
114         reg: InlineAsmRegOrRegClass,
115         expr: &hir::Expr<'tcx>,
116         template: &[InlineAsmTemplatePiece],
117         is_input: bool,
118         tied_input: Option<(&hir::Expr<'tcx>, Option<InlineAsmType>)>,
119         target_features: &FxHashSet<Symbol>,
120     ) -> Option<InlineAsmType> {
121         // Check the type against the allowed types for inline asm.
122         let ty = self.typeck_results.borrow().expr_ty_adjusted(expr);
123         let ty = self.resolve_vars_if_possible(ty);
124         let asm_ty_isize = match self.tcx.sess.target.pointer_width {
125             16 => InlineAsmType::I16,
126             32 => InlineAsmType::I32,
127             64 => InlineAsmType::I64,
128             _ => unreachable!(),
129         };
130         let asm_ty = match *ty.kind() {
131             // `!` is allowed for input but not for output (issue #87802)
132             ty::Never if is_input => return None,
133             ty::Error(_) => return None,
134             ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => Some(InlineAsmType::I8),
135             ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => Some(InlineAsmType::I16),
136             // Somewhat of a hack: fallback in the presence of errors does not actually
137             // fall back to i32, but to ty::Error. For integer inference variables this
138             // means that they don't get any fallback and stay as `{integer}`.
139             // Since compilation can't succeed anyway, it's fine to use this to avoid printing
140             // "cannot use value of type `{integer}`", even though that would absolutely
141             // work due due i32 fallback if the current function had no other errors.
142             ty::Infer(InferTy::IntVar(_)) => {
143                 assert!(self.is_tainted_by_errors());
144                 Some(InlineAsmType::I32)
145             }
146             ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => Some(InlineAsmType::I32),
147             ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => Some(InlineAsmType::I64),
148             ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => Some(InlineAsmType::I128),
149             ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => Some(asm_ty_isize),
150             ty::Infer(InferTy::FloatVar(_)) => {
151                 assert!(self.is_tainted_by_errors());
152                 Some(InlineAsmType::F32)
153             }
154             ty::Float(FloatTy::F32) => Some(InlineAsmType::F32),
155             ty::Float(FloatTy::F64) => Some(InlineAsmType::F64),
156             ty::FnPtr(_) => Some(asm_ty_isize),
157             ty::RawPtr(ty::TypeAndMut { ty, mutbl: _ }) if self.is_thin_ptr_ty(ty) => {
158                 Some(asm_ty_isize)
159             }
160             ty::Adt(adt, substs) if adt.repr().simd() => {
161                 let fields = &adt.non_enum_variant().fields;
162                 let elem_ty = fields[0].ty(self.tcx, substs);
163                 match elem_ty.kind() {
164                     ty::Never | ty::Error(_) => return None,
165                     ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => {
166                         Some(InlineAsmType::VecI8(fields.len() as u64))
167                     }
168                     ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => {
169                         Some(InlineAsmType::VecI16(fields.len() as u64))
170                     }
171                     ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => {
172                         Some(InlineAsmType::VecI32(fields.len() as u64))
173                     }
174                     ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => {
175                         Some(InlineAsmType::VecI64(fields.len() as u64))
176                     }
177                     ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => {
178                         Some(InlineAsmType::VecI128(fields.len() as u64))
179                     }
180                     ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => {
181                         Some(match self.tcx.sess.target.pointer_width {
182                             16 => InlineAsmType::VecI16(fields.len() as u64),
183                             32 => InlineAsmType::VecI32(fields.len() as u64),
184                             64 => InlineAsmType::VecI64(fields.len() as u64),
185                             _ => unreachable!(),
186                         })
187                     }
188                     ty::Float(FloatTy::F32) => Some(InlineAsmType::VecF32(fields.len() as u64)),
189                     ty::Float(FloatTy::F64) => Some(InlineAsmType::VecF64(fields.len() as u64)),
190                     _ => None,
191                 }
192             }
193             _ => None,
194         };
195         let Some(asm_ty) = asm_ty else {
196             let msg = &format!("cannot use value of type `{ty}` for inline assembly");
197             let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
198             err.note(
199                 "only integers, floats, SIMD vectors, pointers and function pointers \
200                  can be used as arguments for inline assembly",
201             );
202             err.emit();
203             return None;
204         };
205
206         if ty.has_infer_types_or_consts() {
207             assert!(self.is_tainted_by_errors());
208             return None;
209         }
210
211         // Check that the type implements Copy. The only case where this can
212         // possibly fail is for SIMD types which don't #[derive(Copy)].
213         if !ty.is_copy_modulo_regions(self.tcx.at(DUMMY_SP), self.param_env) {
214             let msg = "arguments for inline assembly must be copyable";
215             let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
216             err.note(&format!("`{ty}` does not implement the Copy trait"));
217             err.emit();
218         }
219
220         // Ideally we wouldn't need to do this, but LLVM's register allocator
221         // really doesn't like it when tied operands have different types.
222         //
223         // This is purely an LLVM limitation, but we have to live with it since
224         // there is no way to hide this with implicit conversions.
225         //
226         // For the purposes of this check we only look at the `InlineAsmType`,
227         // which means that pointers and integers are treated as identical (modulo
228         // size).
229         if let Some((in_expr, Some(in_asm_ty))) = tied_input {
230             if in_asm_ty != asm_ty {
231                 let msg = "incompatible types for asm inout argument";
232                 let mut err = self.tcx.sess.struct_span_err(vec![in_expr.span, expr.span], msg);
233
234                 let in_expr_ty = self.typeck_results.borrow().expr_ty_adjusted(in_expr);
235                 let in_expr_ty = self.resolve_vars_if_possible(in_expr_ty);
236                 err.span_label(in_expr.span, &format!("type `{in_expr_ty}`"));
237                 err.span_label(expr.span, &format!("type `{ty}`"));
238                 err.note(
239                     "asm inout arguments must have the same type, \
240                     unless they are both pointers or integers of the same size",
241                 );
242                 err.emit();
243             }
244
245             // All of the later checks have already been done on the input, so
246             // let's not emit errors and warnings twice.
247             return Some(asm_ty);
248         }
249
250         // Check the type against the list of types supported by the selected
251         // register class.
252         let asm_arch = self.tcx.sess.asm_arch.unwrap();
253         let reg_class = reg.reg_class();
254         let supported_tys = reg_class.supported_types(asm_arch);
255         let Some((_, feature)) = supported_tys.iter().find(|&&(t, _)| t == asm_ty) else {
256             let msg = &format!("type `{ty}` cannot be used with this register class");
257             let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
258             let supported_tys: Vec<_> =
259                 supported_tys.iter().map(|(t, _)| t.to_string()).collect();
260             err.note(&format!(
261                 "register class `{}` supports these types: {}",
262                 reg_class.name(),
263                 supported_tys.join(", "),
264             ));
265             if let Some(suggest) = reg_class.suggest_class(asm_arch, asm_ty) {
266                 err.help(&format!(
267                     "consider using the `{}` register class instead",
268                     suggest.name()
269                 ));
270             }
271             err.emit();
272             return Some(asm_ty);
273         };
274
275         // Check whether the selected type requires a target feature. Note that
276         // this is different from the feature check we did earlier. While the
277         // previous check checked that this register class is usable at all
278         // with the currently enabled features, some types may only be usable
279         // with a register class when a certain feature is enabled. We check
280         // this here since it depends on the results of typeck.
281         //
282         // Also note that this check isn't run when the operand type is never
283         // (!). In that case we still need the earlier check to verify that the
284         // register class is usable at all.
285         if let Some(feature) = feature {
286             if !target_features.contains(&feature) {
287                 let msg = &format!("`{}` target feature is not enabled", feature);
288                 let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
289                 err.note(&format!(
290                     "this is required to use type `{}` with register class `{}`",
291                     ty,
292                     reg_class.name(),
293                 ));
294                 err.emit();
295                 return Some(asm_ty);
296             }
297         }
298
299         // Check whether a modifier is suggested for using this type.
300         if let Some((suggested_modifier, suggested_result)) =
301             reg_class.suggest_modifier(asm_arch, asm_ty)
302         {
303             // Search for any use of this operand without a modifier and emit
304             // the suggestion for them.
305             let mut spans = vec![];
306             for piece in template {
307                 if let &InlineAsmTemplatePiece::Placeholder { operand_idx, modifier, span } = piece
308                 {
309                     if operand_idx == idx && modifier.is_none() {
310                         spans.push(span);
311                     }
312                 }
313             }
314             if !spans.is_empty() {
315                 let (default_modifier, default_result) =
316                     reg_class.default_modifier(asm_arch).unwrap();
317                 self.tcx.struct_span_lint_hir(
318                     lint::builtin::ASM_SUB_REGISTER,
319                     expr.hir_id,
320                     spans,
321                     |lint| {
322                         let msg = "formatting may not be suitable for sub-register argument";
323                         let mut err = lint.build(msg);
324                         err.span_label(expr.span, "for this argument");
325                         err.help(&format!(
326                             "use the `{suggested_modifier}` modifier to have the register formatted as `{suggested_result}`",
327                         ));
328                         err.help(&format!(
329                             "or use the `{default_modifier}` modifier to keep the default formatting of `{default_result}`",
330                         ));
331                         err.emit();
332                     },
333                 );
334             }
335         }
336
337         Some(asm_ty)
338     }
339
340     pub fn check_asm(&self, asm: &hir::InlineAsm<'tcx>, enclosing_id: hir::HirId) {
341         let hir = self.tcx.hir();
342         let enclosing_def_id = hir.local_def_id(enclosing_id).to_def_id();
343         let target_features = self.tcx.asm_target_features(enclosing_def_id);
344         let Some(asm_arch) = self.tcx.sess.asm_arch else {
345             self.tcx.sess.delay_span_bug(DUMMY_SP, "target architecture does not support asm");
346             return;
347         };
348         for (idx, (op, op_sp)) in asm.operands.iter().enumerate() {
349             // Validate register classes against currently enabled target
350             // features. We check that at least one type is available for
351             // the enabled features.
352             //
353             // We ignore target feature requirements for clobbers: if the
354             // feature is disabled then the compiler doesn't care what we
355             // do with the registers.
356             //
357             // Note that this is only possible for explicit register
358             // operands, which cannot be used in the asm string.
359             if let Some(reg) = op.reg() {
360                 // Some explicit registers cannot be used depending on the
361                 // target. Reject those here.
362                 if let InlineAsmRegOrRegClass::Reg(reg) = reg {
363                     if let InlineAsmReg::Err = reg {
364                         // `validate` will panic on `Err`, as an error must
365                         // already have been reported.
366                         continue;
367                     }
368                     if let Err(msg) = reg.validate(
369                         asm_arch,
370                         self.tcx.sess.relocation_model(),
371                         &target_features,
372                         &self.tcx.sess.target,
373                         op.is_clobber(),
374                     ) {
375                         let msg = format!("cannot use register `{}`: {}", reg.name(), msg);
376                         self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
377                         continue;
378                     }
379                 }
380
381                 if !op.is_clobber() {
382                     let mut missing_required_features = vec![];
383                     let reg_class = reg.reg_class();
384                     if let InlineAsmRegClass::Err = reg_class {
385                         continue;
386                     }
387                     for &(_, feature) in reg_class.supported_types(asm_arch) {
388                         match feature {
389                             Some(feature) => {
390                                 if target_features.contains(&feature) {
391                                     missing_required_features.clear();
392                                     break;
393                                 } else {
394                                     missing_required_features.push(feature);
395                                 }
396                             }
397                             None => {
398                                 missing_required_features.clear();
399                                 break;
400                             }
401                         }
402                     }
403
404                     // We are sorting primitive strs here and can use unstable sort here
405                     missing_required_features.sort_unstable();
406                     missing_required_features.dedup();
407                     match &missing_required_features[..] {
408                         [] => {}
409                         [feature] => {
410                             let msg = format!(
411                                 "register class `{}` requires the `{}` target feature",
412                                 reg_class.name(),
413                                 feature
414                             );
415                             self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
416                             // register isn't enabled, don't do more checks
417                             continue;
418                         }
419                         features => {
420                             let msg = format!(
421                                 "register class `{}` requires at least one of the following target features: {}",
422                                 reg_class.name(),
423                                 features
424                                     .iter()
425                                     .map(|f| f.as_str())
426                                     .intersperse(", ")
427                                     .collect::<String>(),
428                             );
429                             self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
430                             // register isn't enabled, don't do more checks
431                             continue;
432                         }
433                     }
434                 }
435             }
436
437             match *op {
438                 hir::InlineAsmOperand::In { reg, ref expr } => {
439                     self.check_asm_operand_type(
440                         idx,
441                         reg,
442                         expr,
443                         asm.template,
444                         true,
445                         None,
446                         &target_features,
447                     );
448                 }
449                 hir::InlineAsmOperand::Out { reg, late: _, ref expr } => {
450                     if let Some(expr) = expr {
451                         self.check_asm_operand_type(
452                             idx,
453                             reg,
454                             expr,
455                             asm.template,
456                             false,
457                             None,
458                             &target_features,
459                         );
460                     }
461                 }
462                 hir::InlineAsmOperand::InOut { reg, late: _, ref expr } => {
463                     self.check_asm_operand_type(
464                         idx,
465                         reg,
466                         expr,
467                         asm.template,
468                         false,
469                         None,
470                         &target_features,
471                     );
472                 }
473                 hir::InlineAsmOperand::SplitInOut { reg, late: _, ref in_expr, ref out_expr } => {
474                     let in_ty = self.check_asm_operand_type(
475                         idx,
476                         reg,
477                         in_expr,
478                         asm.template,
479                         true,
480                         None,
481                         &target_features,
482                     );
483                     if let Some(out_expr) = out_expr {
484                         self.check_asm_operand_type(
485                             idx,
486                             reg,
487                             out_expr,
488                             asm.template,
489                             false,
490                             Some((in_expr, in_ty)),
491                             &target_features,
492                         );
493                     }
494                 }
495                 // No special checking is needed for these:
496                 // - Typeck has checked that Const operands are integers.
497                 // - AST lowering guarantees that SymStatic points to a static.
498                 hir::InlineAsmOperand::Const { .. } | hir::InlineAsmOperand::SymStatic { .. } => {}
499                 // Check that sym actually points to a function. Later passes
500                 // depend on this.
501                 hir::InlineAsmOperand::SymFn { anon_const } => {
502                     let ty = self.tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id);
503                     match ty.kind() {
504                         ty::Never | ty::Error(_) => {}
505                         ty::FnDef(..) => {}
506                         _ => {
507                             let mut err =
508                                 self.tcx.sess.struct_span_err(*op_sp, "invalid `sym` operand");
509                             err.span_label(
510                                 self.tcx.hir().span(anon_const.body.hir_id),
511                                 &format!("is {} `{}`", ty.kind().article(), ty),
512                             );
513                             err.help("`sym` operands must refer to either a function or a static");
514                             err.emit();
515                         }
516                     };
517                 }
518             }
519         }
520     }
521 }