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 ty
::subst
::{self, Subst, Substs}
;
13 use hir
::map
::definitions
::DefPathData
;
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}
;
19 use ty
::{TyBox, TyTrait, TyInt, TyUint, TyInfer}
;
20 use ty
::{self, Ty, TyCtxt, TypeFoldable}
;
21 use ty
::fold
::{TypeFolder, TypeVisitor}
;
28 use syntax
::parse
::token
;
29 use syntax
::ast
::CRATE_NODE_ID
;
32 pub fn verbose() -> bool
{
33 ty
::tls
::with(|tcx
| tcx
.sess
.verbose())
36 fn fn_sig(f
: &mut fmt
::Formatter
,
42 let mut inputs
= inputs
.iter();
43 if let Some(&ty
) = inputs
.next() {
46 write
!(f
, ", {}", ty
)?
;
54 write
!(f
, " -> {}", output
)?
;
60 pub fn parameterized(f
: &mut fmt
::Formatter
,
61 substs
: &subst
::Substs
,
63 projections
: &[ty
::ProjectionPredicate
])
65 let key
= ty
::tls
::with(|tcx
| tcx
.def_key(did
));
66 let mut item_name
= if let Some(name
) = key
.disambiguated_data
.data
.get_opt_name() {
69 did
.index
= key
.parent
.unwrap_or_else(
70 || bug
!("finding type for {:?}, encountered def-id {:?} with no parent",
72 parameterized(f
, substs
, did
, projections
)?
;
73 return write
!(f
, "::{}", key
.disambiguated_data
.data
.as_interned_str());
76 let mut verbose
= false;
77 let mut num_supplied_defaults
= 0;
78 let mut has_self
= false;
79 let mut num_regions
= 0;
80 let mut num_types
= 0;
81 let mut is_value_path
= false;
82 let fn_trait_kind
= ty
::tls
::with(|tcx
| {
83 // Unfortunately, some kinds of items (e.g., closures) don't have
84 // generics. So walk back up the find the closest parent that DOES
86 let mut item_def_id
= did
;
88 let key
= tcx
.def_key(item_def_id
);
89 match key
.disambiguated_data
.data
{
90 DefPathData
::TypeNs(_
) => {
93 DefPathData
::ValueNs(_
) | DefPathData
::EnumVariant(_
) => {
98 // if we're making a symbol for something, there ought
99 // to be a value or type-def or something in there
101 item_def_id
.index
= key
.parent
.unwrap_or_else(|| {
102 bug
!("finding type for {:?}, encountered def-id {:?} with no \
103 parent", did
, item_def_id
);
108 let mut generics
= tcx
.lookup_generics(item_def_id
);
109 let mut path_def_id
= did
;
110 verbose
= tcx
.sess
.verbose();
111 has_self
= generics
.has_self
;
113 let mut child_types
= 0;
114 if let Some(def_id
) = generics
.parent
{
116 assert
!(is_value_path
);
117 child_types
= generics
.types
.len();
118 generics
= tcx
.lookup_generics(def_id
);
119 num_regions
= generics
.regions
.len();
120 num_types
= generics
.types
.len();
123 write
!(f
, "<{} as ", substs
.type_at(0))?
;
126 path_def_id
= def_id
;
132 assert_eq
!(has_self
, false);
135 num_regions
= generics
.regions
.len();
136 num_types
= generics
.types
.len();
141 if generics
.types
.last().map_or(false, |def
| def
.default.is_some()) {
142 if let Some(substs
) = tcx
.lift(&substs
) {
143 let tps
= substs
.types().rev().skip(child_types
);
144 for (def
, actual
) in generics
.types
.iter().rev().zip(tps
) {
145 if def
.default.subst(tcx
, substs
) != Some(actual
) {
148 num_supplied_defaults
+= 1;
154 write
!(f
, "{}", tcx
.item_path_str(path_def_id
))?
;
155 Ok(tcx
.lang_items
.fn_trait_kind(path_def_id
))
158 if !verbose
&& fn_trait_kind
.is_some() && projections
.len() == 1 {
159 let projection_ty
= projections
[0].ty
;
160 if let TyTuple(ref args
) = substs
.type_at(1).sty
{
161 return fn_sig(f
, args
, false, projection_ty
);
165 let empty
= Cell
::new(true);
166 let start_or_continue
= |f
: &mut fmt
::Formatter
, start
: &str, cont
: &str| {
169 write
!(f
, "{}", start
)
171 write
!(f
, "{}", cont
)
175 let print_regions
= |f
: &mut fmt
::Formatter
, start
: &str, skip
, count
| {
176 // Don't print any regions if they're all erased.
177 let regions
= || substs
.regions().skip(skip
).take(count
);
178 if regions().all(|r
: &ty
::Region
| *r
== ty
::ReErased
) {
182 for region
in regions() {
183 let region
: &ty
::Region
= region
;
184 start_or_continue(f
, start
, ", ")?
;
186 write
!(f
, "{:?}", region
)?
;
188 let s
= region
.to_string();
190 // This happens when the value of the region
191 // parameter is not easily serialized. This may be
192 // because the user omitted it in the first place,
193 // or because it refers to some block in the code,
194 // etc. I'm not sure how best to serialize this.
205 print_regions(f
, "<", 0, num_regions
)?
;
207 let tps
= substs
.types().take(num_types
- num_supplied_defaults
)
208 .skip(has_self
as usize);
211 start_or_continue(f
, "<", ", ")?
;
212 write
!(f
, "{}", ty
)?
;
215 for projection
in projections
{
216 start_or_continue(f
, "<", ", ")?
;
218 projection
.projection_ty
.item_name
,
222 start_or_continue(f
, "", ">")?
;
224 // For values, also print their name and type parameters.
232 if let Some(item_name
) = item_name
{
233 write
!(f
, "::{}", item_name
)?
;
236 print_regions(f
, "::<", num_regions
, usize::MAX
)?
;
238 // FIXME: consider being smart with defaults here too
239 for ty
in substs
.types().skip(num_types
) {
240 start_or_continue(f
, "::<", ", ")?
;
241 write
!(f
, "{}", ty
)?
;
244 start_or_continue(f
, "", ">")?
;
250 fn in_binder
<'a
, 'gcx
, 'tcx
, T
, U
>(f
: &mut fmt
::Formatter
,
251 tcx
: TyCtxt
<'a
, 'gcx
, 'tcx
>,
252 original
: &ty
::Binder
<T
>,
253 lifted
: Option
<ty
::Binder
<U
>>) -> fmt
::Result
254 where T
: fmt
::Display
, U
: fmt
::Display
+ TypeFoldable
<'tcx
>
256 // Replace any anonymous late-bound regions with named
257 // variants, using gensym'd identifiers, so that we can
258 // clearly differentiate between named and unnamed regions in
259 // the output. We'll probably want to tweak this over time to
260 // decide just how much information to give.
261 let value
= if let Some(v
) = lifted
{
264 return write
!(f
, "{}", original
.0);
267 let mut empty
= true;
268 let mut start_or_continue
= |f
: &mut fmt
::Formatter
, start
: &str, cont
: &str| {
271 write
!(f
, "{}", start
)
273 write
!(f
, "{}", cont
)
277 let new_value
= tcx
.replace_late_bound_regions(&value
, |br
| {
278 let _
= start_or_continue(f
, "for<", ", ");
280 ty
::BrNamed(_
, name
, _
) => {
281 let _
= write
!(f
, "{}", name
);
287 let name
= token
::intern("'r");
288 let _
= write
!(f
, "{}", name
);
289 ty
::BrNamed(tcx
.map
.local_def_id(CRATE_NODE_ID
),
291 ty
::Issue32330
::WontChange
)
294 tcx
.mk_region(ty
::ReLateBound(ty
::DebruijnIndex
::new(1), br
))
297 start_or_continue(f
, "", "> ")?
;
298 write
!(f
, "{}", new_value
)
301 /// This curious type is here to help pretty-print trait objects. In
302 /// a trait object, the projections are stored separately from the
303 /// main trait bound, but in fact we want to package them together
304 /// when printing out; they also have separate binders, but we want
305 /// them to share a binder when we print them out. (And the binder
306 /// pretty-printing logic is kind of clever and we don't want to
307 /// reproduce it.) So we just repackage up the structure somewhat.
309 /// Right now there is only one trait in an object that can have
310 /// projection bounds, so we just stuff them altogether. But in
311 /// reality we should eventually sort things out better.
312 #[derive(Clone, Debug)]
313 struct TraitAndProjections
<'tcx
>(ty
::TraitRef
<'tcx
>,
314 Vec
<ty
::ProjectionPredicate
<'tcx
>>);
316 impl<'tcx
> TypeFoldable
<'tcx
> for TraitAndProjections
<'tcx
> {
317 fn super_fold_with
<'gcx
: 'tcx
, F
: TypeFolder
<'gcx
, 'tcx
>>(&self, folder
: &mut F
) -> Self {
318 TraitAndProjections(self.0.fold_with
(folder
), self.1.fold_with
(folder
))
321 fn super_visit_with
<V
: TypeVisitor
<'tcx
>>(&self, visitor
: &mut V
) -> bool
{
322 self.0.visit_with(visitor
) || self.1.visit_with(visitor
)
326 impl<'tcx
> fmt
::Display
for TraitAndProjections
<'tcx
> {
327 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
328 let TraitAndProjections(ref trait_ref
, ref projection_bounds
) = *self;
329 parameterized(f
, trait_ref
.substs
,
335 impl<'tcx
> fmt
::Display
for ty
::TraitObject
<'tcx
> {
336 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
337 // Generate the main trait ref, including associated types.
338 ty
::tls
::with(|tcx
| {
339 // Use a type that can't appear in defaults of type parameters.
340 let dummy_self
= tcx
.mk_infer(ty
::FreshTy(0));
342 let principal
= tcx
.lift(&self.principal
)
343 .expect("could not lift TraitRef for printing")
344 .with_self_ty(tcx
, dummy_self
).0;
345 let projections
= self.projection_bounds
.iter().map(|p
| {
347 .expect("could not lift projection for printing")
348 .with_self_ty(tcx
, dummy_self
).0
351 let tap
= ty
::Binder(TraitAndProjections(principal
, projections
));
352 in_binder(f
, tcx
, &ty
::Binder(""), Some(tap
))
356 for bound
in &self.builtin_bounds
{
357 write
!(f
, " + {:?}", bound
)?
;
360 // FIXME: It'd be nice to compute from context when this bound
361 // is implied, but that's non-trivial -- we'd perhaps have to
362 // use thread-local data of some kind? There are also
363 // advantages to just showing the region, since it makes
364 // people aware that it's there.
365 let bound
= self.region_bound
.to_string();
366 if !bound
.is_empty() {
367 write
!(f
, " + {}", bound
)?
;
374 impl<'tcx
> fmt
::Debug
for ty
::TypeParameterDef
<'tcx
> {
375 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
376 write
!(f
, "TypeParameterDef({}, {:?}, {})",
383 impl<'tcx
> fmt
::Debug
for ty
::RegionParameterDef
<'tcx
> {
384 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
385 write
!(f
, "RegionParameterDef({}, {:?}, {}, {:?})",
393 impl<'tcx
> fmt
::Debug
for ty
::TyS
<'tcx
> {
394 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
395 write
!(f
, "{}", *self)
399 impl<'tcx
> fmt
::Display
for ty
::TypeAndMut
<'tcx
> {
400 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
402 if self.mutbl
== hir
::MutMutable { "mut " }
else { "" }
,
407 impl<'tcx
> fmt
::Debug
for ty
::ItemSubsts
<'tcx
> {
408 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
409 write
!(f
, "ItemSubsts({:?})", self.substs
)
413 impl<'tcx
> fmt
::Debug
for ty
::TraitRef
<'tcx
> {
414 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
415 // when printing out the debug representation, we don't need
416 // to enumerate the `for<...>` etc because the debruijn index
417 // tells you everything you need to know.
418 write
!(f
, "<{:?} as {}>", self.self_ty(), *self)
422 impl<'tcx
> fmt
::Debug
for ty
::ExistentialTraitRef
<'tcx
> {
423 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
424 ty
::tls
::with(|tcx
| {
425 let dummy_self
= tcx
.mk_infer(ty
::FreshTy(0));
427 let trait_ref
= tcx
.lift(&ty
::Binder(*self))
428 .expect("could not lift TraitRef for printing")
429 .with_self_ty(tcx
, dummy_self
).0;
430 parameterized(f
, trait_ref
.substs
, trait_ref
.def_id
, &[])
435 impl<'tcx
> fmt
::Debug
for ty
::TraitDef
<'tcx
> {
436 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
437 write
!(f
, "TraitDef(generics={:?}, trait_ref={:?})",
438 self.generics
, self.trait_ref
)
442 impl<'tcx
, 'container
> fmt
::Debug
for ty
::AdtDefData
<'tcx
, 'container
> {
443 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
444 ty
::tls
::with(|tcx
| {
445 write
!(f
, "{}", tcx
.item_path_str(self.did
))
450 impl<'tcx
> fmt
::Debug
for ty
::adjustment
::AutoAdjustment
<'tcx
> {
451 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
453 ty
::adjustment
::AdjustNeverToAny(ref target
) => {
454 write
!(f
, "AdjustNeverToAny({:?})", target
)
456 ty
::adjustment
::AdjustReifyFnPointer
=> {
457 write
!(f
, "AdjustReifyFnPointer")
459 ty
::adjustment
::AdjustUnsafeFnPointer
=> {
460 write
!(f
, "AdjustUnsafeFnPointer")
462 ty
::adjustment
::AdjustMutToConstPointer
=> {
463 write
!(f
, "AdjustMutToConstPointer")
465 ty
::adjustment
::AdjustDerefRef(ref data
) => {
466 write
!(f
, "{:?}", data
)
472 impl<'tcx
> fmt
::Debug
for ty
::adjustment
::AutoDerefRef
<'tcx
> {
473 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
474 write
!(f
, "AutoDerefRef({}, unsize={:?}, {:?})",
475 self.autoderefs
, self.unsize
, self.autoref
)
479 impl<'tcx
> fmt
::Debug
for ty
::TraitObject
<'tcx
> {
480 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
481 let mut empty
= true;
482 let mut maybe_continue
= |f
: &mut fmt
::Formatter
| {
492 write
!(f
, "{:?}", self.principal
)?
;
494 let region_str
= format
!("{:?}", self.region_bound
);
495 if !region_str
.is_empty() {
497 write
!(f
, "{}", region_str
)?
;
500 for bound
in &self.builtin_bounds
{
502 write
!(f
, "{:?}", bound
)?
;
505 for projection_bound
in &self.projection_bounds
{
507 write
!(f
, "{:?}", projection_bound
)?
;
514 impl<'tcx
> fmt
::Debug
for ty
::Predicate
<'tcx
> {
515 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
517 ty
::Predicate
::Trait(ref a
) => write
!(f
, "{:?}", a
),
518 ty
::Predicate
::Equate(ref pair
) => write
!(f
, "{:?}", pair
),
519 ty
::Predicate
::RegionOutlives(ref pair
) => write
!(f
, "{:?}", pair
),
520 ty
::Predicate
::TypeOutlives(ref pair
) => write
!(f
, "{:?}", pair
),
521 ty
::Predicate
::Projection(ref pair
) => write
!(f
, "{:?}", pair
),
522 ty
::Predicate
::WellFormed(ty
) => write
!(f
, "WF({:?})", ty
),
523 ty
::Predicate
::ObjectSafe(trait_def_id
) => {
524 write
!(f
, "ObjectSafe({:?})", trait_def_id
)
526 ty
::Predicate
::ClosureKind(closure_def_id
, kind
) => {
527 write
!(f
, "ClosureKind({:?}, {:?})", closure_def_id
, kind
)
533 impl fmt
::Display
for ty
::BoundRegion
{
534 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
536 return write
!(f
, "{:?}", *self);
540 BrNamed(_
, name
, _
) => write
!(f
, "{}", name
),
541 BrAnon(_
) | BrFresh(_
) | BrEnv
=> Ok(())
546 impl fmt
::Debug
for ty
::BoundRegion
{
547 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
549 BrAnon(n
) => write
!(f
, "BrAnon({:?})", n
),
550 BrFresh(n
) => write
!(f
, "BrFresh({:?})", n
),
551 BrNamed(did
, name
, issue32330
) => {
552 write
!(f
, "BrNamed({:?}:{:?}, {:?}, {:?})",
553 did
.krate
, did
.index
, name
, issue32330
)
555 BrEnv
=> "BrEnv".fmt(f
),
560 impl fmt
::Debug
for ty
::Region
{
561 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
563 ty
::ReEarlyBound(ref data
) => {
564 write
!(f
, "ReEarlyBound({}, {})",
569 ty
::ReLateBound(binder_id
, ref bound_region
) => {
570 write
!(f
, "ReLateBound({:?}, {:?})",
575 ty
::ReFree(ref fr
) => write
!(f
, "{:?}", fr
),
578 write
!(f
, "ReScope({:?})", id
)
581 ty
::ReStatic
=> write
!(f
, "ReStatic"),
583 ty
::ReVar(ref vid
) => {
584 write
!(f
, "{:?}", vid
)
587 ty
::ReSkolemized(id
, ref bound_region
) => {
588 write
!(f
, "ReSkolemized({}, {:?})", id
.index
, bound_region
)
591 ty
::ReEmpty
=> write
!(f
, "ReEmpty"),
593 ty
::ReErased
=> write
!(f
, "ReErased")
598 impl<'tcx
> fmt
::Debug
for ty
::ClosureTy
<'tcx
> {
599 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
600 write
!(f
, "ClosureTy({},{:?},{})",
607 impl<'tcx
> fmt
::Debug
for ty
::ClosureUpvar
<'tcx
> {
608 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
609 write
!(f
, "ClosureUpvar({:?},{:?})",
615 impl<'tcx
> fmt
::Debug
for ty
::ParameterEnvironment
<'tcx
> {
616 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
617 write
!(f
, "ParameterEnvironment(\
619 implicit_region_bound={:?}, \
620 caller_bounds={:?})",
622 self.implicit_region_bound
,
627 impl<'tcx
> fmt
::Debug
for ty
::ObjectLifetimeDefault
<'tcx
> {
628 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
630 ty
::ObjectLifetimeDefault
::Ambiguous
=> write
!(f
, "Ambiguous"),
631 ty
::ObjectLifetimeDefault
::BaseDefault
=> write
!(f
, "BaseDefault"),
632 ty
::ObjectLifetimeDefault
::Specific(ref r
) => write
!(f
, "{:?}", r
),
637 impl fmt
::Display
for ty
::Region
{
638 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
640 return write
!(f
, "{:?}", *self);
643 // These printouts are concise. They do not contain all the information
644 // the user might want to diagnose an error, but there is basically no way
645 // to fit that into a short string. Hence the recommendation to use
646 // `explain_region()` or `note_and_explain_region()`.
648 ty
::ReEarlyBound(ref data
) => {
649 write
!(f
, "{}", data
.name
)
651 ty
::ReLateBound(_
, br
) |
652 ty
::ReFree(ty
::FreeRegion { bound_region: br, .. }
) |
653 ty
::ReSkolemized(_
, br
) => {
658 ty
::ReErased
=> Ok(()),
659 ty
::ReStatic
=> write
!(f
, "'static"),
660 ty
::ReEmpty
=> write
!(f
, "'<empty>"),
665 impl fmt
::Debug
for ty
::FreeRegion
{
666 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
667 write
!(f
, "ReFree({:?}, {:?})",
668 self.scope
, self.bound_region
)
672 impl fmt
::Debug
for ty
::Variance
{
673 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
674 f
.write_str(match *self {
675 ty
::Covariant
=> "+",
676 ty
::Contravariant
=> "-",
677 ty
::Invariant
=> "o",
678 ty
::Bivariant
=> "*",
683 impl<'tcx
> fmt
::Debug
for ty
::GenericPredicates
<'tcx
> {
684 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
685 write
!(f
, "GenericPredicates({:?})", self.predicates
)
689 impl<'tcx
> fmt
::Debug
for ty
::InstantiatedPredicates
<'tcx
> {
690 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
691 write
!(f
, "InstantiatedPredicates({:?})",
696 impl<'tcx
> fmt
::Debug
for ty
::ImplOrTraitItem
<'tcx
> {
697 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
698 write
!(f
, "ImplOrTraitItem(")?
;
700 ty
::ImplOrTraitItem
::MethodTraitItem(ref i
) => write
!(f
, "{:?}", i
),
701 ty
::ImplOrTraitItem
::ConstTraitItem(ref i
) => write
!(f
, "{:?}", i
),
702 ty
::ImplOrTraitItem
::TypeTraitItem(ref i
) => write
!(f
, "{:?}", i
),
708 impl<'tcx
> fmt
::Display
for ty
::FnSig
<'tcx
> {
709 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
711 fn_sig(f
, &self.inputs
, self.variadic
, self.output
)
715 impl fmt
::Display
for ty
::BuiltinBounds
{
716 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
717 let mut bounds
= self.iter();
718 if let Some(bound
) = bounds
.next() {
719 write
!(f
, "{:?}", bound
)?
;
720 for bound
in bounds
{
721 write
!(f
, " + {:?}", bound
)?
;
728 impl fmt
::Debug
for ty
::TyVid
{
729 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
730 write
!(f
, "_#{}t", self.index
)
734 impl fmt
::Debug
for ty
::IntVid
{
735 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
736 write
!(f
, "_#{}i", self.index
)
740 impl fmt
::Debug
for ty
::FloatVid
{
741 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
742 write
!(f
, "_#{}f", self.index
)
746 impl fmt
::Debug
for ty
::RegionVid
{
747 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
748 write
!(f
, "'_#{}r", self.index
)
752 impl<'tcx
> fmt
::Debug
for ty
::FnSig
<'tcx
> {
753 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
754 write
!(f
, "({:?}; variadic: {})->{:?}", self.inputs
, self.variadic
, self.output
)
758 impl fmt
::Debug
for ty
::InferTy
{
759 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
761 ty
::TyVar(ref v
) => v
.fmt(f
),
762 ty
::IntVar(ref v
) => v
.fmt(f
),
763 ty
::FloatVar(ref v
) => v
.fmt(f
),
764 ty
::FreshTy(v
) => write
!(f
, "FreshTy({:?})", v
),
765 ty
::FreshIntTy(v
) => write
!(f
, "FreshIntTy({:?})", v
),
766 ty
::FreshFloatTy(v
) => write
!(f
, "FreshFloatTy({:?})", v
)
771 impl fmt
::Debug
for ty
::IntVarValue
{
772 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
774 ty
::IntType(ref v
) => v
.fmt(f
),
775 ty
::UintType(ref v
) => v
.fmt(f
),
780 // The generic impl doesn't work yet because projections are not
781 // normalized under HRTB.
782 /*impl<T> fmt::Display for ty::Binder<T>
783 where T: fmt::Display + for<'a> ty::Lift<'a>,
784 for<'a> <T as ty::Lift<'a>>::Lifted: fmt::Display + TypeFoldable<'a>
786 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
787 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
791 impl<'tcx
> fmt
::Display
for ty
::Binder
<ty
::TraitRef
<'tcx
>> {
792 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
793 ty
::tls
::with(|tcx
| in_binder(f
, tcx
, self, tcx
.lift(self)))
797 impl<'tcx
> fmt
::Display
for ty
::Binder
<ty
::TraitPredicate
<'tcx
>> {
798 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
799 ty
::tls
::with(|tcx
| in_binder(f
, tcx
, self, tcx
.lift(self)))
803 impl<'tcx
> fmt
::Display
for ty
::Binder
<ty
::EquatePredicate
<'tcx
>> {
804 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
805 ty
::tls
::with(|tcx
| in_binder(f
, tcx
, self, tcx
.lift(self)))
809 impl<'tcx
> fmt
::Display
for ty
::Binder
<ty
::ProjectionPredicate
<'tcx
>> {
810 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
811 ty
::tls
::with(|tcx
| in_binder(f
, tcx
, self, tcx
.lift(self)))
815 impl<'tcx
> fmt
::Display
for ty
::Binder
<ty
::OutlivesPredicate
<Ty
<'tcx
>, &'tcx ty
::Region
>> {
816 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
817 ty
::tls
::with(|tcx
| in_binder(f
, tcx
, self, tcx
.lift(self)))
821 impl<'tcx
> fmt
::Display
for ty
::Binder
<ty
::OutlivesPredicate
<&'tcx ty
::Region
,
823 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
824 ty
::tls
::with(|tcx
| in_binder(f
, tcx
, self, tcx
.lift(self)))
828 impl<'tcx
> fmt
::Display
for ty
::TraitRef
<'tcx
> {
829 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
830 parameterized(f
, self.substs
, self.def_id
, &[])
834 impl<'tcx
> fmt
::Display
for ty
::TypeVariants
<'tcx
> {
835 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
837 TyBool
=> write
!(f
, "bool"),
838 TyChar
=> write
!(f
, "char"),
839 TyInt(t
) => write
!(f
, "{}", t
.ty_to_string()),
840 TyUint(t
) => write
!(f
, "{}", t
.ty_to_string()),
841 TyFloat(t
) => write
!(f
, "{}", t
.ty_to_string()),
842 TyBox(typ
) => write
!(f
, "Box<{}>", typ
),
843 TyRawPtr(ref tm
) => {
844 write
!(f
, "*{} {}", match tm
.mutbl
{
845 hir
::MutMutable
=> "mut",
846 hir
::MutImmutable
=> "const",
849 TyRef(r
, ref tm
) => {
851 let s
= r
.to_string();
858 TyNever
=> write
!(f
, "!"),
859 TyTuple(ref tys
) => {
861 let mut tys
= tys
.iter();
862 if let Some(&ty
) = tys
.next() {
863 write
!(f
, "{},", ty
)?
;
864 if let Some(&ty
) = tys
.next() {
865 write
!(f
, " {}", ty
)?
;
867 write
!(f
, ", {}", ty
)?
;
873 TyFnDef(def_id
, substs
, ref bare_fn
) => {
874 if bare_fn
.unsafety
== hir
::Unsafety
::Unsafe
{
875 write
!(f
, "unsafe ")?
;
878 if bare_fn
.abi
!= Abi
::Rust
{
879 write
!(f
, "extern {} ", bare_fn
.abi
)?
;
882 write
!(f
, "{} {{", bare_fn
.sig
.0)?
;
883 parameterized(f
, substs
, def_id
, &[])?
;
886 TyFnPtr(ref bare_fn
) => {
887 if bare_fn
.unsafety
== hir
::Unsafety
::Unsafe
{
888 write
!(f
, "unsafe ")?
;
891 if bare_fn
.abi
!= Abi
::Rust
{
892 write
!(f
, "extern {} ", bare_fn
.abi
)?
;
895 write
!(f
, "{}", bare_fn
.sig
.0)
897 TyInfer(infer_ty
) => write
!(f
, "{}", infer_ty
),
898 TyError
=> write
!(f
, "[type error]"),
899 TyParam(ref param_ty
) => write
!(f
, "{}", param_ty
),
900 TyAdt(def
, substs
) => {
901 ty
::tls
::with(|tcx
| {
902 if def
.did
.is_local() &&
903 !tcx
.tcache
.borrow().contains_key(&def
.did
) {
904 write
!(f
, "{}<..>", tcx
.item_path_str(def
.did
))
906 parameterized(f
, substs
, def
.did
, &[])
910 TyTrait(ref data
) => write
!(f
, "{}", data
),
911 ty
::TyProjection(ref data
) => write
!(f
, "{}", data
),
912 ty
::TyAnon(def_id
, substs
) => {
913 ty
::tls
::with(|tcx
| {
914 // Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
915 // by looking up the projections associated with the def_id.
916 let item_predicates
= tcx
.lookup_predicates(def_id
);
917 let substs
= tcx
.lift(&substs
).unwrap_or_else(|| {
920 let bounds
= item_predicates
.instantiate(tcx
, substs
);
922 let mut first
= true;
923 let mut is_sized
= false;
925 for predicate
in bounds
.predicates
{
926 if let Some(trait_ref
) = predicate
.to_opt_poly_trait_ref() {
927 // Don't print +Sized, but rather +?Sized if absent.
928 if Some(trait_ref
.def_id()) == tcx
.lang_items
.sized_trait() {
933 write
!(f
, "{}{}", if first { " " }
else { "+" }
, trait_ref
)?
;
938 write
!(f
, "{}?Sized", if first { " " }
else { "+" }
)?
;
943 TyStr
=> write
!(f
, "str"),
944 TyClosure(did
, substs
) => ty
::tls
::with(|tcx
| {
945 write
!(f
, "[closure")?
;
947 if let Some(node_id
) = tcx
.map
.as_local_node_id(did
) {
948 write
!(f
, "@{:?}", tcx
.map
.span(node_id
))?
;
950 tcx
.with_freevars(node_id
, |freevars
| {
951 for (freevar
, upvar_ty
) in freevars
.iter().zip(substs
.upvar_tys
) {
952 let def_id
= freevar
.def
.def_id();
953 let node_id
= tcx
.map
.as_local_node_id(def_id
).unwrap();
957 tcx
.local_var_name_str(node_id
),
964 // cross-crate closure types should only be
965 // visible in trans bug reports, I imagine.
966 write
!(f
, "@{:?}", did
)?
;
968 for (index
, upvar_ty
) in substs
.upvar_tys
.iter().enumerate() {
969 write
!(f
, "{}{}:{}", sep
, index
, upvar_ty
)?
;
976 TyArray(ty
, sz
) => write
!(f
, "[{}; {}]", ty
, sz
),
977 TySlice(ty
) => write
!(f
, "[{}]", ty
)
982 impl<'tcx
> fmt
::Display
for ty
::TyS
<'tcx
> {
983 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
984 write
!(f
, "{}", self.sty
)
988 impl fmt
::Debug
for ty
::UpvarId
{
989 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
990 write
!(f
, "UpvarId({};`{}`;{})",
992 ty
::tls
::with(|tcx
| tcx
.local_var_name_str(self.var_id
)),
993 self.closure_expr_id
)
997 impl<'tcx
> fmt
::Debug
for ty
::UpvarBorrow
<'tcx
> {
998 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
999 write
!(f
, "UpvarBorrow({:?}, {:?})",
1000 self.kind
, self.region
)
1004 impl fmt
::Display
for ty
::InferTy
{
1005 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1006 let print_var_ids
= verbose();
1008 ty
::TyVar(ref vid
) if print_var_ids
=> write
!(f
, "{:?}", vid
),
1009 ty
::IntVar(ref vid
) if print_var_ids
=> write
!(f
, "{:?}", vid
),
1010 ty
::FloatVar(ref vid
) if print_var_ids
=> write
!(f
, "{:?}", vid
),
1011 ty
::TyVar(_
) => write
!(f
, "_"),
1012 ty
::IntVar(_
) => write
!(f
, "{}", "{integer}"),
1013 ty
::FloatVar(_
) => write
!(f
, "{}", "{float}"),
1014 ty
::FreshTy(v
) => write
!(f
, "FreshTy({})", v
),
1015 ty
::FreshIntTy(v
) => write
!(f
, "FreshIntTy({})", v
),
1016 ty
::FreshFloatTy(v
) => write
!(f
, "FreshFloatTy({})", v
)
1021 impl<'tcx
> fmt
::Display
for ty
::ExplicitSelfCategory
<'tcx
> {
1022 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1023 f
.write_str(match *self {
1024 ty
::ExplicitSelfCategory
::Static
=> "static",
1025 ty
::ExplicitSelfCategory
::ByValue
=> "self",
1026 ty
::ExplicitSelfCategory
::ByReference(_
, hir
::MutMutable
) => {
1029 ty
::ExplicitSelfCategory
::ByReference(_
, hir
::MutImmutable
) => "&self",
1030 ty
::ExplicitSelfCategory
::ByBox
=> "Box<self>",
1035 impl fmt
::Display
for ty
::ParamTy
{
1036 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1037 write
!(f
, "{}", self.name
)
1041 impl fmt
::Debug
for ty
::ParamTy
{
1042 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1043 write
!(f
, "{}/#{}", self, self.idx
)
1047 impl<'tcx
, T
, U
> fmt
::Display
for ty
::OutlivesPredicate
<T
,U
>
1048 where T
: fmt
::Display
, U
: fmt
::Display
1050 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1051 write
!(f
, "{} : {}", self.0, self.1)
1055 impl<'tcx
> fmt
::Display
for ty
::EquatePredicate
<'tcx
> {
1056 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1057 write
!(f
, "{} == {}", self.0, self.1)
1061 impl<'tcx
> fmt
::Debug
for ty
::TraitPredicate
<'tcx
> {
1062 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1063 write
!(f
, "TraitPredicate({:?})",
1068 impl<'tcx
> fmt
::Display
for ty
::TraitPredicate
<'tcx
> {
1069 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1070 write
!(f
, "{}: {}", self.trait_ref
.self_ty(), self.trait_ref
)
1074 impl<'tcx
> fmt
::Debug
for ty
::ProjectionPredicate
<'tcx
> {
1075 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1076 write
!(f
, "ProjectionPredicate({:?}, {:?})",
1082 impl<'tcx
> fmt
::Display
for ty
::ProjectionPredicate
<'tcx
> {
1083 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1084 write
!(f
, "{} == {}",
1090 impl<'tcx
> fmt
::Display
for ty
::ProjectionTy
<'tcx
> {
1091 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1092 write
!(f
, "{:?}::{}",
1098 impl fmt
::Display
for ty
::ClosureKind
{
1099 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1101 ty
::ClosureKind
::Fn
=> write
!(f
, "Fn"),
1102 ty
::ClosureKind
::FnMut
=> write
!(f
, "FnMut"),
1103 ty
::ClosureKind
::FnOnce
=> write
!(f
, "FnOnce"),
1108 impl<'tcx
> fmt
::Display
for ty
::Predicate
<'tcx
> {
1109 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1111 ty
::Predicate
::Trait(ref data
) => write
!(f
, "{}", data
),
1112 ty
::Predicate
::Equate(ref predicate
) => write
!(f
, "{}", predicate
),
1113 ty
::Predicate
::RegionOutlives(ref predicate
) => write
!(f
, "{}", predicate
),
1114 ty
::Predicate
::TypeOutlives(ref predicate
) => write
!(f
, "{}", predicate
),
1115 ty
::Predicate
::Projection(ref predicate
) => write
!(f
, "{}", predicate
),
1116 ty
::Predicate
::WellFormed(ty
) => write
!(f
, "{} well-formed", ty
),
1117 ty
::Predicate
::ObjectSafe(trait_def_id
) =>
1118 ty
::tls
::with(|tcx
| {
1119 write
!(f
, "the trait `{}` is object-safe", tcx
.item_path_str(trait_def_id
))
1121 ty
::Predicate
::ClosureKind(closure_def_id
, kind
) =>
1122 ty
::tls
::with(|tcx
| {
1123 write
!(f
, "the closure `{}` implements the trait `{}`",
1124 tcx
.item_path_str(closure_def_id
), kind
)