]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/traits/structural_impls.rs
Rollup merge of #31061 - brson:bib, r=steveklabnik
[rust.git] / src / librustc / middle / traits / structural_impls.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use middle::traits;
12 use middle::traits::project::Normalized;
13 use middle::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
14
15 use std::fmt;
16
17 // structural impls for the structs in middle::traits
18
19 impl<'tcx, T: fmt::Debug> fmt::Debug for Normalized<'tcx, T> {
20     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21         write!(f, "Normalized({:?},{:?})",
22                self.value,
23                self.obligations)
24     }
25 }
26
27 impl<'tcx> fmt::Debug for traits::RegionObligation<'tcx> {
28     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29         write!(f, "RegionObligation(sub_region={:?}, sup_type={:?})",
30                self.sub_region,
31                self.sup_type)
32     }
33 }
34 impl<'tcx, O: fmt::Debug> fmt::Debug for traits::Obligation<'tcx, O> {
35     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36         write!(f, "Obligation(predicate={:?},depth={})",
37                self.predicate,
38                self.recursion_depth)
39     }
40 }
41
42 impl<'tcx, N: fmt::Debug> fmt::Debug for traits::Vtable<'tcx, N> {
43     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44         match *self {
45             super::VtableImpl(ref v) =>
46                 write!(f, "{:?}", v),
47
48             super::VtableDefaultImpl(ref t) =>
49                 write!(f, "{:?}", t),
50
51             super::VtableClosure(ref d) =>
52                 write!(f, "{:?}", d),
53
54             super::VtableFnPointer(ref d) =>
55                 write!(f, "VtableFnPointer({:?})", d),
56
57             super::VtableObject(ref d) =>
58                 write!(f, "{:?}", d),
59
60             super::VtableParam(ref n) =>
61                 write!(f, "VtableParam({:?})", n),
62
63             super::VtableBuiltin(ref d) =>
64                 write!(f, "{:?}", d)
65         }
66     }
67 }
68
69 impl<'tcx, N: fmt::Debug> fmt::Debug for traits::VtableImplData<'tcx, N> {
70     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71         write!(f, "VtableImpl(impl_def_id={:?}, substs={:?}, nested={:?})",
72                self.impl_def_id,
73                self.substs,
74                self.nested)
75     }
76 }
77
78 impl<'tcx, N: fmt::Debug> fmt::Debug for traits::VtableClosureData<'tcx, N> {
79     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80         write!(f, "VtableClosure(closure_def_id={:?}, substs={:?}, nested={:?})",
81                self.closure_def_id,
82                self.substs,
83                self.nested)
84     }
85 }
86
87 impl<'tcx, N: fmt::Debug> fmt::Debug for traits::VtableBuiltinData<N> {
88     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89         write!(f, "VtableBuiltin(nested={:?})", self.nested)
90     }
91 }
92
93 impl<'tcx, N: fmt::Debug> fmt::Debug for traits::VtableDefaultImplData<N> {
94     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95         write!(f, "VtableDefaultImplData(trait_def_id={:?}, nested={:?})",
96                self.trait_def_id,
97                self.nested)
98     }
99 }
100
101 impl<'tcx> fmt::Debug for traits::VtableObjectData<'tcx> {
102     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
103         write!(f, "VtableObject(upcast={:?}, vtable_base={})",
104                self.upcast_trait_ref,
105                self.vtable_base)
106     }
107 }
108
109 impl<'tcx> fmt::Debug for traits::FulfillmentError<'tcx> {
110     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
111         write!(f, "FulfillmentError({:?},{:?})",
112                self.obligation,
113                self.code)
114     }
115 }
116
117 impl<'tcx> fmt::Debug for traits::FulfillmentErrorCode<'tcx> {
118     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
119         match *self {
120             super::CodeSelectionError(ref e) => write!(f, "{:?}", e),
121             super::CodeProjectionError(ref e) => write!(f, "{:?}", e),
122             super::CodeAmbiguity => write!(f, "Ambiguity")
123         }
124     }
125 }
126
127 impl<'tcx> fmt::Debug for traits::MismatchedProjectionTypes<'tcx> {
128     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
129         write!(f, "MismatchedProjectionTypes({:?})", self.err)
130     }
131 }
132
133 impl<'tcx, O: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::Obligation<'tcx, O>
134 {
135     fn super_fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
136         traits::Obligation {
137             cause: self.cause.clone(),
138             recursion_depth: self.recursion_depth,
139             predicate: self.predicate.fold_with(folder),
140         }
141     }
142
143     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
144         self.predicate.visit_with(visitor)
145     }
146 }
147
148 impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::VtableImplData<'tcx, N> {
149     fn super_fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
150         traits::VtableImplData {
151             impl_def_id: self.impl_def_id,
152             substs: self.substs.fold_with(folder),
153             nested: self.nested.fold_with(folder),
154         }
155     }
156
157     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
158         self.substs.visit_with(visitor) || self.nested.visit_with(visitor)
159     }
160 }
161
162 impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::VtableClosureData<'tcx, N> {
163     fn super_fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
164         traits::VtableClosureData {
165             closure_def_id: self.closure_def_id,
166             substs: self.substs.fold_with(folder),
167             nested: self.nested.fold_with(folder),
168         }
169     }
170
171     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
172         self.substs.visit_with(visitor) || self.nested.visit_with(visitor)
173     }
174 }
175
176 impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::VtableDefaultImplData<N> {
177     fn super_fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
178         traits::VtableDefaultImplData {
179             trait_def_id: self.trait_def_id,
180             nested: self.nested.fold_with(folder),
181         }
182     }
183
184     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
185         self.nested.visit_with(visitor)
186     }
187 }
188
189 impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::VtableBuiltinData<N> {
190     fn super_fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
191         traits::VtableBuiltinData {
192             nested: self.nested.fold_with(folder),
193         }
194     }
195
196     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
197         self.nested.visit_with(visitor)
198     }
199 }
200
201 impl<'tcx> TypeFoldable<'tcx> for traits::VtableObjectData<'tcx> {
202     fn super_fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
203         traits::VtableObjectData {
204             upcast_trait_ref: self.upcast_trait_ref.fold_with(folder),
205             vtable_base: self.vtable_base
206         }
207     }
208
209     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
210         self.upcast_trait_ref.visit_with(visitor)
211     }
212 }
213
214 impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::Vtable<'tcx, N> {
215     fn super_fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
216         match *self {
217             traits::VtableImpl(ref v) => traits::VtableImpl(v.fold_with(folder)),
218             traits::VtableDefaultImpl(ref t) => traits::VtableDefaultImpl(t.fold_with(folder)),
219             traits::VtableClosure(ref d) => {
220                 traits::VtableClosure(d.fold_with(folder))
221             }
222             traits::VtableFnPointer(ref d) => {
223                 traits::VtableFnPointer(d.fold_with(folder))
224             }
225             traits::VtableParam(ref n) => traits::VtableParam(n.fold_with(folder)),
226             traits::VtableBuiltin(ref d) => traits::VtableBuiltin(d.fold_with(folder)),
227             traits::VtableObject(ref d) => traits::VtableObject(d.fold_with(folder)),
228         }
229     }
230
231     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
232         match *self {
233             traits::VtableImpl(ref v) => v.visit_with(visitor),
234             traits::VtableDefaultImpl(ref t) => t.visit_with(visitor),
235             traits::VtableClosure(ref d) => d.visit_with(visitor),
236             traits::VtableFnPointer(ref d) => d.visit_with(visitor),
237             traits::VtableParam(ref n) => n.visit_with(visitor),
238             traits::VtableBuiltin(ref d) => d.visit_with(visitor),
239             traits::VtableObject(ref d) => d.visit_with(visitor),
240         }
241     }
242 }
243
244 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Normalized<'tcx, T> {
245     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
246         Normalized {
247             value: self.value.fold_with(folder),
248             obligations: self.obligations.fold_with(folder),
249         }
250     }
251
252     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
253         self.value.visit_with(visitor) || self.obligations.visit_with(visitor)
254     }
255 }