Skip to content

Commit 6930d3e

Browse files
committed
rustc: uniformize ty::print's error handling by requiring Result.
1 parent e3bce90 commit 6930d3e

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
@@ -110,16 +110,20 @@ impl<P> PrintCx<'a, 'gcx, 'tcx, P> {
110110

111111
pub trait Print<'tcx, P> {
112112
type Output;
113+
type Error;
113114

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

131135
pub trait Printer: Sized {
136+
type Error;
137+
132138
type Path;
133139

134-
#[must_use]
135140
fn print_def_path(
136141
self: &mut PrintCx<'_, '_, 'tcx, Self>,
137142
def_id: DefId,
138143
substs: Option<&'tcx Substs<'tcx>>,
139144
ns: Namespace,
140145
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
141-
) -> Self::Path {
146+
) -> Result<Self::Path, Self::Error> {
142147
self.default_print_def_path(def_id, substs, ns, projections)
143148
}
144-
#[must_use]
145149
fn print_impl_path(
146150
self: &mut PrintCx<'_, '_, 'tcx, Self>,
147151
impl_def_id: DefId,
148152
substs: Option<&'tcx Substs<'tcx>>,
149153
ns: Namespace,
150154
self_ty: Ty<'tcx>,
151155
trait_ref: Option<ty::TraitRef<'tcx>>,
152-
) -> Self::Path {
156+
) -> Result<Self::Path, Self::Error> {
153157
self.default_print_impl_path(impl_def_id, substs, ns, self_ty, trait_ref)
154158
}
155159

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

183186
#[must_use]
@@ -186,7 +189,7 @@ pub struct PrettyPath {
186189
}
187190

188191
/// 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 {}
190193

191194
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
192195
// 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<&'tcx Substs<'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;
@@ -518,7 +527,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
518527
},
519528
};
520529
debug!("try_print_visible_def_path: symbol={:?}", symbol);
521-
Some(self.path_append(path, &symbol))
530+
Ok(Some(self.path_append(path, &symbol)?))
522531
}
523532

524533
pub fn pretty_path_qualified(
@@ -527,7 +536,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
527536
self_ty: Ty<'tcx>,
528537
trait_ref: Option<ty::TraitRef<'tcx>>,
529538
ns: Namespace,
530-
) -> P::Path {
539+
) -> Result<P::Path, P::Error> {
531540
if let Some(prefix) = impl_prefix {
532541
// HACK(eddyb) going through `path_append` means symbol name
533542
// computation gets to handle its equivalent of `::` correctly.
@@ -585,9 +594,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
585594
substs: &'tcx Substs<'tcx>,
586595
ns: Namespace,
587596
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
588-
) -> P::Path {
589-
let path = path?;
590-
597+
) -> Result<P::Path, P::Error> {
591598
let mut empty = true;
592599
let mut start_or_continue = |cx: &mut Self, start: &str, cont: &str| {
593600
if empty {
@@ -669,28 +676,30 @@ impl<F: fmt::Write> fmt::Write for FmtPrinter<F> {
669676
}
670677

671678
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;
673682

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

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

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> {
723735
if cnum == LOCAL_CRATE {
724736
if self.tcx.sess.rust_2018() {
725737
// We add the `crate::` keyword on Rust 2018, only when desired.
@@ -740,16 +752,14 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
740752
self_ty: Ty<'tcx>,
741753
trait_ref: Option<ty::TraitRef<'tcx>>,
742754
ns: Namespace,
743-
) -> Self::Path {
755+
) -> Result<Self::Path, Self::Error> {
744756
self.pretty_path_qualified(impl_prefix, self_ty, trait_ref, ns)
745757
}
746758
fn path_append(
747759
self: &mut PrintCx<'_, '_, '_, Self>,
748760
path: Self::Path,
749761
text: &str,
750-
) -> Self::Path {
751-
let path = path?;
752-
762+
) -> Result<Self::Path, Self::Error> {
753763
// FIXME(eddyb) this shouldn't happen, but is currently
754764
// the case for `extern { ... }` "foreign modules".
755765
if text.is_empty() {
@@ -769,7 +779,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
769779
substs: &'tcx Substs<'tcx>,
770780
ns: Namespace,
771781
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
772-
) -> Self::Path {
782+
) -> Result<Self::Path, Self::Error> {
773783
self.pretty_path_generic_args(path, params, substs, ns, projections)
774784
}
775785
}

0 commit comments

Comments
 (0)