2 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
3 // file at the top-level directory of this distribution and at
4 // http://rust-lang.org/COPYRIGHT.
6 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9 // option. This file may not be copied, modified, or distributed
10 // except according to those terms.
13 use middle::subst::{ParamSpace, Substs, VecPerParamSpace};
14 use middle::typeck::infer::InferCtxt;
15 use middle::ty::{mod, Ty};
16 use std::collections::HashSet;
20 use syntax::codemap::Span;
21 use util::common::ErrorReported;
22 use util::ppaux::Repr;
24 use super::{Obligation, ObligationCause, VtableImpl,
25 VtableParam, VtableParamData, VtableImplData};
27 ///////////////////////////////////////////////////////////////////////////
28 // Supertrait iterator
30 pub struct Supertraits<'cx, 'tcx:'cx> {
31 tcx: &'cx ty::ctxt<'tcx>,
32 stack: Vec<SupertraitEntry<'tcx>>,
33 visited: HashSet<Rc<ty::TraitRef<'tcx>>>,
36 struct SupertraitEntry<'tcx> {
38 supertraits: Vec<Rc<ty::TraitRef<'tcx>>>,
41 pub fn supertraits<'cx, 'tcx>(tcx: &'cx ty::ctxt<'tcx>,
42 trait_ref: Rc<ty::TraitRef<'tcx>>)
43 -> Supertraits<'cx, 'tcx>
45 /// Returns an iterator over the trait reference `T` and all of its supertrait references. May
46 /// contain duplicates. In general the ordering is not defined.
52 /// trait Bar : Foo { ... }
53 /// trait Baz : Bar+Foo { ... }
56 /// `supertraits(Baz)` yields `[Baz, Bar, Foo, Foo]` in some order.
57 transitive_bounds(tcx, &[trait_ref])
60 pub fn transitive_bounds<'cx, 'tcx>(tcx: &'cx ty::ctxt<'tcx>,
61 bounds: &[Rc<ty::TraitRef<'tcx>>])
62 -> Supertraits<'cx, 'tcx>
64 let bounds = Vec::from_fn(bounds.len(), |i| bounds[i].clone());
66 let visited: HashSet<Rc<ty::TraitRef>> =
68 .map(|b| (*b).clone())
71 let entry = SupertraitEntry { position: 0, supertraits: bounds };
72 Supertraits { tcx: tcx, stack: vec![entry], visited: visited }
75 impl<'cx, 'tcx> Supertraits<'cx, 'tcx> {
76 fn push(&mut self, trait_ref: &ty::TraitRef<'tcx>) {
77 let ty::ParamBounds { builtin_bounds, mut trait_bounds, .. } =
78 ty::bounds_for_trait_ref(self.tcx, trait_ref);
79 for builtin_bound in builtin_bounds.iter() {
80 let bound_trait_ref = trait_ref_for_builtin_bound(self.tcx,
83 bound_trait_ref.map(|trait_ref| trait_bounds.push(trait_ref));
86 // Only keep those bounds that we haven't already seen. This
87 // is necessary to prevent infinite recursion in some cases.
88 // One common case is when people define `trait Sized { }`
89 // rather than `trait Sized for Sized? { }`.
90 trait_bounds.retain(|r| self.visited.insert((*r).clone()));
92 let entry = SupertraitEntry { position: 0, supertraits: trait_bounds };
93 self.stack.push(entry);
96 /// Returns the path taken through the trait supertraits to reach the current point.
97 pub fn indices(&self) -> Vec<uint> {
98 self.stack.iter().map(|e| e.position).collect()
102 impl<'cx, 'tcx> Iterator<Rc<ty::TraitRef<'tcx>>> for Supertraits<'cx, 'tcx> {
103 fn next(&mut self) -> Option<Rc<ty::TraitRef<'tcx>>> {
105 // Extract next item from top-most stack frame, if any.
106 let next_trait = match self.stack.last_mut() {
108 // No more stack frames. Done.
112 let p = entry.position;
113 if p < entry.supertraits.len() {
114 // Still more supertraits left in the top stack frame.
117 let next_trait = entry.supertraits[p].clone();
126 Some(next_trait) => {
127 self.push(&*next_trait);
128 return Some(next_trait);
132 // Top stack frame is exhausted, pop it.
140 // determine the `self` type, using fresh variables for all variables
141 // declared on the impl declaration e.g., `impl<A,B> for ~[(A,B)]`
142 // would return ($0, $1) where $0 and $1 are freshly instantiated type
144 pub fn fresh_substs_for_impl<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
146 impl_def_id: ast::DefId)
150 let impl_generics = ty::lookup_item_type(tcx, impl_def_id).generics;
151 infcx.fresh_substs_for_generics(span, &impl_generics)
154 impl<'tcx, N> fmt::Show for VtableImplData<'tcx, N> {
155 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
156 write!(f, "VtableImpl({})", self.impl_def_id)
160 impl<'tcx> fmt::Show for VtableParamData<'tcx> {
161 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
162 write!(f, "VtableParam(...)")
166 /// See `super::obligations_for_generics`
167 pub fn obligations_for_generics<'tcx>(tcx: &ty::ctxt<'tcx>,
168 cause: ObligationCause<'tcx>,
169 recursion_depth: uint,
170 generic_bounds: &ty::GenericBounds<'tcx>,
171 type_substs: &VecPerParamSpace<Ty<'tcx>>)
172 -> VecPerParamSpace<Obligation<'tcx>>
175 debug!("obligations_for_generics(generic_bounds={}, type_substs={})",
176 generic_bounds.repr(tcx), type_substs.repr(tcx));
178 let mut obligations = VecPerParamSpace::empty();
180 for (space, index, bounds) in generic_bounds.types.iter_enumerated() {
181 push_obligations_for_param_bounds(tcx,
191 debug!("obligations() ==> {}", obligations.repr(tcx));
196 fn push_obligations_for_param_bounds<'tcx>(
197 tcx: &ty::ctxt<'tcx>,
198 cause: ObligationCause<'tcx>,
199 recursion_depth: uint,
200 space: subst::ParamSpace,
202 param_bounds: &ty::ParamBounds<'tcx>,
203 param_type_substs: &VecPerParamSpace<Ty<'tcx>>,
204 obligations: &mut VecPerParamSpace<Obligation<'tcx>>)
206 let param_ty = *param_type_substs.get(space, index);
207 for builtin_bound in param_bounds.builtin_bounds.iter() {
208 let obligation = obligation_for_builtin_bound(tcx,
214 Ok(ob) => obligations.push(space, ob),
219 for bound_trait_ref in param_bounds.trait_bounds.iter() {
222 Obligation { cause: cause,
223 recursion_depth: recursion_depth,
224 trait_ref: (*bound_trait_ref).clone() });
228 pub fn trait_ref_for_builtin_bound<'tcx>(
229 tcx: &ty::ctxt<'tcx>,
230 builtin_bound: ty::BuiltinBound,
232 -> Option<Rc<ty::TraitRef<'tcx>>>
234 match tcx.lang_items.from_builtin_kind(builtin_bound) {
236 Some(Rc::new(ty::TraitRef {
238 substs: Substs::empty().with_self_ty(param_ty)
242 tcx.sess.err(e.as_slice());
248 pub fn obligation_for_builtin_bound<'tcx>(
249 tcx: &ty::ctxt<'tcx>,
250 cause: ObligationCause<'tcx>,
251 builtin_bound: ty::BuiltinBound,
252 recursion_depth: uint,
254 -> Result<Obligation<'tcx>, ErrorReported>
256 let trait_ref = trait_ref_for_builtin_bound(tcx, builtin_bound, param_ty);
258 Some(trait_ref) => Ok(Obligation {
260 recursion_depth: recursion_depth,
263 None => Err(ErrorReported)
267 /// Starting from a caller obligation `caller_bound` (which has coordinates `space`/`i` in the list
268 /// of caller obligations), search through the trait and supertraits to find one where `test(d)` is
269 /// true, where `d` is the def-id of the trait/supertrait. If any is found, return `Some(p)` where
270 /// `p` is the path to that trait/supertrait. Else `None`.
271 pub fn search_trait_and_supertraits_from_bound<'tcx>(tcx: &ty::ctxt<'tcx>,
272 caller_bound: Rc<ty::TraitRef<'tcx>>,
273 test: |ast::DefId| -> bool)
274 -> Option<VtableParamData<'tcx>>
276 for bound in transitive_bounds(tcx, &[caller_bound]) {
277 if test(bound.def_id) {
278 let vtable_param = VtableParamData { bound: bound };
279 return Some(vtable_param);
286 impl<'tcx> Repr<'tcx> for super::Obligation<'tcx> {
287 fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
288 format!("Obligation(trait_ref={},depth={})",
289 self.trait_ref.repr(tcx),
290 self.recursion_depth)
294 impl<'tcx, N:Repr<'tcx>> Repr<'tcx> for super::Vtable<'tcx, N> {
295 fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
297 super::VtableImpl(ref v) =>
300 super::VtableUnboxedClosure(ref d, ref s) =>
301 format!("VtableUnboxedClosure({},{})",
305 super::VtableParam(ref v) =>
306 format!("VtableParam({})", v.repr(tcx)),
308 super::VtableBuiltin(ref d) =>
314 impl<'tcx, N:Repr<'tcx>> Repr<'tcx> for super::VtableImplData<'tcx, N> {
315 fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
316 format!("VtableImpl(impl_def_id={}, substs={}, nested={})",
317 self.impl_def_id.repr(tcx),
318 self.substs.repr(tcx),
319 self.nested.repr(tcx))
323 impl<'tcx, N:Repr<'tcx>> Repr<'tcx> for super::VtableBuiltinData<N> {
324 fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
325 format!("VtableBuiltin(nested={})",
326 self.nested.repr(tcx))
330 impl<'tcx> Repr<'tcx> for super::VtableParamData<'tcx> {
331 fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
332 format!("VtableParam(bound={})",
333 self.bound.repr(tcx))
337 impl<'tcx> Repr<'tcx> for super::SelectionError<'tcx> {
338 fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
343 super::Unimplemented =>
344 format!("Unimplemented"),
346 super::OutputTypeParameterMismatch(ref t, ref e) =>
347 format!("OutputTypeParameterMismatch({}, {})",
354 impl<'tcx> Repr<'tcx> for super::FulfillmentError<'tcx> {
355 fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
356 format!("FulfillmentError({},{})",
357 self.obligation.repr(tcx),
362 impl<'tcx> Repr<'tcx> for super::FulfillmentErrorCode<'tcx> {
363 fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
365 super::CodeSelectionError(ref o) => o.repr(tcx),
366 super::CodeAmbiguity => format!("Ambiguity")
371 impl<'tcx> fmt::Show for super::FulfillmentErrorCode<'tcx> {
372 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
374 super::CodeSelectionError(ref e) => write!(f, "{}", e),
375 super::CodeAmbiguity => write!(f, "Ambiguity")
380 impl<'tcx> Repr<'tcx> for ty::type_err<'tcx> {
381 fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
382 ty::type_err_to_str(tcx, self)