]> git.lizzy.rs Git - rust.git/blob - src/librustc_middle/traits/structural_impls.rs
Suggest boxing or borrowing unsized fields
[rust.git] / src / librustc_middle / traits / structural_impls.rs
1 use crate::traits;
2 use crate::ty::{Lift, TyCtxt};
3
4 use std::fmt;
5 use std::rc::Rc;
6
7 // Structural impls for the structs in `traits`.
8
9 impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSource<'tcx, N> {
10     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11         match *self {
12             super::ImplSourceUserDefined(ref v) => write!(f, "{:?}", v),
13
14             super::ImplSourceAutoImpl(ref t) => write!(f, "{:?}", t),
15
16             super::ImplSourceClosure(ref d) => write!(f, "{:?}", d),
17
18             super::ImplSourceGenerator(ref d) => write!(f, "{:?}", d),
19
20             super::ImplSourceFnPointer(ref d) => write!(f, "ImplSourceFnPointer({:?})", d),
21
22             super::ImplSourceDiscriminantKind(ref d) => write!(f, "{:?}", d),
23
24             super::ImplSourceObject(ref d) => write!(f, "{:?}", d),
25
26             super::ImplSourceParam(ref n) => write!(f, "ImplSourceParam({:?})", n),
27
28             super::ImplSourceBuiltin(ref d) => write!(f, "{:?}", d),
29
30             super::ImplSourceTraitAlias(ref d) => write!(f, "{:?}", d),
31         }
32     }
33 }
34
35 impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceUserDefinedData<'tcx, N> {
36     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37         write!(
38             f,
39             "ImplSourceUserDefinedData(impl_def_id={:?}, substs={:?}, nested={:?})",
40             self.impl_def_id, self.substs, self.nested
41         )
42     }
43 }
44
45 impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceGeneratorData<'tcx, N> {
46     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47         write!(
48             f,
49             "ImplSourceGeneratorData(generator_def_id={:?}, substs={:?}, nested={:?})",
50             self.generator_def_id, self.substs, self.nested
51         )
52     }
53 }
54
55 impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceClosureData<'tcx, N> {
56     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57         write!(
58             f,
59             "ImplSourceClosureData(closure_def_id={:?}, substs={:?}, nested={:?})",
60             self.closure_def_id, self.substs, self.nested
61         )
62     }
63 }
64
65 impl<N: fmt::Debug> fmt::Debug for traits::ImplSourceBuiltinData<N> {
66     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67         write!(f, "ImplSourceBuiltinData(nested={:?})", self.nested)
68     }
69 }
70
71 impl<N: fmt::Debug> fmt::Debug for traits::ImplSourceAutoImplData<N> {
72     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73         write!(
74             f,
75             "ImplSourceAutoImplData(trait_def_id={:?}, nested={:?})",
76             self.trait_def_id, self.nested
77         )
78     }
79 }
80
81 impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceObjectData<'tcx, N> {
82     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83         write!(
84             f,
85             "ImplSourceObjectData(upcast={:?}, vtable_base={}, nested={:?})",
86             self.upcast_trait_ref, self.vtable_base, self.nested
87         )
88     }
89 }
90
91 impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceFnPointerData<'tcx, N> {
92     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93         write!(f, "ImplSourceFnPointerData(fn_ty={:?}, nested={:?})", self.fn_ty, self.nested)
94     }
95 }
96
97 impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceTraitAliasData<'tcx, N> {
98     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99         write!(
100             f,
101             "ImplSourceTraitAlias(alias_def_id={:?}, substs={:?}, nested={:?})",
102             self.alias_def_id, self.substs, self.nested
103         )
104     }
105 }
106
107 ///////////////////////////////////////////////////////////////////////////
108 // Lift implementations
109
110 impl<'a, 'tcx> Lift<'tcx> for traits::SelectionError<'a> {
111     type Lifted = traits::SelectionError<'tcx>;
112     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
113         match *self {
114             super::Unimplemented => Some(super::Unimplemented),
115             super::OutputTypeParameterMismatch(a, b, ref err) => {
116                 tcx.lift(&(a, b)).and_then(|(a, b)| {
117                     tcx.lift(err).map(|err| super::OutputTypeParameterMismatch(a, b, err))
118                 })
119             }
120             super::TraitNotObjectSafe(def_id) => Some(super::TraitNotObjectSafe(def_id)),
121             super::ConstEvalFailure(err) => Some(super::ConstEvalFailure(err)),
122             super::Overflow => Some(super::Overflow),
123         }
124     }
125 }
126
127 impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
128     type Lifted = traits::ObligationCauseCode<'tcx>;
129     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
130         match *self {
131             super::ReturnNoExpression => Some(super::ReturnNoExpression),
132             super::MiscObligation => Some(super::MiscObligation),
133             super::SliceOrArrayElem => Some(super::SliceOrArrayElem),
134             super::TupleElem => Some(super::TupleElem),
135             super::ProjectionWf(proj) => tcx.lift(&proj).map(super::ProjectionWf),
136             super::ItemObligation(def_id) => Some(super::ItemObligation(def_id)),
137             super::BindingObligation(def_id, span) => Some(super::BindingObligation(def_id, span)),
138             super::ReferenceOutlivesReferent(ty) => {
139                 tcx.lift(&ty).map(super::ReferenceOutlivesReferent)
140             }
141             super::ObjectTypeBound(ty, r) => {
142                 tcx.lift(&ty).and_then(|ty| tcx.lift(&r).map(|r| super::ObjectTypeBound(ty, r)))
143             }
144             super::ObjectCastObligation(ty) => tcx.lift(&ty).map(super::ObjectCastObligation),
145             super::Coercion { source, target } => {
146                 Some(super::Coercion { source: tcx.lift(&source)?, target: tcx.lift(&target)? })
147             }
148             super::AssignmentLhsSized => Some(super::AssignmentLhsSized),
149             super::TupleInitializerSized => Some(super::TupleInitializerSized),
150             super::StructInitializerSized => Some(super::StructInitializerSized),
151             super::VariableType(id) => Some(super::VariableType(id)),
152             super::ReturnValue(id) => Some(super::ReturnValue(id)),
153             super::ReturnType => Some(super::ReturnType),
154             super::SizedArgumentType(sp) => Some(super::SizedArgumentType(sp)),
155             super::SizedReturnType => Some(super::SizedReturnType),
156             super::SizedYieldType => Some(super::SizedYieldType),
157             super::InlineAsmSized => Some(super::InlineAsmSized),
158             super::RepeatVec(suggest_flag) => Some(super::RepeatVec(suggest_flag)),
159             super::FieldSized { adt_kind, span, last } => {
160                 Some(super::FieldSized { adt_kind, span, last })
161             }
162             super::ConstSized => Some(super::ConstSized),
163             super::ConstPatternStructural => Some(super::ConstPatternStructural),
164             super::SharedStatic => Some(super::SharedStatic),
165             super::BuiltinDerivedObligation(ref cause) => {
166                 tcx.lift(cause).map(super::BuiltinDerivedObligation)
167             }
168             super::ImplDerivedObligation(ref cause) => {
169                 tcx.lift(cause).map(super::ImplDerivedObligation)
170             }
171             super::DerivedObligation(ref cause) => tcx.lift(cause).map(super::DerivedObligation),
172             super::CompareImplConstObligation => Some(super::CompareImplConstObligation),
173             super::CompareImplMethodObligation {
174                 item_name,
175                 impl_item_def_id,
176                 trait_item_def_id,
177             } => Some(super::CompareImplMethodObligation {
178                 item_name,
179                 impl_item_def_id,
180                 trait_item_def_id,
181             }),
182             super::CompareImplTypeObligation { item_name, impl_item_def_id, trait_item_def_id } => {
183                 Some(super::CompareImplTypeObligation {
184                     item_name,
185                     impl_item_def_id,
186                     trait_item_def_id,
187                 })
188             }
189             super::ExprAssignable => Some(super::ExprAssignable),
190             super::MatchExpressionArm(box super::MatchExpressionArmCause {
191                 arm_span,
192                 source,
193                 ref prior_arms,
194                 last_ty,
195                 scrut_hir_id,
196             }) => tcx.lift(&last_ty).map(|last_ty| {
197                 super::MatchExpressionArm(box super::MatchExpressionArmCause {
198                     arm_span,
199                     source,
200                     prior_arms: prior_arms.clone(),
201                     last_ty,
202                     scrut_hir_id,
203                 })
204             }),
205             super::Pattern { span, root_ty, origin_expr } => {
206                 tcx.lift(&root_ty).map(|root_ty| super::Pattern { span, root_ty, origin_expr })
207             }
208             super::IfExpression(box super::IfExpressionCause { then, outer, semicolon }) => {
209                 Some(super::IfExpression(box super::IfExpressionCause { then, outer, semicolon }))
210             }
211             super::IfExpressionWithNoElse => Some(super::IfExpressionWithNoElse),
212             super::MainFunctionType => Some(super::MainFunctionType),
213             super::StartFunctionType => Some(super::StartFunctionType),
214             super::IntrinsicType => Some(super::IntrinsicType),
215             super::MethodReceiver => Some(super::MethodReceiver),
216             super::BlockTailExpression(id) => Some(super::BlockTailExpression(id)),
217             super::TrivialBound => Some(super::TrivialBound),
218         }
219     }
220 }
221
222 impl<'a, 'tcx> Lift<'tcx> for traits::DerivedObligationCause<'a> {
223     type Lifted = traits::DerivedObligationCause<'tcx>;
224     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
225         tcx.lift(&self.parent_trait_ref).and_then(|trait_ref| {
226             tcx.lift(&*self.parent_code).map(|code| traits::DerivedObligationCause {
227                 parent_trait_ref: trait_ref,
228                 parent_code: Rc::new(code),
229             })
230         })
231     }
232 }
233
234 impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCause<'a> {
235     type Lifted = traits::ObligationCause<'tcx>;
236     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
237         tcx.lift(&self.code).map(|code| traits::ObligationCause::new(self.span, self.body_id, code))
238     }
239 }
240
241 // For codegen only.
242 impl<'a, 'tcx> Lift<'tcx> for traits::ImplSource<'a, ()> {
243     type Lifted = traits::ImplSource<'tcx, ()>;
244     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
245         match self.clone() {
246             traits::ImplSourceUserDefined(traits::ImplSourceUserDefinedData {
247                 impl_def_id,
248                 substs,
249                 nested,
250             }) => tcx.lift(&substs).map(|substs| {
251                 traits::ImplSourceUserDefined(traits::ImplSourceUserDefinedData {
252                     impl_def_id,
253                     substs,
254                     nested,
255                 })
256             }),
257             traits::ImplSourceAutoImpl(t) => Some(traits::ImplSourceAutoImpl(t)),
258             traits::ImplSourceGenerator(traits::ImplSourceGeneratorData {
259                 generator_def_id,
260                 substs,
261                 nested,
262             }) => tcx.lift(&substs).map(|substs| {
263                 traits::ImplSourceGenerator(traits::ImplSourceGeneratorData {
264                     generator_def_id,
265                     substs,
266                     nested,
267                 })
268             }),
269             traits::ImplSourceClosure(traits::ImplSourceClosureData {
270                 closure_def_id,
271                 substs,
272                 nested,
273             }) => tcx.lift(&substs).map(|substs| {
274                 traits::ImplSourceClosure(traits::ImplSourceClosureData {
275                     closure_def_id,
276                     substs,
277                     nested,
278                 })
279             }),
280             traits::ImplSourceFnPointer(traits::ImplSourceFnPointerData { fn_ty, nested }) => {
281                 tcx.lift(&fn_ty).map(|fn_ty| {
282                     traits::ImplSourceFnPointer(traits::ImplSourceFnPointerData { fn_ty, nested })
283                 })
284             }
285             traits::ImplSourceDiscriminantKind(traits::ImplSourceDiscriminantKindData) => {
286                 Some(traits::ImplSourceDiscriminantKind(traits::ImplSourceDiscriminantKindData))
287             }
288             traits::ImplSourceParam(n) => Some(traits::ImplSourceParam(n)),
289             traits::ImplSourceBuiltin(n) => Some(traits::ImplSourceBuiltin(n)),
290             traits::ImplSourceObject(traits::ImplSourceObjectData {
291                 upcast_trait_ref,
292                 vtable_base,
293                 nested,
294             }) => tcx.lift(&upcast_trait_ref).map(|trait_ref| {
295                 traits::ImplSourceObject(traits::ImplSourceObjectData {
296                     upcast_trait_ref: trait_ref,
297                     vtable_base,
298                     nested,
299                 })
300             }),
301             traits::ImplSourceTraitAlias(traits::ImplSourceTraitAliasData {
302                 alias_def_id,
303                 substs,
304                 nested,
305             }) => tcx.lift(&substs).map(|substs| {
306                 traits::ImplSourceTraitAlias(traits::ImplSourceTraitAliasData {
307                     alias_def_id,
308                     substs,
309                     nested,
310                 })
311             }),
312         }
313     }
314 }