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