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, BrEnv};
19 use middle::ty::{mt, t, ParamTy};
20 use middle::ty::{ty_bool, ty_char, ty_bot, 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(_, _, ast::MatchIfLetDesugar) => explain_span(cx, "if let", expr.span),
96 ast::ExprMatch(_, _, ast::MatchWhileLetDesugar) => {
97 explain_span(cx, "while let", expr.span)
99 ast::ExprMatch(..) => explain_span(cx, "match", expr.span),
100 _ => explain_span(cx, "expression", expr.span)
103 Some(ast_map::NodeStmt(stmt)) => {
104 explain_span(cx, "statement", stmt.span)
106 Some(ast_map::NodeItem(it)) => {
107 let tag = item_scope_tag(&*it);
108 explain_span(cx, tag, it.span)
111 // this really should not happen
112 (format!("unknown scope: {}. Please report a bug.", node_id), None)
118 let prefix = match fr.bound_region {
120 format!("the anonymous lifetime #{} defined on", idx + 1)
122 BrFresh(_) => "an anonymous lifetime defined on".to_string(),
124 format!("the lifetime {} as defined on",
125 bound_region_ptr_to_string(cx, fr.bound_region))
129 match cx.map.find(fr.scope_id) {
130 Some(ast_map::NodeBlock(ref blk)) => {
131 let (msg, opt_span) = explain_span(cx, "block", blk.span);
132 (format!("{} {}", prefix, msg), opt_span)
134 Some(ast_map::NodeItem(it)) => {
135 let tag = item_scope_tag(&*it);
136 let (msg, opt_span) = explain_span(cx, tag, it.span);
137 (format!("{} {}", prefix, msg), opt_span)
140 // this really should not happen
141 (format!("{} node {}", prefix, fr.scope_id), None)
146 ReStatic => { ("the static lifetime".to_string(), None) }
148 ReEmpty => { ("the empty lifetime".to_string(), None) }
150 ReEarlyBound(_, _, _, name) => {
151 (format!("{}", token::get_name(name)), None)
154 // I believe these cases should not occur (except when debugging,
156 ty::ReInfer(_) | ty::ReLateBound(..) => {
157 (format!("lifetime {}", region), None)
161 fn explain_span(cx: &ctxt, heading: &str, span: Span)
162 -> (String, Option<Span>) {
163 let lo = cx.sess.codemap().lookup_char_pos_adj(span.lo);
164 (format!("the {} at {}:{}", heading, lo.line, lo.col.to_uint()),
169 pub fn bound_region_ptr_to_string(cx: &ctxt, br: BoundRegion) -> String {
170 bound_region_to_string(cx, "", false, br)
173 pub fn bound_region_to_string(cx: &ctxt,
174 prefix: &str, space: bool,
175 br: BoundRegion) -> String {
176 let space_str = if space { " " } else { "" };
178 if cx.sess.verbose() {
179 return format!("{}{}{}", prefix, br.repr(cx), space_str)
183 BrNamed(_, name) => {
184 format!("{}{}{}", prefix, token::get_name(name), space_str)
186 BrAnon(_) | BrFresh(_) | BrEnv => prefix.to_string()
190 // In general, if you are giving a region error message,
191 // you should use `explain_region()` or, better yet,
192 // `note_and_explain_region()`
193 pub fn region_ptr_to_string(cx: &ctxt, region: Region) -> String {
194 region_to_string(cx, "&", true, region)
197 pub fn region_to_string(cx: &ctxt, prefix: &str, space: bool, region: Region) -> String {
198 let space_str = if space { " " } else { "" };
200 if cx.sess.verbose() {
201 return format!("{}{}{}", prefix, region.repr(cx), space_str)
204 // These printouts are concise. They do not contain all the information
205 // the user might want to diagnose an error, but there is basically no way
206 // to fit that into a short string. Hence the recommendation to use
207 // `explain_region()` or `note_and_explain_region()`.
209 ty::ReScope(_) => prefix.to_string(),
210 ty::ReEarlyBound(_, _, _, name) => {
211 token::get_name(name).get().to_string()
213 ty::ReLateBound(_, br) => bound_region_to_string(cx, prefix, space, br),
214 ty::ReFree(ref fr) => bound_region_to_string(cx, prefix, space, fr.bound_region),
215 ty::ReInfer(ReSkolemized(_, br)) => {
216 bound_region_to_string(cx, prefix, space, br)
218 ty::ReInfer(ReVar(_)) => prefix.to_string(),
219 ty::ReStatic => format!("{}'static{}", prefix, space_str),
220 ty::ReEmpty => format!("{}'<empty>{}", prefix, space_str),
224 pub fn mutability_to_string(m: ast::Mutability) -> String {
226 ast::MutMutable => "mut ".to_string(),
227 ast::MutImmutable => "".to_string(),
231 pub fn mt_to_string(cx: &ctxt, m: &mt) -> String {
232 format!("{}{}", mutability_to_string(m.mutbl), ty_to_string(cx, m.ty))
235 pub fn trait_store_to_string(cx: &ctxt, s: ty::TraitStore) -> String {
237 ty::UniqTraitStore => "Box ".to_string(),
238 ty::RegionTraitStore(r, m) => {
239 format!("{}{}", region_ptr_to_string(cx, r), mutability_to_string(m))
244 pub fn vec_map_to_string<T>(ts: &[T], f: |t: &T| -> String) -> String {
245 let tstrs = ts.iter().map(f).collect::<Vec<String>>();
246 format!("[{}]", tstrs.connect(", "))
249 pub fn fn_sig_to_string(cx: &ctxt, typ: &ty::FnSig) -> String {
250 format!("fn{}{} -> {}", typ.binder_id, typ.inputs.repr(cx),
254 pub fn trait_ref_to_string(cx: &ctxt, trait_ref: &ty::TraitRef) -> String {
255 trait_ref.user_string(cx).to_string()
258 pub fn ty_to_string(cx: &ctxt, typ: t) -> String {
259 fn fn_input_to_string(cx: &ctxt, input: ty::t) -> String {
260 ty_to_string(cx, input).to_string()
262 fn bare_fn_to_string(cx: &ctxt,
263 fn_style: ast::FnStyle,
265 ident: Option<ast::Ident>,
268 let mut s = String::new();
272 s.push_str(fn_style.to_string().as_slice());
277 if abi != abi::Rust {
278 s.push_str(format!("extern {} ", abi.to_string()).as_slice());
286 s.push_str(token::get_ident(i).get());
291 push_sig_to_string(cx, &mut s, '(', ')', sig, "");
296 fn closure_to_string(cx: &ctxt, cty: &ty::ClosureTy) -> String {
297 let mut s = String::new();
300 ty::UniqTraitStore => {}
301 ty::RegionTraitStore(region, _) => {
302 s.push_str(region_to_string(cx, "", true, region).as_slice());
309 s.push_str(cty.fn_style.to_string().as_slice());
314 let bounds_str = cty.bounds.user_string(cx);
317 ty::UniqTraitStore => {
318 assert_eq!(cty.onceness, ast::Once);
320 push_sig_to_string(cx, &mut s, '(', ')', &cty.sig,
321 bounds_str.as_slice());
323 ty::RegionTraitStore(..) => {
326 ast::Once => s.push_str("once ")
328 push_sig_to_string(cx, &mut s, '|', '|', &cty.sig,
329 bounds_str.as_slice());
336 fn push_sig_to_string(cx: &ctxt,
343 let strs: Vec<String> = sig.inputs.iter().map(|a| fn_input_to_string(cx, *a)).collect();
344 s.push_str(strs.connect(", ").as_slice());
350 if !bounds.is_empty() {
355 if ty::get(sig.output).sty != ty_nil {
357 if ty::type_is_bot(sig.output) {
360 s.push_str(ty_to_string(cx, sig.output).as_slice());
365 // if there is an id, print that instead of the structural type:
366 /*for def_id in ty::type_def_id(typ).iter() {
367 // note that this typedef cannot have type parameters
368 return ty::item_path_str(cx, *def_id);
371 // pretty print the structural type representation:
372 return match ty::get(typ).sty {
373 ty_nil => "()".to_string(),
374 ty_bot => "!".to_string(),
375 ty_bool => "bool".to_string(),
376 ty_char => "char".to_string(),
377 ty_int(t) => ast_util::int_ty_to_string(t, None).to_string(),
378 ty_uint(t) => ast_util::uint_ty_to_string(t, None).to_string(),
379 ty_float(t) => ast_util::float_ty_to_string(t).to_string(),
380 ty_uniq(typ) => format!("Box<{}>", ty_to_string(cx, typ)),
382 format!("*{} {}", match tm.mutbl {
383 ast::MutMutable => "mut",
384 ast::MutImmutable => "const",
385 }, ty_to_string(cx, tm.ty))
387 ty_rptr(r, ref tm) => {
388 let mut buf = region_ptr_to_string(cx, r);
389 buf.push_str(mt_to_string(cx, tm).as_slice());
392 ty_open(typ) => format!("opened<{}>", ty_to_string(cx, typ)),
393 ty_tup(ref elems) => {
394 let strs: Vec<String> = elems.iter().map(|elem| ty_to_string(cx, *elem)).collect();
395 format!("({})", strs.connect(","))
397 ty_closure(ref f) => {
398 closure_to_string(cx, &**f)
400 ty_bare_fn(ref f) => {
401 bare_fn_to_string(cx, f.fn_style, f.abi, None, &f.sig)
403 ty_infer(infer_ty) => infer_ty.to_string(),
404 ty_err => "[type error]".to_string(),
405 ty_param(ref param_ty) => {
408 ty_enum(did, ref substs) | ty_struct(did, ref substs) => {
409 let base = ty::item_path_str(cx, did);
410 let generics = ty::lookup_item_type(cx, did).generics;
411 parameterized(cx, base.as_slice(), substs, &generics)
413 ty_trait(box ty::TyTrait {
414 def_id: did, ref substs, ref bounds
416 let base = ty::item_path_str(cx, did);
417 let trait_def = ty::lookup_trait_def(cx, did);
418 let ty = parameterized(cx, base.as_slice(),
419 substs, &trait_def.generics);
420 let bound_str = bounds.user_string(cx);
421 let bound_sep = if bound_str.is_empty() { "" } else { "+" };
427 ty_str => "str".to_string(),
428 ty_unboxed_closure(..) => "closure".to_string(),
432 format!("[{}, ..{}]", ty_to_string(cx, t), n)
434 None => format!("[{}]", ty_to_string(cx, t)),
440 pub fn explicit_self_category_to_str(category: &ty::ExplicitSelfCategory)
443 ty::StaticExplicitSelfCategory => "static",
444 ty::ByValueExplicitSelfCategory => "self",
445 ty::ByReferenceExplicitSelfCategory(_, ast::MutMutable) => {
448 ty::ByReferenceExplicitSelfCategory(_, ast::MutImmutable) => "&self",
449 ty::ByBoxExplicitSelfCategory => "Box<self>",
453 pub fn parameterized(cx: &ctxt,
455 substs: &subst::Substs,
456 generics: &ty::Generics)
459 let mut strs = Vec::new();
461 match substs.regions {
462 subst::ErasedRegions => { }
463 subst::NonerasedRegions(ref regions) => {
464 for &r in regions.iter() {
465 let s = region_to_string(cx, "", false, r);
469 // This happens when the value of the region
470 // parameter is not easily serialized. This may be
471 // because the user omitted it in the first place,
472 // or because it refers to some block in the code,
473 // etc. I'm not sure how best to serialize this.
474 strs.push(format!("'_"));
480 let tps = substs.types.get_slice(subst::TypeSpace);
481 let ty_params = generics.types.get_slice(subst::TypeSpace);
482 let has_defaults = ty_params.last().map_or(false, |def| def.default.is_some());
483 let num_defaults = if has_defaults && !cx.sess.verbose() {
484 ty_params.iter().zip(tps.iter()).rev().take_while(|&(def, &actual)| {
486 Some(default) => default.subst(cx, substs) == actual,
494 for t in tps[..tps.len() - num_defaults].iter() {
495 strs.push(ty_to_string(cx, *t))
498 if cx.sess.verbose() {
499 for t in substs.types.get_slice(subst::SelfSpace).iter() {
500 strs.push(format!("self {}", t.repr(cx)));
503 // generally there shouldn't be any substs in the fn param
504 // space, but in verbose mode, print them out.
505 for t in substs.types.get_slice(subst::FnSpace).iter() {
506 strs.push(format!("fn {}", t.repr(cx)));
511 format!("{}<{}>", base, strs.connect(","))
517 pub fn ty_to_short_str(cx: &ctxt, typ: t) -> String {
518 let mut s = typ.repr(cx).to_string();
520 s = s.as_slice().slice(0u, 32u).to_string();
525 impl<T:Repr> Repr for Option<T> {
526 fn repr(&self, tcx: &ctxt) -> String {
528 &None => "None".to_string(),
529 &Some(ref t) => t.repr(tcx),
534 impl<T:Repr,U:Repr> Repr for Result<T,U> {
535 fn repr(&self, tcx: &ctxt) -> String {
537 &Ok(ref t) => t.repr(tcx),
538 &Err(ref u) => format!("Err({})", u.repr(tcx))
544 fn repr(&self, _tcx: &ctxt) -> String {
549 impl<'a,T:Repr> Repr for &'a T {
550 fn repr(&self, tcx: &ctxt) -> String {
555 impl<T:Repr> Repr for Rc<T> {
556 fn repr(&self, tcx: &ctxt) -> String {
561 impl<T:Repr> Repr for Box<T> {
562 fn repr(&self, tcx: &ctxt) -> String {
567 fn repr_vec<T:Repr>(tcx: &ctxt, v: &[T]) -> String {
568 vec_map_to_string(v, |t| t.repr(tcx))
571 impl<'a, T:Repr> Repr for &'a [T] {
572 fn repr(&self, tcx: &ctxt) -> String {
577 impl<T:Repr> Repr for OwnedSlice<T> {
578 fn repr(&self, tcx: &ctxt) -> String {
579 repr_vec(tcx, self.as_slice())
583 // This is necessary to handle types like Option<~[T]>, for which
584 // autoderef cannot convert the &[T] handler
585 impl<T:Repr> Repr for Vec<T> {
586 fn repr(&self, tcx: &ctxt) -> String {
587 repr_vec(tcx, self.as_slice())
591 impl<T:UserString> UserString for Vec<T> {
592 fn user_string(&self, tcx: &ctxt) -> String {
593 let strs: Vec<String> =
594 self.iter().map(|t| t.user_string(tcx)).collect();
599 impl Repr for def::Def {
600 fn repr(&self, _tcx: &ctxt) -> String {
605 impl Repr for ty::TypeParameterDef {
606 fn repr(&self, tcx: &ctxt) -> String {
607 format!("TypeParameterDef({}, {}, {}/{})",
609 self.bounds.repr(tcx),
615 impl Repr for ty::RegionParameterDef {
616 fn repr(&self, tcx: &ctxt) -> String {
617 format!("RegionParameterDef(name={}, def_id={}, bounds={})",
618 token::get_name(self.name),
619 self.def_id.repr(tcx),
620 self.bounds.repr(tcx))
624 impl Repr for ty::t {
625 fn repr(&self, tcx: &ctxt) -> String {
626 ty_to_string(tcx, *self)
630 impl Repr for ty::mt {
631 fn repr(&self, tcx: &ctxt) -> String {
632 mt_to_string(tcx, self)
636 impl Repr for subst::Substs {
637 fn repr(&self, tcx: &ctxt) -> String {
638 format!("Substs[types={}, regions={}]",
639 self.types.repr(tcx),
640 self.regions.repr(tcx))
644 impl<T:Repr> Repr for subst::VecPerParamSpace<T> {
645 fn repr(&self, tcx: &ctxt) -> String {
646 format!("[{};{};{}]",
647 self.get_slice(subst::TypeSpace).repr(tcx),
648 self.get_slice(subst::SelfSpace).repr(tcx),
649 self.get_slice(subst::FnSpace).repr(tcx))
653 impl Repr for ty::ItemSubsts {
654 fn repr(&self, tcx: &ctxt) -> String {
655 format!("ItemSubsts({})", self.substs.repr(tcx))
659 impl Repr for subst::RegionSubsts {
660 fn repr(&self, tcx: &ctxt) -> String {
662 subst::ErasedRegions => "erased".to_string(),
663 subst::NonerasedRegions(ref regions) => regions.repr(tcx)
668 impl Repr for ty::BuiltinBounds {
669 fn repr(&self, _tcx: &ctxt) -> String {
670 let mut res = Vec::new();
671 for b in self.iter() {
673 ty::BoundSend => "Send".to_string(),
674 ty::BoundSized => "Sized".to_string(),
675 ty::BoundCopy => "Copy".to_string(),
676 ty::BoundSync => "Sync".to_string(),
683 impl Repr for ty::ExistentialBounds {
684 fn repr(&self, tcx: &ctxt) -> String {
685 self.user_string(tcx)
689 impl Repr for ty::ParamBounds {
690 fn repr(&self, tcx: &ctxt) -> String {
691 let mut res = Vec::new();
692 res.push(self.builtin_bounds.repr(tcx));
693 for t in self.trait_bounds.iter() {
694 res.push(t.repr(tcx));
700 impl Repr for ty::TraitRef {
701 fn repr(&self, tcx: &ctxt) -> String {
702 let base = ty::item_path_str(tcx, self.def_id);
703 let trait_def = ty::lookup_trait_def(tcx, self.def_id);
704 format!("<{} as {}>",
705 self.substs.self_ty().repr(tcx),
706 parameterized(tcx, base.as_slice(), &self.substs, &trait_def.generics))
710 impl Repr for ty::TraitDef {
711 fn repr(&self, tcx: &ctxt) -> String {
712 format!("TraitDef(generics={}, bounds={}, trait_ref={})",
713 self.generics.repr(tcx),
714 self.bounds.repr(tcx),
715 self.trait_ref.repr(tcx))
719 impl Repr for ast::Expr {
720 fn repr(&self, _tcx: &ctxt) -> String {
721 format!("expr({}: {})", self.id, pprust::expr_to_string(self))
725 impl Repr for ast::Path {
726 fn repr(&self, _tcx: &ctxt) -> String {
727 format!("path({})", pprust::path_to_string(self))
731 impl UserString for ast::Path {
732 fn user_string(&self, _tcx: &ctxt) -> String {
733 pprust::path_to_string(self)
737 impl Repr for ast::Item {
738 fn repr(&self, tcx: &ctxt) -> String {
739 format!("item({})", tcx.map.node_to_string(self.id))
743 impl Repr for ast::Lifetime {
744 fn repr(&self, _tcx: &ctxt) -> String {
745 format!("lifetime({}: {})", self.id, pprust::lifetime_to_string(self))
749 impl Repr for ast::Stmt {
750 fn repr(&self, _tcx: &ctxt) -> String {
751 format!("stmt({}: {})",
752 ast_util::stmt_id(self),
753 pprust::stmt_to_string(self))
757 impl Repr for ast::Pat {
758 fn repr(&self, _tcx: &ctxt) -> String {
759 format!("pat({}: {})", self.id, pprust::pat_to_string(self))
763 impl Repr for ty::BoundRegion {
764 fn repr(&self, tcx: &ctxt) -> String {
766 ty::BrAnon(id) => format!("BrAnon({})", id),
767 ty::BrNamed(id, name) => {
768 format!("BrNamed({}, {})", id.repr(tcx), token::get_name(name))
770 ty::BrFresh(id) => format!("BrFresh({})", id),
771 ty::BrEnv => "BrEnv".to_string()
776 impl Repr for ty::Region {
777 fn repr(&self, tcx: &ctxt) -> String {
779 ty::ReEarlyBound(id, space, index, name) => {
780 format!("ReEarlyBound({}, {}, {}, {})",
784 token::get_name(name))
787 ty::ReLateBound(binder_id, ref bound_region) => {
788 format!("ReLateBound({}, {})",
790 bound_region.repr(tcx))
793 ty::ReFree(ref fr) => fr.repr(tcx),
796 format!("ReScope({})", id)
800 "ReStatic".to_string()
803 ty::ReInfer(ReVar(ref vid)) => {
804 format!("ReInfer({})", vid.index)
807 ty::ReInfer(ReSkolemized(id, ref bound_region)) => {
808 format!("re_skolemized({}, {})", id, bound_region.repr(tcx))
812 "ReEmpty".to_string()
818 impl UserString for ty::Region {
819 fn user_string(&self, tcx: &ctxt) -> String {
820 region_to_string(tcx, "", false, *self)
824 impl Repr for ty::FreeRegion {
825 fn repr(&self, tcx: &ctxt) -> String {
826 format!("ReFree({}, {})",
828 self.bound_region.repr(tcx))
832 impl Repr for ast::DefId {
833 fn repr(&self, tcx: &ctxt) -> String {
834 // Unfortunately, there seems to be no way to attempt to print
835 // a path for a def-id, so I'll just make a best effort for now
836 // and otherwise fallback to just printing the crate/node pair
837 if self.krate == ast::LOCAL_CRATE {
838 match tcx.map.find(self.node) {
839 Some(ast_map::NodeItem(..)) |
840 Some(ast_map::NodeForeignItem(..)) |
841 Some(ast_map::NodeImplItem(..)) |
842 Some(ast_map::NodeTraitItem(..)) |
843 Some(ast_map::NodeVariant(..)) |
844 Some(ast_map::NodeStructCtor(..)) => {
848 ty::item_path_str(tcx, *self))
853 return format!("{}", *self)
857 impl Repr for ty::Polytype {
858 fn repr(&self, tcx: &ctxt) -> String {
859 format!("Polytype {{generics: {}, ty: {}}}",
860 self.generics.repr(tcx),
865 impl Repr for ty::Generics {
866 fn repr(&self, tcx: &ctxt) -> String {
867 format!("Generics(types: {}, regions: {})",
868 self.types.repr(tcx),
869 self.regions.repr(tcx))
873 impl Repr for ty::ItemVariances {
874 fn repr(&self, tcx: &ctxt) -> String {
875 format!("ItemVariances(types={}, \
877 self.types.repr(tcx),
878 self.regions.repr(tcx))
882 impl Repr for ty::Variance {
883 fn repr(&self, _: &ctxt) -> String {
884 // The first `.to_string()` returns a &'static str (it is not an implementation
885 // of the ToString trait). Because of that, we need to call `.to_string()` again
886 // if we want to have a `String`.
887 let result: &'static str = (*self).to_string();
892 impl Repr for ty::Method {
893 fn repr(&self, tcx: &ctxt) -> String {
894 format!("method(name: {}, generics: {}, fty: {}, \
895 explicit_self: {}, vis: {}, def_id: {})",
897 self.generics.repr(tcx),
899 self.explicit_self.repr(tcx),
901 self.def_id.repr(tcx))
905 impl Repr for ast::Name {
906 fn repr(&self, _tcx: &ctxt) -> String {
907 token::get_name(*self).get().to_string()
911 impl UserString for ast::Name {
912 fn user_string(&self, _tcx: &ctxt) -> String {
913 token::get_name(*self).get().to_string()
917 impl Repr for ast::Ident {
918 fn repr(&self, _tcx: &ctxt) -> String {
919 token::get_ident(*self).get().to_string()
923 impl Repr for ast::ExplicitSelf_ {
924 fn repr(&self, _tcx: &ctxt) -> String {
929 impl Repr for ast::Visibility {
930 fn repr(&self, _tcx: &ctxt) -> String {
935 impl Repr for ty::BareFnTy {
936 fn repr(&self, tcx: &ctxt) -> String {
937 format!("BareFnTy {{fn_style: {}, abi: {}, sig: {}}}",
939 self.abi.to_string(),
944 impl Repr for ty::FnSig {
945 fn repr(&self, tcx: &ctxt) -> String {
946 fn_sig_to_string(tcx, self)
950 impl Repr for typeck::MethodCallee {
951 fn repr(&self, tcx: &ctxt) -> String {
952 format!("MethodCallee {{origin: {}, ty: {}, {}}}",
953 self.origin.repr(tcx),
955 self.substs.repr(tcx))
959 impl Repr for typeck::MethodOrigin {
960 fn repr(&self, tcx: &ctxt) -> String {
962 &typeck::MethodStatic(def_id) => {
963 format!("MethodStatic({})", def_id.repr(tcx))
965 &typeck::MethodStaticUnboxedClosure(def_id) => {
966 format!("MethodStaticUnboxedClosure({})", def_id.repr(tcx))
968 &typeck::MethodTypeParam(ref p) => {
971 &typeck::MethodTraitObject(ref p) => {
978 impl Repr for typeck::MethodParam {
979 fn repr(&self, tcx: &ctxt) -> String {
980 format!("MethodParam({},{})",
981 self.trait_ref.repr(tcx),
986 impl Repr for typeck::MethodObject {
987 fn repr(&self, tcx: &ctxt) -> String {
988 format!("MethodObject({},{},{})",
989 self.trait_ref.repr(tcx),
995 impl Repr for ty::TraitStore {
996 fn repr(&self, tcx: &ctxt) -> String {
997 trait_store_to_string(tcx, *self)
1001 impl Repr for ty::BuiltinBound {
1002 fn repr(&self, _tcx: &ctxt) -> String {
1003 format!("{}", *self)
1007 impl UserString for ty::BuiltinBound {
1008 fn user_string(&self, _tcx: &ctxt) -> String {
1010 ty::BoundSend => "Send".to_string(),
1011 ty::BoundSized => "Sized".to_string(),
1012 ty::BoundCopy => "Copy".to_string(),
1013 ty::BoundSync => "Sync".to_string(),
1018 impl Repr for Span {
1019 fn repr(&self, tcx: &ctxt) -> String {
1020 tcx.sess.codemap().span_to_string(*self).to_string()
1024 impl<A:UserString> UserString for Rc<A> {
1025 fn user_string(&self, tcx: &ctxt) -> String {
1026 let this: &A = &**self;
1027 this.user_string(tcx)
1031 impl UserString for ty::ParamBounds {
1032 fn user_string(&self, tcx: &ctxt) -> String {
1033 let mut result = Vec::new();
1034 let s = self.builtin_bounds.user_string(tcx);
1038 for n in self.trait_bounds.iter() {
1039 result.push(n.user_string(tcx));
1045 impl UserString for ty::ExistentialBounds {
1046 fn user_string(&self, tcx: &ctxt) -> String {
1047 if self.builtin_bounds.contains_elem(ty::BoundSend) &&
1048 self.region_bound == ty::ReStatic
1049 { // Region bound is implied by builtin bounds:
1050 return self.builtin_bounds.repr(tcx);
1053 let mut res = Vec::new();
1055 let region_str = self.region_bound.user_string(tcx);
1056 if !region_str.is_empty() {
1057 res.push(region_str);
1060 for bound in self.builtin_bounds.iter() {
1061 res.push(bound.user_string(tcx));
1068 impl UserString for ty::BuiltinBounds {
1069 fn user_string(&self, tcx: &ctxt) -> String {
1071 .map(|bb| bb.user_string(tcx))
1072 .collect::<Vec<String>>()
1078 impl UserString for ty::TraitRef {
1079 fn user_string(&self, tcx: &ctxt) -> String {
1080 let base = ty::item_path_str(tcx, self.def_id);
1081 let trait_def = ty::lookup_trait_def(tcx, self.def_id);
1082 parameterized(tcx, base.as_slice(), &self.substs, &trait_def.generics)
1086 impl UserString for ty::t {
1087 fn user_string(&self, tcx: &ctxt) -> String {
1088 ty_to_string(tcx, *self)
1092 impl UserString for ast::Ident {
1093 fn user_string(&self, _tcx: &ctxt) -> String {
1094 token::get_name(self.name).get().to_string()
1098 impl Repr for abi::Abi {
1099 fn repr(&self, _tcx: &ctxt) -> String {
1104 impl UserString for abi::Abi {
1105 fn user_string(&self, _tcx: &ctxt) -> String {
1110 impl Repr for ty::UpvarId {
1111 fn repr(&self, tcx: &ctxt) -> String {
1112 format!("UpvarId({};`{}`;{})",
1114 ty::local_var_name_str(tcx, self.var_id),
1115 self.closure_expr_id)
1119 impl Repr for ast::Mutability {
1120 fn repr(&self, _tcx: &ctxt) -> String {
1121 format!("{}", *self)
1125 impl Repr for ty::BorrowKind {
1126 fn repr(&self, _tcx: &ctxt) -> String {
1127 format!("{}", *self)
1131 impl Repr for ty::UpvarBorrow {
1132 fn repr(&self, tcx: &ctxt) -> String {
1133 format!("UpvarBorrow({}, {})",
1134 self.kind.repr(tcx),
1135 self.region.repr(tcx))
1139 impl Repr for ty::IntVid {
1140 fn repr(&self, _tcx: &ctxt) -> String {
1145 impl Repr for ty::FloatVid {
1146 fn repr(&self, _tcx: &ctxt) -> String {
1151 impl Repr for ty::RegionVid {
1152 fn repr(&self, _tcx: &ctxt) -> String {
1157 impl Repr for ty::TyVid {
1158 fn repr(&self, _tcx: &ctxt) -> String {
1163 impl Repr for ty::IntVarValue {
1164 fn repr(&self, _tcx: &ctxt) -> String {
1165 format!("{}", *self)
1169 impl Repr for ast::IntTy {
1170 fn repr(&self, _tcx: &ctxt) -> String {
1171 format!("{}", *self)
1175 impl Repr for ast::UintTy {
1176 fn repr(&self, _tcx: &ctxt) -> String {
1177 format!("{}", *self)
1181 impl Repr for ast::FloatTy {
1182 fn repr(&self, _tcx: &ctxt) -> String {
1183 format!("{}", *self)
1187 impl Repr for ty::ExplicitSelfCategory {
1188 fn repr(&self, _: &ctxt) -> String {
1189 explicit_self_category_to_str(self).to_string()
1194 impl Repr for regionmanip::WfConstraint {
1195 fn repr(&self, tcx: &ctxt) -> String {
1197 regionmanip::RegionSubRegionConstraint(_, r_a, r_b) => {
1198 format!("RegionSubRegionConstraint({}, {})",
1203 regionmanip::RegionSubParamConstraint(_, r, p) => {
1204 format!("RegionSubParamConstraint({}, {})",
1212 impl UserString for ParamTy {
1213 fn user_string(&self, tcx: &ctxt) -> String {
1215 let did = self.def_id;
1216 let ident = match tcx.ty_param_defs.borrow().find(&did.node) {
1217 Some(def) => token::get_name(def.name).get().to_string(),
1219 // This can only happen when a type mismatch error happens and
1220 // the actual type has more type parameters than the expected one.
1221 None => format!("<generic #{}>", id),
1227 impl Repr for ParamTy {
1228 fn repr(&self, tcx: &ctxt) -> String {
1229 self.user_string(tcx)
1233 impl<A:Repr,B:Repr> Repr for (A,B) {
1234 fn repr(&self, tcx: &ctxt) -> String {
1235 let &(ref a, ref b) = self;
1236 format!("({},{})", a.repr(tcx), b.repr(tcx))