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