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, Subst, Substs, VecPerParamSpace};
14 use middle::typeck::infer::InferCtxt;
16 use std::collections::HashSet;
20 use syntax::codemap::Span;
21 use util::ppaux::Repr;
23 use super::{ErrorReported, Obligation, ObligationCause, VtableImpl,
24 VtableParam, VtableParamData, VtableImplData};
26 ///////////////////////////////////////////////////////////////////////////
27 // Supertrait iterator
29 pub struct Supertraits<'cx, 'tcx:'cx> {
30 tcx: &'cx ty::ctxt<'tcx>,
31 stack: Vec<SupertraitEntry>,
32 visited: HashSet<Rc<ty::TraitRef>>,
35 struct SupertraitEntry {
37 supertraits: Vec<Rc<ty::TraitRef>>,
40 pub fn supertraits<'cx, 'tcx>(tcx: &'cx ty::ctxt<'tcx>,
41 trait_ref: Rc<ty::TraitRef>)
42 -> Supertraits<'cx, 'tcx>
45 * Returns an iterator over the trait reference `T` and all of its
46 * supertrait references. May contain duplicates. In general
47 * the ordering is not defined.
53 * trait Bar : Foo { ... }
54 * trait Baz : Bar+Foo { ... }
57 * `supertraits(Baz)` yields `[Baz, Bar, Foo, Foo]` in some order.
60 transitive_bounds(tcx, [trait_ref])
63 pub fn transitive_bounds<'cx, 'tcx>(tcx: &'cx ty::ctxt<'tcx>,
64 bounds: &[Rc<ty::TraitRef>])
65 -> Supertraits<'cx, 'tcx>
67 let bounds = Vec::from_fn(bounds.len(), |i| bounds[i].clone());
69 let visited: HashSet<Rc<ty::TraitRef>> =
71 .map(|b| (*b).clone())
74 let entry = SupertraitEntry { position: 0, supertraits: bounds };
75 Supertraits { tcx: tcx, stack: vec![entry], visited: visited }
78 impl<'cx, 'tcx> Supertraits<'cx, 'tcx> {
79 fn push(&mut self, trait_ref: &ty::TraitRef) {
80 let ty::ParamBounds { builtin_bounds, mut trait_bounds, .. } =
81 ty::bounds_for_trait_ref(self.tcx, trait_ref);
82 for builtin_bound in builtin_bounds.iter() {
83 let bound_trait_ref = trait_ref_for_builtin_bound(self.tcx,
86 bound_trait_ref.map(|trait_ref| trait_bounds.push(trait_ref));
89 // Only keep those bounds that we haven't already seen. This
90 // is necessary to prevent infinite recursion in some cases.
91 // One common case is when people define `trait Sized { }`
92 // rather than `trait Sized for Sized? { }`.
93 trait_bounds.retain(|r| self.visited.insert((*r).clone()));
95 let entry = SupertraitEntry { position: 0, supertraits: trait_bounds };
96 self.stack.push(entry);
99 pub fn indices(&self) -> Vec<uint> {
101 * Returns the path taken through the trait supertraits to
102 * reach the current point.
105 self.stack.iter().map(|e| e.position).collect()
109 impl<'cx, 'tcx> Iterator<Rc<ty::TraitRef>> for Supertraits<'cx, 'tcx> {
110 fn next(&mut self) -> Option<Rc<ty::TraitRef>> {
112 // Extract next item from top-most stack frame, if any.
113 let next_trait = match self.stack.mut_last() {
115 // No more stack frames. Done.
119 let p = entry.position;
120 if p < entry.supertraits.len() {
121 // Still more supertraits left in the top stack frame.
125 (*entry.supertraits.get(p)).clone();
134 Some(next_trait) => {
135 self.push(&*next_trait);
136 return Some(next_trait);
140 // Top stack frame is exhausted, pop it.
148 // determine the `self` type, using fresh variables for all variables
149 // declared on the impl declaration e.g., `impl<A,B> for ~[(A,B)]`
150 // would return ($0, $1) where $0 and $1 are freshly instantiated type
152 pub fn fresh_substs_for_impl(infcx: &InferCtxt,
154 impl_def_id: ast::DefId)
158 let impl_generics = ty::lookup_item_type(tcx, impl_def_id).generics;
159 infcx.fresh_substs_for_generics(span, &impl_generics)
162 impl<N> fmt::Show for VtableImplData<N> {
163 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
164 write!(f, "VtableImpl({})", self.impl_def_id)
168 impl fmt::Show for VtableParamData {
169 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
170 write!(f, "VtableParam(...)")
174 pub fn obligations_for_generics(tcx: &ty::ctxt,
175 cause: ObligationCause,
176 recursion_depth: uint,
177 generics: &ty::Generics,
179 -> VecPerParamSpace<Obligation>
181 /*! See `super::obligations_for_generics` */
183 debug!("obligations_for_generics(generics={}, substs={})",
184 generics.repr(tcx), substs.repr(tcx));
186 let mut obligations = VecPerParamSpace::empty();
188 for def in generics.types.iter() {
189 push_obligations_for_param_bounds(tcx,
199 debug!("obligations() ==> {}", obligations.repr(tcx));
204 fn push_obligations_for_param_bounds(
206 cause: ObligationCause,
207 recursion_depth: uint,
208 space: subst::ParamSpace,
210 param_bounds: &ty::ParamBounds,
211 param_substs: &Substs,
212 obligations: &mut VecPerParamSpace<Obligation>)
214 let param_ty = *param_substs.types.get(space, index);
216 for builtin_bound in param_bounds.builtin_bounds.iter() {
217 let obligation = obligation_for_builtin_bound(tcx,
223 Ok(ob) => obligations.push(space, ob),
228 for bound_trait_ref in param_bounds.trait_bounds.iter() {
229 let bound_trait_ref = bound_trait_ref.subst(tcx, param_substs);
232 Obligation { cause: cause,
233 recursion_depth: recursion_depth,
234 trait_ref: bound_trait_ref });
238 pub fn trait_ref_for_builtin_bound(
240 builtin_bound: ty::BuiltinBound,
242 -> Option<Rc<ty::TraitRef>>
244 match tcx.lang_items.from_builtin_kind(builtin_bound) {
246 Some(Rc::new(ty::TraitRef {
248 substs: Substs::empty().with_self_ty(param_ty)
252 tcx.sess.err(e.as_slice());
258 pub fn obligation_for_builtin_bound(
260 cause: ObligationCause,
261 builtin_bound: ty::BuiltinBound,
262 recursion_depth: uint,
264 -> Result<Obligation, ErrorReported>
266 let trait_ref = trait_ref_for_builtin_bound(tcx, builtin_bound, param_ty);
268 Some(trait_ref) => Ok(Obligation {
270 recursion_depth: recursion_depth,
273 None => Err(ErrorReported)
277 pub fn search_trait_and_supertraits_from_bound(tcx: &ty::ctxt,
278 caller_bound: Rc<ty::TraitRef>,
279 test: |ast::DefId| -> bool)
280 -> Option<VtableParamData>
283 * Starting from a caller obligation `caller_bound` (which has
284 * coordinates `space`/`i` in the list of caller obligations),
285 * search through the trait and supertraits to find one where
286 * `test(d)` is true, where `d` is the def-id of the
287 * trait/supertrait. If any is found, return `Some(p)` where `p`
288 * is the path to that trait/supertrait. Else `None`.
291 for bound in transitive_bounds(tcx, &[caller_bound]) {
292 if test(bound.def_id) {
293 let vtable_param = VtableParamData { bound: bound };
294 return Some(vtable_param);
301 impl Repr for super::Obligation {
302 fn repr(&self, tcx: &ty::ctxt) -> String {
303 format!("Obligation(trait_ref={},depth={})",
304 self.trait_ref.repr(tcx),
305 self.recursion_depth)
309 impl<N:Repr> Repr for super::Vtable<N> {
310 fn repr(&self, tcx: &ty::ctxt) -> String {
312 super::VtableImpl(ref v) =>
315 super::VtableUnboxedClosure(ref d) =>
316 format!("VtableUnboxedClosure({})",
319 super::VtableParam(ref v) =>
320 format!("VtableParam({})", v.repr(tcx)),
322 super::VtableBuiltin =>
328 impl<N:Repr> Repr for super::VtableImplData<N> {
329 fn repr(&self, tcx: &ty::ctxt) -> String {
330 format!("VtableImpl(impl_def_id={}, substs={}, nested={})",
331 self.impl_def_id.repr(tcx),
332 self.substs.repr(tcx),
333 self.nested.repr(tcx))
337 impl Repr for super::VtableParamData {
338 fn repr(&self, tcx: &ty::ctxt) -> String {
339 format!("VtableParam(bound={})",
340 self.bound.repr(tcx))
344 impl Repr for super::SelectionError {
345 fn repr(&self, tcx: &ty::ctxt) -> String {
347 super::Unimplemented =>
348 format!("Unimplemented"),
353 super::OutputTypeParameterMismatch(ref t, ref e) =>
354 format!("OutputTypeParameterMismatch({}, {})",
361 impl Repr for super::FulfillmentError {
362 fn repr(&self, tcx: &ty::ctxt) -> String {
363 format!("FulfillmentError({},{})",
364 self.obligation.repr(tcx),
369 impl Repr for super::FulfillmentErrorCode {
370 fn repr(&self, tcx: &ty::ctxt) -> String {
372 super::CodeSelectionError(ref o) => o.repr(tcx),
373 super::CodeAmbiguity => format!("Ambiguity")
378 impl fmt::Show for super::FulfillmentErrorCode {
379 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
381 super::CodeSelectionError(ref e) => write!(f, "{}", e),
382 super::CodeAmbiguity => write!(f, "Ambiguity")
387 impl Repr for ty::type_err {
388 fn repr(&self, tcx: &ty::ctxt) -> String {
389 ty::type_err_to_str(tcx, self)