]> git.proxmox.com Git - rustc.git/blob - src/librustc/util/ppaux.rs
New upstream version 1.13.0+dfsg1
[rustc.git] / src / librustc / util / ppaux.rs
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.
4 //
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.
10
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};
18 use ty::TyClosure;
19 use ty::{TyBox, TyTrait, TyInt, TyUint, TyInfer};
20 use ty::{self, Ty, TyCtxt, TypeFoldable};
21 use ty::fold::{TypeFolder, TypeVisitor};
22
23 use std::cell::Cell;
24 use std::fmt;
25 use std::usize;
26
27 use syntax::abi::Abi;
28 use syntax::parse::token;
29 use syntax::ast::CRATE_NODE_ID;
30 use hir;
31
32 pub fn verbose() -> bool {
33 ty::tls::with(|tcx| tcx.sess.verbose())
34 }
35
36 fn fn_sig(f: &mut fmt::Formatter,
37 inputs: &[Ty],
38 variadic: bool,
39 output: Ty)
40 -> fmt::Result {
41 write!(f, "(")?;
42 let mut inputs = inputs.iter();
43 if let Some(&ty) = inputs.next() {
44 write!(f, "{}", ty)?;
45 for &ty in inputs {
46 write!(f, ", {}", ty)?;
47 }
48 if variadic {
49 write!(f, ", ...")?;
50 }
51 }
52 write!(f, ")")?;
53 if !output.is_nil() {
54 write!(f, " -> {}", output)?;
55 }
56
57 Ok(())
58 }
59
60 pub fn parameterized(f: &mut fmt::Formatter,
61 substs: &subst::Substs,
62 mut did: DefId,
63 projections: &[ty::ProjectionPredicate])
64 -> fmt::Result {
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() {
67 Some(name)
68 } else {
69 did.index = key.parent.unwrap_or_else(
70 || bug!("finding type for {:?}, encountered def-id {:?} with no parent",
71 did, did));
72 parameterized(f, substs, did, projections)?;
73 return write!(f, "::{}", key.disambiguated_data.data.as_interned_str());
74 };
75
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
85 // have them.
86 let mut item_def_id = did;
87 loop {
88 let key = tcx.def_key(item_def_id);
89 match key.disambiguated_data.data {
90 DefPathData::TypeNs(_) => {
91 break;
92 }
93 DefPathData::ValueNs(_) | DefPathData::EnumVariant(_) => {
94 is_value_path = true;
95 break;
96 }
97 _ => {
98 // if we're making a symbol for something, there ought
99 // to be a value or type-def or something in there
100 // *somewhere*
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);
104 });
105 }
106 }
107 }
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;
112
113 let mut child_types = 0;
114 if let Some(def_id) = generics.parent {
115 // Methods.
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();
121
122 if has_self {
123 write!(f, "<{} as ", substs.type_at(0))?;
124 }
125
126 path_def_id = def_id;
127 } else {
128 item_name = None;
129
130 if is_value_path {
131 // Functions.
132 assert_eq!(has_self, false);
133 } else {
134 // Types and traits.
135 num_regions = generics.regions.len();
136 num_types = generics.types.len();
137 }
138 }
139
140 if !verbose {
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) {
146 break;
147 }
148 num_supplied_defaults += 1;
149 }
150 }
151 }
152 }
153
154 write!(f, "{}", tcx.item_path_str(path_def_id))?;
155 Ok(tcx.lang_items.fn_trait_kind(path_def_id))
156 })?;
157
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);
162 }
163 }
164
165 let empty = Cell::new(true);
166 let start_or_continue = |f: &mut fmt::Formatter, start: &str, cont: &str| {
167 if empty.get() {
168 empty.set(false);
169 write!(f, "{}", start)
170 } else {
171 write!(f, "{}", cont)
172 }
173 };
174
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) {
179 return Ok(());
180 }
181
182 for region in regions() {
183 let region: &ty::Region = region;
184 start_or_continue(f, start, ", ")?;
185 if verbose {
186 write!(f, "{:?}", region)?;
187 } else {
188 let s = region.to_string();
189 if s.is_empty() {
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.
195 write!(f, "'_")?;
196 } else {
197 write!(f, "{}", s)?;
198 }
199 }
200 }
201
202 Ok(())
203 };
204
205 print_regions(f, "<", 0, num_regions)?;
206
207 let tps = substs.types().take(num_types - num_supplied_defaults)
208 .skip(has_self as usize);
209
210 for ty in tps {
211 start_or_continue(f, "<", ", ")?;
212 write!(f, "{}", ty)?;
213 }
214
215 for projection in projections {
216 start_or_continue(f, "<", ", ")?;
217 write!(f, "{}={}",
218 projection.projection_ty.item_name,
219 projection.ty)?;
220 }
221
222 start_or_continue(f, "", ">")?;
223
224 // For values, also print their name and type parameters.
225 if is_value_path {
226 empty.set(true);
227
228 if has_self {
229 write!(f, ">")?;
230 }
231
232 if let Some(item_name) = item_name {
233 write!(f, "::{}", item_name)?;
234 }
235
236 print_regions(f, "::<", num_regions, usize::MAX)?;
237
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)?;
242 }
243
244 start_or_continue(f, "", ">")?;
245 }
246
247 Ok(())
248 }
249
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>
255 {
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 {
262 v
263 } else {
264 return write!(f, "{}", original.0);
265 };
266
267 let mut empty = true;
268 let mut start_or_continue = |f: &mut fmt::Formatter, start: &str, cont: &str| {
269 if empty {
270 empty = false;
271 write!(f, "{}", start)
272 } else {
273 write!(f, "{}", cont)
274 }
275 };
276
277 let new_value = tcx.replace_late_bound_regions(&value, |br| {
278 let _ = start_or_continue(f, "for<", ", ");
279 let br = match br {
280 ty::BrNamed(_, name, _) => {
281 let _ = write!(f, "{}", name);
282 br
283 }
284 ty::BrAnon(_) |
285 ty::BrFresh(_) |
286 ty::BrEnv => {
287 let name = token::intern("'r");
288 let _ = write!(f, "{}", name);
289 ty::BrNamed(tcx.map.local_def_id(CRATE_NODE_ID),
290 name,
291 ty::Issue32330::WontChange)
292 }
293 };
294 tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(1), br))
295 }).0;
296
297 start_or_continue(f, "", "> ")?;
298 write!(f, "{}", new_value)
299 }
300
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.
308 ///
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>>);
315
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))
319 }
320
321 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
322 self.0.visit_with(visitor) || self.1.visit_with(visitor)
323 }
324 }
325
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,
330 trait_ref.def_id,
331 projection_bounds)
332 }
333 }
334
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));
341
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| {
346 tcx.lift(p)
347 .expect("could not lift projection for printing")
348 .with_self_ty(tcx, dummy_self).0
349 }).collect();
350
351 let tap = ty::Binder(TraitAndProjections(principal, projections));
352 in_binder(f, tcx, &ty::Binder(""), Some(tap))
353 })?;
354
355 // Builtin bounds.
356 for bound in &self.builtin_bounds {
357 write!(f, " + {:?}", bound)?;
358 }
359
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)?;
368 }
369
370 Ok(())
371 }
372 }
373
374 impl<'tcx> fmt::Debug for ty::TypeParameterDef<'tcx> {
375 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
376 write!(f, "TypeParameterDef({}, {:?}, {})",
377 self.name,
378 self.def_id,
379 self.index)
380 }
381 }
382
383 impl<'tcx> fmt::Debug for ty::RegionParameterDef<'tcx> {
384 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
385 write!(f, "RegionParameterDef({}, {:?}, {}, {:?})",
386 self.name,
387 self.def_id,
388 self.index,
389 self.bounds)
390 }
391 }
392
393 impl<'tcx> fmt::Debug for ty::TyS<'tcx> {
394 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
395 write!(f, "{}", *self)
396 }
397 }
398
399 impl<'tcx> fmt::Display for ty::TypeAndMut<'tcx> {
400 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
401 write!(f, "{}{}",
402 if self.mutbl == hir::MutMutable { "mut " } else { "" },
403 self.ty)
404 }
405 }
406
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)
410 }
411 }
412
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)
419 }
420 }
421
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));
426
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, &[])
431 })
432 }
433 }
434
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)
439 }
440 }
441
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))
446 })
447 }
448 }
449
450 impl<'tcx> fmt::Debug for ty::adjustment::AutoAdjustment<'tcx> {
451 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
452 match *self {
453 ty::adjustment::AdjustNeverToAny(ref target) => {
454 write!(f, "AdjustNeverToAny({:?})", target)
455 }
456 ty::adjustment::AdjustReifyFnPointer => {
457 write!(f, "AdjustReifyFnPointer")
458 }
459 ty::adjustment::AdjustUnsafeFnPointer => {
460 write!(f, "AdjustUnsafeFnPointer")
461 }
462 ty::adjustment::AdjustMutToConstPointer => {
463 write!(f, "AdjustMutToConstPointer")
464 }
465 ty::adjustment::AdjustDerefRef(ref data) => {
466 write!(f, "{:?}", data)
467 }
468 }
469 }
470 }
471
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)
476 }
477 }
478
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| {
483 if empty {
484 empty = false;
485 Ok(())
486 } else {
487 write!(f, " + ")
488 }
489 };
490
491 maybe_continue(f)?;
492 write!(f, "{:?}", self.principal)?;
493
494 let region_str = format!("{:?}", self.region_bound);
495 if !region_str.is_empty() {
496 maybe_continue(f)?;
497 write!(f, "{}", region_str)?;
498 }
499
500 for bound in &self.builtin_bounds {
501 maybe_continue(f)?;
502 write!(f, "{:?}", bound)?;
503 }
504
505 for projection_bound in &self.projection_bounds {
506 maybe_continue(f)?;
507 write!(f, "{:?}", projection_bound)?;
508 }
509
510 Ok(())
511 }
512 }
513
514 impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
515 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
516 match *self {
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)
525 }
526 ty::Predicate::ClosureKind(closure_def_id, kind) => {
527 write!(f, "ClosureKind({:?}, {:?})", closure_def_id, kind)
528 }
529 }
530 }
531 }
532
533 impl fmt::Display for ty::BoundRegion {
534 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
535 if verbose() {
536 return write!(f, "{:?}", *self);
537 }
538
539 match *self {
540 BrNamed(_, name, _) => write!(f, "{}", name),
541 BrAnon(_) | BrFresh(_) | BrEnv => Ok(())
542 }
543 }
544 }
545
546 impl fmt::Debug for ty::BoundRegion {
547 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
548 match *self {
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)
554 }
555 BrEnv => "BrEnv".fmt(f),
556 }
557 }
558 }
559
560 impl fmt::Debug for ty::Region {
561 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
562 match *self {
563 ty::ReEarlyBound(ref data) => {
564 write!(f, "ReEarlyBound({}, {})",
565 data.index,
566 data.name)
567 }
568
569 ty::ReLateBound(binder_id, ref bound_region) => {
570 write!(f, "ReLateBound({:?}, {:?})",
571 binder_id,
572 bound_region)
573 }
574
575 ty::ReFree(ref fr) => write!(f, "{:?}", fr),
576
577 ty::ReScope(id) => {
578 write!(f, "ReScope({:?})", id)
579 }
580
581 ty::ReStatic => write!(f, "ReStatic"),
582
583 ty::ReVar(ref vid) => {
584 write!(f, "{:?}", vid)
585 }
586
587 ty::ReSkolemized(id, ref bound_region) => {
588 write!(f, "ReSkolemized({}, {:?})", id.index, bound_region)
589 }
590
591 ty::ReEmpty => write!(f, "ReEmpty"),
592
593 ty::ReErased => write!(f, "ReErased")
594 }
595 }
596 }
597
598 impl<'tcx> fmt::Debug for ty::ClosureTy<'tcx> {
599 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
600 write!(f, "ClosureTy({},{:?},{})",
601 self.unsafety,
602 self.sig,
603 self.abi)
604 }
605 }
606
607 impl<'tcx> fmt::Debug for ty::ClosureUpvar<'tcx> {
608 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
609 write!(f, "ClosureUpvar({:?},{:?})",
610 self.def,
611 self.ty)
612 }
613 }
614
615 impl<'tcx> fmt::Debug for ty::ParameterEnvironment<'tcx> {
616 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
617 write!(f, "ParameterEnvironment(\
618 free_substs={:?}, \
619 implicit_region_bound={:?}, \
620 caller_bounds={:?})",
621 self.free_substs,
622 self.implicit_region_bound,
623 self.caller_bounds)
624 }
625 }
626
627 impl<'tcx> fmt::Debug for ty::ObjectLifetimeDefault<'tcx> {
628 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
629 match *self {
630 ty::ObjectLifetimeDefault::Ambiguous => write!(f, "Ambiguous"),
631 ty::ObjectLifetimeDefault::BaseDefault => write!(f, "BaseDefault"),
632 ty::ObjectLifetimeDefault::Specific(ref r) => write!(f, "{:?}", r),
633 }
634 }
635 }
636
637 impl fmt::Display for ty::Region {
638 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
639 if verbose() {
640 return write!(f, "{:?}", *self);
641 }
642
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()`.
647 match *self {
648 ty::ReEarlyBound(ref data) => {
649 write!(f, "{}", data.name)
650 }
651 ty::ReLateBound(_, br) |
652 ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
653 ty::ReSkolemized(_, br) => {
654 write!(f, "{}", br)
655 }
656 ty::ReScope(_) |
657 ty::ReVar(_) |
658 ty::ReErased => Ok(()),
659 ty::ReStatic => write!(f, "'static"),
660 ty::ReEmpty => write!(f, "'<empty>"),
661 }
662 }
663 }
664
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)
669 }
670 }
671
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 => "*",
679 })
680 }
681 }
682
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)
686 }
687 }
688
689 impl<'tcx> fmt::Debug for ty::InstantiatedPredicates<'tcx> {
690 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
691 write!(f, "InstantiatedPredicates({:?})",
692 self.predicates)
693 }
694 }
695
696 impl<'tcx> fmt::Debug for ty::ImplOrTraitItem<'tcx> {
697 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
698 write!(f, "ImplOrTraitItem(")?;
699 match *self {
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),
703 }?;
704 write!(f, ")")
705 }
706 }
707
708 impl<'tcx> fmt::Display for ty::FnSig<'tcx> {
709 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
710 write!(f, "fn")?;
711 fn_sig(f, &self.inputs, self.variadic, self.output)
712 }
713 }
714
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)?;
722 }
723 }
724 Ok(())
725 }
726 }
727
728 impl fmt::Debug for ty::TyVid {
729 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
730 write!(f, "_#{}t", self.index)
731 }
732 }
733
734 impl fmt::Debug for ty::IntVid {
735 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
736 write!(f, "_#{}i", self.index)
737 }
738 }
739
740 impl fmt::Debug for ty::FloatVid {
741 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
742 write!(f, "_#{}f", self.index)
743 }
744 }
745
746 impl fmt::Debug for ty::RegionVid {
747 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
748 write!(f, "'_#{}r", self.index)
749 }
750 }
751
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)
755 }
756 }
757
758 impl fmt::Debug for ty::InferTy {
759 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
760 match *self {
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)
767 }
768 }
769 }
770
771 impl fmt::Debug for ty::IntVarValue {
772 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
773 match *self {
774 ty::IntType(ref v) => v.fmt(f),
775 ty::UintType(ref v) => v.fmt(f),
776 }
777 }
778 }
779
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>
785 {
786 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
787 ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
788 }
789 }*/
790
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)))
794 }
795 }
796
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)))
800 }
801 }
802
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)))
806 }
807 }
808
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)))
812 }
813 }
814
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)))
818 }
819 }
820
821 impl<'tcx> fmt::Display for ty::Binder<ty::OutlivesPredicate<&'tcx ty::Region,
822 &'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)))
825 }
826 }
827
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, &[])
831 }
832 }
833
834 impl<'tcx> fmt::Display for ty::TypeVariants<'tcx> {
835 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
836 match *self {
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",
847 }, tm.ty)
848 }
849 TyRef(r, ref tm) => {
850 write!(f, "&")?;
851 let s = r.to_string();
852 write!(f, "{}", s)?;
853 if !s.is_empty() {
854 write!(f, " ")?;
855 }
856 write!(f, "{}", tm)
857 }
858 TyNever => write!(f, "!"),
859 TyTuple(ref tys) => {
860 write!(f, "(")?;
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)?;
866 for &ty in tys {
867 write!(f, ", {}", ty)?;
868 }
869 }
870 }
871 write!(f, ")")
872 }
873 TyFnDef(def_id, substs, ref bare_fn) => {
874 if bare_fn.unsafety == hir::Unsafety::Unsafe {
875 write!(f, "unsafe ")?;
876 }
877
878 if bare_fn.abi != Abi::Rust {
879 write!(f, "extern {} ", bare_fn.abi)?;
880 }
881
882 write!(f, "{} {{", bare_fn.sig.0)?;
883 parameterized(f, substs, def_id, &[])?;
884 write!(f, "}}")
885 }
886 TyFnPtr(ref bare_fn) => {
887 if bare_fn.unsafety == hir::Unsafety::Unsafe {
888 write!(f, "unsafe ")?;
889 }
890
891 if bare_fn.abi != Abi::Rust {
892 write!(f, "extern {} ", bare_fn.abi)?;
893 }
894
895 write!(f, "{}", bare_fn.sig.0)
896 }
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))
905 } else {
906 parameterized(f, substs, def.did, &[])
907 }
908 })
909 }
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(|| {
918 Substs::empty(tcx)
919 });
920 let bounds = item_predicates.instantiate(tcx, substs);
921
922 let mut first = true;
923 let mut is_sized = false;
924 write!(f, "impl")?;
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() {
929 is_sized = true;
930 continue;
931 }
932
933 write!(f, "{}{}", if first { " " } else { "+" }, trait_ref)?;
934 first = false;
935 }
936 }
937 if !is_sized {
938 write!(f, "{}?Sized", if first { " " } else { "+" })?;
939 }
940 Ok(())
941 })
942 }
943 TyStr => write!(f, "str"),
944 TyClosure(did, substs) => ty::tls::with(|tcx| {
945 write!(f, "[closure")?;
946
947 if let Some(node_id) = tcx.map.as_local_node_id(did) {
948 write!(f, "@{:?}", tcx.map.span(node_id))?;
949 let mut sep = " ";
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();
954 write!(f,
955 "{}{}:{}",
956 sep,
957 tcx.local_var_name_str(node_id),
958 upvar_ty)?;
959 sep = ", ";
960 }
961 Ok(())
962 })?
963 } else {
964 // cross-crate closure types should only be
965 // visible in trans bug reports, I imagine.
966 write!(f, "@{:?}", did)?;
967 let mut sep = " ";
968 for (index, upvar_ty) in substs.upvar_tys.iter().enumerate() {
969 write!(f, "{}{}:{}", sep, index, upvar_ty)?;
970 sep = ", ";
971 }
972 }
973
974 write!(f, "]")
975 }),
976 TyArray(ty, sz) => write!(f, "[{}; {}]", ty, sz),
977 TySlice(ty) => write!(f, "[{}]", ty)
978 }
979 }
980 }
981
982 impl<'tcx> fmt::Display for ty::TyS<'tcx> {
983 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
984 write!(f, "{}", self.sty)
985 }
986 }
987
988 impl fmt::Debug for ty::UpvarId {
989 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
990 write!(f, "UpvarId({};`{}`;{})",
991 self.var_id,
992 ty::tls::with(|tcx| tcx.local_var_name_str(self.var_id)),
993 self.closure_expr_id)
994 }
995 }
996
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)
1001 }
1002 }
1003
1004 impl fmt::Display for ty::InferTy {
1005 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1006 let print_var_ids = verbose();
1007 match *self {
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)
1017 }
1018 }
1019 }
1020
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) => {
1027 "&mut self"
1028 }
1029 ty::ExplicitSelfCategory::ByReference(_, hir::MutImmutable) => "&self",
1030 ty::ExplicitSelfCategory::ByBox => "Box<self>",
1031 })
1032 }
1033 }
1034
1035 impl fmt::Display for ty::ParamTy {
1036 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1037 write!(f, "{}", self.name)
1038 }
1039 }
1040
1041 impl fmt::Debug for ty::ParamTy {
1042 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1043 write!(f, "{}/#{}", self, self.idx)
1044 }
1045 }
1046
1047 impl<'tcx, T, U> fmt::Display for ty::OutlivesPredicate<T,U>
1048 where T: fmt::Display, U: fmt::Display
1049 {
1050 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1051 write!(f, "{} : {}", self.0, self.1)
1052 }
1053 }
1054
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)
1058 }
1059 }
1060
1061 impl<'tcx> fmt::Debug for ty::TraitPredicate<'tcx> {
1062 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1063 write!(f, "TraitPredicate({:?})",
1064 self.trait_ref)
1065 }
1066 }
1067
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)
1071 }
1072 }
1073
1074 impl<'tcx> fmt::Debug for ty::ProjectionPredicate<'tcx> {
1075 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1076 write!(f, "ProjectionPredicate({:?}, {:?})",
1077 self.projection_ty,
1078 self.ty)
1079 }
1080 }
1081
1082 impl<'tcx> fmt::Display for ty::ProjectionPredicate<'tcx> {
1083 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1084 write!(f, "{} == {}",
1085 self.projection_ty,
1086 self.ty)
1087 }
1088 }
1089
1090 impl<'tcx> fmt::Display for ty::ProjectionTy<'tcx> {
1091 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1092 write!(f, "{:?}::{}",
1093 self.trait_ref,
1094 self.item_name)
1095 }
1096 }
1097
1098 impl fmt::Display for ty::ClosureKind {
1099 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1100 match *self {
1101 ty::ClosureKind::Fn => write!(f, "Fn"),
1102 ty::ClosureKind::FnMut => write!(f, "FnMut"),
1103 ty::ClosureKind::FnOnce => write!(f, "FnOnce"),
1104 }
1105 }
1106 }
1107
1108 impl<'tcx> fmt::Display for ty::Predicate<'tcx> {
1109 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1110 match *self {
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))
1120 }),
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)
1125 }),
1126 }
1127 }
1128 }