1 // Copyright 2012 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.
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.
13 use middle::subst::{VecPerParamSpace,Subst};
15 use middle::ty::{BoundRegion, BrAnon, BrNamed};
16 use middle::ty::{ReEarlyBound, BrFresh, ctxt};
17 use middle::ty::{ReFree, ReScope, ReInfer, ReStatic, Region, ReEmpty};
18 use middle::ty::{ReSkolemized, ReVar};
19 use middle::ty::{mt, t, ParamTy};
20 use middle::ty::{ty_bool, ty_char, ty_bot, ty_box, ty_struct, ty_enum};
21 use middle::ty::{ty_err, ty_str, ty_vec, ty_float, ty_bare_fn, ty_closure};
22 use middle::ty::{ty_nil, ty_param, ty_ptr, ty_rptr, ty_tup, ty_open};
23 use middle::ty::{ty_unboxed_closure};
24 use middle::ty::{ty_uniq, ty_trait, ty_int, ty_uint, ty_infer};
27 use middle::typeck::check::regionmanip;
28 use middle::typeck::infer;
33 use syntax::codemap::{Span, Pos};
34 use syntax::parse::token;
35 use syntax::print::pprust;
36 use syntax::{ast, ast_util};
37 use syntax::owned_slice::OwnedSlice;
39 /// Produces a string suitable for debugging output.
41 fn repr(&self, tcx: &ctxt) -> String;
44 /// Produces a string suitable for showing to the user.
45 pub trait UserString {
46 fn user_string(&self, tcx: &ctxt) -> String;
49 pub fn note_and_explain_region(cx: &ctxt,
53 match explain_region_and_span(cx, region) {
54 (ref str, Some(span)) => {
57 format!("{}{}{}", prefix, *str, suffix).as_slice());
61 format!("{}{}{}", prefix, *str, suffix).as_slice());
66 fn item_scope_tag(item: &ast::Item) -> &'static str {
68 * When a free region is associated with `item`, how should we describe
69 * the item in the error message.
73 ast::ItemImpl(..) => "impl",
74 ast::ItemStruct(..) => "struct",
75 ast::ItemEnum(..) => "enum",
76 ast::ItemTrait(..) => "trait",
77 ast::ItemFn(..) => "function body",
82 pub fn explain_region_and_span(cx: &ctxt, region: ty::Region)
83 -> (String, Option<Span>) {
86 match cx.map.find(node_id) {
87 Some(ast_map::NodeBlock(ref blk)) => {
88 explain_span(cx, "block", blk.span)
90 Some(ast_map::NodeExpr(expr)) => {
92 ast::ExprCall(..) => explain_span(cx, "call", expr.span),
93 ast::ExprMethodCall(..) => {
94 explain_span(cx, "method call", expr.span)
96 ast::ExprMatch(..) => explain_span(cx, "match", expr.span),
97 _ => explain_span(cx, "expression", expr.span)
100 Some(ast_map::NodeStmt(stmt)) => {
101 explain_span(cx, "statement", stmt.span)
103 Some(ast_map::NodeItem(it)) => {
104 let tag = item_scope_tag(&*it);
105 explain_span(cx, tag, it.span)
108 // this really should not happen
109 (format!("unknown scope: {}. Please report a bug.", node_id), None)
115 let prefix = match fr.bound_region {
117 format!("the anonymous lifetime #{} defined on", idx + 1)
119 BrFresh(_) => "an anonymous lifetime defined on".to_string(),
121 format!("the lifetime {} as defined on",
122 bound_region_ptr_to_string(cx, fr.bound_region))
126 match cx.map.find(fr.scope_id) {
127 Some(ast_map::NodeBlock(ref blk)) => {
128 let (msg, opt_span) = explain_span(cx, "block", blk.span);
129 (format!("{} {}", prefix, msg), opt_span)
131 Some(ast_map::NodeItem(it)) => {
132 let tag = item_scope_tag(&*it);
133 let (msg, opt_span) = explain_span(cx, tag, it.span);
134 (format!("{} {}", prefix, msg), opt_span)
137 // this really should not happen
138 (format!("{} node {}", prefix, fr.scope_id), None)
143 ReStatic => { ("the static lifetime".to_string(), None) }
145 ReEmpty => { ("the empty lifetime".to_string(), None) }
147 ReEarlyBound(_, _, _, name) => {
148 (format!("{}", token::get_name(name)), None)
151 // I believe these cases should not occur (except when debugging,
153 ty::ReInfer(_) | ty::ReLateBound(..) => {
154 (format!("lifetime {:?}", region), None)
158 fn explain_span(cx: &ctxt, heading: &str, span: Span)
159 -> (String, Option<Span>) {
160 let lo = cx.sess.codemap().lookup_char_pos_adj(span.lo);
161 (format!("the {} at {}:{}", heading, lo.line, lo.col.to_uint()),
166 pub fn bound_region_ptr_to_string(cx: &ctxt, br: BoundRegion) -> String {
167 bound_region_to_string(cx, "", false, br)
170 pub fn bound_region_to_string(cx: &ctxt,
171 prefix: &str, space: bool,
172 br: BoundRegion) -> String {
173 let space_str = if space { " " } else { "" };
175 if cx.sess.verbose() {
176 return format!("{}{}{}", prefix, br.repr(cx), space_str)
180 BrNamed(_, name) => {
181 format!("{}{}{}", prefix, token::get_name(name), space_str)
183 BrAnon(_) => prefix.to_string(),
184 BrFresh(_) => prefix.to_string(),
188 // In general, if you are giving a region error message,
189 // you should use `explain_region()` or, better yet,
190 // `note_and_explain_region()`
191 pub fn region_ptr_to_string(cx: &ctxt, region: Region) -> String {
192 region_to_string(cx, "&", true, region)
195 pub fn region_to_string(cx: &ctxt, prefix: &str, space: bool, region: Region) -> String {
196 let space_str = if space { " " } else { "" };
198 if cx.sess.verbose() {
199 return format!("{}{}{}", prefix, region.repr(cx), space_str)
202 // These printouts are concise. They do not contain all the information
203 // the user might want to diagnose an error, but there is basically no way
204 // to fit that into a short string. Hence the recommendation to use
205 // `explain_region()` or `note_and_explain_region()`.
207 ty::ReScope(_) => prefix.to_string(),
208 ty::ReEarlyBound(_, _, _, name) => {
209 token::get_name(name).get().to_string()
211 ty::ReLateBound(_, br) => bound_region_to_string(cx, prefix, space, br),
212 ty::ReFree(ref fr) => bound_region_to_string(cx, prefix, space, fr.bound_region),
213 ty::ReInfer(ReSkolemized(_, br)) => {
214 bound_region_to_string(cx, prefix, space, br)
216 ty::ReInfer(ReVar(_)) => prefix.to_string(),
217 ty::ReStatic => format!("{}'static{}", prefix, space_str),
218 ty::ReEmpty => format!("{}'<empty>{}", prefix, space_str),
222 pub fn mutability_to_string(m: ast::Mutability) -> String {
224 ast::MutMutable => "mut ".to_string(),
225 ast::MutImmutable => "".to_string(),
229 pub fn mt_to_string(cx: &ctxt, m: &mt) -> String {
230 format!("{}{}", mutability_to_string(m.mutbl), ty_to_string(cx, m.ty))
233 pub fn trait_store_to_string(cx: &ctxt, s: ty::TraitStore) -> String {
235 ty::UniqTraitStore => "Box ".to_string(),
236 ty::RegionTraitStore(r, m) => {
237 format!("{}{}", region_ptr_to_string(cx, r), mutability_to_string(m))
242 pub fn vec_map_to_string<T>(ts: &[T], f: |t: &T| -> String) -> String {
243 let tstrs = ts.iter().map(f).collect::<Vec<String>>();
244 format!("[{}]", tstrs.connect(", "))
247 pub fn fn_sig_to_string(cx: &ctxt, typ: &ty::FnSig) -> String {
248 format!("fn{}{} -> {}", typ.binder_id, typ.inputs.repr(cx),
252 pub fn trait_ref_to_string(cx: &ctxt, trait_ref: &ty::TraitRef) -> String {
253 trait_ref.user_string(cx).to_string()
256 pub fn ty_to_string(cx: &ctxt, typ: t) -> String {
257 fn fn_input_to_string(cx: &ctxt, input: ty::t) -> String {
258 ty_to_string(cx, input).to_string()
260 fn bare_fn_to_string(cx: &ctxt,
261 fn_style: ast::FnStyle,
263 ident: Option<ast::Ident>,
266 let mut s = String::new();
270 s.push_str(fn_style.to_string().as_slice());
275 if abi != abi::Rust {
276 s.push_str(format!("extern {} ", abi.to_string()).as_slice());
284 s.push_str(token::get_ident(i).get());
289 push_sig_to_string(cx, &mut s, '(', ')', sig, "");
294 fn closure_to_string(cx: &ctxt, cty: &ty::ClosureTy) -> String {
295 let mut s = String::new();
298 ty::UniqTraitStore => {}
299 ty::RegionTraitStore(region, _) => {
300 s.push_str(region_to_string(cx, "", true, region).as_slice());
307 s.push_str(cty.fn_style.to_string().as_slice());
312 let bounds_str = cty.bounds.user_string(cx);
315 ty::UniqTraitStore => {
316 assert_eq!(cty.onceness, ast::Once);
318 push_sig_to_string(cx, &mut s, '(', ')', &cty.sig,
319 bounds_str.as_slice());
321 ty::RegionTraitStore(..) => {
324 ast::Once => s.push_str("once ")
326 push_sig_to_string(cx, &mut s, '|', '|', &cty.sig,
327 bounds_str.as_slice());
334 fn push_sig_to_string(cx: &ctxt,
341 let strs: Vec<String> = sig.inputs.iter().map(|a| fn_input_to_string(cx, *a)).collect();
342 s.push_str(strs.connect(", ").as_slice());
348 if !bounds.is_empty() {
353 if ty::get(sig.output).sty != ty_nil {
355 if ty::type_is_bot(sig.output) {
358 s.push_str(ty_to_string(cx, sig.output).as_slice());
363 // if there is an id, print that instead of the structural type:
364 /*for def_id in ty::type_def_id(typ).iter() {
365 // note that this typedef cannot have type parameters
366 return ty::item_path_str(cx, *def_id);
369 // pretty print the structural type representation:
370 return match ty::get(typ).sty {
371 ty_nil => "()".to_string(),
372 ty_bot => "!".to_string(),
373 ty_bool => "bool".to_string(),
374 ty_char => "char".to_string(),
375 ty_int(t) => ast_util::int_ty_to_string(t, None).to_string(),
376 ty_uint(t) => ast_util::uint_ty_to_string(t, None).to_string(),
377 ty_float(t) => ast_util::float_ty_to_string(t).to_string(),
378 ty_box(typ) => format!("Gc<{}>", ty_to_string(cx, typ)),
379 ty_uniq(typ) => format!("Box<{}>", ty_to_string(cx, typ)),
381 format!("*{} {}", match tm.mutbl {
382 ast::MutMutable => "mut",
383 ast::MutImmutable => "const",
384 }, ty_to_string(cx, tm.ty))
386 ty_rptr(r, ref tm) => {
387 let mut buf = region_ptr_to_string(cx, r);
388 buf.push_str(mt_to_string(cx, tm).as_slice());
391 ty_open(typ) => format!("opened<{}>", ty_to_string(cx, typ)),
392 ty_tup(ref elems) => {
393 let strs: Vec<String> = elems.iter().map(|elem| ty_to_string(cx, *elem)).collect();
394 format!("({})", strs.connect(","))
396 ty_closure(ref f) => {
397 closure_to_string(cx, &**f)
399 ty_bare_fn(ref f) => {
400 bare_fn_to_string(cx, f.fn_style, f.abi, None, &f.sig)
402 ty_infer(infer_ty) => infer_ty.to_string(),
403 ty_err => "[type error]".to_string(),
404 ty_param(ref param_ty) => {
407 ty_enum(did, ref substs) | ty_struct(did, ref substs) => {
408 let base = ty::item_path_str(cx, did);
409 let generics = ty::lookup_item_type(cx, did).generics;
410 parameterized(cx, base.as_slice(), substs, &generics)
412 ty_trait(box ty::TyTrait {
413 def_id: did, ref substs, ref bounds
415 let base = ty::item_path_str(cx, did);
416 let trait_def = ty::lookup_trait_def(cx, did);
417 let ty = parameterized(cx, base.as_slice(),
418 substs, &trait_def.generics);
419 let bound_str = bounds.user_string(cx);
420 let bound_sep = if bound_str.is_empty() { "" } else { "+" };
426 ty_str => "str".to_string(),
427 ty_unboxed_closure(..) => "closure".to_string(),
431 format!("[{}, .. {}]", ty_to_string(cx, t), n)
433 None => format!("[{}]", ty_to_string(cx, t)),
439 pub fn explicit_self_category_to_str(category: &ty::ExplicitSelfCategory)
442 ty::StaticExplicitSelfCategory => "static",
443 ty::ByValueExplicitSelfCategory => "self",
444 ty::ByReferenceExplicitSelfCategory(_, ast::MutMutable) => {
447 ty::ByReferenceExplicitSelfCategory(_, ast::MutImmutable) => "&self",
448 ty::ByBoxExplicitSelfCategory => "Box<self>",
452 pub fn parameterized(cx: &ctxt,
454 substs: &subst::Substs,
455 generics: &ty::Generics)
458 let mut strs = Vec::new();
460 match substs.regions {
461 subst::ErasedRegions => { }
462 subst::NonerasedRegions(ref regions) => {
463 for &r in regions.iter() {
464 let s = region_to_string(cx, "", false, r);
468 // This happens when the value of the region
469 // parameter is not easily serialized. This may be
470 // because the user omitted it in the first place,
471 // or because it refers to some block in the code,
472 // etc. I'm not sure how best to serialize this.
473 strs.push(format!("'_"));
479 let tps = substs.types.get_slice(subst::TypeSpace);
480 let ty_params = generics.types.get_slice(subst::TypeSpace);
481 let has_defaults = ty_params.last().map_or(false, |def| def.default.is_some());
482 let num_defaults = if has_defaults && !cx.sess.verbose() {
483 ty_params.iter().zip(tps.iter()).rev().take_while(|&(def, &actual)| {
485 Some(default) => default.subst(cx, substs) == actual,
493 for t in tps.slice_to(tps.len() - num_defaults).iter() {
494 strs.push(ty_to_string(cx, *t))
497 if cx.sess.verbose() {
498 for t in substs.types.get_slice(subst::SelfSpace).iter() {
499 strs.push(format!("for {}", t.repr(cx)));
504 format!("{}<{}>", base, strs.connect(","))
510 pub fn ty_to_short_str(cx: &ctxt, typ: t) -> String {
511 let mut s = typ.repr(cx).to_string();
513 s = s.as_slice().slice(0u, 32u).to_string();
518 impl<T:Repr> Repr for Option<T> {
519 fn repr(&self, tcx: &ctxt) -> String {
521 &None => "None".to_string(),
522 &Some(ref t) => t.repr(tcx),
527 impl<T:Repr,U:Repr> Repr for Result<T,U> {
528 fn repr(&self, tcx: &ctxt) -> String {
530 &Ok(ref t) => t.repr(tcx),
531 &Err(ref u) => format!("Err({})", u.repr(tcx))
537 fn repr(&self, _tcx: &ctxt) -> String {
542 impl<T:Repr> Repr for Rc<T> {
543 fn repr(&self, tcx: &ctxt) -> String {
548 impl<'a, T:Repr> Repr for &'a T {
549 fn repr(&self, tcx: &ctxt) -> String {
554 impl<T:Repr> Repr for Box<T> {
555 fn repr(&self, tcx: &ctxt) -> String {
560 fn repr_vec<T:Repr>(tcx: &ctxt, v: &[T]) -> String {
561 vec_map_to_string(v, |t| t.repr(tcx))
564 impl<'a, T:Repr> Repr for &'a [T] {
565 fn repr(&self, tcx: &ctxt) -> String {
570 impl<T:Repr> Repr for OwnedSlice<T> {
571 fn repr(&self, tcx: &ctxt) -> String {
572 repr_vec(tcx, self.as_slice())
576 // This is necessary to handle types like Option<~[T]>, for which
577 // autoderef cannot convert the &[T] handler
578 impl<T:Repr> Repr for Vec<T> {
579 fn repr(&self, tcx: &ctxt) -> String {
580 repr_vec(tcx, self.as_slice())
584 impl<T:UserString> UserString for Vec<T> {
585 fn user_string(&self, tcx: &ctxt) -> String {
586 let strs: Vec<String> =
587 self.iter().map(|t| t.user_string(tcx)).collect();
592 impl Repr for def::Def {
593 fn repr(&self, _tcx: &ctxt) -> String {
594 format!("{:?}", *self)
598 impl Repr for ty::TypeParameterDef {
599 fn repr(&self, tcx: &ctxt) -> String {
600 format!("TypeParameterDef({}, {})",
601 self.def_id.repr(tcx),
602 self.bounds.repr(tcx))
606 impl Repr for ty::RegionParameterDef {
607 fn repr(&self, tcx: &ctxt) -> String {
608 format!("RegionParameterDef(name={}, def_id={}, bounds={})",
609 token::get_name(self.name),
610 self.def_id.repr(tcx),
611 self.bounds.repr(tcx))
615 impl Repr for ty::t {
616 fn repr(&self, tcx: &ctxt) -> String {
617 ty_to_string(tcx, *self)
621 impl Repr for ty::mt {
622 fn repr(&self, tcx: &ctxt) -> String {
623 mt_to_string(tcx, self)
627 impl Repr for subst::Substs {
628 fn repr(&self, tcx: &ctxt) -> String {
629 format!("Substs[types={}, regions={}]",
630 self.types.repr(tcx),
631 self.regions.repr(tcx))
635 impl<T:Repr> Repr for subst::VecPerParamSpace<T> {
636 fn repr(&self, tcx: &ctxt) -> String {
637 format!("[{};{};{}]",
638 self.get_slice(subst::TypeSpace).repr(tcx),
639 self.get_slice(subst::SelfSpace).repr(tcx),
640 self.get_slice(subst::FnSpace).repr(tcx))
644 impl Repr for ty::ItemSubsts {
645 fn repr(&self, tcx: &ctxt) -> String {
646 format!("ItemSubsts({})", self.substs.repr(tcx))
650 impl Repr for subst::RegionSubsts {
651 fn repr(&self, tcx: &ctxt) -> String {
653 subst::ErasedRegions => "erased".to_string(),
654 subst::NonerasedRegions(ref regions) => regions.repr(tcx)
659 impl Repr for ty::BuiltinBounds {
660 fn repr(&self, _tcx: &ctxt) -> String {
661 let mut res = Vec::new();
662 for b in self.iter() {
664 ty::BoundSend => "Send".to_owned(),
665 ty::BoundSized => "Sized".to_owned(),
666 ty::BoundCopy => "Copy".to_owned(),
667 ty::BoundSync => "Sync".to_owned(),
674 impl Repr for ty::ExistentialBounds {
675 fn repr(&self, tcx: &ctxt) -> String {
676 self.user_string(tcx)
680 impl Repr for ty::ParamBounds {
681 fn repr(&self, tcx: &ctxt) -> String {
682 let mut res = Vec::new();
683 res.push(self.builtin_bounds.repr(tcx));
684 for t in self.trait_bounds.iter() {
685 res.push(t.repr(tcx));
691 impl Repr for ty::TraitRef {
692 fn repr(&self, tcx: &ctxt) -> String {
693 trait_ref_to_string(tcx, self)
697 impl Repr for ty::TraitDef {
698 fn repr(&self, tcx: &ctxt) -> String {
699 format!("TraitDef(generics={}, bounds={}, trait_ref={})",
700 self.generics.repr(tcx),
701 self.bounds.repr(tcx),
702 self.trait_ref.repr(tcx))
706 impl Repr for ast::Expr {
707 fn repr(&self, _tcx: &ctxt) -> String {
708 format!("expr({}: {})", self.id, pprust::expr_to_string(self))
712 impl Repr for ast::Path {
713 fn repr(&self, _tcx: &ctxt) -> String {
714 format!("path({})", pprust::path_to_string(self))
718 impl UserString for ast::Path {
719 fn user_string(&self, _tcx: &ctxt) -> String {
720 pprust::path_to_string(self)
724 impl Repr for ast::Item {
725 fn repr(&self, tcx: &ctxt) -> String {
726 format!("item({})", tcx.map.node_to_string(self.id))
730 impl Repr for ast::Lifetime {
731 fn repr(&self, _tcx: &ctxt) -> String {
732 format!("lifetime({}: {})", self.id, pprust::lifetime_to_string(self))
736 impl Repr for ast::Stmt {
737 fn repr(&self, _tcx: &ctxt) -> String {
738 format!("stmt({}: {})",
739 ast_util::stmt_id(self),
740 pprust::stmt_to_string(self))
744 impl Repr for ast::Pat {
745 fn repr(&self, _tcx: &ctxt) -> String {
746 format!("pat({}: {})", self.id, pprust::pat_to_string(self))
750 impl Repr for ty::BoundRegion {
751 fn repr(&self, tcx: &ctxt) -> String {
753 ty::BrAnon(id) => format!("BrAnon({})", id),
754 ty::BrNamed(id, name) => {
755 format!("BrNamed({}, {})", id.repr(tcx), token::get_name(name))
757 ty::BrFresh(id) => format!("BrFresh({})", id),
762 impl Repr for ty::Region {
763 fn repr(&self, tcx: &ctxt) -> String {
765 ty::ReEarlyBound(id, space, index, name) => {
766 format!("ReEarlyBound({}, {}, {}, {})",
770 token::get_name(name))
773 ty::ReLateBound(binder_id, ref bound_region) => {
774 format!("ReLateBound({}, {})",
776 bound_region.repr(tcx))
779 ty::ReFree(ref fr) => fr.repr(tcx),
782 format!("ReScope({})", id)
786 "ReStatic".to_string()
789 ty::ReInfer(ReVar(ref vid)) => {
790 format!("ReInfer({})", vid.index)
793 ty::ReInfer(ReSkolemized(id, ref bound_region)) => {
794 format!("re_skolemized({}, {})", id, bound_region.repr(tcx))
798 "ReEmpty".to_string()
804 impl UserString for ty::Region {
805 fn user_string(&self, tcx: &ctxt) -> String {
806 region_to_string(tcx, "", false, *self)
810 impl Repr for ty::FreeRegion {
811 fn repr(&self, tcx: &ctxt) -> String {
812 format!("ReFree({}, {})",
814 self.bound_region.repr(tcx))
818 impl Repr for ast::DefId {
819 fn repr(&self, tcx: &ctxt) -> String {
820 // Unfortunately, there seems to be no way to attempt to print
821 // a path for a def-id, so I'll just make a best effort for now
822 // and otherwise fallback to just printing the crate/node pair
823 if self.krate == ast::LOCAL_CRATE {
824 match tcx.map.find(self.node) {
825 Some(ast_map::NodeItem(..)) |
826 Some(ast_map::NodeForeignItem(..)) |
827 Some(ast_map::NodeImplItem(..)) |
828 Some(ast_map::NodeTraitItem(..)) |
829 Some(ast_map::NodeVariant(..)) |
830 Some(ast_map::NodeStructCtor(..)) => {
834 ty::item_path_str(tcx, *self))
839 return format!("{:?}", *self)
843 impl Repr for ty::Polytype {
844 fn repr(&self, tcx: &ctxt) -> String {
845 format!("Polytype {{generics: {}, ty: {}}}",
846 self.generics.repr(tcx),
851 impl Repr for ty::Generics {
852 fn repr(&self, tcx: &ctxt) -> String {
853 format!("Generics(types: {}, regions: {})",
854 self.types.repr(tcx),
855 self.regions.repr(tcx))
859 impl Repr for ty::ItemVariances {
860 fn repr(&self, tcx: &ctxt) -> String {
861 format!("ItemVariances(types={}, \
863 self.types.repr(tcx),
864 self.regions.repr(tcx))
868 impl Repr for ty::Variance {
869 fn repr(&self, _: &ctxt) -> String {
870 // The first `.to_string()` returns a &'static str (it is not an implementation
871 // of the ToString trait). Because of that, we need to call `.to_string()` again
872 // if we want to have a `String`.
873 self.to_string().to_string()
877 impl Repr for ty::Method {
878 fn repr(&self, tcx: &ctxt) -> String {
879 format!("method(ident: {}, generics: {}, fty: {}, \
880 explicit_self: {}, vis: {}, def_id: {})",
881 self.ident.repr(tcx),
882 self.generics.repr(tcx),
884 self.explicit_self.repr(tcx),
886 self.def_id.repr(tcx))
890 impl Repr for ast::Name {
891 fn repr(&self, _tcx: &ctxt) -> String {
892 token::get_name(*self).get().to_string()
896 impl UserString for ast::Name {
897 fn user_string(&self, _tcx: &ctxt) -> String {
898 token::get_name(*self).get().to_string()
902 impl Repr for ast::Ident {
903 fn repr(&self, _tcx: &ctxt) -> String {
904 token::get_ident(*self).get().to_string()
908 impl Repr for ast::ExplicitSelf_ {
909 fn repr(&self, _tcx: &ctxt) -> String {
910 format!("{:?}", *self)
914 impl Repr for ast::Visibility {
915 fn repr(&self, _tcx: &ctxt) -> String {
916 format!("{:?}", *self)
920 impl Repr for ty::BareFnTy {
921 fn repr(&self, tcx: &ctxt) -> String {
922 format!("BareFnTy {{fn_style: {:?}, abi: {}, sig: {}}}",
924 self.abi.to_string(),
929 impl Repr for ty::FnSig {
930 fn repr(&self, tcx: &ctxt) -> String {
931 fn_sig_to_string(tcx, self)
935 impl Repr for typeck::MethodCallee {
936 fn repr(&self, tcx: &ctxt) -> String {
937 format!("MethodCallee {{origin: {}, ty: {}, {}}}",
938 self.origin.repr(tcx),
940 self.substs.repr(tcx))
944 impl Repr for typeck::MethodOrigin {
945 fn repr(&self, tcx: &ctxt) -> String {
947 &typeck::MethodStatic(def_id) => {
948 format!("MethodStatic({})", def_id.repr(tcx))
950 &typeck::MethodStaticUnboxedClosure(def_id) => {
951 format!("MethodStaticUnboxedClosure({})", def_id.repr(tcx))
953 &typeck::MethodParam(ref p) => {
956 &typeck::MethodObject(ref p) => {
963 impl Repr for typeck::MethodParam {
964 fn repr(&self, tcx: &ctxt) -> String {
965 format!("MethodParam({},{:?},{:?},{:?})",
966 self.trait_id.repr(tcx),
973 impl Repr for typeck::MethodObject {
974 fn repr(&self, tcx: &ctxt) -> String {
975 format!("MethodObject({},{:?},{:?})",
976 self.trait_id.repr(tcx),
982 impl Repr for ty::TraitStore {
983 fn repr(&self, tcx: &ctxt) -> String {
984 trait_store_to_string(tcx, *self)
988 impl Repr for ty::BuiltinBound {
989 fn repr(&self, _tcx: &ctxt) -> String {
990 format!("{:?}", *self)
994 impl UserString for ty::BuiltinBound {
995 fn user_string(&self, _tcx: &ctxt) -> String {
997 ty::BoundSend => "Send".to_owned(),
998 ty::BoundSized => "Sized".to_owned(),
999 ty::BoundCopy => "Copy".to_owned(),
1000 ty::BoundSync => "Sync".to_owned(),
1005 impl Repr for Span {
1006 fn repr(&self, tcx: &ctxt) -> String {
1007 tcx.sess.codemap().span_to_string(*self).to_string()
1011 impl<A:UserString> UserString for Rc<A> {
1012 fn user_string(&self, tcx: &ctxt) -> String {
1013 let this: &A = &**self;
1014 this.user_string(tcx)
1018 impl UserString for ty::ParamBounds {
1019 fn user_string(&self, tcx: &ctxt) -> String {
1020 let mut result = Vec::new();
1021 let s = self.builtin_bounds.user_string(tcx);
1025 for n in self.trait_bounds.iter() {
1026 result.push(n.user_string(tcx));
1032 impl UserString for ty::ExistentialBounds {
1033 fn user_string(&self, tcx: &ctxt) -> String {
1034 if self.builtin_bounds.contains_elem(ty::BoundSend) &&
1035 self.region_bound == ty::ReStatic
1036 { // Region bound is implied by builtin bounds:
1037 return self.builtin_bounds.repr(tcx);
1040 let mut res = Vec::new();
1042 let region_str = self.region_bound.user_string(tcx);
1043 if !region_str.is_empty() {
1044 res.push(region_str);
1047 for bound in self.builtin_bounds.iter() {
1048 res.push(bound.user_string(tcx));
1055 impl UserString for ty::BuiltinBounds {
1056 fn user_string(&self, tcx: &ctxt) -> String {
1058 .map(|bb| bb.user_string(tcx))
1059 .collect::<Vec<String>>()
1065 impl UserString for ty::TraitRef {
1066 fn user_string(&self, tcx: &ctxt) -> String {
1067 let base = ty::item_path_str(tcx, self.def_id);
1068 let trait_def = ty::lookup_trait_def(tcx, self.def_id);
1069 parameterized(tcx, base.as_slice(), &self.substs, &trait_def.generics)
1073 impl UserString for ty::t {
1074 fn user_string(&self, tcx: &ctxt) -> String {
1075 ty_to_string(tcx, *self)
1079 impl UserString for ast::Ident {
1080 fn user_string(&self, _tcx: &ctxt) -> String {
1081 token::get_name(self.name).get().to_string()
1085 impl Repr for abi::Abi {
1086 fn repr(&self, _tcx: &ctxt) -> String {
1091 impl UserString for abi::Abi {
1092 fn user_string(&self, _tcx: &ctxt) -> String {
1097 impl Repr for ty::UpvarId {
1098 fn repr(&self, tcx: &ctxt) -> String {
1099 format!("UpvarId({};`{}`;{})",
1101 ty::local_var_name_str(tcx, self.var_id),
1102 self.closure_expr_id)
1106 impl Repr for ast::Mutability {
1107 fn repr(&self, _tcx: &ctxt) -> String {
1108 format!("{:?}", *self)
1112 impl Repr for ty::BorrowKind {
1113 fn repr(&self, _tcx: &ctxt) -> String {
1114 format!("{:?}", *self)
1118 impl Repr for ty::UpvarBorrow {
1119 fn repr(&self, tcx: &ctxt) -> String {
1120 format!("UpvarBorrow({}, {})",
1121 self.kind.repr(tcx),
1122 self.region.repr(tcx))
1126 impl Repr for ty::IntVid {
1127 fn repr(&self, _tcx: &ctxt) -> String {
1132 impl Repr for ty::FloatVid {
1133 fn repr(&self, _tcx: &ctxt) -> String {
1138 impl Repr for ty::RegionVid {
1139 fn repr(&self, _tcx: &ctxt) -> String {
1144 impl Repr for ty::TyVid {
1145 fn repr(&self, _tcx: &ctxt) -> String {
1150 impl Repr for ty::IntVarValue {
1151 fn repr(&self, _tcx: &ctxt) -> String {
1152 format!("{:?}", *self)
1156 impl Repr for ast::IntTy {
1157 fn repr(&self, _tcx: &ctxt) -> String {
1158 format!("{:?}", *self)
1162 impl Repr for ast::UintTy {
1163 fn repr(&self, _tcx: &ctxt) -> String {
1164 format!("{:?}", *self)
1168 impl Repr for ast::FloatTy {
1169 fn repr(&self, _tcx: &ctxt) -> String {
1170 format!("{:?}", *self)
1174 impl<T:Repr> Repr for infer::Bounds<T> {
1175 fn repr(&self, tcx: &ctxt) -> String {
1176 format!("({} <= {})",
1182 impl Repr for ty::ExplicitSelfCategory {
1183 fn repr(&self, _: &ctxt) -> String {
1184 explicit_self_category_to_str(self).to_string()
1189 impl Repr for regionmanip::WfConstraint {
1190 fn repr(&self, tcx: &ctxt) -> String {
1192 regionmanip::RegionSubRegionConstraint(_, r_a, r_b) => {
1193 format!("RegionSubRegionConstraint({}, {})",
1198 regionmanip::RegionSubParamConstraint(_, r, p) => {
1199 format!("RegionSubParamConstraint({}, {})",
1207 impl UserString for ParamTy {
1208 fn user_string(&self, tcx: &ctxt) -> String {
1210 let did = self.def_id;
1211 let ident = match tcx.ty_param_defs.borrow().find(&did.node) {
1212 Some(def) => token::get_ident(def.ident).get().to_string(),
1214 // This can only happen when a type mismatch error happens and
1215 // the actual type has more type parameters than the expected one.
1216 None => format!("<generic #{}>", id),
1222 impl Repr for ParamTy {
1223 fn repr(&self, tcx: &ctxt) -> String {
1224 self.user_string(tcx)
1228 impl<A:Repr,B:Repr> Repr for (A,B) {
1229 fn repr(&self, tcx: &ctxt) -> String {
1230 let &(ref a, ref b) = self;
1231 format!("({},{})", a.repr(tcx), b.repr(tcx))