Skip to content

Commit 5616ca8

Browse files
committed
rustc: uniformize ty::print's error handling by requiring Result.
1 parent 972af5e commit 5616ca8

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
@@ -454,9 +454,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
454454
struct NonTrivialPath;
455455

456456
impl Printer for AbsolutePathPrinter {
457-
type Path = Result<Vec<String>, NonTrivialPath>;
457+
type Error = NonTrivialPath;
458458

459-
fn path_crate(self: &mut PrintCx<'_, '_, '_, Self>, cnum: CrateNum) -> Self::Path {
459+
type Path = Vec<String>;
460+
461+
fn path_crate(
462+
self: &mut PrintCx<'_, '_, '_, Self>,
463+
cnum: CrateNum,
464+
) -> Result<Self::Path, Self::Error> {
460465
Ok(vec![self.tcx.original_crate_name(cnum).to_string()])
461466
}
462467
fn path_qualified<'tcx>(
@@ -465,15 +470,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
465470
_self_ty: Ty<'tcx>,
466471
_trait_ref: Option<ty::TraitRef<'tcx>>,
467472
_ns: Namespace,
468-
) -> Self::Path {
473+
) -> Result<Self::Path, Self::Error> {
469474
Err(NonTrivialPath)
470475
}
471476
fn path_append(
472477
self: &mut PrintCx<'_, '_, '_, Self>,
473-
path: Self::Path,
478+
mut path: Self::Path,
474479
text: &str,
475-
) -> Self::Path {
476-
let mut path = path?;
480+
) -> Result<Self::Path, Self::Error> {
477481
path.push(text.to_string());
478482
Ok(path)
479483
}
@@ -484,8 +488,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
484488
_substs: SubstsRef<'tcx>,
485489
_ns: Namespace,
486490
_projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
487-
) -> Self::Path {
488-
path
491+
) -> Result<Self::Path, Self::Error> {
492+
Ok(path)
489493
}
490494
}
491495

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<SubstsRef<'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<SubstsRef<'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: SubstsRef<'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
@@ -225,7 +228,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
225228
substs: Option<SubstsRef<'tcx>>,
226229
ns: Namespace,
227230
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
228-
) -> P::Path {
231+
) -> Result<P::Path, P::Error> {
229232
debug!("default_print_def_path: def_id={:?}, substs={:?}, ns={:?}", def_id, substs, ns);
230233
let key = self.tcx.def_key(def_id);
231234
debug!("default_print_def_path: key={:?}", key);
@@ -263,12 +266,12 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
263266
parent_generics.has_self && parent_generics.parent_count == 0;
264267
if let (Some(substs), true) = (substs, parent_has_own_self) {
265268
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)?
267270
} 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())?
269272
}
270273
} 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())?
272275
};
273276
let path = match key.disambiguated_data.data {
274277
// Skip `::{{constructor}}` on tuple/unit structs.
@@ -278,7 +281,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
278281
self.path_append(
279282
path,
280283
&key.disambiguated_data.data.as_interned_str().as_str(),
281-
)
284+
)?
282285
}
283286
};
284287

@@ -287,7 +290,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
287290
let params = &generics.params[has_own_self as usize..];
288291
self.path_generic_args(path, params, substs, ns, projections)
289292
} else {
290-
path
293+
Ok(path)
291294
}
292295
}
293296
}
@@ -300,7 +303,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
300303
ns: Namespace,
301304
self_ty: Ty<'tcx>,
302305
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
303-
) -> P::Path {
306+
) -> Result<P::Path, P::Error> {
304307
debug!("default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}",
305308
impl_def_id, self_ty, impl_trait_ref);
306309

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

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

401404
if cnum == LOCAL_CRATE {
402-
return Some(self.path_crate(cnum));
405+
return Ok(Some(self.path_crate(cnum)?));
403406
}
404407

405408
// In local mode, when we encounter a crate other than
@@ -421,21 +424,21 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
421424
}) => {
422425
debug!("try_print_visible_def_path: def_id={:?}", def_id);
423426
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())?
425428
} else {
426-
self.path_crate(cnum)
429+
self.path_crate(cnum)?
427430
};
428-
return Some(path);
431+
return Ok(Some(path));
429432
}
430433
None => {
431-
return Some(self.path_crate(cnum));
434+
return Ok(Some(self.path_crate(cnum)?));
432435
}
433436
_ => {},
434437
}
435438
}
436439

437440
if def_id.is_local() {
438-
return None;
441+
return Ok(None);
439442
}
440443

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

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

460469
let data = cur_def_key.disambiguated_data.data;
@@ -515,7 +524,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
515524
},
516525
};
517526
debug!("try_print_visible_def_path: symbol={:?}", symbol);
518-
Some(self.path_append(path, &symbol))
527+
Ok(Some(self.path_append(path, &symbol)?))
519528
}
520529

521530
pub fn pretty_path_qualified(
@@ -524,7 +533,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
524533
self_ty: Ty<'tcx>,
525534
trait_ref: Option<ty::TraitRef<'tcx>>,
526535
ns: Namespace,
527-
) -> P::Path {
536+
) -> Result<P::Path, P::Error> {
528537
if let Some(prefix) = impl_prefix {
529538
// HACK(eddyb) going through `path_append` means symbol name
530539
// computation gets to handle its equivalent of `::` correctly.
@@ -582,9 +591,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
582591
substs: SubstsRef<'tcx>,
583592
ns: Namespace,
584593
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
585-
) -> P::Path {
586-
let path = path?;
587-
594+
) -> Result<P::Path, P::Error> {
588595
let mut empty = true;
589596
let mut start_or_continue = |cx: &mut Self, start: &str, cont: &str| {
590597
if empty {
@@ -671,28 +678,30 @@ impl<F: fmt::Write> fmt::Write for FmtPrinter<F> {
671678
}
672679

673680
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;
675684

676685
fn print_def_path(
677686
self: &mut PrintCx<'_, '_, 'tcx, Self>,
678687
def_id: DefId,
679688
substs: Option<SubstsRef<'tcx>>,
680689
ns: Namespace,
681690
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
682-
) -> Self::Path {
691+
) -> Result<Self::Path, Self::Error> {
683692
// FIXME(eddyb) avoid querying `tcx.generics_of` and `tcx.def_key`
684693
// both here and in `default_print_def_path`.
685694
let generics = substs.map(|_| self.tcx.generics_of(def_id));
686695
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)? {
688697
let path = if let (Some(generics), Some(substs)) = (generics, substs) {
689698
let has_own_self = generics.has_self && generics.parent_count == 0;
690699
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)?
692701
} else {
693702
path
694703
};
695-
return path;
704+
return Ok(path);
696705
}
697706
}
698707

@@ -712,7 +721,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
712721
// pretty printing some span information. This should
713722
// only occur very early in the compiler pipeline.
714723
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())?;
716725
let span = self.tcx.def_span(def_id);
717726
return self.path_append(path, &format!("<impl at {:?}>", span));
718727
}
@@ -721,7 +730,10 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
721730
self.default_print_def_path(def_id, substs, ns, projections)
722731
}
723732

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> {
725737
if cnum == LOCAL_CRATE {
726738
if self.tcx.sess.rust_2018() {
727739
// We add the `crate::` keyword on Rust 2018, only when desired.
@@ -742,16 +754,14 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
742754
self_ty: Ty<'tcx>,
743755
trait_ref: Option<ty::TraitRef<'tcx>>,
744756
ns: Namespace,
745-
) -> Self::Path {
757+
) -> Result<Self::Path, Self::Error> {
746758
self.pretty_path_qualified(impl_prefix, self_ty, trait_ref, ns)
747759
}
748760
fn path_append(
749761
self: &mut PrintCx<'_, '_, '_, Self>,
750762
path: Self::Path,
751763
text: &str,
752-
) -> Self::Path {
753-
let path = path?;
754-
764+
) -> Result<Self::Path, Self::Error> {
755765
// FIXME(eddyb) this shouldn't happen, but is currently
756766
// the case for `extern { ... }` "foreign modules".
757767
if text.is_empty() {
@@ -771,7 +781,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
771781
substs: SubstsRef<'tcx>,
772782
ns: Namespace,
773783
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
774-
) -> Self::Path {
784+
) -> Result<Self::Path, Self::Error> {
775785
self.pretty_path_generic_args(path, params, substs, ns, projections)
776786
}
777787
}

0 commit comments

Comments
 (0)