2 use crate::hir::def_id::DefId;
3 use crate::ty::{self, Ty, TyCtxt};
4 use crate::ty::subst::SubstsRef;
5 use rustc_macros::HashStable;
8 #[derive(Clone, Copy, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
10 /// Go from a fn-item type to a fn-pointer type.
13 /// Go from a safe fn pointer to an unsafe fn pointer.
16 /// Go from a non-capturing closure to an fn pointer or an unsafe fn pointer.
17 /// It cannot convert a closure that requires unsafe.
18 ClosureFnPointer(hir::Unsafety),
20 /// Go from a mut raw pointer to a const raw pointer.
23 /// Unsize a pointer/reference value, e.g., `&[T; n]` to
24 /// `&[T]`. Note that the source could be a thin or fat pointer.
25 /// This will do things like convert thin pointers to fat
26 /// pointers, or convert structs containing thin pointers to
27 /// structs containing fat pointers, or convert between fat
28 /// pointers. We don't store the details of how the transform is
29 /// done (in fact, we don't know that, because it might depend on
30 /// the precise type parameters). We just store the target
31 /// type. Codegen backends and miri figure out what has to be done
32 /// based on the precise source/target type at hand.
36 /// Represents coercing a value to a different type of value.
38 /// We transform values by following a number of `Adjust` steps in order.
39 /// See the documentation on variants of `Adjust` for more details.
41 /// Here are some common scenarios:
43 /// 1. The simplest cases are where a pointer is not adjusted fat vs thin.
44 /// Here the pointer will be dereferenced N times (where a dereference can
45 /// happen to raw or borrowed pointers or any smart pointer which implements
46 /// Deref, including Box<_>). The types of dereferences is given by
47 /// `autoderefs`. It can then be auto-referenced zero or one times, indicated
48 /// by `autoref`, to either a raw or borrowed pointer. In these cases unsize is
51 /// 2. A thin-to-fat coercion involves unsizing the underlying data. We start
52 /// with a thin pointer, deref a number of times, unsize the underlying data,
53 /// then autoref. The 'unsize' phase may change a fixed length array to a
54 /// dynamically sized one, a concrete object to a trait object, or statically
55 /// sized struct to a dynamically sized one. E.g., &[i32; 4] -> &[i32] is
59 /// Deref(None) -> [i32; 4],
60 /// Borrow(AutoBorrow::Ref) -> &[i32; 4],
64 /// Note that for a struct, the 'deep' unsizing of the struct is not recorded.
65 /// E.g., `struct Foo<T> { x: T }` we can coerce &Foo<[i32; 4]> to &Foo<[i32]>
66 /// The autoderef and -ref are the same as in the above example, but the type
67 /// stored in `unsize` is `Foo<[i32]>`, we don't store any further detail about
68 /// the underlying conversions from `[i32; 4]` to `[i32]`.
70 /// 3. Coercing a `Box<T>` to `Box<dyn Trait>` is an interesting special case. In
71 /// that case, we have the pointer we need coming in, so there are no
72 /// autoderefs, and no autoref. Instead we just do the `Unsize` transformation.
73 /// At some point, of course, `Box` should move out of the compiler, in which
74 /// case this is analogous to transforming a struct. E.g., Box<[i32; 4]> ->
75 /// Box<[i32]> is an `Adjust::Unsize` with the target `Box<[i32]>`.
76 #[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
77 pub struct Adjustment<'tcx> {
78 pub kind: Adjust<'tcx>,
82 #[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
83 pub enum Adjust<'tcx> {
84 /// Go from ! to any type.
87 /// Dereference once, producing a place.
88 Deref(Option<OverloadedDeref<'tcx>>),
90 /// Take the address and produce either a `&` or `*` pointer.
91 Borrow(AutoBorrow<'tcx>),
96 /// An overloaded autoderef step, representing a `Deref(Mut)::deref(_mut)`
97 /// call, with the signature `&'a T -> &'a U` or `&'a mut T -> &'a mut U`.
98 /// The target type is `U` in both cases, with the region and mutability
99 /// being those shared by both the receiver and the returned reference.
100 #[derive(Copy, Clone, PartialEq, Debug, RustcEncodable, RustcDecodable, HashStable)]
101 pub struct OverloadedDeref<'tcx> {
102 pub region: ty::Region<'tcx>,
103 pub mutbl: hir::Mutability,
106 impl<'a, 'gcx, 'tcx> OverloadedDeref<'tcx> {
107 pub fn method_call(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, source: Ty<'tcx>)
108 -> (DefId, SubstsRef<'tcx>) {
109 let trait_def_id = match self.mutbl {
110 hir::MutImmutable => tcx.lang_items().deref_trait(),
111 hir::MutMutable => tcx.lang_items().deref_mut_trait()
113 let method_def_id = tcx.associated_items(trait_def_id.unwrap())
114 .find(|m| m.kind == ty::AssocKind::Method).unwrap().def_id;
115 (method_def_id, tcx.mk_substs_trait(source, &[]))
119 /// At least for initial deployment, we want to limit two-phase borrows to
120 /// only a few specific cases. Right now, those are mostly "things that desugar"
121 /// into method calls:
122 /// - using `x.some_method()` syntax, where some_method takes `&mut self`,
123 /// - using `Foo::some_method(&mut x, ...)` syntax,
124 /// - binary assignment operators (`+=`, `-=`, `*=`, etc.).
125 /// Anything else should be rejected until generalized two-phase borrow support
126 /// is implemented. Right now, dataflow can't handle the general case where there
127 /// is more than one use of a mutable borrow, and we don't want to accept too much
128 /// new code via two-phase borrows, so we try to limit where we create two-phase
129 /// capable mutable borrows.
130 /// See #49434 for tracking.
131 #[derive(Copy, Clone, PartialEq, Debug, RustcEncodable, RustcDecodable, HashStable)]
132 pub enum AllowTwoPhase {
137 #[derive(Copy, Clone, PartialEq, Debug, RustcEncodable, RustcDecodable, HashStable)]
138 pub enum AutoBorrowMutability {
139 Mutable { allow_two_phase_borrow: AllowTwoPhase },
143 impl From<AutoBorrowMutability> for hir::Mutability {
144 fn from(m: AutoBorrowMutability) -> Self {
146 AutoBorrowMutability::Mutable { .. } => hir::MutMutable,
147 AutoBorrowMutability::Immutable => hir::MutImmutable,
152 #[derive(Copy, Clone, PartialEq, Debug, RustcEncodable, RustcDecodable, HashStable)]
153 pub enum AutoBorrow<'tcx> {
154 /// Converts from T to &T.
155 Ref(ty::Region<'tcx>, AutoBorrowMutability),
157 /// Converts from T to *T.
158 RawPtr(hir::Mutability),
161 /// Information for `CoerceUnsized` impls, storing information we
162 /// have computed about the coercion.
164 /// This struct can be obtained via the `coerce_impl_info` query.
165 /// Demanding this struct also has the side-effect of reporting errors
166 /// for inappropriate impls.
167 #[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug, HashStable)]
168 pub struct CoerceUnsizedInfo {
169 /// If this is a "custom coerce" impl, then what kind of custom
170 /// coercion is it? This applies to impls of `CoerceUnsized` for
171 /// structs, primarily, where we store a bit of info about which
172 /// fields need to be coerced.
173 pub custom_kind: Option<CustomCoerceUnsized>
176 #[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug, HashStable)]
177 pub enum CustomCoerceUnsized {
178 /// Records the index of the field being coerced.