9
9
// except according to those terms.
10
10
11
11
use hir:: def_id:: DefId ;
12
+ use hir:: map:: definitions:: DefPathData ;
12
13
use ty:: subst:: { self , Subst } ;
13
14
use ty:: { BrAnon , BrEnv , BrFresh , BrNamed } ;
14
15
use ty:: { TyBool , TyChar , TyAdt } ;
@@ -56,17 +57,9 @@ fn fn_sig(f: &mut fmt::Formatter,
56
57
Ok ( ( ) )
57
58
}
58
59
59
- /// Namespace of the path given to parameterized to print.
60
- #[ derive( Copy , Clone , PartialEq , Debug ) ]
61
- pub enum Ns {
62
- Type ,
63
- Value
64
- }
65
-
66
60
pub fn parameterized ( f : & mut fmt:: Formatter ,
67
61
substs : & subst:: Substs ,
68
62
did : DefId ,
69
- ns : Ns ,
70
63
projections : & [ ty:: ProjectionPredicate ] )
71
64
-> fmt:: Result {
72
65
let mut verbose = false ;
@@ -75,16 +68,42 @@ pub fn parameterized(f: &mut fmt::Formatter,
75
68
let mut num_regions = 0 ;
76
69
let mut num_types = 0 ;
77
70
let mut item_name = None ;
71
+ let mut is_value_path = false ;
78
72
let fn_trait_kind = ty:: tls:: with ( |tcx| {
79
- let mut generics = tcx. lookup_generics ( did) ;
73
+ // Unfortunately, some kinds of items (e.g., closures) don't have
74
+ // generics. So walk back up the find the closest parent that DOES
75
+ // have them.
76
+ let mut item_def_id = did;
77
+ loop {
78
+ let key = tcx. def_key ( item_def_id) ;
79
+ match key. disambiguated_data . data {
80
+ DefPathData :: TypeNs ( _) => {
81
+ break ;
82
+ }
83
+ DefPathData :: ValueNs ( _) | DefPathData :: EnumVariant ( _) => {
84
+ is_value_path = true ;
85
+ break ;
86
+ }
87
+ _ => {
88
+ // if we're making a symbol for something, there ought
89
+ // to be a value or type-def or something in there
90
+ // *somewhere*
91
+ item_def_id. index = key. parent . unwrap_or_else ( || {
92
+ bug ! ( "finding type for {:?}, encountered def-id {:?} with no \
93
+ parent", did, item_def_id) ;
94
+ } ) ;
95
+ }
96
+ }
97
+ }
98
+ let mut generics = tcx. lookup_generics ( item_def_id) ;
80
99
let mut path_def_id = did;
81
100
verbose = tcx. sess . verbose ( ) ;
82
101
has_self = generics. has_self ;
83
102
84
103
let mut child_types = 0 ;
85
104
if let Some ( def_id) = generics. parent {
86
105
// Methods.
87
- assert_eq ! ( ns , Ns :: Value ) ;
106
+ assert ! ( is_value_path ) ;
88
107
child_types = generics. types . len ( ) ;
89
108
generics = tcx. lookup_generics ( def_id) ;
90
109
num_regions = generics. regions . len ( ) ;
@@ -97,7 +116,7 @@ pub fn parameterized(f: &mut fmt::Formatter,
97
116
item_name = Some ( tcx. item_name ( did) ) ;
98
117
path_def_id = def_id;
99
118
} else {
100
- if ns == Ns :: Value {
119
+ if is_value_path {
101
120
// Functions.
102
121
assert_eq ! ( has_self, false ) ;
103
122
} else {
@@ -192,7 +211,7 @@ pub fn parameterized(f: &mut fmt::Formatter,
192
211
start_or_continue ( f, "" , ">" ) ?;
193
212
194
213
// For values, also print their name and type parameters.
195
- if ns == Ns :: Value {
214
+ if is_value_path {
196
215
empty. set ( true ) ;
197
216
198
217
if has_self {
@@ -298,7 +317,6 @@ impl<'tcx> fmt::Display for TraitAndProjections<'tcx> {
298
317
let TraitAndProjections ( ref trait_ref, ref projection_bounds) = * self ;
299
318
parameterized ( f, trait_ref. substs ,
300
319
trait_ref. def_id ,
301
- Ns :: Type ,
302
320
projection_bounds)
303
321
}
304
322
}
@@ -398,7 +416,7 @@ impl<'tcx> fmt::Debug for ty::ExistentialTraitRef<'tcx> {
398
416
let trait_ref = tcx. lift ( & ty:: Binder ( * self ) )
399
417
. expect ( "could not lift TraitRef for printing" )
400
418
. with_self_ty ( tcx, dummy_self) . 0 ;
401
- parameterized ( f, trait_ref. substs , trait_ref. def_id , Ns :: Type , & [ ] )
419
+ parameterized ( f, trait_ref. substs , trait_ref. def_id , & [ ] )
402
420
} )
403
421
}
404
422
}
@@ -798,7 +816,7 @@ impl<'tcx> fmt::Display for ty::Binder<ty::OutlivesPredicate<&'tcx ty::Region,
798
816
799
817
impl < ' tcx > fmt:: Display for ty:: TraitRef < ' tcx > {
800
818
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
801
- parameterized ( f, self . substs , self . def_id , Ns :: Type , & [ ] )
819
+ parameterized ( f, self . substs , self . def_id , & [ ] )
802
820
}
803
821
}
804
822
@@ -851,7 +869,7 @@ impl<'tcx> fmt::Display for ty::TypeVariants<'tcx> {
851
869
}
852
870
853
871
write ! ( f, "{} {{" , bare_fn. sig. 0 ) ?;
854
- parameterized ( f, substs, def_id, Ns :: Value , & [ ] ) ?;
872
+ parameterized ( f, substs, def_id, & [ ] ) ?;
855
873
write ! ( f, "}}" )
856
874
}
857
875
TyFnPtr ( ref bare_fn) => {
@@ -874,7 +892,7 @@ impl<'tcx> fmt::Display for ty::TypeVariants<'tcx> {
874
892
!tcx. tcache . borrow ( ) . contains_key ( & def. did ) {
875
893
write ! ( f, "{}<..>" , tcx. item_path_str( def. did) )
876
894
} else {
877
- parameterized ( f, substs, def. did , Ns :: Type , & [ ] )
895
+ parameterized ( f, substs, def. did , & [ ] )
878
896
}
879
897
} )
880
898
}
0 commit comments