Skip to content

Commit c938fc4

Browse files
committed
rustc: uniformize ty::print's error handling by requiring Result.
1 parent 14c88de commit c938fc4

File tree

6 files changed

+114
-85
lines changed

6 files changed

+114
-85
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
455455
struct NonTrivialPath;
456456

457457
impl Printer for AbsolutePathPrinter {
458-
type Path = Result<Vec<String>, NonTrivialPath>;
458+
type Error = NonTrivialPath;
459459

460-
fn path_crate(self: &mut PrintCx<'_, '_, '_, Self>, cnum: CrateNum) -> Self::Path {
460+
type Path = Vec<String>;
461+
462+
fn path_crate(
463+
self: &mut PrintCx<'_, '_, '_, Self>,
464+
cnum: CrateNum,
465+
) -> Result<Self::Path, Self::Error> {
461466
Ok(vec![self.tcx.original_crate_name(cnum).to_string()])
462467
}
463468
fn path_qualified<'tcx>(
@@ -466,15 +471,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
466471
_self_ty: Ty<'tcx>,
467472
_trait_ref: Option<ty::TraitRef<'tcx>>,
468473
_ns: Namespace,
469-
) -> Self::Path {
474+
) -> Result<Self::Path, Self::Error> {
470475
Err(NonTrivialPath)
471476
}
472477
fn path_append(
473478
self: &mut PrintCx<'_, '_, '_, Self>,
474-
path: Self::Path,
479+
mut path: Self::Path,
475480
text: &str,
476-
) -> Self::Path {
477-
let mut path = path?;
481+
) -> Result<Self::Path, Self::Error> {
478482
path.push(text.to_string());
479483
Ok(path)
480484
}
@@ -485,8 +489,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
485489
_substs: &'tcx Substs<'tcx>,
486490
_ns: Namespace,
487491
_projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
488-
) -> Self::Path {
489-
path
492+
) -> Result<Self::Path, Self::Error> {
493+
Ok(path)
490494
}
491495
}
492496

src/librustc/ty/print.rs

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,20 @@ impl<P> PrintCx<'a, 'gcx, 'tcx, P> {
109109

110110
pub trait Print<'tcx, P> {
111111
type Output;
112+
type Error;
112113

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> {
115119
let old_debug = cx.is_debug;
116120
cx.is_debug = false;
117121
let result = self.print(cx);
118122
cx.is_debug = old_debug;
119123
result
120124
}
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> {
122126
let old_debug = cx.is_debug;
123127
cx.is_debug = true;
124128
let result = self.print(cx);
@@ -128,55 +132,54 @@ pub trait Print<'tcx, P> {
128132
}
129133

130134
pub trait Printer: Sized {
135+
type Error;
136+
131137
type Path;
132138

133-
#[must_use]
134139
fn print_def_path(
135140
self: &mut PrintCx<'_, '_, 'tcx, Self>,
136141
def_id: DefId,
137142
substs: Option<&'tcx Substs<'tcx>>,
138143
ns: Namespace,
139144
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
140-
) -> Self::Path {
145+
) -> Result<Self::Path, Self::Error> {
141146
self.default_print_def_path(def_id, substs, ns, projections)
142147
}
143-
#[must_use]
144148
fn print_impl_path(
145149
self: &mut PrintCx<'_, '_, 'tcx, Self>,
146150
impl_def_id: DefId,
147151
substs: Option<&'tcx Substs<'tcx>>,
148152
ns: Namespace,
149153
self_ty: Ty<'tcx>,
150154
trait_ref: Option<ty::TraitRef<'tcx>>,
151-
) -> Self::Path {
155+
) -> Result<Self::Path, Self::Error> {
152156
self.default_print_impl_path(impl_def_id, substs, ns, self_ty, trait_ref)
153157
}
154158

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>;
158163
fn path_qualified(
159164
self: &mut PrintCx<'_, '_, 'tcx, Self>,
160165
impl_prefix: Option<Self::Path>,
161166
self_ty: Ty<'tcx>,
162167
trait_ref: Option<ty::TraitRef<'tcx>>,
163168
ns: Namespace,
164-
) -> Self::Path;
165-
#[must_use]
169+
) -> Result<Self::Path, Self::Error>;
166170
fn path_append(
167171
self: &mut PrintCx<'_, '_, '_, Self>,
168172
path: Self::Path,
169173
text: &str,
170-
) -> Self::Path;
171-
#[must_use]
174+
) -> Result<Self::Path, Self::Error>;
172175
fn path_generic_args(
173176
self: &mut PrintCx<'_, '_, 'tcx, Self>,
174177
path: Self::Path,
175178
params: &[ty::GenericParamDef],
176179
substs: &'tcx Substs<'tcx>,
177180
ns: Namespace,
178181
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
179-
) -> Self::Path;
182+
) -> Result<Self::Path, Self::Error>;
180183
}
181184

182185
#[must_use]
@@ -185,7 +188,7 @@ pub struct PrettyPath {
185188
}
186189

187190
/// 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 {}
189192

190193
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
191194
// 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> {
224227
substs: Option<&'tcx Substs<'tcx>>,
225228
ns: Namespace,
226229
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
227-
) -> P::Path {
230+
) -> Result<P::Path, P::Error> {
228231
debug!("default_print_def_path: def_id={:?}, substs={:?}, ns={:?}", def_id, substs, ns);
229232
let key = self.tcx.def_key(def_id);
230233
debug!("default_print_def_path: key={:?}", key);
@@ -262,12 +265,12 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
262265
parent_generics.has_self && parent_generics.parent_count == 0;
263266
if let (Some(substs), true) = (substs, parent_has_own_self) {
264267
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)?
266269
} 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())?
268271
}
269272
} 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())?
271274
};
272275
let path = match key.disambiguated_data.data {
273276
// Skip `::{{constructor}}` on tuple/unit structs.
@@ -277,7 +280,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
277280
self.path_append(
278281
path,
279282
&key.disambiguated_data.data.as_interned_str().as_str(),
280-
)
283+
)?
281284
}
282285
};
283286

@@ -286,7 +289,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
286289
let params = &generics.params[has_own_self as usize..];
287290
self.path_generic_args(path, params, substs, ns, projections)
288291
} else {
289-
path
292+
Ok(path)
290293
}
291294
}
292295
}
@@ -299,7 +302,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
299302
ns: Namespace,
300303
self_ty: Ty<'tcx>,
301304
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
302-
) -> P::Path {
305+
) -> Result<P::Path, P::Error> {
303306
debug!("default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}",
304307
impl_def_id, self_ty, impl_trait_ref);
305308

@@ -322,7 +325,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
322325
// If the impl is not co-located with either self-type or
323326
// trait-type, then fallback to a format that identifies
324327
// 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())?)
326329
} else {
327330
// Otherwise, try to give a good form that would be valid language
328331
// syntax. Preferably using associated item notation.
@@ -389,7 +392,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
389392
/// If possible, this returns a global path resolving to `def_id` that is visible
390393
/// from at least one local module and returns true. If the crate defining `def_id` is
391394
/// 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> {
393396
debug!("try_print_visible_def_path: def_id={:?}", def_id);
394397

395398
// If `def_id` is a direct or injected extern crate, return the
@@ -398,7 +401,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
398401
let cnum = def_id.krate;
399402

400403
if cnum == LOCAL_CRATE {
401-
return Some(self.path_crate(cnum));
404+
return Ok(Some(self.path_crate(cnum)?));
402405
}
403406

404407
// In local mode, when we encounter a crate other than
@@ -420,21 +423,21 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
420423
}) => {
421424
debug!("try_print_visible_def_path: def_id={:?}", def_id);
422425
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())?
424427
} else {
425-
self.path_crate(cnum)
428+
self.path_crate(cnum)?
426429
};
427-
return Some(path);
430+
return Ok(Some(path));
428431
}
429432
None => {
430-
return Some(self.path_crate(cnum));
433+
return Ok(Some(self.path_crate(cnum)?));
431434
}
432435
_ => {},
433436
}
434437
}
435438

436439
if def_id.is_local() {
437-
return None;
440+
return Ok(None);
438441
}
439442

440443
let visible_parent_map = self.tcx.visible_parent_map(LOCAL_CRATE);
@@ -452,8 +455,14 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
452455
cur_def_key = self.tcx.def_key(parent);
453456
}
454457

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+
};
457466
let actual_parent = self.tcx.parent(def_id);
458467

459468
let data = cur_def_key.disambiguated_data.data;
@@ -514,7 +523,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
514523
},
515524
};
516525
debug!("try_print_visible_def_path: symbol={:?}", symbol);
517-
Some(self.path_append(path, &symbol))
526+
Ok(Some(self.path_append(path, &symbol)?))
518527
}
519528

520529
pub fn pretty_path_qualified(
@@ -523,7 +532,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
523532
self_ty: Ty<'tcx>,
524533
trait_ref: Option<ty::TraitRef<'tcx>>,
525534
ns: Namespace,
526-
) -> P::Path {
535+
) -> Result<P::Path, P::Error> {
527536
if let Some(prefix) = impl_prefix {
528537
// HACK(eddyb) going through `path_append` means symbol name
529538
// computation gets to handle its equivalent of `::` correctly.
@@ -581,9 +590,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
581590
substs: &'tcx Substs<'tcx>,
582591
ns: Namespace,
583592
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
584-
) -> P::Path {
585-
let path = path?;
586-
593+
) -> Result<P::Path, P::Error> {
587594
let mut empty = true;
588595
let mut start_or_continue = |cx: &mut Self, start: &str, cont: &str| {
589596
if empty {
@@ -665,28 +672,30 @@ impl<F: fmt::Write> fmt::Write for FmtPrinter<F> {
665672
}
666673

667674
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;
669678

670679
fn print_def_path(
671680
self: &mut PrintCx<'_, '_, 'tcx, Self>,
672681
def_id: DefId,
673682
substs: Option<&'tcx Substs<'tcx>>,
674683
ns: Namespace,
675684
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
676-
) -> Self::Path {
685+
) -> Result<Self::Path, Self::Error> {
677686
// FIXME(eddyb) avoid querying `tcx.generics_of` and `tcx.def_key`
678687
// both here and in `default_print_def_path`.
679688
let generics = substs.map(|_| self.tcx.generics_of(def_id));
680689
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)? {
682691
let path = if let (Some(generics), Some(substs)) = (generics, substs) {
683692
let has_own_self = generics.has_self && generics.parent_count == 0;
684693
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)?
686695
} else {
687696
path
688697
};
689-
return path;
698+
return Ok(path);
690699
}
691700
}
692701

@@ -706,7 +715,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
706715
// pretty printing some span information. This should
707716
// only occur very early in the compiler pipeline.
708717
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())?;
710719
let span = self.tcx.def_span(def_id);
711720
return self.path_append(path, &format!("<impl at {:?}>", span));
712721
}
@@ -715,7 +724,10 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
715724
self.default_print_def_path(def_id, substs, ns, projections)
716725
}
717726

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> {
719731
if cnum == LOCAL_CRATE {
720732
if self.tcx.sess.rust_2018() {
721733
// We add the `crate::` keyword on Rust 2018, only when desired.
@@ -736,16 +748,14 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
736748
self_ty: Ty<'tcx>,
737749
trait_ref: Option<ty::TraitRef<'tcx>>,
738750
ns: Namespace,
739-
) -> Self::Path {
751+
) -> Result<Self::Path, Self::Error> {
740752
self.pretty_path_qualified(impl_prefix, self_ty, trait_ref, ns)
741753
}
742754
fn path_append(
743755
self: &mut PrintCx<'_, '_, '_, Self>,
744756
path: Self::Path,
745757
text: &str,
746-
) -> Self::Path {
747-
let path = path?;
748-
758+
) -> Result<Self::Path, Self::Error> {
749759
// FIXME(eddyb) this shouldn't happen, but is currently
750760
// the case for `extern { ... }` "foreign modules".
751761
if text.is_empty() {
@@ -765,7 +775,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
765775
substs: &'tcx Substs<'tcx>,
766776
ns: Namespace,
767777
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
768-
) -> Self::Path {
778+
) -> Result<Self::Path, Self::Error> {
769779
self.pretty_path_generic_args(path, params, substs, ns, projections)
770780
}
771781
}

0 commit comments

Comments
 (0)