@@ -110,16 +110,20 @@ impl<P> PrintCx<'a, 'gcx, 'tcx, P> {
110
110
111
111
pub trait Print < ' tcx , P > {
112
112
type Output ;
113
+ type Error ;
113
114
114
- fn print ( & self , cx : & mut PrintCx < ' _ , ' _ , ' tcx , P > ) -> Self :: Output ;
115
- fn print_display ( & self , cx : & mut PrintCx < ' _ , ' _ , ' tcx , P > ) -> Self :: Output {
115
+ fn print ( & self , cx : & mut PrintCx < ' _ , ' _ , ' tcx , P > ) -> Result < Self :: Output , Self :: Error > ;
116
+ fn print_display (
117
+ & self ,
118
+ cx : & mut PrintCx < ' _ , ' _ , ' tcx , P > ,
119
+ ) -> Result < Self :: Output , Self :: Error > {
116
120
let old_debug = cx. is_debug ;
117
121
cx. is_debug = false ;
118
122
let result = self . print ( cx) ;
119
123
cx. is_debug = old_debug;
120
124
result
121
125
}
122
- fn print_debug ( & self , cx : & mut PrintCx < ' _ , ' _ , ' tcx , P > ) -> Self :: Output {
126
+ fn print_debug ( & self , cx : & mut PrintCx < ' _ , ' _ , ' tcx , P > ) -> Result < Self :: Output , Self :: Error > {
123
127
let old_debug = cx. is_debug ;
124
128
cx. is_debug = true ;
125
129
let result = self . print ( cx) ;
@@ -129,55 +133,54 @@ pub trait Print<'tcx, P> {
129
133
}
130
134
131
135
pub trait Printer : Sized {
136
+ type Error ;
137
+
132
138
type Path ;
133
139
134
- #[ must_use]
135
140
fn print_def_path (
136
141
self : & mut PrintCx < ' _ , ' _ , ' tcx , Self > ,
137
142
def_id : DefId ,
138
143
substs : Option < & ' tcx Substs < ' tcx > > ,
139
144
ns : Namespace ,
140
145
projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
141
- ) -> Self :: Path {
146
+ ) -> Result < Self :: Path , Self :: Error > {
142
147
self . default_print_def_path ( def_id, substs, ns, projections)
143
148
}
144
- #[ must_use]
145
149
fn print_impl_path (
146
150
self : & mut PrintCx < ' _ , ' _ , ' tcx , Self > ,
147
151
impl_def_id : DefId ,
148
152
substs : Option < & ' tcx Substs < ' tcx > > ,
149
153
ns : Namespace ,
150
154
self_ty : Ty < ' tcx > ,
151
155
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
152
- ) -> Self :: Path {
156
+ ) -> Result < Self :: Path , Self :: Error > {
153
157
self . default_print_impl_path ( impl_def_id, substs, ns, self_ty, trait_ref)
154
158
}
155
159
156
- #[ must_use]
157
- fn path_crate ( self : & mut PrintCx < ' _ , ' _ , ' _ , Self > , cnum : CrateNum ) -> Self :: Path ;
158
- #[ must_use]
160
+ fn path_crate (
161
+ self : & mut PrintCx < ' _ , ' _ , ' _ , Self > ,
162
+ cnum : CrateNum ,
163
+ ) -> Result < Self :: Path , Self :: Error > ;
159
164
fn path_qualified (
160
165
self : & mut PrintCx < ' _ , ' _ , ' tcx , Self > ,
161
166
impl_prefix : Option < Self :: Path > ,
162
167
self_ty : Ty < ' tcx > ,
163
168
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
164
169
ns : Namespace ,
165
- ) -> Self :: Path ;
166
- #[ must_use]
170
+ ) -> Result < Self :: Path , Self :: Error > ;
167
171
fn path_append (
168
172
self : & mut PrintCx < ' _ , ' _ , ' _ , Self > ,
169
173
path : Self :: Path ,
170
174
text : & str ,
171
- ) -> Self :: Path ;
172
- #[ must_use]
175
+ ) -> Result < Self :: Path , Self :: Error > ;
173
176
fn path_generic_args (
174
177
self : & mut PrintCx < ' _ , ' _ , ' tcx , Self > ,
175
178
path : Self :: Path ,
176
179
params : & [ ty:: GenericParamDef ] ,
177
180
substs : & ' tcx Substs < ' tcx > ,
178
181
ns : Namespace ,
179
182
projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
180
- ) -> Self :: Path ;
183
+ ) -> Result < Self :: Path , Self :: Error > ;
181
184
}
182
185
183
186
#[ must_use]
@@ -186,7 +189,7 @@ pub struct PrettyPath {
186
189
}
187
190
188
191
/// Trait for printers that pretty-print using `fmt::Write` to the printer.
189
- pub trait PrettyPrinter : Printer < Path = Result < PrettyPath , fmt:: Error > > + fmt:: Write { }
192
+ pub trait PrettyPrinter : Printer < Error = fmt:: Error , Path = PrettyPath > + fmt:: Write { }
190
193
191
194
impl < ' a , ' gcx , ' tcx > TyCtxt < ' a , ' gcx , ' tcx > {
192
195
// HACK(eddyb) get rid of `def_path_str` and/or pass `Namespace` explicitly always
@@ -225,7 +228,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
225
228
substs : Option < & ' tcx Substs < ' tcx > > ,
226
229
ns : Namespace ,
227
230
projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
228
- ) -> P :: Path {
231
+ ) -> Result < P :: Path , P :: Error > {
229
232
debug ! ( "default_print_def_path: def_id={:?}, substs={:?}, ns={:?}" , def_id, substs, ns) ;
230
233
let key = self . tcx . def_key ( def_id) ;
231
234
debug ! ( "default_print_def_path: key={:?}" , key) ;
@@ -263,12 +266,12 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
263
266
parent_generics. has_self && parent_generics. parent_count == 0 ;
264
267
if let ( Some ( substs) , true ) = ( substs, parent_has_own_self) {
265
268
let trait_ref = ty:: TraitRef :: new ( parent_def_id, substs) ;
266
- self . path_qualified ( None , trait_ref. self_ty ( ) , Some ( trait_ref) , ns)
269
+ self . path_qualified ( None , trait_ref. self_ty ( ) , Some ( trait_ref) , ns) ?
267
270
} else {
268
- self . print_def_path ( parent_def_id, substs, ns, iter:: empty ( ) )
271
+ self . print_def_path ( parent_def_id, substs, ns, iter:: empty ( ) ) ?
269
272
}
270
273
} else {
271
- self . print_def_path ( parent_def_id, None , ns, iter:: empty ( ) )
274
+ self . print_def_path ( parent_def_id, None , ns, iter:: empty ( ) ) ?
272
275
} ;
273
276
let path = match key. disambiguated_data . data {
274
277
// Skip `::{{constructor}}` on tuple/unit structs.
@@ -278,7 +281,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
278
281
self . path_append (
279
282
path,
280
283
& key. disambiguated_data . data . as_interned_str ( ) . as_str ( ) ,
281
- )
284
+ ) ?
282
285
}
283
286
} ;
284
287
@@ -287,7 +290,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
287
290
let params = & generics. params [ has_own_self as usize ..] ;
288
291
self . path_generic_args ( path, params, substs, ns, projections)
289
292
} else {
290
- path
293
+ Ok ( path)
291
294
}
292
295
}
293
296
}
@@ -300,7 +303,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
300
303
ns : Namespace ,
301
304
self_ty : Ty < ' tcx > ,
302
305
impl_trait_ref : Option < ty:: TraitRef < ' tcx > > ,
303
- ) -> P :: Path {
306
+ ) -> Result < P :: Path , P :: Error > {
304
307
debug ! ( "default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}" ,
305
308
impl_def_id, self_ty, impl_trait_ref) ;
306
309
@@ -323,7 +326,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
323
326
// If the impl is not co-located with either self-type or
324
327
// trait-type, then fallback to a format that identifies
325
328
// the module more clearly.
326
- Some ( self . print_def_path ( parent_def_id, None , ns, iter:: empty ( ) ) )
329
+ Some ( self . print_def_path ( parent_def_id, None , ns, iter:: empty ( ) ) ? )
327
330
} else {
328
331
// Otherwise, try to give a good form that would be valid language
329
332
// syntax. Preferably using associated item notation.
@@ -390,7 +393,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
390
393
/// If possible, this returns a global path resolving to `def_id` that is visible
391
394
/// from at least one local module and returns true. If the crate defining `def_id` is
392
395
/// declared with an `extern crate`, the path is guaranteed to use the `extern crate`.
393
- fn try_print_visible_def_path ( & mut self , def_id : DefId ) -> Option < P :: Path > {
396
+ fn try_print_visible_def_path ( & mut self , def_id : DefId ) -> Result < Option < P :: Path > , P :: Error > {
394
397
debug ! ( "try_print_visible_def_path: def_id={:?}" , def_id) ;
395
398
396
399
// If `def_id` is a direct or injected extern crate, return the
@@ -399,7 +402,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
399
402
let cnum = def_id. krate ;
400
403
401
404
if cnum == LOCAL_CRATE {
402
- return Some ( self . path_crate ( cnum) ) ;
405
+ return Ok ( Some ( self . path_crate ( cnum) ? ) ) ;
403
406
}
404
407
405
408
// In local mode, when we encounter a crate other than
@@ -421,21 +424,21 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
421
424
} ) => {
422
425
debug ! ( "try_print_visible_def_path: def_id={:?}" , def_id) ;
423
426
let path = if !span. is_dummy ( ) {
424
- self . print_def_path ( def_id, None , Namespace :: TypeNS , iter:: empty ( ) )
427
+ self . print_def_path ( def_id, None , Namespace :: TypeNS , iter:: empty ( ) ) ?
425
428
} else {
426
- self . path_crate ( cnum)
429
+ self . path_crate ( cnum) ?
427
430
} ;
428
- return Some ( path) ;
431
+ return Ok ( Some ( path) ) ;
429
432
}
430
433
None => {
431
- return Some ( self . path_crate ( cnum) ) ;
434
+ return Ok ( Some ( self . path_crate ( cnum) ? ) ) ;
432
435
}
433
436
_ => { } ,
434
437
}
435
438
}
436
439
437
440
if def_id. is_local ( ) {
438
- return None ;
441
+ return Ok ( None ) ;
439
442
}
440
443
441
444
let visible_parent_map = self . tcx . visible_parent_map ( LOCAL_CRATE ) ;
@@ -453,8 +456,14 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
453
456
cur_def_key = self . tcx . def_key ( parent) ;
454
457
}
455
458
456
- let visible_parent = visible_parent_map. get ( & def_id) . cloned ( ) ?;
457
- let path = self . try_print_visible_def_path ( visible_parent) ?;
459
+ let visible_parent = match visible_parent_map. get ( & def_id) . cloned ( ) {
460
+ Some ( parent) => parent,
461
+ None => return Ok ( None ) ,
462
+ } ;
463
+ let path = match self . try_print_visible_def_path ( visible_parent) ? {
464
+ Some ( path) => path,
465
+ None => return Ok ( None ) ,
466
+ } ;
458
467
let actual_parent = self . tcx . parent ( def_id) ;
459
468
460
469
let data = cur_def_key. disambiguated_data . data ;
@@ -518,7 +527,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
518
527
} ,
519
528
} ;
520
529
debug ! ( "try_print_visible_def_path: symbol={:?}" , symbol) ;
521
- Some ( self . path_append ( path, & symbol) )
530
+ Ok ( Some ( self . path_append ( path, & symbol) ? ) )
522
531
}
523
532
524
533
pub fn pretty_path_qualified (
@@ -527,7 +536,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
527
536
self_ty : Ty < ' tcx > ,
528
537
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
529
538
ns : Namespace ,
530
- ) -> P :: Path {
539
+ ) -> Result < P :: Path , P :: Error > {
531
540
if let Some ( prefix) = impl_prefix {
532
541
// HACK(eddyb) going through `path_append` means symbol name
533
542
// computation gets to handle its equivalent of `::` correctly.
@@ -585,9 +594,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
585
594
substs : & ' tcx Substs < ' tcx > ,
586
595
ns : Namespace ,
587
596
projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
588
- ) -> P :: Path {
589
- let path = path?;
590
-
597
+ ) -> Result < P :: Path , P :: Error > {
591
598
let mut empty = true ;
592
599
let mut start_or_continue = |cx : & mut Self , start : & str , cont : & str | {
593
600
if empty {
@@ -669,28 +676,30 @@ impl<F: fmt::Write> fmt::Write for FmtPrinter<F> {
669
676
}
670
677
671
678
impl < F : fmt:: Write > Printer for FmtPrinter < F > {
672
- type Path = Result < PrettyPath , fmt:: Error > ;
679
+ type Error = fmt:: Error ;
680
+
681
+ type Path = PrettyPath ;
673
682
674
683
fn print_def_path (
675
684
self : & mut PrintCx < ' _ , ' _ , ' tcx , Self > ,
676
685
def_id : DefId ,
677
686
substs : Option < & ' tcx Substs < ' tcx > > ,
678
687
ns : Namespace ,
679
688
projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
680
- ) -> Self :: Path {
689
+ ) -> Result < Self :: Path , Self :: Error > {
681
690
// FIXME(eddyb) avoid querying `tcx.generics_of` and `tcx.def_key`
682
691
// both here and in `default_print_def_path`.
683
692
let generics = substs. map ( |_| self . tcx . generics_of ( def_id) ) ;
684
693
if generics. as_ref ( ) . and_then ( |g| g. parent ) . is_none ( ) {
685
- if let Some ( path) = self . try_print_visible_def_path ( def_id) {
694
+ if let Some ( path) = self . try_print_visible_def_path ( def_id) ? {
686
695
let path = if let ( Some ( generics) , Some ( substs) ) = ( generics, substs) {
687
696
let has_own_self = generics. has_self && generics. parent_count == 0 ;
688
697
let params = & generics. params [ has_own_self as usize ..] ;
689
- self . path_generic_args ( path, params, substs, ns, projections)
698
+ self . path_generic_args ( path, params, substs, ns, projections) ?
690
699
} else {
691
700
path
692
701
} ;
693
- return path;
702
+ return Ok ( path) ;
694
703
}
695
704
}
696
705
@@ -710,7 +719,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
710
719
// pretty printing some span information. This should
711
720
// only occur very early in the compiler pipeline.
712
721
let parent_def_id = DefId { index : key. parent . unwrap ( ) , ..def_id } ;
713
- let path = self . print_def_path ( parent_def_id, None , ns, iter:: empty ( ) ) ;
722
+ let path = self . print_def_path ( parent_def_id, None , ns, iter:: empty ( ) ) ? ;
714
723
let span = self . tcx . def_span ( def_id) ;
715
724
return self . path_append ( path, & format ! ( "<impl at {:?}>" , span) ) ;
716
725
}
@@ -719,7 +728,10 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
719
728
self . default_print_def_path ( def_id, substs, ns, projections)
720
729
}
721
730
722
- fn path_crate ( self : & mut PrintCx < ' _ , ' _ , ' _ , Self > , cnum : CrateNum ) -> Self :: Path {
731
+ fn path_crate (
732
+ self : & mut PrintCx < ' _ , ' _ , ' _ , Self > ,
733
+ cnum : CrateNum ,
734
+ ) -> Result < Self :: Path , Self :: Error > {
723
735
if cnum == LOCAL_CRATE {
724
736
if self . tcx . sess . rust_2018 ( ) {
725
737
// We add the `crate::` keyword on Rust 2018, only when desired.
@@ -740,16 +752,14 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
740
752
self_ty : Ty < ' tcx > ,
741
753
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
742
754
ns : Namespace ,
743
- ) -> Self :: Path {
755
+ ) -> Result < Self :: Path , Self :: Error > {
744
756
self . pretty_path_qualified ( impl_prefix, self_ty, trait_ref, ns)
745
757
}
746
758
fn path_append (
747
759
self : & mut PrintCx < ' _ , ' _ , ' _ , Self > ,
748
760
path : Self :: Path ,
749
761
text : & str ,
750
- ) -> Self :: Path {
751
- let path = path?;
752
-
762
+ ) -> Result < Self :: Path , Self :: Error > {
753
763
// FIXME(eddyb) this shouldn't happen, but is currently
754
764
// the case for `extern { ... }` "foreign modules".
755
765
if text. is_empty ( ) {
@@ -769,7 +779,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
769
779
substs : & ' tcx Substs < ' tcx > ,
770
780
ns : Namespace ,
771
781
projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
772
- ) -> Self :: Path {
782
+ ) -> Result < Self :: Path , Self :: Error > {
773
783
self . pretty_path_generic_args ( path, params, substs, ns, projections)
774
784
}
775
785
}
0 commit comments