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;
32 use syntax::codemap::{Span, Pos};
33 use syntax::parse::token;
34 use syntax::print::pprust;
35 use syntax::{ast, ast_util};
36 use syntax::owned_slice::OwnedSlice;
38 /// Produces a string suitable for debugging output.
40 fn repr(&self, tcx: &ctxt) -> String;
43 /// Produces a string suitable for showing to the user.
44 pub trait UserString {
45 fn user_string(&self, tcx: &ctxt) -> String;
48 pub fn note_and_explain_region(cx: &ctxt,
52 match explain_region_and_span(cx, region) {
53 (ref str, Some(span)) => {
56 format!("{}{}{}", prefix, *str, suffix).as_slice());
60 format!("{}{}{}", prefix, *str, suffix).as_slice());
65 fn item_scope_tag(item: &ast::Item) -> &'static str {
67 * When a free region is associated with `item`, how should we describe
68 * the item in the error message.
72 ast::ItemImpl(..) => "impl",
73 ast::ItemStruct(..) => "struct",
74 ast::ItemEnum(..) => "enum",
75 ast::ItemTrait(..) => "trait",
76 ast::ItemFn(..) => "function body",
81 pub fn explain_region_and_span(cx: &ctxt, region: ty::Region)
82 -> (String, Option<Span>) {
85 match cx.map.find(node_id) {
86 Some(ast_map::NodeBlock(ref blk)) => {
87 explain_span(cx, "block", blk.span)
89 Some(ast_map::NodeExpr(expr)) => {
91 ast::ExprCall(..) => explain_span(cx, "call", expr.span),
92 ast::ExprMethodCall(..) => {
93 explain_span(cx, "method call", expr.span)
95 ast::ExprMatch(..) => explain_span(cx, "match", expr.span),
96 _ => explain_span(cx, "expression", expr.span)
99 Some(ast_map::NodeStmt(stmt)) => {
100 explain_span(cx, "statement", stmt.span)
102 Some(ast_map::NodeItem(it)) => {
103 let tag = item_scope_tag(&*it);
104 explain_span(cx, tag, it.span)
107 // this really should not happen
108 (format!("unknown scope: {}. Please report a bug.", node_id), None)
114 let prefix = match fr.bound_region {
116 format!("the anonymous lifetime #{} defined on", idx + 1)
118 BrFresh(_) => "an anonymous lifetime defined on".to_string(),
120 format!("the lifetime {} as defined on",
121 bound_region_ptr_to_string(cx, fr.bound_region))
125 match cx.map.find(fr.scope_id) {
126 Some(ast_map::NodeBlock(ref blk)) => {
127 let (msg, opt_span) = explain_span(cx, "block", blk.span);
128 (format!("{} {}", prefix, msg), opt_span)
130 Some(ast_map::NodeItem(it)) => {
131 let tag = item_scope_tag(&*it);
132 let (msg, opt_span) = explain_span(cx, tag, it.span);
133 (format!("{} {}", prefix, msg), opt_span)
136 // this really should not happen
137 (format!("{} node {}", prefix, fr.scope_id), None)
142 ReStatic => { ("the static lifetime".to_string(), None) }
144 ReEmpty => { ("the empty lifetime".to_string(), None) }
146 ReEarlyBound(_, _, _, name) => {
147 (format!("{}", token::get_name(name)), None)
150 // I believe these cases should not occur (except when debugging,
152 ty::ReInfer(_) | ty::ReLateBound(..) => {
153 (format!("lifetime {:?}", region), None)
157 fn explain_span(cx: &ctxt, heading: &str, span: Span)
158 -> (String, Option<Span>) {
159 let lo = cx.sess.codemap().lookup_char_pos_adj(span.lo);
160 (format!("the {} at {}:{}", heading, lo.line, lo.col.to_uint()),
165 pub fn bound_region_ptr_to_string(cx: &ctxt, br: BoundRegion) -> String {
166 bound_region_to_string(cx, "", false, br)
169 pub fn bound_region_to_string(cx: &ctxt,
170 prefix: &str, space: bool,
171 br: BoundRegion) -> String {
172 let space_str = if space { " " } else { "" };
174 if cx.sess.verbose() {
175 return format!("{}{}{}", prefix, br.repr(cx), space_str)
179 BrNamed(_, name) => {
180 format!("{}{}{}", prefix, token::get_name(name), space_str)
182 BrAnon(_) => prefix.to_string(),
183 BrFresh(_) => prefix.to_string(),
187 // In general, if you are giving a region error message,
188 // you should use `explain_region()` or, better yet,
189 // `note_and_explain_region()`
190 pub fn region_ptr_to_string(cx: &ctxt, region: Region) -> String {
191 region_to_string(cx, "&", true, region)
194 pub fn region_to_string(cx: &ctxt, prefix: &str, space: bool, region: Region) -> String {
195 let space_str = if space { " " } else { "" };
197 if cx.sess.verbose() {
198 return format!("{}{}{}", prefix, region.repr(cx), space_str)
201 // These printouts are concise. They do not contain all the information
202 // the user might want to diagnose an error, but there is basically no way
203 // to fit that into a short string. Hence the recommendation to use
204 // `explain_region()` or `note_and_explain_region()`.
206 ty::ReScope(_) => prefix.to_string(),
207 ty::ReEarlyBound(_, _, _, name) => {
208 token::get_name(name).get().to_string()
210 ty::ReLateBound(_, br) => bound_region_to_string(cx, prefix, space, br),
211 ty::ReFree(ref fr) => bound_region_to_string(cx, prefix, space, fr.bound_region),
212 ty::ReInfer(ReSkolemized(_, br)) => {
213 bound_region_to_string(cx, prefix, space, br)
215 ty::ReInfer(ReVar(_)) => prefix.to_string(),
216 ty::ReStatic => format!("{}'static{}", prefix, space_str),
217 ty::ReEmpty => format!("{}'<empty>{}", prefix, space_str),
221 pub fn mutability_to_string(m: ast::Mutability) -> String {
223 ast::MutMutable => "mut ".to_string(),
224 ast::MutImmutable => "".to_string(),
228 pub fn mt_to_string(cx: &ctxt, m: &mt) -> String {
229 format!("{}{}", mutability_to_string(m.mutbl), ty_to_string(cx, m.ty))
232 pub fn trait_store_to_string(cx: &ctxt, s: ty::TraitStore) -> String {
234 ty::UniqTraitStore => "Box ".to_string(),
235 ty::RegionTraitStore(r, m) => {
236 format!("{}{}", region_ptr_to_string(cx, r), mutability_to_string(m))
241 pub fn vec_map_to_string<T>(ts: &[T], f: |t: &T| -> String) -> String {
242 let tstrs = ts.iter().map(f).collect::<Vec<String>>();
243 format!("[{}]", tstrs.connect(", "))
246 pub fn fn_sig_to_string(cx: &ctxt, typ: &ty::FnSig) -> String {
247 format!("fn{}{} -> {}", typ.binder_id, typ.inputs.repr(cx),
251 pub fn trait_ref_to_string(cx: &ctxt, trait_ref: &ty::TraitRef) -> String {
252 trait_ref.user_string(cx).to_string()
255 pub fn ty_to_string(cx: &ctxt, typ: t) -> String {
256 fn fn_input_to_string(cx: &ctxt, input: ty::t) -> String {
257 ty_to_string(cx, input).to_string()
259 fn bare_fn_to_string(cx: &ctxt,
260 fn_style: ast::FnStyle,
262 ident: Option<ast::Ident>,
265 let mut s = String::new();
269 s.push_str(fn_style.to_string().as_slice());
274 if abi != abi::Rust {
275 s.push_str(format!("extern {} ", abi.to_string()).as_slice());
283 s.push_str(token::get_ident(i).get());
288 push_sig_to_string(cx, &mut s, '(', ')', sig, "");
293 fn closure_to_string(cx: &ctxt, cty: &ty::ClosureTy) -> String {
294 let mut s = String::new();
297 ty::UniqTraitStore => {}
298 ty::RegionTraitStore(region, _) => {
299 s.push_str(region_to_string(cx, "", true, region).as_slice());
306 s.push_str(cty.fn_style.to_string().as_slice());
311 let bounds_str = cty.bounds.user_string(cx);
314 ty::UniqTraitStore => {
315 assert_eq!(cty.onceness, ast::Once);
317 push_sig_to_string(cx, &mut s, '(', ')', &cty.sig,
318 bounds_str.as_slice());
320 ty::RegionTraitStore(..) => {
323 ast::Once => s.push_str("once ")
325 push_sig_to_string(cx, &mut s, '|', '|', &cty.sig,
326 bounds_str.as_slice());
333 fn push_sig_to_string(cx: &ctxt,
340 let strs: Vec<String> = sig.inputs.iter().map(|a| fn_input_to_string(cx, *a)).collect();
341 s.push_str(strs.connect(", ").as_slice());
347 if !bounds.is_empty() {
352 if ty::get(sig.output).sty != ty_nil {
354 if ty::type_is_bot(sig.output) {
357 s.push_str(ty_to_string(cx, sig.output).as_slice());
362 // if there is an id, print that instead of the structural type:
363 /*for def_id in ty::type_def_id(typ).iter() {
364 // note that this typedef cannot have type parameters
365 return ty::item_path_str(cx, *def_id);
368 // pretty print the structural type representation:
369 return match ty::get(typ).sty {
370 ty_nil => "()".to_string(),
371 ty_bot => "!".to_string(),
372 ty_bool => "bool".to_string(),
373 ty_char => "char".to_string(),
374 ty_int(t) => ast_util::int_ty_to_string(t, None).to_string(),
375 ty_uint(t) => ast_util::uint_ty_to_string(t, None).to_string(),
376 ty_float(t) => ast_util::float_ty_to_string(t).to_string(),
377 ty_box(typ) => format!("Gc<{}>", ty_to_string(cx, typ)),
378 ty_uniq(typ) => format!("Box<{}>", ty_to_string(cx, typ)),
380 format!("*{} {}", match tm.mutbl {
381 ast::MutMutable => "mut",
382 ast::MutImmutable => "const",
383 }, ty_to_string(cx, tm.ty))
385 ty_rptr(r, ref tm) => {
386 let mut buf = region_ptr_to_string(cx, r);
387 buf.push_str(mt_to_string(cx, tm).as_slice());
390 ty_open(typ) => format!("opened<{}>", ty_to_string(cx, typ)),
391 ty_tup(ref elems) => {
392 let strs: Vec<String> = elems.iter().map(|elem| ty_to_string(cx, *elem)).collect();
393 format!("({})", strs.connect(","))
395 ty_closure(ref f) => {
396 closure_to_string(cx, &**f)
398 ty_bare_fn(ref f) => {
399 bare_fn_to_string(cx, f.fn_style, f.abi, None, &f.sig)
401 ty_infer(infer_ty) => infer_ty.to_string(),
402 ty_err => "[type error]".to_string(),
403 ty_param(ref param_ty) => {
406 ty_enum(did, ref substs) | ty_struct(did, ref substs) => {
407 let base = ty::item_path_str(cx, did);
408 let generics = ty::lookup_item_type(cx, did).generics;
409 parameterized(cx, base.as_slice(), substs, &generics)
411 ty_trait(box ty::TyTrait {
412 def_id: did, ref substs, ref bounds
414 let base = ty::item_path_str(cx, did);
415 let trait_def = ty::lookup_trait_def(cx, did);
416 let ty = parameterized(cx, base.as_slice(),
417 substs, &trait_def.generics);
418 let bound_str = bounds.user_string(cx);
419 let bound_sep = if bound_str.is_empty() { "" } else { "+" };
425 ty_str => "str".to_string(),
426 ty_unboxed_closure(..) => "closure".to_string(),
430 format!("[{}, .. {}]", ty_to_string(cx, t), n)
432 None => format!("[{}]", ty_to_string(cx, t)),
438 pub fn explicit_self_category_to_str(category: &ty::ExplicitSelfCategory)
441 ty::StaticExplicitSelfCategory => "static",
442 ty::ByValueExplicitSelfCategory => "self",
443 ty::ByReferenceExplicitSelfCategory(_, ast::MutMutable) => {
446 ty::ByReferenceExplicitSelfCategory(_, ast::MutImmutable) => "&self",
447 ty::ByBoxExplicitSelfCategory => "Box<self>",
451 pub fn parameterized(cx: &ctxt,
453 substs: &subst::Substs,
454 generics: &ty::Generics)
457 let mut strs = Vec::new();
459 match substs.regions {
460 subst::ErasedRegions => { }
461 subst::NonerasedRegions(ref regions) => {
462 for &r in regions.iter() {
463 let s = region_to_string(cx, "", false, r);
467 // This happens when the value of the region
468 // parameter is not easily serialized. This may be
469 // because the user omitted it in the first place,
470 // or because it refers to some block in the code,
471 // etc. I'm not sure how best to serialize this.
472 strs.push(format!("'_"));
478 let tps = substs.types.get_slice(subst::TypeSpace);
479 let ty_params = generics.types.get_slice(subst::TypeSpace);
480 let has_defaults = ty_params.last().map_or(false, |def| def.default.is_some());
481 let num_defaults = if has_defaults && !cx.sess.verbose() {
482 ty_params.iter().zip(tps.iter()).rev().take_while(|&(def, &actual)| {
484 Some(default) => default.subst(cx, substs) == actual,
492 for t in tps.slice_to(tps.len() - num_defaults).iter() {
493 strs.push(ty_to_string(cx, *t))
496 if cx.sess.verbose() {
497 for t in substs.types.get_slice(subst::SelfSpace).iter() {
498 strs.push(format!("self {}", t.repr(cx)));
501 // generally there shouldn't be any substs in the fn param
502 // space, but in verbose mode, print them out.
503 for t in substs.types.get_slice(subst::FnSpace).iter() {
504 strs.push(format!("fn {}", t.repr(cx)));
509 format!("{}<{}>", base, strs.connect(","))
515 pub fn ty_to_short_str(cx: &ctxt, typ: t) -> String {
516 let mut s = typ.repr(cx).to_string();
518 s = s.as_slice().slice(0u, 32u).to_string();
523 impl<T:Repr> Repr for Option<T> {
524 fn repr(&self, tcx: &ctxt) -> String {
526 &None => "None".to_string(),
527 &Some(ref t) => t.repr(tcx),
532 impl<T:Repr,U:Repr> Repr for Result<T,U> {
533 fn repr(&self, tcx: &ctxt) -> String {
535 &Ok(ref t) => t.repr(tcx),
536 &Err(ref u) => format!("Err({})", u.repr(tcx))
542 fn repr(&self, _tcx: &ctxt) -> String {
547 impl<'a,T:Repr> Repr for &'a T {
548 fn repr(&self, tcx: &ctxt) -> String {
553 impl<T:Repr> Repr for Rc<T> {
554 fn repr(&self, tcx: &ctxt) -> String {
559 impl<T:Repr> Repr for Box<T> {
560 fn repr(&self, tcx: &ctxt) -> String {
565 fn repr_vec<T:Repr>(tcx: &ctxt, v: &[T]) -> String {
566 vec_map_to_string(v, |t| t.repr(tcx))
569 impl<'a, T:Repr> Repr for &'a [T] {
570 fn repr(&self, tcx: &ctxt) -> String {
575 impl<T:Repr> Repr for OwnedSlice<T> {
576 fn repr(&self, tcx: &ctxt) -> String {
577 repr_vec(tcx, self.as_slice())
581 // This is necessary to handle types like Option<~[T]>, for which
582 // autoderef cannot convert the &[T] handler
583 impl<T:Repr> Repr for Vec<T> {
584 fn repr(&self, tcx: &ctxt) -> String {
585 repr_vec(tcx, self.as_slice())
589 impl<T:UserString> UserString for Vec<T> {
590 fn user_string(&self, tcx: &ctxt) -> String {
591 let strs: Vec<String> =
592 self.iter().map(|t| t.user_string(tcx)).collect();
597 impl Repr for def::Def {
598 fn repr(&self, _tcx: &ctxt) -> String {
599 format!("{:?}", *self)
603 impl Repr for ty::TypeParameterDef {
604 fn repr(&self, tcx: &ctxt) -> String {
605 format!("TypeParameterDef({}, {}, {}/{})",
607 self.bounds.repr(tcx),
613 impl Repr for ty::RegionParameterDef {
614 fn repr(&self, tcx: &ctxt) -> String {
615 format!("RegionParameterDef(name={}, def_id={}, bounds={})",
616 token::get_name(self.name),
617 self.def_id.repr(tcx),
618 self.bounds.repr(tcx))
622 impl Repr for ty::t {
623 fn repr(&self, tcx: &ctxt) -> String {
624 ty_to_string(tcx, *self)
628 impl Repr for ty::mt {
629 fn repr(&self, tcx: &ctxt) -> String {
630 mt_to_string(tcx, self)
634 impl Repr for subst::Substs {
635 fn repr(&self, tcx: &ctxt) -> String {
636 format!("Substs[types={}, regions={}]",
637 self.types.repr(tcx),
638 self.regions.repr(tcx))
642 impl<T:Repr> Repr for subst::VecPerParamSpace<T> {
643 fn repr(&self, tcx: &ctxt) -> String {
644 format!("[{};{};{}]",
645 self.get_slice(subst::TypeSpace).repr(tcx),
646 self.get_slice(subst::SelfSpace).repr(tcx),
647 self.get_slice(subst::FnSpace).repr(tcx))
651 impl Repr for ty::ItemSubsts {
652 fn repr(&self, tcx: &ctxt) -> String {
653 format!("ItemSubsts({})", self.substs.repr(tcx))
657 impl Repr for subst::RegionSubsts {
658 fn repr(&self, tcx: &ctxt) -> String {
660 subst::ErasedRegions => "erased".to_string(),
661 subst::NonerasedRegions(ref regions) => regions.repr(tcx)
666 impl Repr for ty::BuiltinBounds {
667 fn repr(&self, _tcx: &ctxt) -> String {
668 let mut res = Vec::new();
669 for b in self.iter() {
671 ty::BoundSend => "Send".to_owned(),
672 ty::BoundSized => "Sized".to_owned(),
673 ty::BoundCopy => "Copy".to_owned(),
674 ty::BoundSync => "Sync".to_owned(),
681 impl Repr for ty::ExistentialBounds {
682 fn repr(&self, tcx: &ctxt) -> String {
683 self.user_string(tcx)
687 impl Repr for ty::ParamBounds {
688 fn repr(&self, tcx: &ctxt) -> String {
689 let mut res = Vec::new();
690 res.push(self.builtin_bounds.repr(tcx));
691 for t in self.trait_bounds.iter() {
692 res.push(t.repr(tcx));
698 impl Repr for ty::TraitRef {
699 fn repr(&self, tcx: &ctxt) -> String {
700 let base = ty::item_path_str(tcx, self.def_id);
701 let trait_def = ty::lookup_trait_def(tcx, self.def_id);
702 format!("<{} as {}>",
703 self.substs.self_ty().repr(tcx),
704 parameterized(tcx, base.as_slice(), &self.substs, &trait_def.generics))
708 impl Repr for ty::TraitDef {
709 fn repr(&self, tcx: &ctxt) -> String {
710 format!("TraitDef(generics={}, bounds={}, trait_ref={})",
711 self.generics.repr(tcx),
712 self.bounds.repr(tcx),
713 self.trait_ref.repr(tcx))
717 impl Repr for ast::Expr {
718 fn repr(&self, _tcx: &ctxt) -> String {
719 format!("expr({}: {})", self.id, pprust::expr_to_string(self))
723 impl Repr for ast::Path {
724 fn repr(&self, _tcx: &ctxt) -> String {
725 format!("path({})", pprust::path_to_string(self))
729 impl UserString for ast::Path {
730 fn user_string(&self, _tcx: &ctxt) -> String {
731 pprust::path_to_string(self)
735 impl Repr for ast::Item {
736 fn repr(&self, tcx: &ctxt) -> String {
737 format!("item({})", tcx.map.node_to_string(self.id))
741 impl Repr for ast::Lifetime {
742 fn repr(&self, _tcx: &ctxt) -> String {
743 format!("lifetime({}: {})", self.id, pprust::lifetime_to_string(self))
747 impl Repr for ast::Stmt {
748 fn repr(&self, _tcx: &ctxt) -> String {
749 format!("stmt({}: {})",
750 ast_util::stmt_id(self),
751 pprust::stmt_to_string(self))
755 impl Repr for ast::Pat {
756 fn repr(&self, _tcx: &ctxt) -> String {
757 format!("pat({}: {})", self.id, pprust::pat_to_string(self))
761 impl Repr for ty::BoundRegion {
762 fn repr(&self, tcx: &ctxt) -> String {
764 ty::BrAnon(id) => format!("BrAnon({})", id),
765 ty::BrNamed(id, name) => {
766 format!("BrNamed({}, {})", id.repr(tcx), token::get_name(name))
768 ty::BrFresh(id) => format!("BrFresh({})", id),
773 impl Repr for ty::Region {
774 fn repr(&self, tcx: &ctxt) -> String {
776 ty::ReEarlyBound(id, space, index, name) => {
777 format!("ReEarlyBound({}, {}, {}, {})",
781 token::get_name(name))
784 ty::ReLateBound(binder_id, ref bound_region) => {
785 format!("ReLateBound({}, {})",
787 bound_region.repr(tcx))
790 ty::ReFree(ref fr) => fr.repr(tcx),
793 format!("ReScope({})", id)
797 "ReStatic".to_string()
800 ty::ReInfer(ReVar(ref vid)) => {
801 format!("ReInfer({})", vid.index)
804 ty::ReInfer(ReSkolemized(id, ref bound_region)) => {
805 format!("re_skolemized({}, {})", id, bound_region.repr(tcx))
809 "ReEmpty".to_string()
815 impl UserString for ty::Region {
816 fn user_string(&self, tcx: &ctxt) -> String {
817 region_to_string(tcx, "", false, *self)
821 impl Repr for ty::FreeRegion {
822 fn repr(&self, tcx: &ctxt) -> String {
823 format!("ReFree({}, {})",
825 self.bound_region.repr(tcx))
829 impl Repr for ast::DefId {
830 fn repr(&self, tcx: &ctxt) -> String {
831 // Unfortunately, there seems to be no way to attempt to print
832 // a path for a def-id, so I'll just make a best effort for now
833 // and otherwise fallback to just printing the crate/node pair
834 if self.krate == ast::LOCAL_CRATE {
835 match tcx.map.find(self.node) {
836 Some(ast_map::NodeItem(..)) |
837 Some(ast_map::NodeForeignItem(..)) |
838 Some(ast_map::NodeImplItem(..)) |
839 Some(ast_map::NodeTraitItem(..)) |
840 Some(ast_map::NodeVariant(..)) |
841 Some(ast_map::NodeStructCtor(..)) => {
845 ty::item_path_str(tcx, *self))
850 return format!("{:?}", *self)
854 impl Repr for ty::Polytype {
855 fn repr(&self, tcx: &ctxt) -> String {
856 format!("Polytype {{generics: {}, ty: {}}}",
857 self.generics.repr(tcx),
862 impl Repr for ty::Generics {
863 fn repr(&self, tcx: &ctxt) -> String {
864 format!("Generics(types: {}, regions: {})",
865 self.types.repr(tcx),
866 self.regions.repr(tcx))
870 impl Repr for ty::ItemVariances {
871 fn repr(&self, tcx: &ctxt) -> String {
872 format!("ItemVariances(types={}, \
874 self.types.repr(tcx),
875 self.regions.repr(tcx))
879 impl Repr for ty::Variance {
880 fn repr(&self, _: &ctxt) -> String {
881 // The first `.to_string()` returns a &'static str (it is not an implementation
882 // of the ToString trait). Because of that, we need to call `.to_string()` again
883 // if we want to have a `String`.
884 let result: &'static str = (*self).to_string();
889 impl Repr for ty::Method {
890 fn repr(&self, tcx: &ctxt) -> String {
891 format!("method(ident: {}, generics: {}, fty: {}, \
892 explicit_self: {}, vis: {}, def_id: {})",
893 self.ident.repr(tcx),
894 self.generics.repr(tcx),
896 self.explicit_self.repr(tcx),
898 self.def_id.repr(tcx))
902 impl Repr for ast::Name {
903 fn repr(&self, _tcx: &ctxt) -> String {
904 token::get_name(*self).get().to_string()
908 impl UserString for ast::Name {
909 fn user_string(&self, _tcx: &ctxt) -> String {
910 token::get_name(*self).get().to_string()
914 impl Repr for ast::Ident {
915 fn repr(&self, _tcx: &ctxt) -> String {
916 token::get_ident(*self).get().to_string()
920 impl Repr for ast::ExplicitSelf_ {
921 fn repr(&self, _tcx: &ctxt) -> String {
922 format!("{:?}", *self)
926 impl Repr for ast::Visibility {
927 fn repr(&self, _tcx: &ctxt) -> String {
928 format!("{:?}", *self)
932 impl Repr for ty::BareFnTy {
933 fn repr(&self, tcx: &ctxt) -> String {
934 format!("BareFnTy {{fn_style: {:?}, abi: {}, sig: {}}}",
936 self.abi.to_string(),
941 impl Repr for ty::FnSig {
942 fn repr(&self, tcx: &ctxt) -> String {
943 fn_sig_to_string(tcx, self)
947 impl Repr for typeck::MethodCallee {
948 fn repr(&self, tcx: &ctxt) -> String {
949 format!("MethodCallee {{origin: {}, ty: {}, {}}}",
950 self.origin.repr(tcx),
952 self.substs.repr(tcx))
956 impl Repr for typeck::MethodOrigin {
957 fn repr(&self, tcx: &ctxt) -> String {
959 &typeck::MethodStatic(def_id) => {
960 format!("MethodStatic({})", def_id.repr(tcx))
962 &typeck::MethodStaticUnboxedClosure(def_id) => {
963 format!("MethodStaticUnboxedClosure({})", def_id.repr(tcx))
965 &typeck::MethodTypeParam(ref p) => {
968 &typeck::MethodTraitObject(ref p) => {
975 impl Repr for typeck::MethodParam {
976 fn repr(&self, tcx: &ctxt) -> String {
977 format!("MethodParam({},{})",
978 self.trait_ref.repr(tcx),
983 impl Repr for typeck::MethodObject {
984 fn repr(&self, tcx: &ctxt) -> String {
985 format!("MethodObject({},{:?},{:?})",
986 self.trait_ref.repr(tcx),
992 impl Repr for ty::TraitStore {
993 fn repr(&self, tcx: &ctxt) -> String {
994 trait_store_to_string(tcx, *self)
998 impl Repr for ty::BuiltinBound {
999 fn repr(&self, _tcx: &ctxt) -> String {
1000 format!("{:?}", *self)
1004 impl UserString for ty::BuiltinBound {
1005 fn user_string(&self, _tcx: &ctxt) -> String {
1007 ty::BoundSend => "Send".to_owned(),
1008 ty::BoundSized => "Sized".to_owned(),
1009 ty::BoundCopy => "Copy".to_owned(),
1010 ty::BoundSync => "Sync".to_owned(),
1015 impl Repr for Span {
1016 fn repr(&self, tcx: &ctxt) -> String {
1017 tcx.sess.codemap().span_to_string(*self).to_string()
1021 impl<A:UserString> UserString for Rc<A> {
1022 fn user_string(&self, tcx: &ctxt) -> String {
1023 let this: &A = &**self;
1024 this.user_string(tcx)
1028 impl UserString for ty::ParamBounds {
1029 fn user_string(&self, tcx: &ctxt) -> String {
1030 let mut result = Vec::new();
1031 let s = self.builtin_bounds.user_string(tcx);
1035 for n in self.trait_bounds.iter() {
1036 result.push(n.user_string(tcx));
1042 impl UserString for ty::ExistentialBounds {
1043 fn user_string(&self, tcx: &ctxt) -> String {
1044 if self.builtin_bounds.contains_elem(ty::BoundSend) &&
1045 self.region_bound == ty::ReStatic
1046 { // Region bound is implied by builtin bounds:
1047 return self.builtin_bounds.repr(tcx);
1050 let mut res = Vec::new();
1052 let region_str = self.region_bound.user_string(tcx);
1053 if !region_str.is_empty() {
1054 res.push(region_str);
1057 for bound in self.builtin_bounds.iter() {
1058 res.push(bound.user_string(tcx));
1065 impl UserString for ty::BuiltinBounds {
1066 fn user_string(&self, tcx: &ctxt) -> String {
1068 .map(|bb| bb.user_string(tcx))
1069 .collect::<Vec<String>>()
1075 impl UserString for ty::TraitRef {
1076 fn user_string(&self, tcx: &ctxt) -> String {
1077 let base = ty::item_path_str(tcx, self.def_id);
1078 let trait_def = ty::lookup_trait_def(tcx, self.def_id);
1079 parameterized(tcx, base.as_slice(), &self.substs, &trait_def.generics)
1083 impl UserString for ty::t {
1084 fn user_string(&self, tcx: &ctxt) -> String {
1085 ty_to_string(tcx, *self)
1089 impl UserString for ast::Ident {
1090 fn user_string(&self, _tcx: &ctxt) -> String {
1091 token::get_name(self.name).get().to_string()
1095 impl Repr for abi::Abi {
1096 fn repr(&self, _tcx: &ctxt) -> String {
1101 impl UserString for abi::Abi {
1102 fn user_string(&self, _tcx: &ctxt) -> String {
1107 impl Repr for ty::UpvarId {
1108 fn repr(&self, tcx: &ctxt) -> String {
1109 format!("UpvarId({};`{}`;{})",
1111 ty::local_var_name_str(tcx, self.var_id),
1112 self.closure_expr_id)
1116 impl Repr for ast::Mutability {
1117 fn repr(&self, _tcx: &ctxt) -> String {
1118 format!("{:?}", *self)
1122 impl Repr for ty::BorrowKind {
1123 fn repr(&self, _tcx: &ctxt) -> String {
1124 format!("{:?}", *self)
1128 impl Repr for ty::UpvarBorrow {
1129 fn repr(&self, tcx: &ctxt) -> String {
1130 format!("UpvarBorrow({}, {})",
1131 self.kind.repr(tcx),
1132 self.region.repr(tcx))
1136 impl Repr for ty::IntVid {
1137 fn repr(&self, _tcx: &ctxt) -> String {
1142 impl Repr for ty::FloatVid {
1143 fn repr(&self, _tcx: &ctxt) -> String {
1148 impl Repr for ty::RegionVid {
1149 fn repr(&self, _tcx: &ctxt) -> String {
1154 impl Repr for ty::TyVid {
1155 fn repr(&self, _tcx: &ctxt) -> String {
1160 impl Repr for ty::IntVarValue {
1161 fn repr(&self, _tcx: &ctxt) -> String {
1162 format!("{:?}", *self)
1166 impl Repr for ast::IntTy {
1167 fn repr(&self, _tcx: &ctxt) -> String {
1168 format!("{:?}", *self)
1172 impl Repr for ast::UintTy {
1173 fn repr(&self, _tcx: &ctxt) -> String {
1174 format!("{:?}", *self)
1178 impl Repr for ast::FloatTy {
1179 fn repr(&self, _tcx: &ctxt) -> String {
1180 format!("{:?}", *self)
1184 impl Repr for ty::ExplicitSelfCategory {
1185 fn repr(&self, _: &ctxt) -> String {
1186 explicit_self_category_to_str(self).to_string()
1191 impl Repr for regionmanip::WfConstraint {
1192 fn repr(&self, tcx: &ctxt) -> String {
1194 regionmanip::RegionSubRegionConstraint(_, r_a, r_b) => {
1195 format!("RegionSubRegionConstraint({}, {})",
1200 regionmanip::RegionSubParamConstraint(_, r, p) => {
1201 format!("RegionSubParamConstraint({}, {})",
1209 impl UserString for ParamTy {
1210 fn user_string(&self, tcx: &ctxt) -> String {
1212 let did = self.def_id;
1213 let ident = match tcx.ty_param_defs.borrow().find(&did.node) {
1214 Some(def) => token::get_ident(def.ident).get().to_string(),
1216 // This can only happen when a type mismatch error happens and
1217 // the actual type has more type parameters than the expected one.
1218 None => format!("<generic #{}>", id),
1224 impl Repr for ParamTy {
1225 fn repr(&self, tcx: &ctxt) -> String {
1226 self.user_string(tcx)
1230 impl<A:Repr,B:Repr> Repr for (A,B) {
1231 fn repr(&self, tcx: &ctxt) -> String {
1232 let &(ref a, ref b) = self;
1233 format!("({},{})", a.repr(tcx), b.repr(tcx))