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.default.is_some()) {
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.default.subst(tcx, substs) != Some(actual) {
147 num_supplied_defaults += 1;
153 write!(f, "{}", tcx.item_path_str(path_def_id))?;
154 Ok(tcx.lang_items.fn_trait_kind(path_def_id))
157 if !verbose && fn_trait_kind.is_some() && projections.len() == 1 {
158 let projection_ty = projections[0].ty;
159 if let TyTuple(ref args, _) = substs.type_at(1).sty {
160 return fn_sig(f, args, false, projection_ty);
164 let empty = Cell::new(true);
165 let start_or_continue = |f: &mut fmt::Formatter, start: &str, cont: &str| {
168 write!(f, "{}", start)
170 write!(f, "{}", cont)
174 let print_regions = |f: &mut fmt::Formatter, start: &str, skip, count| {
175 // Don't print any regions if they're all erased.
176 let regions = || substs.regions().skip(skip).take(count);
177 if regions().all(|r: &ty::Region| *r == ty::ReErased) {
181 for region in regions() {
182 let region: &ty::Region = region;
183 start_or_continue(f, start, ", ")?;
185 write!(f, "{:?}", region)?;
187 let s = region.to_string();
189 // This happens when the value of the region
190 // parameter is not easily serialized. This may be
191 // because the user omitted it in the first place,
192 // or because it refers to some block in the code,
193 // etc. I'm not sure how best to serialize this.
204 print_regions(f, "<", 0, num_regions)?;
206 let tps = substs.types().take(num_types - num_supplied_defaults)
207 .skip(has_self as usize);
210 start_or_continue(f, "<", ", ")?;
211 write!(f, "{}", ty)?;
214 for projection in projections {
215 start_or_continue(f, "<", ", ")?;
217 projection.projection_ty.item_name,
221 start_or_continue(f, "", ">")?;
223 // For values, also print their name and type parameters.
231 if let Some(item_name) = item_name {
232 write!(f, "::{}", item_name)?;
235 print_regions(f, "::<", num_regions, usize::MAX)?;
237 // FIXME: consider being smart with defaults here too
238 for ty in substs.types().skip(num_types) {
239 start_or_continue(f, "::<", ", ")?;
240 write!(f, "{}", ty)?;
243 start_or_continue(f, "", ">")?;
249 fn in_binder<'a, 'gcx, 'tcx, T, U>(f: &mut fmt::Formatter,
250 tcx: TyCtxt<'a, 'gcx, 'tcx>,
251 original: &ty::Binder<T>,
252 lifted: Option<ty::Binder<U>>) -> fmt::Result
253 where T: fmt::Display, U: fmt::Display + TypeFoldable<'tcx>
255 // Replace any anonymous late-bound regions with named
256 // variants, using gensym'd identifiers, so that we can
257 // clearly differentiate between named and unnamed regions in
258 // the output. We'll probably want to tweak this over time to
259 // decide just how much information to give.
260 let value = if let Some(v) = lifted {
263 return write!(f, "{}", original.0);
266 let mut empty = true;
267 let mut start_or_continue = |f: &mut fmt::Formatter, start: &str, cont: &str| {
270 write!(f, "{}", start)
272 write!(f, "{}", cont)
276 let new_value = tcx.replace_late_bound_regions(&value, |br| {
277 let _ = start_or_continue(f, "for<", ", ");
279 ty::BrNamed(_, name, _) => {
280 let _ = write!(f, "{}", name);
286 let name = Symbol::intern("'r");
287 let _ = write!(f, "{}", name);
288 ty::BrNamed(tcx.hir.local_def_id(CRATE_NODE_ID),
290 ty::Issue32330::WontChange)
293 tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(1), br))
296 start_or_continue(f, "", "> ")?;
297 write!(f, "{}", new_value)
300 impl<'tcx> fmt::Display for &'tcx ty::Slice<ty::ExistentialPredicate<'tcx>> {
301 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
302 // Generate the main trait ref, including associated types.
303 ty::tls::with(|tcx| {
304 // Use a type that can't appear in defaults of type parameters.
305 let dummy_self = tcx.mk_infer(ty::FreshTy(0));
307 if let Some(p) = self.principal() {
308 let principal = tcx.lift(&p).expect("could not lift TraitRef for printing")
309 .with_self_ty(tcx, dummy_self);
310 let projections = self.projection_bounds().map(|p| {
312 .expect("could not lift projection for printing")
313 .with_self_ty(tcx, dummy_self)
314 }).collect::<Vec<_>>();
315 parameterized(f, principal.substs, principal.def_id, &projections)?;
319 for did in self.auto_traits() {
320 write!(f, " + {}", tcx.item_path_str(did))?;
330 impl<'tcx> fmt::Debug for ty::TypeParameterDef<'tcx> {
331 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
332 write!(f, "TypeParameterDef({}, {:?}, {})",
339 impl fmt::Debug for ty::RegionParameterDef {
340 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
341 write!(f, "RegionParameterDef({}, {:?}, {})",
348 impl<'tcx> fmt::Debug for ty::TyS<'tcx> {
349 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
350 write!(f, "{}", *self)
354 impl<'tcx> fmt::Display for ty::TypeAndMut<'tcx> {
355 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
357 if self.mutbl == hir::MutMutable { "mut " } else { "" },
362 impl<'tcx> fmt::Debug for ty::ItemSubsts<'tcx> {
363 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
364 write!(f, "ItemSubsts({:?})", self.substs)
368 impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
369 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
370 // when printing out the debug representation, we don't need
371 // to enumerate the `for<...>` etc because the debruijn index
372 // tells you everything you need to know.
373 write!(f, "<{:?} as {}>", self.self_ty(), *self)
377 impl<'tcx> fmt::Debug for ty::ExistentialTraitRef<'tcx> {
378 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
379 ty::tls::with(|tcx| {
380 let dummy_self = tcx.mk_infer(ty::FreshTy(0));
382 let trait_ref = tcx.lift(&ty::Binder(*self))
383 .expect("could not lift TraitRef for printing")
384 .with_self_ty(tcx, dummy_self).0;
385 parameterized(f, trait_ref.substs, trait_ref.def_id, &[])
390 impl fmt::Debug for ty::TraitDef {
391 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
392 ty::tls::with(|tcx| {
393 write!(f, "{}", tcx.item_path_str(self.def_id))
398 impl fmt::Debug for ty::AdtDef {
399 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
400 ty::tls::with(|tcx| {
401 write!(f, "{}", tcx.item_path_str(self.did))
406 impl<'tcx> fmt::Debug for ty::adjustment::Adjustment<'tcx> {
407 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
408 write!(f, "{:?} -> {}", self.kind, self.target)
412 impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
413 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
415 ty::Predicate::Trait(ref a) => write!(f, "{:?}", a),
416 ty::Predicate::Equate(ref pair) => write!(f, "{:?}", pair),
417 ty::Predicate::RegionOutlives(ref pair) => write!(f, "{:?}", pair),
418 ty::Predicate::TypeOutlives(ref pair) => write!(f, "{:?}", pair),
419 ty::Predicate::Projection(ref pair) => write!(f, "{:?}", pair),
420 ty::Predicate::WellFormed(ty) => write!(f, "WF({:?})", ty),
421 ty::Predicate::ObjectSafe(trait_def_id) => {
422 write!(f, "ObjectSafe({:?})", trait_def_id)
424 ty::Predicate::ClosureKind(closure_def_id, kind) => {
425 write!(f, "ClosureKind({:?}, {:?})", closure_def_id, kind)
431 impl fmt::Display for ty::BoundRegion {
432 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
434 return write!(f, "{:?}", *self);
438 BrNamed(_, name, _) => write!(f, "{}", name),
439 BrAnon(_) | BrFresh(_) | BrEnv => Ok(())
444 impl fmt::Debug for ty::BoundRegion {
445 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
447 BrAnon(n) => write!(f, "BrAnon({:?})", n),
448 BrFresh(n) => write!(f, "BrFresh({:?})", n),
449 BrNamed(did, name, issue32330) => {
450 write!(f, "BrNamed({:?}:{:?}, {:?}, {:?})",
451 did.krate, did.index, name, issue32330)
453 BrEnv => "BrEnv".fmt(f),
458 impl fmt::Debug for ty::Region {
459 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
461 ty::ReEarlyBound(ref data) => {
462 write!(f, "ReEarlyBound({}, {})",
467 ty::ReLateBound(binder_id, ref bound_region) => {
468 write!(f, "ReLateBound({:?}, {:?})",
473 ty::ReFree(ref fr) => write!(f, "{:?}", fr),
476 write!(f, "ReScope({:?})", id)
479 ty::ReStatic => write!(f, "ReStatic"),
481 ty::ReVar(ref vid) => {
482 write!(f, "{:?}", vid)
485 ty::ReSkolemized(id, ref bound_region) => {
486 write!(f, "ReSkolemized({}, {:?})", id.index, bound_region)
489 ty::ReEmpty => write!(f, "ReEmpty"),
491 ty::ReErased => write!(f, "ReErased")
496 impl<'tcx> fmt::Debug for ty::ClosureTy<'tcx> {
497 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
498 write!(f, "ClosureTy({},{:?},{})",
505 impl<'tcx> fmt::Debug for ty::ClosureUpvar<'tcx> {
506 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
507 write!(f, "ClosureUpvar({:?},{:?})",
513 impl<'tcx> fmt::Debug for ty::ParameterEnvironment<'tcx> {
514 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
515 write!(f, "ParameterEnvironment(\
517 implicit_region_bound={:?}, \
518 caller_bounds={:?})",
520 self.implicit_region_bound,
525 impl fmt::Display for ty::Region {
526 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
528 return write!(f, "{:?}", *self);
531 // These printouts are concise. They do not contain all the information
532 // the user might want to diagnose an error, but there is basically no way
533 // to fit that into a short string. Hence the recommendation to use
534 // `explain_region()` or `note_and_explain_region()`.
536 ty::ReEarlyBound(ref data) => {
537 write!(f, "{}", data.name)
539 ty::ReLateBound(_, br) |
540 ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
541 ty::ReSkolemized(_, br) => {
546 ty::ReErased => Ok(()),
547 ty::ReStatic => write!(f, "'static"),
548 ty::ReEmpty => write!(f, "'<empty>"),
553 impl fmt::Debug for ty::FreeRegion {
554 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
555 write!(f, "ReFree({:?}, {:?})",
556 self.scope, self.bound_region)
560 impl fmt::Debug for ty::Variance {
561 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
562 f.write_str(match *self {
563 ty::Covariant => "+",
564 ty::Contravariant => "-",
565 ty::Invariant => "o",
566 ty::Bivariant => "*",
571 impl<'tcx> fmt::Debug for ty::GenericPredicates<'tcx> {
572 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
573 write!(f, "GenericPredicates({:?})", self.predicates)
577 impl<'tcx> fmt::Debug for ty::InstantiatedPredicates<'tcx> {
578 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
579 write!(f, "InstantiatedPredicates({:?})",
584 impl<'tcx> fmt::Display for ty::FnSig<'tcx> {
585 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
587 fn_sig(f, self.inputs(), self.variadic, self.output())
591 impl fmt::Debug for ty::TyVid {
592 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
593 write!(f, "_#{}t", self.index)
597 impl fmt::Debug for ty::IntVid {
598 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
599 write!(f, "_#{}i", self.index)
603 impl fmt::Debug for ty::FloatVid {
604 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
605 write!(f, "_#{}f", self.index)
609 impl fmt::Debug for ty::RegionVid {
610 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
611 write!(f, "'_#{}r", self.index)
615 impl<'tcx> fmt::Debug for ty::FnSig<'tcx> {
616 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
617 write!(f, "({:?}; variadic: {})->{:?}", self.inputs(), self.variadic, self.output())
621 impl fmt::Debug for ty::InferTy {
622 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
624 ty::TyVar(ref v) => v.fmt(f),
625 ty::IntVar(ref v) => v.fmt(f),
626 ty::FloatVar(ref v) => v.fmt(f),
627 ty::FreshTy(v) => write!(f, "FreshTy({:?})", v),
628 ty::FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v),
629 ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v)
634 impl fmt::Debug for ty::IntVarValue {
635 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
637 ty::IntType(ref v) => v.fmt(f),
638 ty::UintType(ref v) => v.fmt(f),
643 // The generic impl doesn't work yet because projections are not
644 // normalized under HRTB.
645 /*impl<T> fmt::Display for ty::Binder<T>
646 where T: fmt::Display + for<'a> ty::Lift<'a>,
647 for<'a> <T as ty::Lift<'a>>::Lifted: fmt::Display + TypeFoldable<'a>
649 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
650 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
654 impl<'tcx> fmt::Display for ty::Binder<&'tcx ty::Slice<ty::ExistentialPredicate<'tcx>>> {
655 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
656 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
660 impl<'tcx> fmt::Display for ty::Binder<ty::TraitRef<'tcx>> {
661 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
662 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
666 impl<'tcx> fmt::Display for ty::Binder<ty::TraitPredicate<'tcx>> {
667 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
668 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
672 impl<'tcx> fmt::Display for ty::Binder<ty::EquatePredicate<'tcx>> {
673 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
674 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
678 impl<'tcx> fmt::Display for ty::Binder<ty::ProjectionPredicate<'tcx>> {
679 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
680 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
684 impl<'tcx> fmt::Display for ty::Binder<ty::OutlivesPredicate<Ty<'tcx>, &'tcx ty::Region>> {
685 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
686 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
690 impl<'tcx> fmt::Display for ty::Binder<ty::OutlivesPredicate<&'tcx ty::Region,
692 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
693 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
697 impl<'tcx> fmt::Display for ty::TraitRef<'tcx> {
698 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
699 parameterized(f, self.substs, self.def_id, &[])
703 impl<'tcx> fmt::Display for ty::TypeVariants<'tcx> {
704 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
706 TyBool => write!(f, "bool"),
707 TyChar => write!(f, "char"),
708 TyInt(t) => write!(f, "{}", t.ty_to_string()),
709 TyUint(t) => write!(f, "{}", t.ty_to_string()),
710 TyFloat(t) => write!(f, "{}", t.ty_to_string()),
711 TyRawPtr(ref tm) => {
712 write!(f, "*{} {}", match tm.mutbl {
713 hir::MutMutable => "mut",
714 hir::MutImmutable => "const",
717 TyRef(r, ref tm) => {
719 let s = r.to_string();
726 TyNever => write!(f, "!"),
727 TyTuple(ref tys, _) => {
729 let mut tys = tys.iter();
730 if let Some(&ty) = tys.next() {
731 write!(f, "{},", ty)?;
732 if let Some(&ty) = tys.next() {
733 write!(f, " {}", ty)?;
735 write!(f, ", {}", ty)?;
741 TyFnDef(def_id, substs, ref bare_fn) => {
742 if bare_fn.unsafety == hir::Unsafety::Unsafe {
743 write!(f, "unsafe ")?;
746 if bare_fn.abi != Abi::Rust {
747 write!(f, "extern {} ", bare_fn.abi)?;
750 write!(f, "{} {{", bare_fn.sig.0)?;
751 parameterized(f, substs, def_id, &[])?;
754 TyFnPtr(ref bare_fn) => {
755 if bare_fn.unsafety == hir::Unsafety::Unsafe {
756 write!(f, "unsafe ")?;
759 if bare_fn.abi != Abi::Rust {
760 write!(f, "extern {} ", bare_fn.abi)?;
763 write!(f, "{}", bare_fn.sig.0)
765 TyInfer(infer_ty) => write!(f, "{}", infer_ty),
766 TyError => write!(f, "[type error]"),
767 TyParam(ref param_ty) => write!(f, "{}", param_ty),
768 TyAdt(def, substs) => {
769 ty::tls::with(|tcx| {
770 if def.did.is_local() &&
771 !tcx.item_types.borrow().contains_key(&def.did) {
772 write!(f, "{}<..>", tcx.item_path_str(def.did))
774 parameterized(f, substs, def.did, &[])
778 TyDynamic(data, r) => {
779 write!(f, "{}", data)?;
780 let r = r.to_string();
782 write!(f, " + {}", r)
787 TyProjection(ref data) => write!(f, "{}", data),
788 TyAnon(def_id, substs) => {
789 ty::tls::with(|tcx| {
790 // Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
791 // by looking up the projections associated with the def_id.
792 let item_predicates = tcx.item_predicates(def_id);
793 let substs = tcx.lift(&substs).unwrap_or_else(|| {
794 tcx.intern_substs(&[])
796 let bounds = item_predicates.instantiate(tcx, substs);
798 let mut first = true;
799 let mut is_sized = false;
801 for predicate in bounds.predicates {
802 if let Some(trait_ref) = predicate.to_opt_poly_trait_ref() {
803 // Don't print +Sized, but rather +?Sized if absent.
804 if Some(trait_ref.def_id()) == tcx.lang_items.sized_trait() {
809 write!(f, "{}{}", if first { " " } else { "+" }, trait_ref)?;
814 write!(f, "{}?Sized", if first { " " } else { "+" })?;
819 TyStr => write!(f, "str"),
820 TyClosure(did, substs) => ty::tls::with(|tcx| {
821 let upvar_tys = substs.upvar_tys(did, tcx);
822 write!(f, "[closure")?;
824 if let Some(node_id) = tcx.hir.as_local_node_id(did) {
825 write!(f, "@{:?}", tcx.hir.span(node_id))?;
827 tcx.with_freevars(node_id, |freevars| {
828 for (freevar, upvar_ty) in freevars.iter().zip(upvar_tys) {
829 let def_id = freevar.def.def_id();
830 let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
834 tcx.local_var_name_str(node_id),
841 // cross-crate closure types should only be
842 // visible in trans bug reports, I imagine.
843 write!(f, "@{:?}", did)?;
845 for (index, upvar_ty) in upvar_tys.enumerate() {
846 write!(f, "{}{}:{}", sep, index, upvar_ty)?;
853 TyArray(ty, sz) => write!(f, "[{}; {}]", ty, sz),
854 TySlice(ty) => write!(f, "[{}]", ty)
859 impl<'tcx> fmt::Display for ty::TyS<'tcx> {
860 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
861 write!(f, "{}", self.sty)
865 impl fmt::Debug for ty::UpvarId {
866 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
867 write!(f, "UpvarId({};`{}`;{})",
869 ty::tls::with(|tcx| tcx.local_var_name_str(self.var_id)),
870 self.closure_expr_id)
874 impl<'tcx> fmt::Debug for ty::UpvarBorrow<'tcx> {
875 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
876 write!(f, "UpvarBorrow({:?}, {:?})",
877 self.kind, self.region)
881 impl fmt::Display for ty::InferTy {
882 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
883 let print_var_ids = verbose();
885 ty::TyVar(ref vid) if print_var_ids => write!(f, "{:?}", vid),
886 ty::IntVar(ref vid) if print_var_ids => write!(f, "{:?}", vid),
887 ty::FloatVar(ref vid) if print_var_ids => write!(f, "{:?}", vid),
888 ty::TyVar(_) => write!(f, "_"),
889 ty::IntVar(_) => write!(f, "{}", "{integer}"),
890 ty::FloatVar(_) => write!(f, "{}", "{float}"),
891 ty::FreshTy(v) => write!(f, "FreshTy({})", v),
892 ty::FreshIntTy(v) => write!(f, "FreshIntTy({})", v),
893 ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({})", v)
898 impl fmt::Display for ty::ParamTy {
899 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
900 write!(f, "{}", self.name)
904 impl fmt::Debug for ty::ParamTy {
905 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
906 write!(f, "{}/#{}", self, self.idx)
910 impl<'tcx, T, U> fmt::Display for ty::OutlivesPredicate<T,U>
911 where T: fmt::Display, U: fmt::Display
913 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
914 write!(f, "{} : {}", self.0, self.1)
918 impl<'tcx> fmt::Display for ty::EquatePredicate<'tcx> {
919 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
920 write!(f, "{} == {}", self.0, self.1)
924 impl<'tcx> fmt::Debug for ty::TraitPredicate<'tcx> {
925 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
926 write!(f, "TraitPredicate({:?})",
931 impl<'tcx> fmt::Display for ty::TraitPredicate<'tcx> {
932 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
933 write!(f, "{}: {}", self.trait_ref.self_ty(), self.trait_ref)
937 impl<'tcx> fmt::Debug for ty::ProjectionPredicate<'tcx> {
938 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
939 write!(f, "ProjectionPredicate({:?}, {:?})",
945 impl<'tcx> fmt::Display for ty::ProjectionPredicate<'tcx> {
946 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
947 write!(f, "{} == {}",
953 impl<'tcx> fmt::Display for ty::ProjectionTy<'tcx> {
954 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
955 write!(f, "{:?}::{}",
961 impl fmt::Display for ty::ClosureKind {
962 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
964 ty::ClosureKind::Fn => write!(f, "Fn"),
965 ty::ClosureKind::FnMut => write!(f, "FnMut"),
966 ty::ClosureKind::FnOnce => write!(f, "FnOnce"),
971 impl<'tcx> fmt::Display for ty::Predicate<'tcx> {
972 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
974 ty::Predicate::Trait(ref data) => write!(f, "{}", data),
975 ty::Predicate::Equate(ref predicate) => write!(f, "{}", predicate),
976 ty::Predicate::RegionOutlives(ref predicate) => write!(f, "{}", predicate),
977 ty::Predicate::TypeOutlives(ref predicate) => write!(f, "{}", predicate),
978 ty::Predicate::Projection(ref predicate) => write!(f, "{}", predicate),
979 ty::Predicate::WellFormed(ty) => write!(f, "{} well-formed", ty),
980 ty::Predicate::ObjectSafe(trait_def_id) =>
981 ty::tls::with(|tcx| {
982 write!(f, "the trait `{}` is object-safe", tcx.item_path_str(trait_def_id))
984 ty::Predicate::ClosureKind(closure_def_id, kind) =>
985 ty::tls::with(|tcx| {
986 write!(f, "the closure `{}` implements the trait `{}`",
987 tcx.item_path_str(closure_def_id), kind)