]> git.lizzy.rs Git - rust.git/blob - src/librustc_middle/traits/structural_impls.rs
Use `ty::Instance::resolve` to identify `'static` bound source
[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::UnifyReceiver(ref ctxt) => tcx.lift(ctxt).map(|ctxt| super::UnifyReceiver(ctxt)),
217             super::BlockTailExpression(id) => Some(super::BlockTailExpression(id)),
218             super::TrivialBound => Some(super::TrivialBound),
219         }
220     }
221 }
222
223 impl<'a, 'tcx> Lift<'tcx> for traits::UnifyReceiverContext<'a> {
224     type Lifted = traits::UnifyReceiverContext<'tcx>;
225     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
226         tcx.lift(&self.param_env).and_then(|param_env| {
227             tcx.lift(&self.substs).map(|substs| traits::UnifyReceiverContext {
228                 assoc_item: self.assoc_item,
229                 param_env,
230                 substs,
231             })
232         })
233     }
234 }
235
236 impl<'a, 'tcx> Lift<'tcx> for traits::DerivedObligationCause<'a> {
237     type Lifted = traits::DerivedObligationCause<'tcx>;
238     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
239         tcx.lift(&self.parent_trait_ref).and_then(|trait_ref| {
240             tcx.lift(&*self.parent_code).map(|code| traits::DerivedObligationCause {
241                 parent_trait_ref: trait_ref,
242                 parent_code: Rc::new(code),
243             })
244         })
245     }
246 }
247
248 impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCause<'a> {
249     type Lifted = traits::ObligationCause<'tcx>;
250     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
251         tcx.lift(&self.code).map(|code| traits::ObligationCause::new(self.span, self.body_id, code))
252     }
253 }
254
255 // For codegen only.
256 impl<'a, 'tcx> Lift<'tcx> for traits::ImplSource<'a, ()> {
257     type Lifted = traits::ImplSource<'tcx, ()>;
258     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
259         match self.clone() {
260             traits::ImplSourceUserDefined(traits::ImplSourceUserDefinedData {
261                 impl_def_id,
262                 substs,
263                 nested,
264             }) => tcx.lift(&substs).map(|substs| {
265                 traits::ImplSourceUserDefined(traits::ImplSourceUserDefinedData {
266                     impl_def_id,
267                     substs,
268                     nested,
269                 })
270             }),
271             traits::ImplSourceAutoImpl(t) => Some(traits::ImplSourceAutoImpl(t)),
272             traits::ImplSourceGenerator(traits::ImplSourceGeneratorData {
273                 generator_def_id,
274                 substs,
275                 nested,
276             }) => tcx.lift(&substs).map(|substs| {
277                 traits::ImplSourceGenerator(traits::ImplSourceGeneratorData {
278                     generator_def_id,
279                     substs,
280                     nested,
281                 })
282             }),
283             traits::ImplSourceClosure(traits::ImplSourceClosureData {
284                 closure_def_id,
285                 substs,
286                 nested,
287             }) => tcx.lift(&substs).map(|substs| {
288                 traits::ImplSourceClosure(traits::ImplSourceClosureData {
289                     closure_def_id,
290                     substs,
291                     nested,
292                 })
293             }),
294             traits::ImplSourceFnPointer(traits::ImplSourceFnPointerData { fn_ty, nested }) => {
295                 tcx.lift(&fn_ty).map(|fn_ty| {
296                     traits::ImplSourceFnPointer(traits::ImplSourceFnPointerData { fn_ty, nested })
297                 })
298             }
299             traits::ImplSourceDiscriminantKind(traits::ImplSourceDiscriminantKindData) => {
300                 Some(traits::ImplSourceDiscriminantKind(traits::ImplSourceDiscriminantKindData))
301             }
302             traits::ImplSourceParam(n) => Some(traits::ImplSourceParam(n)),
303             traits::ImplSourceBuiltin(n) => Some(traits::ImplSourceBuiltin(n)),
304             traits::ImplSourceObject(traits::ImplSourceObjectData {
305                 upcast_trait_ref,
306                 vtable_base,
307                 nested,
308             }) => tcx.lift(&upcast_trait_ref).map(|trait_ref| {
309                 traits::ImplSourceObject(traits::ImplSourceObjectData {
310                     upcast_trait_ref: trait_ref,
311                     vtable_base,
312                     nested,
313                 })
314             }),
315             traits::ImplSourceTraitAlias(traits::ImplSourceTraitAliasData {
316                 alias_def_id,
317                 substs,
318                 nested,
319             }) => tcx.lift(&substs).map(|substs| {
320                 traits::ImplSourceTraitAlias(traits::ImplSourceTraitAliasData {
321                     alias_def_id,
322                     substs,
323                     nested,
324                 })
325             }),
326         }
327     }
328 }