]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_typeck/src/intrinsicck.rs
Rollup merge of #104359 - Nilstrieb:plus-one, r=fee1-dead
[rust.git] / compiler / rustc_hir_typeck / src / intrinsicck.rs
1 use hir::HirId;
2 use rustc_errors::struct_span_err;
3 use rustc_hir as hir;
4 use rustc_index::vec::Idx;
5 use rustc_middle::ty::layout::{LayoutError, SizeSkeleton};
6 use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable};
7 use rustc_target::abi::{Pointer, VariantIdx};
8
9 use super::FnCtxt;
10
11 /// If the type is `Option<T>`, it will return `T`, otherwise
12 /// the type itself. Works on most `Option`-like types.
13 fn unpack_option_like<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
14     let ty::Adt(def, substs) = *ty.kind() else { return ty };
15
16     if def.variants().len() == 2 && !def.repr().c() && def.repr().int.is_none() {
17         let data_idx;
18
19         let one = VariantIdx::new(1);
20         let zero = VariantIdx::new(0);
21
22         if def.variant(zero).fields.is_empty() {
23             data_idx = one;
24         } else if def.variant(one).fields.is_empty() {
25             data_idx = zero;
26         } else {
27             return ty;
28         }
29
30         if def.variant(data_idx).fields.len() == 1 {
31             return def.variant(data_idx).fields[0].ty(tcx, substs);
32         }
33     }
34
35     ty
36 }
37
38 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
39     pub fn check_transmute(&self, from: Ty<'tcx>, to: Ty<'tcx>, hir_id: HirId) {
40         let tcx = self.tcx;
41         let span = tcx.hir().span(hir_id);
42         let normalize = |ty| {
43             let ty = self.resolve_vars_if_possible(ty);
44             self.tcx.normalize_erasing_regions(self.param_env, ty)
45         };
46         let from = normalize(from);
47         let to = normalize(to);
48         trace!(?from, ?to);
49         if from.has_non_region_infer() || to.has_non_region_infer() {
50             tcx.sess.delay_span_bug(span, "argument to transmute has inference variables");
51             return;
52         }
53         // Transmutes that are only changing lifetimes are always ok.
54         if from == to {
55             return;
56         }
57
58         let skel = |ty| SizeSkeleton::compute(ty, tcx, self.param_env);
59         let sk_from = skel(from);
60         let sk_to = skel(to);
61         trace!(?sk_from, ?sk_to);
62
63         // Check for same size using the skeletons.
64         if let (Ok(sk_from), Ok(sk_to)) = (sk_from, sk_to) {
65             if sk_from.same_size(sk_to) {
66                 return;
67             }
68
69             // Special-case transmuting from `typeof(function)` and
70             // `Option<typeof(function)>` to present a clearer error.
71             let from = unpack_option_like(tcx, from);
72             if let (&ty::FnDef(..), SizeSkeleton::Known(size_to)) = (from.kind(), sk_to) && size_to == Pointer.size(&tcx) {
73                 struct_span_err!(tcx.sess, span, E0591, "can't transmute zero-sized type")
74                     .note(&format!("source type: {from}"))
75                     .note(&format!("target type: {to}"))
76                     .help("cast with `as` to a pointer instead")
77                     .emit();
78                 return;
79             }
80         }
81
82         // Try to display a sensible error with as much information as possible.
83         let skeleton_string = |ty: Ty<'tcx>, sk| match sk {
84             Ok(SizeSkeleton::Known(size)) => format!("{} bits", size.bits()),
85             Ok(SizeSkeleton::Pointer { tail, .. }) => format!("pointer to `{tail}`"),
86             Err(LayoutError::Unknown(bad)) => {
87                 if bad == ty {
88                     "this type does not have a fixed size".to_owned()
89                 } else {
90                     format!("size can vary because of {bad}")
91                 }
92             }
93             Err(err) => err.to_string(),
94         };
95
96         let mut err = struct_span_err!(
97             tcx.sess,
98             span,
99             E0512,
100             "cannot transmute between types of different sizes, \
101                                         or dependently-sized types"
102         );
103         if from == to {
104             err.note(&format!("`{from}` does not have a fixed size"));
105         } else {
106             err.note(&format!("source type: `{}` ({})", from, skeleton_string(from, sk_from)))
107                 .note(&format!("target type: `{}` ({})", to, skeleton_string(to, sk_to)));
108         }
109         err.emit();
110     }
111 }