@@ -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 < SubstsRef < ' 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 < SubstsRef < ' 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 : SubstsRef < ' 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
@@ -225,7 +228,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
225
228
substs : Option < SubstsRef < ' 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 ;
@@ -515,7 +524,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
515
524
} ,
516
525
} ;
517
526
debug ! ( "try_print_visible_def_path: symbol={:?}" , symbol) ;
518
- Some ( self . path_append ( path, & symbol) )
527
+ Ok ( Some ( self . path_append ( path, & symbol) ? ) )
519
528
}
520
529
521
530
pub fn pretty_path_qualified (
@@ -524,7 +533,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
524
533
self_ty : Ty < ' tcx > ,
525
534
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
526
535
ns : Namespace ,
527
- ) -> P :: Path {
536
+ ) -> Result < P :: Path , P :: Error > {
528
537
if let Some ( prefix) = impl_prefix {
529
538
// HACK(eddyb) going through `path_append` means symbol name
530
539
// computation gets to handle its equivalent of `::` correctly.
@@ -582,9 +591,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
582
591
substs : SubstsRef < ' tcx > ,
583
592
ns : Namespace ,
584
593
projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
585
- ) -> P :: Path {
586
- let path = path?;
587
-
594
+ ) -> Result < P :: Path , P :: Error > {
588
595
let mut empty = true ;
589
596
let mut start_or_continue = |cx : & mut Self , start : & str , cont : & str | {
590
597
if empty {
@@ -671,28 +678,30 @@ impl<F: fmt::Write> fmt::Write for FmtPrinter<F> {
671
678
}
672
679
673
680
impl < F : fmt:: Write > Printer for FmtPrinter < F > {
674
- type Path = Result < PrettyPath , fmt:: Error > ;
681
+ type Error = fmt:: Error ;
682
+
683
+ type Path = PrettyPath ;
675
684
676
685
fn print_def_path (
677
686
self : & mut PrintCx < ' _ , ' _ , ' tcx , Self > ,
678
687
def_id : DefId ,
679
688
substs : Option < SubstsRef < ' tcx > > ,
680
689
ns : Namespace ,
681
690
projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
682
- ) -> Self :: Path {
691
+ ) -> Result < Self :: Path , Self :: Error > {
683
692
// FIXME(eddyb) avoid querying `tcx.generics_of` and `tcx.def_key`
684
693
// both here and in `default_print_def_path`.
685
694
let generics = substs. map ( |_| self . tcx . generics_of ( def_id) ) ;
686
695
if generics. as_ref ( ) . and_then ( |g| g. parent ) . is_none ( ) {
687
- if let Some ( path) = self . try_print_visible_def_path ( def_id) {
696
+ if let Some ( path) = self . try_print_visible_def_path ( def_id) ? {
688
697
let path = if let ( Some ( generics) , Some ( substs) ) = ( generics, substs) {
689
698
let has_own_self = generics. has_self && generics. parent_count == 0 ;
690
699
let params = & generics. params [ has_own_self as usize ..] ;
691
- self . path_generic_args ( path, params, substs, ns, projections)
700
+ self . path_generic_args ( path, params, substs, ns, projections) ?
692
701
} else {
693
702
path
694
703
} ;
695
- return path;
704
+ return Ok ( path) ;
696
705
}
697
706
}
698
707
@@ -712,7 +721,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
712
721
// pretty printing some span information. This should
713
722
// only occur very early in the compiler pipeline.
714
723
let parent_def_id = DefId { index : key. parent . unwrap ( ) , ..def_id } ;
715
- let path = self . print_def_path ( parent_def_id, None , ns, iter:: empty ( ) ) ;
724
+ let path = self . print_def_path ( parent_def_id, None , ns, iter:: empty ( ) ) ? ;
716
725
let span = self . tcx . def_span ( def_id) ;
717
726
return self . path_append ( path, & format ! ( "<impl at {:?}>" , span) ) ;
718
727
}
@@ -721,7 +730,10 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
721
730
self . default_print_def_path ( def_id, substs, ns, projections)
722
731
}
723
732
724
- fn path_crate ( self : & mut PrintCx < ' _ , ' _ , ' _ , Self > , cnum : CrateNum ) -> Self :: Path {
733
+ fn path_crate (
734
+ self : & mut PrintCx < ' _ , ' _ , ' _ , Self > ,
735
+ cnum : CrateNum ,
736
+ ) -> Result < Self :: Path , Self :: Error > {
725
737
if cnum == LOCAL_CRATE {
726
738
if self . tcx . sess . rust_2018 ( ) {
727
739
// We add the `crate::` keyword on Rust 2018, only when desired.
@@ -742,16 +754,14 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
742
754
self_ty : Ty < ' tcx > ,
743
755
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
744
756
ns : Namespace ,
745
- ) -> Self :: Path {
757
+ ) -> Result < Self :: Path , Self :: Error > {
746
758
self . pretty_path_qualified ( impl_prefix, self_ty, trait_ref, ns)
747
759
}
748
760
fn path_append (
749
761
self : & mut PrintCx < ' _ , ' _ , ' _ , Self > ,
750
762
path : Self :: Path ,
751
763
text : & str ,
752
- ) -> Self :: Path {
753
- let path = path?;
754
-
764
+ ) -> Result < Self :: Path , Self :: Error > {
755
765
// FIXME(eddyb) this shouldn't happen, but is currently
756
766
// the case for `extern { ... }` "foreign modules".
757
767
if text. is_empty ( ) {
@@ -771,7 +781,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
771
781
substs : SubstsRef < ' tcx > ,
772
782
ns : Namespace ,
773
783
projections : impl Iterator < Item = ty:: ExistentialProjection < ' tcx > > ,
774
- ) -> Self :: Path {
784
+ ) -> Result < Self :: Path , Self :: Error > {
775
785
self . pretty_path_generic_args ( path, params, substs, ns, projections)
776
786
}
777
787
}
0 commit comments