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_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() {
356 ty::FnConverging(t) => {
357 if !ty::type_is_nil(t) {
359 s.push_str(ty_to_string(cx, t).as_slice());
368 // if there is an id, print that instead of the structural type:
369 /*for def_id in ty::type_def_id(typ).iter() {
370 // note that this typedef cannot have type parameters
371 return ty::item_path_str(cx, *def_id);
374 // pretty print the structural type representation:
375 return match ty::get(typ).sty {
376 ty_nil => "()".to_string(),
377 ty_bool => "bool".to_string(),
378 ty_char => "char".to_string(),
379 ty_int(t) => ast_util::int_ty_to_string(t, None).to_string(),
380 ty_uint(t) => ast_util::uint_ty_to_string(t, None).to_string(),
381 ty_float(t) => ast_util::float_ty_to_string(t).to_string(),
382 ty_uniq(typ) => format!("Box<{}>", ty_to_string(cx, typ)),
384 format!("*{} {}", match tm.mutbl {
385 ast::MutMutable => "mut",
386 ast::MutImmutable => "const",
387 }, ty_to_string(cx, tm.ty))
389 ty_rptr(r, ref tm) => {
390 let mut buf = region_ptr_to_string(cx, r);
391 buf.push_str(mt_to_string(cx, tm).as_slice());
394 ty_open(typ) => format!("opened<{}>", ty_to_string(cx, typ)),
395 ty_tup(ref elems) => {
396 let strs: Vec<String> = elems.iter().map(|elem| ty_to_string(cx, *elem)).collect();
397 format!("({})", strs.connect(","))
399 ty_closure(ref f) => {
400 closure_to_string(cx, &**f)
402 ty_bare_fn(ref f) => {
403 bare_fn_to_string(cx, f.fn_style, f.abi, None, &f.sig)
405 ty_infer(infer_ty) => infer_ty.to_string(),
406 ty_err => "[type error]".to_string(),
407 ty_param(ref param_ty) => {
410 ty_enum(did, ref substs) | ty_struct(did, ref substs) => {
411 let base = ty::item_path_str(cx, did);
412 let generics = ty::lookup_item_type(cx, did).generics;
413 parameterized(cx, base.as_slice(), substs, &generics)
415 ty_trait(box ty::TyTrait {
416 def_id: did, ref substs, ref bounds
418 let base = ty::item_path_str(cx, did);
419 let trait_def = ty::lookup_trait_def(cx, did);
420 let ty = parameterized(cx, base.as_slice(),
421 substs, &trait_def.generics);
422 let bound_str = bounds.user_string(cx);
423 let bound_sep = if bound_str.is_empty() { "" } else { "+" };
429 ty_str => "str".to_string(),
430 ty_unboxed_closure(ref did, _, ref substs) => {
431 let unboxed_closures = cx.unboxed_closures.borrow();
432 unboxed_closures.find(did).map(|cl| {
433 closure_to_string(cx, &cl.closure_type.subst(cx, substs))
434 }).unwrap_or_else(|| "closure".to_string())
439 format!("[{}, ..{}]", ty_to_string(cx, t), n)
441 None => format!("[{}]", ty_to_string(cx, t)),
447 pub fn explicit_self_category_to_str(category: &ty::ExplicitSelfCategory)
450 ty::StaticExplicitSelfCategory => "static",
451 ty::ByValueExplicitSelfCategory => "self",
452 ty::ByReferenceExplicitSelfCategory(_, ast::MutMutable) => {
455 ty::ByReferenceExplicitSelfCategory(_, ast::MutImmutable) => "&self",
456 ty::ByBoxExplicitSelfCategory => "Box<self>",
460 pub fn parameterized(cx: &ctxt,
462 substs: &subst::Substs,
463 generics: &ty::Generics)
466 let mut strs = Vec::new();
468 match substs.regions {
469 subst::ErasedRegions => { }
470 subst::NonerasedRegions(ref regions) => {
471 for &r in regions.iter() {
472 let s = region_to_string(cx, "", false, r);
476 // This happens when the value of the region
477 // parameter is not easily serialized. This may be
478 // because the user omitted it in the first place,
479 // or because it refers to some block in the code,
480 // etc. I'm not sure how best to serialize this.
481 strs.push(format!("'_"));
487 let tps = substs.types.get_slice(subst::TypeSpace);
488 let ty_params = generics.types.get_slice(subst::TypeSpace);
489 let has_defaults = ty_params.last().map_or(false, |def| def.default.is_some());
490 let num_defaults = if has_defaults && !cx.sess.verbose() {
491 ty_params.iter().zip(tps.iter()).rev().take_while(|&(def, &actual)| {
493 Some(default) => default.subst(cx, substs) == actual,
501 for t in tps[..tps.len() - num_defaults].iter() {
502 strs.push(ty_to_string(cx, *t))
505 if cx.sess.verbose() {
506 for t in substs.types.get_slice(subst::SelfSpace).iter() {
507 strs.push(format!("self {}", t.repr(cx)));
510 // generally there shouldn't be any substs in the fn param
511 // space, but in verbose mode, print them out.
512 for t in substs.types.get_slice(subst::FnSpace).iter() {
513 strs.push(format!("fn {}", t.repr(cx)));
518 format!("{}<{}>", base, strs.connect(","))
524 pub fn ty_to_short_str(cx: &ctxt, typ: t) -> String {
525 let mut s = typ.repr(cx).to_string();
527 s = s.as_slice().slice(0u, 32u).to_string();
532 impl<T:Repr> Repr for Option<T> {
533 fn repr(&self, tcx: &ctxt) -> String {
535 &None => "None".to_string(),
536 &Some(ref t) => t.repr(tcx),
541 impl<T:Repr,U:Repr> Repr for Result<T,U> {
542 fn repr(&self, tcx: &ctxt) -> String {
544 &Ok(ref t) => t.repr(tcx),
545 &Err(ref u) => format!("Err({})", u.repr(tcx))
551 fn repr(&self, _tcx: &ctxt) -> String {
556 impl<'a,T:Repr> Repr for &'a T {
557 fn repr(&self, tcx: &ctxt) -> String {
562 impl<T:Repr> Repr for Rc<T> {
563 fn repr(&self, tcx: &ctxt) -> String {
568 impl<T:Repr> Repr for Box<T> {
569 fn repr(&self, tcx: &ctxt) -> String {
574 fn repr_vec<T:Repr>(tcx: &ctxt, v: &[T]) -> String {
575 vec_map_to_string(v, |t| t.repr(tcx))
578 impl<'a, T:Repr> Repr for &'a [T] {
579 fn repr(&self, tcx: &ctxt) -> String {
584 impl<T:Repr> Repr for OwnedSlice<T> {
585 fn repr(&self, tcx: &ctxt) -> String {
586 repr_vec(tcx, self.as_slice())
590 // This is necessary to handle types like Option<~[T]>, for which
591 // autoderef cannot convert the &[T] handler
592 impl<T:Repr> Repr for Vec<T> {
593 fn repr(&self, tcx: &ctxt) -> String {
594 repr_vec(tcx, self.as_slice())
598 impl<T:UserString> UserString for Vec<T> {
599 fn user_string(&self, tcx: &ctxt) -> String {
600 let strs: Vec<String> =
601 self.iter().map(|t| t.user_string(tcx)).collect();
606 impl Repr for def::Def {
607 fn repr(&self, _tcx: &ctxt) -> String {
612 impl Repr for ty::TypeParameterDef {
613 fn repr(&self, tcx: &ctxt) -> String {
614 format!("TypeParameterDef({}, {}, {}/{})",
616 self.bounds.repr(tcx),
622 impl Repr for ty::RegionParameterDef {
623 fn repr(&self, tcx: &ctxt) -> String {
624 format!("RegionParameterDef(name={}, def_id={}, bounds={})",
625 token::get_name(self.name),
626 self.def_id.repr(tcx),
627 self.bounds.repr(tcx))
631 impl Repr for ty::t {
632 fn repr(&self, tcx: &ctxt) -> String {
633 ty_to_string(tcx, *self)
637 impl Repr for ty::mt {
638 fn repr(&self, tcx: &ctxt) -> String {
639 mt_to_string(tcx, self)
643 impl Repr for subst::Substs {
644 fn repr(&self, tcx: &ctxt) -> String {
645 format!("Substs[types={}, regions={}]",
646 self.types.repr(tcx),
647 self.regions.repr(tcx))
651 impl<T:Repr> Repr for subst::VecPerParamSpace<T> {
652 fn repr(&self, tcx: &ctxt) -> String {
653 format!("[{};{};{}]",
654 self.get_slice(subst::TypeSpace).repr(tcx),
655 self.get_slice(subst::SelfSpace).repr(tcx),
656 self.get_slice(subst::FnSpace).repr(tcx))
660 impl Repr for ty::ItemSubsts {
661 fn repr(&self, tcx: &ctxt) -> String {
662 format!("ItemSubsts({})", self.substs.repr(tcx))
666 impl Repr for subst::RegionSubsts {
667 fn repr(&self, tcx: &ctxt) -> String {
669 subst::ErasedRegions => "erased".to_string(),
670 subst::NonerasedRegions(ref regions) => regions.repr(tcx)
675 impl Repr for ty::BuiltinBounds {
676 fn repr(&self, _tcx: &ctxt) -> String {
677 let mut res = Vec::new();
678 for b in self.iter() {
680 ty::BoundSend => "Send".to_string(),
681 ty::BoundSized => "Sized".to_string(),
682 ty::BoundCopy => "Copy".to_string(),
683 ty::BoundSync => "Sync".to_string(),
690 impl Repr for ty::ExistentialBounds {
691 fn repr(&self, tcx: &ctxt) -> String {
692 self.user_string(tcx)
696 impl Repr for ty::ParamBounds {
697 fn repr(&self, tcx: &ctxt) -> String {
698 let mut res = Vec::new();
699 res.push(self.builtin_bounds.repr(tcx));
700 for t in self.trait_bounds.iter() {
701 res.push(t.repr(tcx));
707 impl Repr for ty::TraitRef {
708 fn repr(&self, tcx: &ctxt) -> String {
709 let base = ty::item_path_str(tcx, self.def_id);
710 let trait_def = ty::lookup_trait_def(tcx, self.def_id);
711 format!("<{} as {}>",
712 self.substs.self_ty().repr(tcx),
713 parameterized(tcx, base.as_slice(), &self.substs, &trait_def.generics))
717 impl Repr for ty::TraitDef {
718 fn repr(&self, tcx: &ctxt) -> String {
719 format!("TraitDef(generics={}, bounds={}, trait_ref={})",
720 self.generics.repr(tcx),
721 self.bounds.repr(tcx),
722 self.trait_ref.repr(tcx))
726 impl Repr for ast::Expr {
727 fn repr(&self, _tcx: &ctxt) -> String {
728 format!("expr({}: {})", self.id, pprust::expr_to_string(self))
732 impl Repr for ast::Path {
733 fn repr(&self, _tcx: &ctxt) -> String {
734 format!("path({})", pprust::path_to_string(self))
738 impl UserString for ast::Path {
739 fn user_string(&self, _tcx: &ctxt) -> String {
740 pprust::path_to_string(self)
744 impl Repr for ast::Item {
745 fn repr(&self, tcx: &ctxt) -> String {
746 format!("item({})", tcx.map.node_to_string(self.id))
750 impl Repr for ast::Lifetime {
751 fn repr(&self, _tcx: &ctxt) -> String {
752 format!("lifetime({}: {})", self.id, pprust::lifetime_to_string(self))
756 impl Repr for ast::Stmt {
757 fn repr(&self, _tcx: &ctxt) -> String {
758 format!("stmt({}: {})",
759 ast_util::stmt_id(self),
760 pprust::stmt_to_string(self))
764 impl Repr for ast::Pat {
765 fn repr(&self, _tcx: &ctxt) -> String {
766 format!("pat({}: {})", self.id, pprust::pat_to_string(self))
770 impl Repr for ty::BoundRegion {
771 fn repr(&self, tcx: &ctxt) -> String {
773 ty::BrAnon(id) => format!("BrAnon({})", id),
774 ty::BrNamed(id, name) => {
775 format!("BrNamed({}, {})", id.repr(tcx), token::get_name(name))
777 ty::BrFresh(id) => format!("BrFresh({})", id),
778 ty::BrEnv => "BrEnv".to_string()
783 impl Repr for ty::Region {
784 fn repr(&self, tcx: &ctxt) -> String {
786 ty::ReEarlyBound(id, space, index, name) => {
787 format!("ReEarlyBound({}, {}, {}, {})",
791 token::get_name(name))
794 ty::ReLateBound(binder_id, ref bound_region) => {
795 format!("ReLateBound({}, {})",
797 bound_region.repr(tcx))
800 ty::ReFree(ref fr) => fr.repr(tcx),
803 format!("ReScope({})", id)
807 "ReStatic".to_string()
810 ty::ReInfer(ReVar(ref vid)) => {
811 format!("ReInfer({})", vid.index)
814 ty::ReInfer(ReSkolemized(id, ref bound_region)) => {
815 format!("re_skolemized({}, {})", id, bound_region.repr(tcx))
819 "ReEmpty".to_string()
825 impl UserString for ty::Region {
826 fn user_string(&self, tcx: &ctxt) -> String {
827 region_to_string(tcx, "", false, *self)
831 impl Repr for ty::FreeRegion {
832 fn repr(&self, tcx: &ctxt) -> String {
833 format!("ReFree({}, {})",
835 self.bound_region.repr(tcx))
839 impl Repr for ast::DefId {
840 fn repr(&self, tcx: &ctxt) -> String {
841 // Unfortunately, there seems to be no way to attempt to print
842 // a path for a def-id, so I'll just make a best effort for now
843 // and otherwise fallback to just printing the crate/node pair
844 if self.krate == ast::LOCAL_CRATE {
845 match tcx.map.find(self.node) {
846 Some(ast_map::NodeItem(..)) |
847 Some(ast_map::NodeForeignItem(..)) |
848 Some(ast_map::NodeImplItem(..)) |
849 Some(ast_map::NodeTraitItem(..)) |
850 Some(ast_map::NodeVariant(..)) |
851 Some(ast_map::NodeStructCtor(..)) => {
855 ty::item_path_str(tcx, *self))
860 return format!("{}", *self)
864 impl Repr for ty::Polytype {
865 fn repr(&self, tcx: &ctxt) -> String {
866 format!("Polytype {{generics: {}, ty: {}}}",
867 self.generics.repr(tcx),
872 impl Repr for ty::Generics {
873 fn repr(&self, tcx: &ctxt) -> String {
874 format!("Generics(types: {}, regions: {})",
875 self.types.repr(tcx),
876 self.regions.repr(tcx))
880 impl Repr for ty::ItemVariances {
881 fn repr(&self, tcx: &ctxt) -> String {
882 format!("ItemVariances(types={}, \
884 self.types.repr(tcx),
885 self.regions.repr(tcx))
889 impl Repr for ty::Variance {
890 fn repr(&self, _: &ctxt) -> String {
891 // The first `.to_string()` returns a &'static str (it is not an implementation
892 // of the ToString trait). Because of that, we need to call `.to_string()` again
893 // if we want to have a `String`.
894 let result: &'static str = (*self).to_string();
899 impl Repr for ty::Method {
900 fn repr(&self, tcx: &ctxt) -> String {
901 format!("method(name: {}, generics: {}, fty: {}, \
902 explicit_self: {}, vis: {}, def_id: {})",
904 self.generics.repr(tcx),
906 self.explicit_self.repr(tcx),
908 self.def_id.repr(tcx))
912 impl Repr for ast::Name {
913 fn repr(&self, _tcx: &ctxt) -> String {
914 token::get_name(*self).get().to_string()
918 impl UserString for ast::Name {
919 fn user_string(&self, _tcx: &ctxt) -> String {
920 token::get_name(*self).get().to_string()
924 impl Repr for ast::Ident {
925 fn repr(&self, _tcx: &ctxt) -> String {
926 token::get_ident(*self).get().to_string()
930 impl Repr for ast::ExplicitSelf_ {
931 fn repr(&self, _tcx: &ctxt) -> String {
936 impl Repr for ast::Visibility {
937 fn repr(&self, _tcx: &ctxt) -> String {
942 impl Repr for ty::BareFnTy {
943 fn repr(&self, tcx: &ctxt) -> String {
944 format!("BareFnTy {{fn_style: {}, abi: {}, sig: {}}}",
946 self.abi.to_string(),
951 impl Repr for ty::FnSig {
952 fn repr(&self, tcx: &ctxt) -> String {
953 fn_sig_to_string(tcx, self)
957 impl Repr for ty::FnOutput {
958 fn repr(&self, tcx: &ctxt) -> String {
960 ty::FnConverging(ty) =>
961 format!("FnConverging({0})", ty.repr(tcx)),
963 "FnDiverging".to_string()
968 impl Repr for typeck::MethodCallee {
969 fn repr(&self, tcx: &ctxt) -> String {
970 format!("MethodCallee {{origin: {}, ty: {}, {}}}",
971 self.origin.repr(tcx),
973 self.substs.repr(tcx))
977 impl Repr for typeck::MethodOrigin {
978 fn repr(&self, tcx: &ctxt) -> String {
980 &typeck::MethodStatic(def_id) => {
981 format!("MethodStatic({})", def_id.repr(tcx))
983 &typeck::MethodStaticUnboxedClosure(def_id) => {
984 format!("MethodStaticUnboxedClosure({})", def_id.repr(tcx))
986 &typeck::MethodTypeParam(ref p) => {
989 &typeck::MethodTraitObject(ref p) => {
996 impl Repr for typeck::MethodParam {
997 fn repr(&self, tcx: &ctxt) -> String {
998 format!("MethodParam({},{})",
999 self.trait_ref.repr(tcx),
1004 impl Repr for typeck::MethodObject {
1005 fn repr(&self, tcx: &ctxt) -> String {
1006 format!("MethodObject({},{},{})",
1007 self.trait_ref.repr(tcx),
1013 impl Repr for ty::TraitStore {
1014 fn repr(&self, tcx: &ctxt) -> String {
1015 trait_store_to_string(tcx, *self)
1019 impl Repr for ty::BuiltinBound {
1020 fn repr(&self, _tcx: &ctxt) -> String {
1021 format!("{}", *self)
1025 impl UserString for ty::BuiltinBound {
1026 fn user_string(&self, _tcx: &ctxt) -> String {
1028 ty::BoundSend => "Send".to_string(),
1029 ty::BoundSized => "Sized".to_string(),
1030 ty::BoundCopy => "Copy".to_string(),
1031 ty::BoundSync => "Sync".to_string(),
1036 impl Repr for Span {
1037 fn repr(&self, tcx: &ctxt) -> String {
1038 tcx.sess.codemap().span_to_string(*self).to_string()
1042 impl<A:UserString> UserString for Rc<A> {
1043 fn user_string(&self, tcx: &ctxt) -> String {
1044 let this: &A = &**self;
1045 this.user_string(tcx)
1049 impl UserString for ty::ParamBounds {
1050 fn user_string(&self, tcx: &ctxt) -> String {
1051 let mut result = Vec::new();
1052 let s = self.builtin_bounds.user_string(tcx);
1056 for n in self.trait_bounds.iter() {
1057 result.push(n.user_string(tcx));
1063 impl UserString for ty::ExistentialBounds {
1064 fn user_string(&self, tcx: &ctxt) -> String {
1065 if self.builtin_bounds.contains_elem(ty::BoundSend) &&
1066 self.region_bound == ty::ReStatic
1067 { // Region bound is implied by builtin bounds:
1068 return self.builtin_bounds.repr(tcx);
1071 let mut res = Vec::new();
1073 let region_str = self.region_bound.user_string(tcx);
1074 if !region_str.is_empty() {
1075 res.push(region_str);
1078 for bound in self.builtin_bounds.iter() {
1079 res.push(bound.user_string(tcx));
1086 impl UserString for ty::BuiltinBounds {
1087 fn user_string(&self, tcx: &ctxt) -> String {
1089 .map(|bb| bb.user_string(tcx))
1090 .collect::<Vec<String>>()
1096 impl UserString for ty::TraitRef {
1097 fn user_string(&self, tcx: &ctxt) -> String {
1098 let base = ty::item_path_str(tcx, self.def_id);
1099 let trait_def = ty::lookup_trait_def(tcx, self.def_id);
1100 parameterized(tcx, base.as_slice(), &self.substs, &trait_def.generics)
1104 impl UserString for ty::t {
1105 fn user_string(&self, tcx: &ctxt) -> String {
1106 ty_to_string(tcx, *self)
1110 impl UserString for ast::Ident {
1111 fn user_string(&self, _tcx: &ctxt) -> String {
1112 token::get_name(self.name).get().to_string()
1116 impl Repr for abi::Abi {
1117 fn repr(&self, _tcx: &ctxt) -> String {
1122 impl UserString for abi::Abi {
1123 fn user_string(&self, _tcx: &ctxt) -> String {
1128 impl Repr for ty::UpvarId {
1129 fn repr(&self, tcx: &ctxt) -> String {
1130 format!("UpvarId({};`{}`;{})",
1132 ty::local_var_name_str(tcx, self.var_id),
1133 self.closure_expr_id)
1137 impl Repr for ast::Mutability {
1138 fn repr(&self, _tcx: &ctxt) -> String {
1139 format!("{}", *self)
1143 impl Repr for ty::BorrowKind {
1144 fn repr(&self, _tcx: &ctxt) -> String {
1145 format!("{}", *self)
1149 impl Repr for ty::UpvarBorrow {
1150 fn repr(&self, tcx: &ctxt) -> String {
1151 format!("UpvarBorrow({}, {})",
1152 self.kind.repr(tcx),
1153 self.region.repr(tcx))
1157 impl Repr for ty::IntVid {
1158 fn repr(&self, _tcx: &ctxt) -> String {
1163 impl Repr for ty::FloatVid {
1164 fn repr(&self, _tcx: &ctxt) -> String {
1169 impl Repr for ty::RegionVid {
1170 fn repr(&self, _tcx: &ctxt) -> String {
1175 impl Repr for ty::TyVid {
1176 fn repr(&self, _tcx: &ctxt) -> String {
1181 impl Repr for ty::IntVarValue {
1182 fn repr(&self, _tcx: &ctxt) -> String {
1183 format!("{}", *self)
1187 impl Repr for ast::IntTy {
1188 fn repr(&self, _tcx: &ctxt) -> String {
1189 format!("{}", *self)
1193 impl Repr for ast::UintTy {
1194 fn repr(&self, _tcx: &ctxt) -> String {
1195 format!("{}", *self)
1199 impl Repr for ast::FloatTy {
1200 fn repr(&self, _tcx: &ctxt) -> String {
1201 format!("{}", *self)
1205 impl Repr for ty::ExplicitSelfCategory {
1206 fn repr(&self, _: &ctxt) -> String {
1207 explicit_self_category_to_str(self).to_string()
1212 impl Repr for regionmanip::WfConstraint {
1213 fn repr(&self, tcx: &ctxt) -> String {
1215 regionmanip::RegionSubRegionConstraint(_, r_a, r_b) => {
1216 format!("RegionSubRegionConstraint({}, {})",
1221 regionmanip::RegionSubParamConstraint(_, r, p) => {
1222 format!("RegionSubParamConstraint({}, {})",
1230 impl UserString for ParamTy {
1231 fn user_string(&self, tcx: &ctxt) -> String {
1233 let did = self.def_id;
1234 let ident = match tcx.ty_param_defs.borrow().find(&did.node) {
1235 Some(def) => token::get_name(def.name).get().to_string(),
1237 // This can only happen when a type mismatch error happens and
1238 // the actual type has more type parameters than the expected one.
1239 None => format!("<generic #{}>", id),
1245 impl Repr for ParamTy {
1246 fn repr(&self, tcx: &ctxt) -> String {
1247 self.user_string(tcx)
1251 impl<A:Repr,B:Repr> Repr for (A,B) {
1252 fn repr(&self, tcx: &ctxt) -> String {
1253 let &(ref a, ref b) = self;
1254 format!("({},{})", a.repr(tcx), b.repr(tcx))