@@ -52,19 +52,13 @@ pub trait UserString<'tcx> : Repr<'tcx> {
52
52
pub fn note_and_explain_region ( cx : & ctxt ,
53
53
prefix : & str ,
54
54
region : ty:: Region ,
55
- suffix : & str ) -> Option < Span > {
56
- match explain_region_and_span ( cx, region) {
57
- ( ref str, Some ( span) ) => {
58
- cx. sess . span_note (
59
- span,
60
- & format ! ( "{}{}{}" , prefix, * str , suffix) ) ;
61
- Some ( span)
62
- }
63
- ( ref str, None ) => {
64
- cx. sess . note (
65
- & format ! ( "{}{}{}" , prefix, * str , suffix) ) ;
66
- None
67
- }
55
+ suffix : & str ) {
56
+ let ( description, span) = explain_region_and_span ( cx, region) ;
57
+ let message = format ! ( "{}{}{}" , prefix, description, suffix) ;
58
+ if let Some ( span) = span {
59
+ cx. sess . span_note ( span, & message) ;
60
+ } else {
61
+ cx. sess . note ( & message) ;
68
62
}
69
63
}
70
64
@@ -81,8 +75,8 @@ fn item_scope_tag(item: &ast::Item) -> &'static str {
81
75
}
82
76
}
83
77
84
- pub fn explain_region_and_span ( cx : & ctxt , region : ty:: Region )
85
- -> ( String , Option < Span > ) {
78
+ fn explain_region_and_span ( cx : & ctxt , region : ty:: Region )
79
+ -> ( String , Option < Span > ) {
86
80
return match region {
87
81
ReScope ( scope) => {
88
82
let new_string;
@@ -138,7 +132,7 @@ pub fn explain_region_and_span(cx: &ctxt, region: ty::Region)
138
132
BrFresh ( _) => "an anonymous lifetime defined on" . to_string ( ) ,
139
133
_ => {
140
134
format ! ( "the lifetime {} as defined on" ,
141
- bound_region_ptr_to_string ( cx , fr. bound_region) )
135
+ fr. bound_region. user_string ( cx ) )
142
136
}
143
137
} ;
144
138
@@ -182,61 +176,6 @@ pub fn explain_region_and_span(cx: &ctxt, region: ty::Region)
182
176
}
183
177
}
184
178
185
- pub fn bound_region_ptr_to_string ( cx : & ctxt , br : BoundRegion ) -> String {
186
- bound_region_to_string ( cx, "" , false , br)
187
- }
188
-
189
- pub fn bound_region_to_string ( cx : & ctxt ,
190
- prefix : & str , space : bool ,
191
- br : BoundRegion ) -> String {
192
- let space_str = if space { " " } else { "" } ;
193
-
194
- if cx. sess . verbose ( ) {
195
- return format ! ( "{}{}{}" , prefix, br. repr( cx) , space_str)
196
- }
197
-
198
- match br {
199
- BrNamed ( _, name) => {
200
- format ! ( "{}{}{}" , prefix, token:: get_name( name) , space_str)
201
- }
202
- BrAnon ( _) | BrFresh ( _) | BrEnv => prefix. to_string ( )
203
- }
204
- }
205
-
206
- // In general, if you are giving a region error message,
207
- // you should use `explain_region()` or, better yet,
208
- // `note_and_explain_region()`
209
- pub fn region_ptr_to_string ( cx : & ctxt , region : Region ) -> String {
210
- region_to_string ( cx, "&" , true , region)
211
- }
212
-
213
- pub fn region_to_string ( cx : & ctxt , prefix : & str , space : bool , region : Region ) -> String {
214
- let space_str = if space { " " } else { "" } ;
215
-
216
- if cx. sess . verbose ( ) {
217
- return format ! ( "{}{}{}" , prefix, region. repr( cx) , space_str)
218
- }
219
-
220
- // These printouts are concise. They do not contain all the information
221
- // the user might want to diagnose an error, but there is basically no way
222
- // to fit that into a short string. Hence the recommendation to use
223
- // `explain_region()` or `note_and_explain_region()`.
224
- match region {
225
- ty:: ReScope ( _) => prefix. to_string ( ) ,
226
- ty:: ReEarlyBound ( ref data) => {
227
- token:: get_name ( data. name ) . to_string ( )
228
- }
229
- ty:: ReLateBound ( _, br) => bound_region_to_string ( cx, prefix, space, br) ,
230
- ty:: ReFree ( ref fr) => bound_region_to_string ( cx, prefix, space, fr. bound_region ) ,
231
- ty:: ReInfer ( ReSkolemized ( _, br) ) => {
232
- bound_region_to_string ( cx, prefix, space, br)
233
- }
234
- ty:: ReInfer ( ReVar ( _) ) => prefix. to_string ( ) ,
235
- ty:: ReStatic => format ! ( "{}'static{}" , prefix, space_str) ,
236
- ty:: ReEmpty => format ! ( "{}'<empty>{}" , prefix, space_str) ,
237
- }
238
- }
239
-
240
179
pub fn mutability_to_string ( m : ast:: Mutability ) -> String {
241
180
match m {
242
181
ast:: MutMutable => "mut " . to_string ( ) ,
@@ -376,7 +315,11 @@ pub fn ty_to_string<'tcx>(cx: &ctxt<'tcx>, typ: &ty::TyS<'tcx>) -> String {
376
315
} , ty_to_string( cx, tm. ty) )
377
316
}
378
317
TyRef ( r, ref tm) => {
379
- let mut buf = region_ptr_to_string ( cx, * r) ;
318
+ let mut buf = "&" . to_owned ( ) ;
319
+ buf. push_str ( & r. user_string ( cx) ) ;
320
+ if !buf. is_empty ( ) {
321
+ buf. push_str ( " " ) ;
322
+ }
380
323
buf. push_str ( & mt_to_string ( cx, tm) ) ;
381
324
buf
382
325
}
@@ -440,26 +383,13 @@ pub fn ty_to_string<'tcx>(cx: &ctxt<'tcx>, typ: &ty::TyS<'tcx>) -> String {
440
383
}
441
384
}
442
385
443
- pub fn explicit_self_category_to_str ( category : & ty:: ExplicitSelfCategory )
444
- -> & ' static str {
445
- match * category {
446
- ty:: StaticExplicitSelfCategory => "static" ,
447
- ty:: ByValueExplicitSelfCategory => "self" ,
448
- ty:: ByReferenceExplicitSelfCategory ( _, ast:: MutMutable ) => {
449
- "&mut self"
450
- }
451
- ty:: ByReferenceExplicitSelfCategory ( _, ast:: MutImmutable ) => "&self" ,
452
- ty:: ByBoxExplicitSelfCategory => "Box<self>" ,
453
- }
454
- }
455
-
456
- pub fn parameterized < ' tcx , GG > ( cx : & ctxt < ' tcx > ,
457
- base : & str ,
458
- substs : & subst:: Substs < ' tcx > ,
459
- did : ast:: DefId ,
460
- projections : & [ ty:: ProjectionPredicate < ' tcx > ] ,
461
- get_generics : GG )
462
- -> String
386
+ fn parameterized < ' tcx , GG > ( cx : & ctxt < ' tcx > ,
387
+ base : & str ,
388
+ substs : & subst:: Substs < ' tcx > ,
389
+ did : ast:: DefId ,
390
+ projections : & [ ty:: ProjectionPredicate < ' tcx > ] ,
391
+ get_generics : GG )
392
+ -> String
463
393
where GG : FnOnce ( ) -> ty:: Generics < ' tcx >
464
394
{
465
395
if cx. sess . verbose ( ) {
@@ -495,7 +425,7 @@ pub fn parameterized<'tcx,GG>(cx: &ctxt<'tcx>,
495
425
subst:: ErasedRegions => { }
496
426
subst:: NonerasedRegions ( ref regions) => {
497
427
for & r in regions {
498
- let s = region_to_string ( cx, "" , false , r ) ;
428
+ let s = r . user_string ( cx) ;
499
429
if s. is_empty ( ) {
500
430
// This happens when the value of the region
501
431
// parameter is not easily serialized. This may be
@@ -579,14 +509,6 @@ pub fn parameterized<'tcx,GG>(cx: &ctxt<'tcx>,
579
509
}
580
510
}
581
511
582
- pub fn ty_to_short_str < ' tcx > ( cx : & ctxt < ' tcx > , typ : Ty < ' tcx > ) -> String {
583
- let mut s = typ. repr ( cx) . to_string ( ) ;
584
- if s. len ( ) >= 32 {
585
- s = ( & s[ 0 ..32 ] ) . to_string ( ) ;
586
- }
587
- return s;
588
- }
589
-
590
512
impl < ' tcx , T : Repr < ' tcx > > Repr < ' tcx > for Option < T > {
591
513
fn repr ( & self , tcx : & ctxt < ' tcx > ) -> String {
592
514
match self {
@@ -915,6 +837,19 @@ impl<'tcx> Repr<'tcx> for ty::BoundRegion {
915
837
}
916
838
}
917
839
840
+ impl < ' tcx > UserString < ' tcx > for ty:: BoundRegion {
841
+ fn user_string ( & self , tcx : & ctxt ) -> String {
842
+ if tcx. sess . verbose ( ) {
843
+ return self . repr ( tcx) ;
844
+ }
845
+
846
+ match * self {
847
+ BrNamed ( _, name) => token:: get_name ( name) . to_string ( ) ,
848
+ BrAnon ( _) | BrFresh ( _) | BrEnv => String :: new ( )
849
+ }
850
+ }
851
+ }
852
+
918
853
impl < ' tcx > Repr < ' tcx > for ty:: Region {
919
854
fn repr ( & self , tcx : & ctxt ) -> String {
920
855
match * self {
@@ -959,7 +894,28 @@ impl<'tcx> Repr<'tcx> for ty::Region {
959
894
960
895
impl < ' tcx > UserString < ' tcx > for ty:: Region {
961
896
fn user_string ( & self , tcx : & ctxt ) -> String {
962
- region_to_string ( tcx, "" , false , * self )
897
+ if tcx. sess . verbose ( ) {
898
+ return self . repr ( tcx) ;
899
+ }
900
+
901
+ // These printouts are concise. They do not contain all the information
902
+ // the user might want to diagnose an error, but there is basically no way
903
+ // to fit that into a short string. Hence the recommendation to use
904
+ // `explain_region()` or `note_and_explain_region()`.
905
+ match * self {
906
+ ty:: ReEarlyBound ( ref data) => {
907
+ token:: get_name ( data. name ) . to_string ( )
908
+ }
909
+ ty:: ReLateBound ( _, br) |
910
+ ty:: ReFree ( ty:: FreeRegion { bound_region : br, .. } ) |
911
+ ty:: ReInfer ( ReSkolemized ( _, br) ) => {
912
+ br. user_string ( tcx)
913
+ }
914
+ ty:: ReScope ( _) |
915
+ ty:: ReInfer ( ReVar ( _) ) => String :: new ( ) ,
916
+ ty:: ReStatic => "'static" . to_owned ( ) ,
917
+ ty:: ReEmpty => "'<empty>" . to_owned ( ) ,
918
+ }
963
919
}
964
920
}
965
921
@@ -1446,7 +1402,15 @@ impl<'tcx> Repr<'tcx> for ast::FloatTy {
1446
1402
1447
1403
impl < ' tcx > Repr < ' tcx > for ty:: ExplicitSelfCategory {
1448
1404
fn repr ( & self , _: & ctxt ) -> String {
1449
- explicit_self_category_to_str ( self ) . to_string ( )
1405
+ match * self {
1406
+ ty:: StaticExplicitSelfCategory => "static" ,
1407
+ ty:: ByValueExplicitSelfCategory => "self" ,
1408
+ ty:: ByReferenceExplicitSelfCategory ( _, ast:: MutMutable ) => {
1409
+ "&mut self"
1410
+ }
1411
+ ty:: ByReferenceExplicitSelfCategory ( _, ast:: MutImmutable ) => "&self" ,
1412
+ ty:: ByBoxExplicitSelfCategory => "Box<self>" ,
1413
+ } . to_owned ( )
1450
1414
}
1451
1415
}
1452
1416
0 commit comments