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.
11 use hir::def_id::DefId;
12 use hir::map::definitions::DefPathData;
13 use ty::subst::{self, Subst};
14 use ty::{BrAnon, BrEnv, BrFresh, BrNamed};
15 use ty::{TyBool, TyChar, TyAdt};
16 use ty::{TyError, TyStr, TyArray, TySlice, TyFloat, TyFnDef, TyFnPtr};
17 use ty::{TyParam, TyRawPtr, TyRef, TyNever, TyTuple};
18 use ty::{TyClosure, TyProjection, TyAnon};
19 use ty::{TyDynamic, TyInt, TyUint, TyInfer};
20 use ty::{self, Ty, TyCtxt, TypeFoldable};
27 use syntax::ast::CRATE_NODE_ID;
28 use syntax::symbol::Symbol;
31 pub fn verbose() -> bool {
32 ty::tls::with(|tcx| tcx.sess.verbose())
35 fn fn_sig(f: &mut fmt::Formatter,
41 let mut inputs = inputs.iter();
42 if let Some(&ty) = inputs.next() {
45 write!(f, ", {}", ty)?;
53 write!(f, " -> {}", output)?;
59 pub fn parameterized(f: &mut fmt::Formatter,
60 substs: &subst::Substs,
62 projections: &[ty::ProjectionPredicate])
64 let key = ty::tls::with(|tcx| tcx.def_key(did));
65 let mut item_name = if let Some(name) = key.disambiguated_data.data.get_opt_name() {
68 did.index = key.parent.unwrap_or_else(
69 || bug!("finding type for {:?}, encountered def-id {:?} with no parent",
71 parameterized(f, substs, did, projections)?;
72 return write!(f, "::{}", key.disambiguated_data.data.as_interned_str());
75 let mut verbose = false;
76 let mut num_supplied_defaults = 0;
77 let mut has_self = false;
78 let mut num_regions = 0;
79 let mut num_types = 0;
80 let mut is_value_path = false;
81 let fn_trait_kind = ty::tls::with(|tcx| {
82 // Unfortunately, some kinds of items (e.g., closures) don't have
83 // generics. So walk back up the find the closest parent that DOES
85 let mut item_def_id = did;
87 let key = tcx.def_key(item_def_id);
88 match key.disambiguated_data.data {
89 DefPathData::TypeNs(_) => {
92 DefPathData::ValueNs(_) | DefPathData::EnumVariant(_) => {
97 // if we're making a symbol for something, there ought
98 // to be a value or type-def or something in there
100 item_def_id.index = key.parent.unwrap_or_else(|| {
101 bug!("finding type for {:?}, encountered def-id {:?} with no \
102 parent", did, item_def_id);
107 let mut generics = tcx.item_generics(item_def_id);
108 let mut path_def_id = did;
109 verbose = tcx.sess.verbose();
110 has_self = generics.has_self;
112 let mut child_types = 0;
113 if let Some(def_id) = generics.parent {
115 assert!(is_value_path);
116 child_types = generics.types.len();
117 generics = tcx.item_generics(def_id);
118 num_regions = generics.regions.len();
119 num_types = generics.types.len();
122 write!(f, "<{} as ", substs.type_at(0))?;
125 path_def_id = def_id;
131 assert_eq!(has_self, false);
134 num_regions = generics.regions.len();
135 num_types = generics.types.len();
140 if generics.types.last().map_or(false, |def| def.has_default) {
141 if let Some(substs) = tcx.lift(&substs) {
142 let tps = substs.types().rev().skip(child_types);
143 for (def, actual) in generics.types.iter().rev().zip(tps) {
144 if !def.has_default {
147 if tcx.item_type(def.def_id).subst(tcx, substs) != actual {
150 num_supplied_defaults += 1;
156 write!(f, "{}", tcx.item_path_str(path_def_id))?;
157 Ok(tcx.lang_items.fn_trait_kind(path_def_id))
160 if !verbose && fn_trait_kind.is_some() && projections.len() == 1 {
161 let projection_ty = projections[0].ty;
162 if let TyTuple(ref args, _) = substs.type_at(1).sty {
163 return fn_sig(f, args, false, projection_ty);
167 let empty = Cell::new(true);
168 let start_or_continue = |f: &mut fmt::Formatter, start: &str, cont: &str| {
171 write!(f, "{}", start)
173 write!(f, "{}", cont)
177 let print_regions = |f: &mut fmt::Formatter, start: &str, skip, count| {
178 // Don't print any regions if they're all erased.
179 let regions = || substs.regions().skip(skip).take(count);
180 if regions().all(|r: &ty::Region| *r == ty::ReErased) {
184 for region in regions() {
185 let region: &ty::Region = region;
186 start_or_continue(f, start, ", ")?;
188 write!(f, "{:?}", region)?;
190 let s = region.to_string();
192 // This happens when the value of the region
193 // parameter is not easily serialized. This may be
194 // because the user omitted it in the first place,
195 // or because it refers to some block in the code,
196 // etc. I'm not sure how best to serialize this.
207 print_regions(f, "<", 0, num_regions)?;
209 let tps = substs.types().take(num_types - num_supplied_defaults)
210 .skip(has_self as usize);
213 start_or_continue(f, "<", ", ")?;
214 write!(f, "{}", ty)?;
217 for projection in projections {
218 start_or_continue(f, "<", ", ")?;
220 projection.projection_ty.item_name,
224 start_or_continue(f, "", ">")?;
226 // For values, also print their name and type parameters.
234 if let Some(item_name) = item_name {
235 write!(f, "::{}", item_name)?;
238 print_regions(f, "::<", num_regions, usize::MAX)?;
240 // FIXME: consider being smart with defaults here too
241 for ty in substs.types().skip(num_types) {
242 start_or_continue(f, "::<", ", ")?;
243 write!(f, "{}", ty)?;
246 start_or_continue(f, "", ">")?;
252 fn in_binder<'a, 'gcx, 'tcx, T, U>(f: &mut fmt::Formatter,
253 tcx: TyCtxt<'a, 'gcx, 'tcx>,
254 original: &ty::Binder<T>,
255 lifted: Option<ty::Binder<U>>) -> fmt::Result
256 where T: fmt::Display, U: fmt::Display + TypeFoldable<'tcx>
258 // Replace any anonymous late-bound regions with named
259 // variants, using gensym'd identifiers, so that we can
260 // clearly differentiate between named and unnamed regions in
261 // the output. We'll probably want to tweak this over time to
262 // decide just how much information to give.
263 let value = if let Some(v) = lifted {
266 return write!(f, "{}", original.0);
269 let mut empty = true;
270 let mut start_or_continue = |f: &mut fmt::Formatter, start: &str, cont: &str| {
273 write!(f, "{}", start)
275 write!(f, "{}", cont)
279 let new_value = tcx.replace_late_bound_regions(&value, |br| {
280 let _ = start_or_continue(f, "for<", ", ");
282 ty::BrNamed(_, name) => {
283 let _ = write!(f, "{}", name);
289 let name = Symbol::intern("'r");
290 let _ = write!(f, "{}", name);
291 ty::BrNamed(tcx.hir.local_def_id(CRATE_NODE_ID),
295 tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(1), br))
298 start_or_continue(f, "", "> ")?;
299 write!(f, "{}", new_value)
302 impl<'tcx> fmt::Display for &'tcx ty::Slice<ty::ExistentialPredicate<'tcx>> {
303 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
304 // Generate the main trait ref, including associated types.
305 ty::tls::with(|tcx| {
306 // Use a type that can't appear in defaults of type parameters.
307 let dummy_self = tcx.mk_infer(ty::FreshTy(0));
309 if let Some(p) = self.principal() {
310 let principal = tcx.lift(&p).expect("could not lift TraitRef for printing")
311 .with_self_ty(tcx, dummy_self);
312 let projections = self.projection_bounds().map(|p| {
314 .expect("could not lift projection for printing")
315 .with_self_ty(tcx, dummy_self)
316 }).collect::<Vec<_>>();
317 parameterized(f, principal.substs, principal.def_id, &projections)?;
321 for did in self.auto_traits() {
322 write!(f, " + {}", tcx.item_path_str(did))?;
332 impl fmt::Debug for ty::TypeParameterDef {
333 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
334 write!(f, "TypeParameterDef({}, {:?}, {})",
341 impl fmt::Debug for ty::RegionParameterDef {
342 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
343 write!(f, "RegionParameterDef({}, {:?}, {})",
350 impl<'tcx> fmt::Debug for ty::TyS<'tcx> {
351 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
352 write!(f, "{}", *self)
356 impl<'tcx> fmt::Display for ty::TypeAndMut<'tcx> {
357 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
359 if self.mutbl == hir::MutMutable { "mut " } else { "" },
364 impl<'tcx> fmt::Debug for ty::ItemSubsts<'tcx> {
365 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
366 write!(f, "ItemSubsts({:?})", self.substs)
370 impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
371 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
372 // when printing out the debug representation, we don't need
373 // to enumerate the `for<...>` etc because the debruijn index
374 // tells you everything you need to know.
375 write!(f, "<{:?} as {}>", self.self_ty(), *self)
379 impl<'tcx> fmt::Debug for ty::ExistentialTraitRef<'tcx> {
380 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
381 ty::tls::with(|tcx| {
382 let dummy_self = tcx.mk_infer(ty::FreshTy(0));
384 let trait_ref = tcx.lift(&ty::Binder(*self))
385 .expect("could not lift TraitRef for printing")
386 .with_self_ty(tcx, dummy_self).0;
387 parameterized(f, trait_ref.substs, trait_ref.def_id, &[])
392 impl fmt::Debug for ty::TraitDef {
393 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
394 ty::tls::with(|tcx| {
395 write!(f, "{}", tcx.item_path_str(self.def_id))
400 impl fmt::Debug for ty::AdtDef {
401 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
402 ty::tls::with(|tcx| {
403 write!(f, "{}", tcx.item_path_str(self.did))
408 impl<'tcx> fmt::Debug for ty::adjustment::Adjustment<'tcx> {
409 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
410 write!(f, "{:?} -> {}", self.kind, self.target)
414 impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
415 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
417 ty::Predicate::Trait(ref a) => write!(f, "{:?}", a),
418 ty::Predicate::Equate(ref pair) => write!(f, "{:?}", pair),
419 ty::Predicate::RegionOutlives(ref pair) => write!(f, "{:?}", pair),
420 ty::Predicate::TypeOutlives(ref pair) => write!(f, "{:?}", pair),
421 ty::Predicate::Projection(ref pair) => write!(f, "{:?}", pair),
422 ty::Predicate::WellFormed(ty) => write!(f, "WF({:?})", ty),
423 ty::Predicate::ObjectSafe(trait_def_id) => {
424 write!(f, "ObjectSafe({:?})", trait_def_id)
426 ty::Predicate::ClosureKind(closure_def_id, kind) => {
427 write!(f, "ClosureKind({:?}, {:?})", closure_def_id, kind)
433 impl fmt::Display for ty::BoundRegion {
434 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
436 return write!(f, "{:?}", *self);
440 BrNamed(_, name) => write!(f, "{}", name),
441 BrAnon(_) | BrFresh(_) | BrEnv => Ok(())
446 impl fmt::Debug for ty::BoundRegion {
447 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
449 BrAnon(n) => write!(f, "BrAnon({:?})", n),
450 BrFresh(n) => write!(f, "BrFresh({:?})", n),
451 BrNamed(did, name) => {
452 write!(f, "BrNamed({:?}:{:?}, {:?})",
453 did.krate, did.index, name)
455 BrEnv => "BrEnv".fmt(f),
460 impl fmt::Debug for ty::Region {
461 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
463 ty::ReEarlyBound(ref data) => {
464 write!(f, "ReEarlyBound({}, {})",
469 ty::ReLateBound(binder_id, ref bound_region) => {
470 write!(f, "ReLateBound({:?}, {:?})",
475 ty::ReFree(ref fr) => write!(f, "{:?}", fr),
478 write!(f, "ReScope({:?})", id)
481 ty::ReStatic => write!(f, "ReStatic"),
483 ty::ReVar(ref vid) => {
484 write!(f, "{:?}", vid)
487 ty::ReSkolemized(id, ref bound_region) => {
488 write!(f, "ReSkolemized({}, {:?})", id.index, bound_region)
491 ty::ReEmpty => write!(f, "ReEmpty"),
493 ty::ReErased => write!(f, "ReErased")
498 impl<'tcx> fmt::Debug for ty::ClosureUpvar<'tcx> {
499 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
500 write!(f, "ClosureUpvar({:?},{:?})",
506 impl<'tcx> fmt::Debug for ty::ParameterEnvironment<'tcx> {
507 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
508 write!(f, "ParameterEnvironment(\
510 implicit_region_bound={:?}, \
511 caller_bounds={:?})",
513 self.implicit_region_bound,
518 impl fmt::Display for ty::Region {
519 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
521 return write!(f, "{:?}", *self);
524 // These printouts are concise. They do not contain all the information
525 // the user might want to diagnose an error, but there is basically no way
526 // to fit that into a short string. Hence the recommendation to use
527 // `explain_region()` or `note_and_explain_region()`.
529 ty::ReEarlyBound(ref data) => {
530 write!(f, "{}", data.name)
532 ty::ReLateBound(_, br) |
533 ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
534 ty::ReSkolemized(_, br) => {
539 ty::ReErased => Ok(()),
540 ty::ReStatic => write!(f, "'static"),
541 ty::ReEmpty => write!(f, "'<empty>"),
546 impl fmt::Debug for ty::FreeRegion {
547 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
548 write!(f, "ReFree({:?}, {:?})",
549 self.scope, self.bound_region)
553 impl fmt::Debug for ty::Variance {
554 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
555 f.write_str(match *self {
556 ty::Covariant => "+",
557 ty::Contravariant => "-",
558 ty::Invariant => "o",
559 ty::Bivariant => "*",
564 impl<'tcx> fmt::Debug for ty::GenericPredicates<'tcx> {
565 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
566 write!(f, "GenericPredicates({:?})", self.predicates)
570 impl<'tcx> fmt::Debug for ty::InstantiatedPredicates<'tcx> {
571 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
572 write!(f, "InstantiatedPredicates({:?})",
577 impl<'tcx> fmt::Display for ty::FnSig<'tcx> {
578 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
579 if self.unsafety == hir::Unsafety::Unsafe {
580 write!(f, "unsafe ")?;
583 if self.abi != Abi::Rust {
584 write!(f, "extern {} ", self.abi)?;
588 fn_sig(f, self.inputs(), self.variadic, self.output())
592 impl fmt::Debug for ty::TyVid {
593 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
594 write!(f, "_#{}t", self.index)
598 impl fmt::Debug for ty::IntVid {
599 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
600 write!(f, "_#{}i", self.index)
604 impl fmt::Debug for ty::FloatVid {
605 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
606 write!(f, "_#{}f", self.index)
610 impl fmt::Debug for ty::RegionVid {
611 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
612 write!(f, "'_#{}r", self.index)
616 impl<'tcx> fmt::Debug for ty::FnSig<'tcx> {
617 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
618 write!(f, "({:?}; variadic: {})->{:?}", self.inputs(), self.variadic, self.output())
622 impl fmt::Debug for ty::InferTy {
623 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
625 ty::TyVar(ref v) => v.fmt(f),
626 ty::IntVar(ref v) => v.fmt(f),
627 ty::FloatVar(ref v) => v.fmt(f),
628 ty::FreshTy(v) => write!(f, "FreshTy({:?})", v),
629 ty::FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v),
630 ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v)
635 impl fmt::Debug for ty::IntVarValue {
636 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
638 ty::IntType(ref v) => v.fmt(f),
639 ty::UintType(ref v) => v.fmt(f),
644 // The generic impl doesn't work yet because projections are not
645 // normalized under HRTB.
646 /*impl<T> fmt::Display for ty::Binder<T>
647 where T: fmt::Display + for<'a> ty::Lift<'a>,
648 for<'a> <T as ty::Lift<'a>>::Lifted: fmt::Display + TypeFoldable<'a>
650 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
651 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
655 impl<'tcx> fmt::Display for ty::Binder<&'tcx ty::Slice<ty::ExistentialPredicate<'tcx>>> {
656 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
657 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
661 impl<'tcx> fmt::Display for ty::Binder<ty::TraitRef<'tcx>> {
662 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
663 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
667 impl<'tcx> fmt::Display for ty::Binder<ty::TraitPredicate<'tcx>> {
668 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
669 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
673 impl<'tcx> fmt::Display for ty::Binder<ty::EquatePredicate<'tcx>> {
674 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
675 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
679 impl<'tcx> fmt::Display for ty::Binder<ty::ProjectionPredicate<'tcx>> {
680 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
681 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
685 impl<'tcx> fmt::Display for ty::Binder<ty::OutlivesPredicate<Ty<'tcx>, &'tcx ty::Region>> {
686 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
687 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
691 impl<'tcx> fmt::Display for ty::Binder<ty::OutlivesPredicate<&'tcx ty::Region,
693 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
694 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
698 impl<'tcx> fmt::Display for ty::TraitRef<'tcx> {
699 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
700 parameterized(f, self.substs, self.def_id, &[])
704 impl<'tcx> fmt::Display for ty::TypeVariants<'tcx> {
705 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
707 TyBool => write!(f, "bool"),
708 TyChar => write!(f, "char"),
709 TyInt(t) => write!(f, "{}", t.ty_to_string()),
710 TyUint(t) => write!(f, "{}", t.ty_to_string()),
711 TyFloat(t) => write!(f, "{}", t.ty_to_string()),
712 TyRawPtr(ref tm) => {
713 write!(f, "*{} {}", match tm.mutbl {
714 hir::MutMutable => "mut",
715 hir::MutImmutable => "const",
718 TyRef(r, ref tm) => {
720 let s = r.to_string();
727 TyNever => write!(f, "!"),
728 TyTuple(ref tys, _) => {
730 let mut tys = tys.iter();
731 if let Some(&ty) = tys.next() {
732 write!(f, "{},", ty)?;
733 if let Some(&ty) = tys.next() {
734 write!(f, " {}", ty)?;
736 write!(f, ", {}", ty)?;
742 TyFnDef(def_id, substs, ref bare_fn) => {
743 write!(f, "{} {{", bare_fn.0)?;
744 parameterized(f, substs, def_id, &[])?;
747 TyFnPtr(ref bare_fn) => {
748 write!(f, "{}", bare_fn.0)
750 TyInfer(infer_ty) => write!(f, "{}", infer_ty),
751 TyError => write!(f, "[type error]"),
752 TyParam(ref param_ty) => write!(f, "{}", param_ty),
753 TyAdt(def, substs) => parameterized(f, substs, def.did, &[]),
754 TyDynamic(data, r) => {
755 write!(f, "{}", data)?;
756 let r = r.to_string();
758 write!(f, " + {}", r)
763 TyProjection(ref data) => write!(f, "{}", data),
764 TyAnon(def_id, substs) => {
765 ty::tls::with(|tcx| {
766 // Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
767 // by looking up the projections associated with the def_id.
768 let item_predicates = tcx.item_predicates(def_id);
769 let substs = tcx.lift(&substs).unwrap_or_else(|| {
770 tcx.intern_substs(&[])
772 let bounds = item_predicates.instantiate(tcx, substs);
774 let mut first = true;
775 let mut is_sized = false;
777 for predicate in bounds.predicates {
778 if let Some(trait_ref) = predicate.to_opt_poly_trait_ref() {
779 // Don't print +Sized, but rather +?Sized if absent.
780 if Some(trait_ref.def_id()) == tcx.lang_items.sized_trait() {
785 write!(f, "{}{}", if first { " " } else { "+" }, trait_ref)?;
790 write!(f, "{}?Sized", if first { " " } else { "+" })?;
795 TyStr => write!(f, "str"),
796 TyClosure(did, substs) => ty::tls::with(|tcx| {
797 let upvar_tys = substs.upvar_tys(did, tcx);
798 write!(f, "[closure")?;
800 if let Some(node_id) = tcx.hir.as_local_node_id(did) {
801 write!(f, "@{:?}", tcx.hir.span(node_id))?;
803 tcx.with_freevars(node_id, |freevars| {
804 for (freevar, upvar_ty) in freevars.iter().zip(upvar_tys) {
805 let def_id = freevar.def.def_id();
806 let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
810 tcx.local_var_name_str(node_id),
817 // cross-crate closure types should only be
818 // visible in trans bug reports, I imagine.
819 write!(f, "@{:?}", did)?;
821 for (index, upvar_ty) in upvar_tys.enumerate() {
822 write!(f, "{}{}:{}", sep, index, upvar_ty)?;
829 TyArray(ty, sz) => write!(f, "[{}; {}]", ty, sz),
830 TySlice(ty) => write!(f, "[{}]", ty)
835 impl<'tcx> fmt::Display for ty::TyS<'tcx> {
836 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
837 write!(f, "{}", self.sty)
841 impl fmt::Debug for ty::UpvarId {
842 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
843 write!(f, "UpvarId({};`{}`;{})",
845 ty::tls::with(|tcx| tcx.local_var_name_str(self.var_id)),
846 self.closure_expr_id)
850 impl<'tcx> fmt::Debug for ty::UpvarBorrow<'tcx> {
851 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
852 write!(f, "UpvarBorrow({:?}, {:?})",
853 self.kind, self.region)
857 impl fmt::Display for ty::InferTy {
858 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
859 let print_var_ids = verbose();
861 ty::TyVar(ref vid) if print_var_ids => write!(f, "{:?}", vid),
862 ty::IntVar(ref vid) if print_var_ids => write!(f, "{:?}", vid),
863 ty::FloatVar(ref vid) if print_var_ids => write!(f, "{:?}", vid),
864 ty::TyVar(_) => write!(f, "_"),
865 ty::IntVar(_) => write!(f, "{}", "{integer}"),
866 ty::FloatVar(_) => write!(f, "{}", "{float}"),
867 ty::FreshTy(v) => write!(f, "FreshTy({})", v),
868 ty::FreshIntTy(v) => write!(f, "FreshIntTy({})", v),
869 ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({})", v)
874 impl fmt::Display for ty::ParamTy {
875 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
876 write!(f, "{}", self.name)
880 impl fmt::Debug for ty::ParamTy {
881 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
882 write!(f, "{}/#{}", self, self.idx)
886 impl<'tcx, T, U> fmt::Display for ty::OutlivesPredicate<T,U>
887 where T: fmt::Display, U: fmt::Display
889 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
890 write!(f, "{} : {}", self.0, self.1)
894 impl<'tcx> fmt::Display for ty::EquatePredicate<'tcx> {
895 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
896 write!(f, "{} == {}", self.0, self.1)
900 impl<'tcx> fmt::Debug for ty::TraitPredicate<'tcx> {
901 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
902 write!(f, "TraitPredicate({:?})",
907 impl<'tcx> fmt::Display for ty::TraitPredicate<'tcx> {
908 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
909 write!(f, "{}: {}", self.trait_ref.self_ty(), self.trait_ref)
913 impl<'tcx> fmt::Debug for ty::ProjectionPredicate<'tcx> {
914 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
915 write!(f, "ProjectionPredicate({:?}, {:?})",
921 impl<'tcx> fmt::Display for ty::ProjectionPredicate<'tcx> {
922 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
923 write!(f, "{} == {}",
929 impl<'tcx> fmt::Display for ty::ProjectionTy<'tcx> {
930 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
931 write!(f, "{:?}::{}",
937 impl fmt::Display for ty::ClosureKind {
938 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
940 ty::ClosureKind::Fn => write!(f, "Fn"),
941 ty::ClosureKind::FnMut => write!(f, "FnMut"),
942 ty::ClosureKind::FnOnce => write!(f, "FnOnce"),
947 impl<'tcx> fmt::Display for ty::Predicate<'tcx> {
948 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
950 ty::Predicate::Trait(ref data) => write!(f, "{}", data),
951 ty::Predicate::Equate(ref predicate) => write!(f, "{}", predicate),
952 ty::Predicate::RegionOutlives(ref predicate) => write!(f, "{}", predicate),
953 ty::Predicate::TypeOutlives(ref predicate) => write!(f, "{}", predicate),
954 ty::Predicate::Projection(ref predicate) => write!(f, "{}", predicate),
955 ty::Predicate::WellFormed(ty) => write!(f, "{} well-formed", ty),
956 ty::Predicate::ObjectSafe(trait_def_id) =>
957 ty::tls::with(|tcx| {
958 write!(f, "the trait `{}` is object-safe", tcx.item_path_str(trait_def_id))
960 ty::Predicate::ClosureKind(closure_def_id, kind) =>
961 ty::tls::with(|tcx| {
962 write!(f, "the closure `{}` implements the trait `{}`",
963 tcx.item_path_str(closure_def_id), kind)