forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppaux.rs
895 lines (797 loc) · 29.6 KB
/
ppaux.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use middle::subst::{self, Subst};
use middle::ty::{BoundRegion, BrAnon, BrNamed};
use middle::ty::{ReEarlyBound, BrFresh, ctxt};
use middle::ty::{ReFree, ReScope, ReInfer, ReStatic, Region, ReEmpty};
use middle::ty::{ReSkolemized, ReVar, BrEnv};
use middle::ty::{mt, Ty};
use middle::ty::{TyBool, TyChar, TyStruct, TyEnum};
use middle::ty::{TyError, TyStr, TyArray, TySlice, TyFloat, TyBareFn};
use middle::ty::{TyParam, TyRawPtr, TyRef, TyTuple};
use middle::ty::TyClosure;
use middle::ty::{TyBox, TyTrait, TyInt, TyUint, TyInfer};
use middle::ty;
use middle::ty_fold::{self, TypeFoldable};
use std::fmt;
use syntax::abi;
use syntax::parse::token;
use syntax::{ast, ast_util};
/// Produces a string suitable for debugging output.
pub trait Repr {
fn repr(&self) -> String;
}
/// Produces a string suitable for showing to the user.
pub trait UserString {
fn user_string(&self) -> String;
}
impl<T: fmt::Debug> Repr for T {
fn repr(&self) -> String {
format!("{:?}", *self)
}
}
impl<T: fmt::Display> UserString for T {
fn user_string(&self) -> String {
format!("{}", *self)
}
}
pub fn verbose() -> bool {
ty::tls::with(|tcx| tcx.sess.verbose())
}
fn fn_sig(f: &mut fmt::Formatter,
inputs: &[Ty],
variadic: bool,
output: ty::FnOutput)
-> fmt::Result {
try!(write!(f, "("));
let mut inputs = inputs.iter();
if let Some(&ty) = inputs.next() {
try!(write!(f, "{}", ty));
for &ty in inputs {
try!(write!(f, ", {}", ty));
}
if variadic {
try!(write!(f, ", ..."));
}
}
try!(write!(f, ")"));
match output {
ty::FnConverging(ty) => {
if !ty::type_is_nil(ty) {
try!(write!(f, " -> {}", ty));
}
Ok(())
}
ty::FnDiverging => {
write!(f, " -> !")
}
}
}
fn parameterized<GG>(f: &mut fmt::Formatter,
substs: &subst::Substs,
did: ast::DefId,
projections: &[ty::ProjectionPredicate],
get_generics: GG)
-> fmt::Result
where GG: for<'tcx> FnOnce(&ty::ctxt<'tcx>) -> ty::Generics<'tcx>
{
let (fn_trait_kind, verbose) = try!(ty::tls::with(|tcx| {
try!(write!(f, "{}", ty::item_path_str(tcx, did)));
Ok((tcx.lang_items.fn_trait_kind(did), tcx.sess.verbose()))
}));
let mut empty = true;
let mut start_or_continue = |f: &mut fmt::Formatter, start: &str, cont: &str| {
if empty {
empty = false;
write!(f, "{}", start)
} else {
write!(f, "{}", cont)
}
};
if verbose {
match substs.regions {
subst::ErasedRegions => {
try!(start_or_continue(f, "<", ", "));
try!(write!(f, ".."));
}
subst::NonerasedRegions(ref regions) => {
for region in regions {
try!(start_or_continue(f, "<", ", "));
try!(write!(f, "{:?}", region));
}
}
}
for &ty in &substs.types {
try!(start_or_continue(f, "<", ", "));
try!(write!(f, "{}", ty));
}
for projection in projections {
try!(start_or_continue(f, "<", ", "));
try!(write!(f, "{}={}",
projection.projection_ty.item_name,
projection.ty));
}
return start_or_continue(f, "", ">");
}
if fn_trait_kind.is_some() && projections.len() == 1 {
let projection_ty = projections[0].ty;
if let TyTuple(ref args) = substs.types.get_slice(subst::TypeSpace)[0].sty {
return fn_sig(f, args, false, ty::FnConverging(projection_ty));
}
}
match substs.regions {
subst::ErasedRegions => { }
subst::NonerasedRegions(ref regions) => {
for &r in regions {
try!(start_or_continue(f, "<", ", "));
let s = r.user_string();
if s.is_empty() {
// This happens when the value of the region
// parameter is not easily serialized. This may be
// because the user omitted it in the first place,
// or because it refers to some block in the code,
// etc. I'm not sure how best to serialize this.
try!(write!(f, "'_"));
} else {
try!(write!(f, "{}", s));
}
}
}
}
// It is important to execute this conditionally, only if -Z
// verbose is false. Otherwise, debug logs can sometimes cause
// ICEs trying to fetch the generics early in the pipeline. This
// is kind of a hacky workaround in that -Z verbose is required to
// avoid those ICEs.
let tps = substs.types.get_slice(subst::TypeSpace);
let num_defaults = ty::tls::with(|tcx| {
let generics = get_generics(tcx);
let has_self = substs.self_ty().is_some();
let ty_params = generics.types.get_slice(subst::TypeSpace);
if ty_params.last().map_or(false, |def| def.default.is_some()) {
let substs = tcx.lift(&substs);
ty_params.iter().zip(tps).rev().take_while(|&(def, &actual)| {
match def.default {
Some(default) => {
if !has_self && ty::type_has_self(default) {
// In an object type, there is no `Self`, and
// thus if the default value references Self,
// the user will be required to give an
// explicit value. We can't even do the
// substitution below to check without causing
// an ICE. (#18956).
false
} else {
let default = tcx.lift(&default);
substs.and_then(|substs| default.subst(tcx, substs)) == Some(actual)
}
}
None => false
}
}).count()
} else {
0
}
});
for &ty in &tps[..tps.len() - num_defaults] {
try!(start_or_continue(f, "<", ", "));
try!(write!(f, "{}", ty));
}
for projection in projections {
try!(start_or_continue(f, "<", ", "));
try!(write!(f, "{}={}",
projection.projection_ty.item_name,
projection.ty));
}
start_or_continue(f, "", ">")
}
fn in_binder<'tcx, T, U>(f: &mut fmt::Formatter,
tcx: &ty::ctxt<'tcx>,
original: &ty::Binder<T>,
lifted: Option<ty::Binder<U>>) -> fmt::Result
where T: fmt::Display, U: fmt::Display + TypeFoldable<'tcx>
{
// Replace any anonymous late-bound regions with named
// variants, using gensym'd identifiers, so that we can
// clearly differentiate between named and unnamed regions in
// the output. We'll probably want to tweak this over time to
// decide just how much information to give.
let value = if let Some(v) = lifted {
v
} else {
return write!(f, "{}", original.0);
};
let mut empty = true;
let mut start_or_continue = |f: &mut fmt::Formatter, start: &str, cont: &str| {
if empty {
empty = false;
write!(f, "{}", start)
} else {
write!(f, "{}", cont)
}
};
let new_value = ty_fold::replace_late_bound_regions(tcx, &value, |br| {
let _ = start_or_continue(f, "for<", ", ");
ty::ReLateBound(ty::DebruijnIndex::new(1), match br {
ty::BrNamed(_, name) => {
let _ = write!(f, "{}", name);
br
}
ty::BrAnon(_) |
ty::BrFresh(_) |
ty::BrEnv => {
let name = token::intern("'r");
let _ = write!(f, "{}", name);
ty::BrNamed(ast_util::local_def(ast::DUMMY_NODE_ID), name)
}
})
}).0;
try!(start_or_continue(f, "", "> "));
write!(f, "{}", new_value)
}
/// This curious type is here to help pretty-print trait objects. In
/// a trait object, the projections are stored separately from the
/// main trait bound, but in fact we want to package them together
/// when printing out; they also have separate binders, but we want
/// them to share a binder when we print them out. (And the binder
/// pretty-printing logic is kind of clever and we don't want to
/// reproduce it.) So we just repackage up the structure somewhat.
///
/// Right now there is only one trait in an object that can have
/// projection bounds, so we just stuff them altogether. But in
/// reality we should eventually sort things out better.
#[derive(Clone, Debug)]
struct TraitAndProjections<'tcx>(ty::TraitRef<'tcx>, Vec<ty::ProjectionPredicate<'tcx>>);
impl<'tcx> TypeFoldable<'tcx> for TraitAndProjections<'tcx> {
fn fold_with<F:ty_fold::TypeFolder<'tcx>>(&self, folder: &mut F)
-> TraitAndProjections<'tcx> {
TraitAndProjections(self.0.fold_with(folder), self.1.fold_with(folder))
}
}
impl<'tcx> fmt::Display for TraitAndProjections<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let TraitAndProjections(ref trait_ref, ref projection_bounds) = *self;
parameterized(f, trait_ref.substs,
trait_ref.def_id,
projection_bounds,
|tcx| ty::lookup_trait_def(tcx, trait_ref.def_id).generics.clone())
}
}
impl<'tcx> fmt::Display for ty::TraitTy<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let bounds = &self.bounds;
// Generate the main trait ref, including associated types.
try!(ty::tls::with(|tcx| {
let principal = tcx.lift(&self.principal.0)
.expect("could not lift TraitRef for printing");
let projections = tcx.lift(&bounds.projection_bounds[..])
.expect("could not lift projections for printing");
let projections = projections.map_in_place(|p| p.0);
let tap = ty::Binder(TraitAndProjections(principal, projections));
in_binder(f, tcx, &ty::Binder(""), Some(tap))
}));
// Builtin bounds.
for bound in &bounds.builtin_bounds {
try!(write!(f, " + {:?}", bound));
}
// Region, if not obviously implied by builtin bounds.
if bounds.region_bound != ty::ReStatic {
// Region bound is implied by builtin bounds:
let bound = bounds.region_bound.user_string();
if !bound.is_empty() {
try!(write!(f, " + {}", bound));
}
}
Ok(())
}
}
impl<'tcx> fmt::Debug for ty::TypeParameterDef<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TypeParameterDef({:?}, {:?}/{})",
self.def_id, self.space, self.index)
}
}
impl<'tcx> fmt::Debug for ty::TyS<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", *self)
}
}
impl<'tcx> fmt::Display for ty::mt<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}",
if self.mutbl == ast::MutMutable { "mut " } else { "" },
self.ty)
}
}
impl<'tcx> fmt::Debug for subst::Substs<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Substs[types={:?}, regions={:?}]",
self.types, self.regions)
}
}
impl<'tcx> fmt::Debug for ty::ItemSubsts<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ItemSubsts({:?})", self.substs)
}
}
impl fmt::Debug for subst::RegionSubsts {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
subst::ErasedRegions => write!(f, "erased"),
subst::NonerasedRegions(ref regions) => write!(f, "{:?}", regions)
}
}
}
impl<'tcx> fmt::Debug for ty::ParamBounds<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{:?}", self.builtin_bounds));
let mut bounds = self.trait_bounds.iter();
if self.builtin_bounds.is_empty() {
if let Some(bound) = bounds.next() {
try!(write!(f, "{:?}", bound));
}
}
for bound in bounds {
try!(write!(f, " + {:?}", bound));
}
Ok(())
}
}
impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// when printing out the debug representation, we don't need
// to enumerate the `for<...>` etc because the debruijn index
// tells you everything you need to know.
match self.substs.self_ty() {
None => write!(f, "{}", *self),
Some(self_ty) => write!(f, "<{:?} as {}>", self_ty, *self)
}
}
}
impl<'tcx> fmt::Debug for ty::TraitDef<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TraitDef(generics={:?}, trait_ref={:?})",
self.generics, self.trait_ref)
}
}
impl fmt::Display for ty::BoundRegion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if verbose() {
return write!(f, "{:?}", *self);
}
match *self {
BrNamed(_, name) => write!(f, "{}", name),
BrAnon(_) | BrFresh(_) | BrEnv => Ok(())
}
}
}
impl fmt::Debug for ty::Region {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ty::ReEarlyBound(ref data) => {
write!(f, "ReEarlyBound({}, {:?}, {}, {})",
data.param_id,
data.space,
data.index,
data.name)
}
ty::ReLateBound(binder_id, ref bound_region) => {
write!(f, "ReLateBound({:?}, {:?})",
binder_id,
bound_region)
}
ty::ReFree(ref fr) => write!(f, "{:?}", fr),
ty::ReScope(id) => {
write!(f, "ReScope({:?})", id)
}
ty::ReStatic => write!(f, "ReStatic"),
ty::ReInfer(ReVar(ref vid)) => {
write!(f, "{:?}", vid)
}
ty::ReInfer(ReSkolemized(id, ref bound_region)) => {
write!(f, "ReSkolemized({}, {:?})", id, bound_region)
}
ty::ReEmpty => write!(f, "ReEmpty")
}
}
}
impl fmt::Display for ty::Region {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if verbose() {
return write!(f, "{:?}", *self);
}
// These printouts are concise. They do not contain all the information
// the user might want to diagnose an error, but there is basically no way
// to fit that into a short string. Hence the recommendation to use
// `explain_region()` or `note_and_explain_region()`.
match *self {
ty::ReEarlyBound(ref data) => {
write!(f, "{}", data.name)
}
ty::ReLateBound(_, br) |
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
ty::ReInfer(ReSkolemized(_, br)) => {
write!(f, "{}", br)
}
ty::ReScope(_) |
ty::ReInfer(ReVar(_)) => Ok(()),
ty::ReStatic => write!(f, "'static"),
ty::ReEmpty => write!(f, "'<empty>"),
}
}
}
impl fmt::Debug for ty::FreeRegion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ReFree({:?}, {:?})",
self.scope, self.bound_region)
}
}
impl fmt::Debug for ty::ItemVariances {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ItemVariances(types={:?}, regions={:?})",
self.types, self.regions)
}
}
impl<'tcx> fmt::Debug for ty::GenericPredicates<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "GenericPredicates({:?})", self.predicates)
}
}
impl<'tcx> fmt::Debug for ty::InstantiatedPredicates<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "InstantiatedPredicates({:?})",
self.predicates)
}
}
impl<'tcx> fmt::Debug for ty::ImplOrTraitItem<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "ImplOrTraitItem("));
try!(match *self {
ty::ImplOrTraitItem::MethodTraitItem(ref i) => write!(f, "{:?}", i),
ty::ImplOrTraitItem::ConstTraitItem(ref i) => write!(f, "{:?}", i),
ty::ImplOrTraitItem::TypeTraitItem(ref i) => write!(f, "{:?}", i),
});
write!(f, ")")
}
}
impl<'tcx> fmt::Display for ty::FnSig<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "fn"));
fn_sig(f, &self.inputs, self.variadic, self.output)
}
}
impl<'tcx> fmt::Debug for ty::MethodOrigin<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ty::MethodStatic(def_id) => {
write!(f, "MethodStatic({:?})", def_id)
}
ty::MethodStaticClosure(def_id) => {
write!(f, "MethodStaticClosure({:?})", def_id)
}
ty::MethodTypeParam(ref p) => write!(f, "{:?}", p),
ty::MethodTraitObject(ref p) => write!(f, "{:?}", p)
}
}
}
impl<'tcx> fmt::Debug for ty::MethodParam<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "MethodParam({:?},{})",
self.trait_ref,
self.method_num)
}
}
impl<'tcx> fmt::Debug for ty::MethodObject<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "MethodObject({:?},{},{})",
self.trait_ref,
self.method_num,
self.vtable_index)
}
}
impl<'tcx> fmt::Display for ty::ParamBounds<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{}", self.builtin_bounds));
let mut bounds = self.trait_bounds.iter();
if self.builtin_bounds.is_empty() {
if let Some(bound) = bounds.next() {
try!(write!(f, "{}", bound));
}
}
for bound in bounds {
try!(write!(f, " + {}", bound));
}
Ok(())
}
}
impl<'tcx> fmt::Debug for ty::ExistentialBounds<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut empty = true;
let mut maybe_continue = |f: &mut fmt::Formatter| {
if empty {
empty = false;
Ok(())
} else {
write!(f, " + ")
}
};
let region_str = self.region_bound.repr();
if !region_str.is_empty() {
try!(maybe_continue(f));
try!(write!(f, "{}", region_str));
}
for bound in &self.builtin_bounds {
try!(maybe_continue(f));
try!(write!(f, "{:?}", bound));
}
for projection_bound in &self.projection_bounds {
try!(maybe_continue(f));
try!(write!(f, "{:?}", projection_bound));
}
Ok(())
}
}
impl fmt::Display for ty::BuiltinBounds {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut bounds = self.iter();
if let Some(bound) = bounds.next() {
try!(write!(f, "{:?}", bound));
for bound in bounds {
try!(write!(f, " + {:?}", bound));
}
}
Ok(())
}
}
// The generic impl doesn't work yet because projections are not
// normalized under HRTB.
/*impl<T> fmt::Display for ty::Binder<T>
where T: fmt::Display + for<'a> ty::Lift<'a>,
for<'a> <T as ty::Lift<'a>>::Lifted: fmt::Display + TypeFoldable<'a>
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
}
}*/
impl<'tcx> fmt::Display for ty::Binder<ty::TraitRef<'tcx>> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
}
}
impl<'tcx> fmt::Display for ty::Binder<ty::TraitPredicate<'tcx>> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
}
}
impl<'tcx> fmt::Display for ty::Binder<ty::EquatePredicate<'tcx>> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
}
}
impl<'tcx> fmt::Display for ty::Binder<ty::ProjectionPredicate<'tcx>> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
}
}
impl<'tcx> fmt::Display for ty::Binder<ty::OutlivesPredicate<Ty<'tcx>, ty::Region>> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
}
}
impl fmt::Display for ty::Binder<ty::OutlivesPredicate<ty::Region, ty::Region>> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
ty::tls::with(|tcx| in_binder(f, tcx, self, tcx.lift(self)))
}
}
impl<'tcx> fmt::Display for ty::TraitRef<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
parameterized(f, self.substs, self.def_id, &[],
|tcx| ty::lookup_trait_def(tcx, self.def_id).generics.clone())
}
}
impl<'tcx> fmt::Display for ty::TypeVariants<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
TyBool => write!(f, "bool"),
TyChar => write!(f, "char"),
TyInt(t) => write!(f, "{}", ast_util::int_ty_to_string(t, None)),
TyUint(t) => write!(f, "{}", ast_util::uint_ty_to_string(t, None)),
TyFloat(t) => write!(f, "{}", ast_util::float_ty_to_string(t)),
TyBox(typ) => write!(f, "Box<{}>", typ),
TyRawPtr(ref tm) => {
write!(f, "*{} {}", match tm.mutbl {
ast::MutMutable => "mut",
ast::MutImmutable => "const",
}, tm.ty)
}
TyRef(r, ref tm) => {
try!(write!(f, "&"));
let s = r.user_string();
try!(write!(f, "{}", s));
if !s.is_empty() {
try!(write!(f, " "));
}
write!(f, "{}", tm)
}
TyTuple(ref tys) => {
try!(write!(f, "("));
let mut tys = tys.iter();
if let Some(&ty) = tys.next() {
try!(write!(f, "{},", ty));
if let Some(&ty) = tys.next() {
try!(write!(f, " {}", ty));
for &ty in tys {
try!(write!(f, ", {}", ty));
}
}
}
write!(f, ")")
}
TyBareFn(opt_def_id, ref bare_fn) => {
if bare_fn.unsafety == ast::Unsafety::Unsafe {
try!(write!(f, "unsafe "));
}
if bare_fn.abi != abi::Rust {
try!(write!(f, "extern {} ", bare_fn.abi));
}
try!(write!(f, "{}", bare_fn.sig.0));
if let Some(def_id) = opt_def_id {
try!(write!(f, " {{{}}}", ty::tls::with(|tcx| {
ty::item_path_str(tcx, def_id)
})));
}
Ok(())
}
TyInfer(infer_ty) => write!(f, "{}", infer_ty),
TyError => write!(f, "[type error]"),
TyParam(ref param_ty) => write!(f, "{}", param_ty),
TyEnum(did, substs) | TyStruct(did, substs) => {
parameterized(f, substs, did, &[],
|tcx| ty::lookup_item_type(tcx, did).generics)
}
TyTrait(ref data) => write!(f, "{}", data),
ty::TyProjection(ref data) => write!(f, "{}", data),
TyStr => write!(f, "str"),
TyClosure(ref did, substs) => ty::tls::with(|tcx| {
try!(write!(f, "[closure"));
let closure_tys = tcx.closure_tys.borrow();
try!(closure_tys.get(did).map(|cty| &cty.sig).and_then(|sig| {
tcx.lift(&substs).map(|substs| sig.subst(tcx, substs))
}).map(|sig| {
fn_sig(f, &sig.0.inputs, false, sig.0.output)
}).unwrap_or_else(|| {
if did.krate == ast::LOCAL_CRATE {
try!(write!(f, " {:?}", tcx.map.span(did.node)));
}
Ok(())
}));
if verbose() {
try!(write!(f, " id={:?}", did));
}
write!(f, "]")
}),
TyArray(ty, sz) => write!(f, "[{}; {}]", ty, sz),
TySlice(ty) => write!(f, "[{}]", ty)
}
}
}
impl<'tcx> fmt::Display for ty::TyS<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.sty)
}
}
impl fmt::Debug for ty::UpvarId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "UpvarId({};`{}`;{})",
self.var_id,
ty::tls::with(|tcx| ty::local_var_name_str(tcx, self.var_id)),
self.closure_expr_id)
}
}
impl fmt::Debug for ty::UpvarBorrow {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "UpvarBorrow({:?}, {:?})",
self.kind, self.region)
}
}
impl fmt::Display for ty::InferTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let print_var_ids = verbose();
match *self {
ty::TyVar(ref vid) if print_var_ids => write!(f, "{:?}", vid),
ty::IntVar(ref vid) if print_var_ids => write!(f, "{:?}", vid),
ty::FloatVar(ref vid) if print_var_ids => write!(f, "{:?}", vid),
ty::TyVar(_) | ty::IntVar(_) | ty::FloatVar(_) => write!(f, "_"),
ty::FreshTy(v) => write!(f, "FreshTy({})", v),
ty::FreshIntTy(v) => write!(f, "FreshIntTy({})", v),
ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({})", v)
}
}
}
impl fmt::Display for ty::ExplicitSelfCategory {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
ty::StaticExplicitSelfCategory => "static",
ty::ByValueExplicitSelfCategory => "self",
ty::ByReferenceExplicitSelfCategory(_, ast::MutMutable) => {
"&mut self"
}
ty::ByReferenceExplicitSelfCategory(_, ast::MutImmutable) => "&self",
ty::ByBoxExplicitSelfCategory => "Box<self>",
})
}
}
impl fmt::Display for ty::ParamTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name)
}
}
impl fmt::Debug for ty::ParamTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}/{:?}.{}", self, self.space, self.idx)
}
}
impl<'tcx, T, U> fmt::Display for ty::OutlivesPredicate<T,U>
where T: fmt::Display, U: fmt::Display
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} : {}", self.0, self.1)
}
}
impl<'tcx> fmt::Display for ty::EquatePredicate<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} == {}", self.0, self.1)
}
}
impl<'tcx> fmt::Debug for ty::TraitPredicate<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TraitPredicate({:?})",
self.trait_ref)
}
}
impl<'tcx> fmt::Display for ty::TraitPredicate<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} : {}",
self.trait_ref.self_ty(),
self.trait_ref)
}
}
impl<'tcx> fmt::Debug for ty::ProjectionPredicate<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ProjectionPredicate({:?}, {:?})",
self.projection_ty,
self.ty)
}
}
impl<'tcx> fmt::Display for ty::ProjectionPredicate<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} == {}",
self.projection_ty,
self.ty)
}
}
impl<'tcx> fmt::Display for ty::ProjectionTy<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}::{}",
self.trait_ref,
self.item_name)
}
}
impl<'tcx> fmt::Display for ty::Predicate<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ty::Predicate::Trait(ref data) => write!(f, "{}", data),
ty::Predicate::Equate(ref predicate) => write!(f, "{}", predicate),
ty::Predicate::RegionOutlives(ref predicate) => write!(f, "{}", predicate),
ty::Predicate::TypeOutlives(ref predicate) => write!(f, "{}", predicate),
ty::Predicate::Projection(ref predicate) => write!(f, "{}", predicate),
}
}
}