rustc: move ty::print::PrintConfig's fields to FmtPrinter.
This commit is contained in:
parent
1a0f3a2856
commit
381fa7aa18
8 changed files with 185 additions and 190 deletions
|
@ -526,9 +526,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
|
|||
// module we could have false positives
|
||||
if !(did1.is_local() || did2.is_local()) && did1.krate != did2.krate {
|
||||
let abs_path = |def_id| {
|
||||
PrintCx::with(self.tcx, AbsolutePathPrinter, |cx| {
|
||||
cx.print_def_path(def_id, None, iter::empty())
|
||||
})
|
||||
PrintCx::new(self.tcx, AbsolutePathPrinter)
|
||||
.print_def_path(def_id, None, iter::empty())
|
||||
};
|
||||
|
||||
// We compare strings because DefPath can be different
|
||||
|
|
|
@ -84,9 +84,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
|
|||
if let Some(highlight) = highlight {
|
||||
printer.region_highlight_mode = highlight;
|
||||
}
|
||||
let _ = ty::print::PrintCx::with(self.tcx, printer, |cx| {
|
||||
ty.print(cx)
|
||||
});
|
||||
let _ = ty.print(ty::print::PrintCx::new(self.tcx, printer));
|
||||
s
|
||||
}
|
||||
|
||||
|
|
|
@ -347,10 +347,8 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
|
|||
let mut printer = ty::print::FmtPrinter::new(f, Namespace::TypeNS);
|
||||
printer.region_highlight_mode = self.highlight;
|
||||
|
||||
ty::print::PrintCx::with(self.tcx, printer, |cx| {
|
||||
self.value.print(cx)?;
|
||||
Ok(())
|
||||
})
|
||||
self.value.print(ty::print::PrintCx::new(self.tcx, printer))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,52 +4,50 @@ use crate::ty::{self, DefIdTree, Ty, TyCtxt};
|
|||
use crate::ty::subst::{Subst, SubstsRef};
|
||||
|
||||
use rustc_data_structures::fx::FxHashSet;
|
||||
use syntax::symbol::InternedString;
|
||||
|
||||
use std::iter;
|
||||
use std::ops::Deref;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
// `pretty` is a separate module only for organization.
|
||||
mod pretty;
|
||||
pub use self::pretty::*;
|
||||
|
||||
#[derive(Default)]
|
||||
struct PrintConfig {
|
||||
used_region_names: Option<FxHashSet<InternedString>>,
|
||||
region_index: usize,
|
||||
binder_depth: usize,
|
||||
}
|
||||
|
||||
pub struct PrintCx<'a, 'gcx, 'tcx, P> {
|
||||
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
||||
pub printer: P,
|
||||
config: &'a mut PrintConfig,
|
||||
inner: P,
|
||||
}
|
||||
|
||||
// HACK(eddyb) this is solely for `self: PrintCx<Self>`, e.g. to
|
||||
// implement traits on the printer and call the methods on the context.
|
||||
impl<P> Deref for PrintCx<'_, '_, '_, P> {
|
||||
type Target = P;
|
||||
fn deref(&self) -> &P {
|
||||
&self.printer
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> DerefMut for PrintCx<'_, '_, '_, P> {
|
||||
fn deref_mut(&mut self) -> &mut P {
|
||||
&mut self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'gcx, 'tcx, P> PrintCx<'a, 'gcx, 'tcx, P> {
|
||||
pub fn with<R>(
|
||||
tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
||||
printer: P,
|
||||
f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, P>) -> R,
|
||||
) -> R {
|
||||
f(PrintCx {
|
||||
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>, inner: P) -> Self {
|
||||
PrintCx {
|
||||
tcx,
|
||||
printer,
|
||||
config: &mut PrintConfig::default(),
|
||||
})
|
||||
inner,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_tls_tcx<R>(printer: P, f: impl FnOnce(PrintCx<'_, '_, '_, P>) -> R) -> R {
|
||||
ty::tls::with(|tcx| PrintCx::with(tcx, printer, f))
|
||||
pub fn with_tls_tcx<R>(inner: P, f: impl FnOnce(PrintCx<'_, '_, '_, P>) -> R) -> R {
|
||||
ty::tls::with(|tcx| f(PrintCx::new(tcx, inner)))
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> P {
|
||||
self.inner
|
||||
}
|
||||
|
||||
pub fn ok<E>(self) -> Result<P, E> {
|
||||
Ok(self.into_inner())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ use syntax::symbol::InternedString;
|
|||
use std::cell::Cell;
|
||||
use std::fmt::{self, Write as _};
|
||||
use std::iter;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
// `pretty` is a separate module only for organization.
|
||||
use super::*;
|
||||
|
@ -26,7 +27,7 @@ macro_rules! nest {
|
|||
}
|
||||
macro_rules! print_inner {
|
||||
(write ($($data:expr),+)) => {
|
||||
write!(scoped_cx!().printer, $($data),+)?
|
||||
write!(scoped_cx!(), $($data),+)?
|
||||
};
|
||||
($kind:ident ($data:expr)) => {
|
||||
nest!(|cx| $data.$kind(cx))
|
||||
|
@ -185,16 +186,7 @@ pub trait PrettyPrinter:
|
|||
self: PrintCx<'a, 'gcx, 'tcx, Self>,
|
||||
f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, Self>) -> Result<Self, E>,
|
||||
) -> Result<PrintCx<'a, 'gcx, 'tcx, Self>, E> {
|
||||
let printer = f(PrintCx {
|
||||
tcx: self.tcx,
|
||||
printer: self.printer,
|
||||
config: self.config,
|
||||
})?;
|
||||
Ok(PrintCx {
|
||||
tcx: self.tcx,
|
||||
printer,
|
||||
config: self.config,
|
||||
})
|
||||
Ok(PrintCx::new(self.tcx, f(self)?))
|
||||
}
|
||||
|
||||
/// Like `print_def_path` but for value paths.
|
||||
|
@ -243,6 +235,12 @@ pub trait PrettyPrinter:
|
|||
) -> bool;
|
||||
}
|
||||
|
||||
impl<P: PrettyPrinter> fmt::Write for PrintCx<'_, '_, '_, P> {
|
||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||
(**self).write_str(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
||||
// HACK(eddyb) get rid of `def_path_str` and/or pass `Namespace` explicitly always
|
||||
// (but also some things just print a `DefId` generally so maybe we need this?)
|
||||
|
@ -268,31 +266,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
|||
let ns = self.guess_def_namespace(def_id);
|
||||
debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns);
|
||||
let mut s = String::new();
|
||||
let _ = PrintCx::with(self, FmtPrinter::new(&mut s, ns), |cx| {
|
||||
cx.print_def_path(def_id, None, iter::empty())
|
||||
});
|
||||
let _ = PrintCx::new(self, FmtPrinter::new(&mut s, ns))
|
||||
.print_def_path(def_id, None, iter::empty());
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FmtPrinter<F: fmt::Write> {
|
||||
fmt: F,
|
||||
empty: bool,
|
||||
in_value: bool,
|
||||
pub region_highlight_mode: RegionHighlightMode,
|
||||
}
|
||||
|
||||
impl<F: fmt::Write> FmtPrinter<F> {
|
||||
pub fn new(fmt: F, ns: Namespace) -> Self {
|
||||
FmtPrinter {
|
||||
fmt,
|
||||
empty: true,
|
||||
in_value: ns == Namespace::ValueNS,
|
||||
region_highlight_mode: RegionHighlightMode::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
||||
/// If possible, this returns a global path resolving to `def_id` that is visible
|
||||
/// from at least one local module and returns true. If the crate defining `def_id` is
|
||||
|
@ -346,7 +325,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
}
|
||||
|
||||
if def_id.is_local() {
|
||||
return Ok((self.printer, false));
|
||||
return self.ok().map(|path| (path, false));
|
||||
}
|
||||
|
||||
let visible_parent_map = self.tcx.visible_parent_map(LOCAL_CRATE);
|
||||
|
@ -366,7 +345,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
|
||||
let visible_parent = match visible_parent_map.get(&def_id).cloned() {
|
||||
Some(parent) => parent,
|
||||
None => return Ok((self.printer, false)),
|
||||
None => return self.ok().map(|path| (path, false)),
|
||||
};
|
||||
// HACK(eddyb) this uses `nest` to avoid knowing ahead of time whether
|
||||
// the entire path will succeed or not. To support printers that do not
|
||||
|
@ -374,12 +353,12 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
// need to be built, before starting to print anything.
|
||||
let mut prefix_success = false;
|
||||
nest!(|cx| {
|
||||
let (printer, success) = cx.try_print_visible_def_path(visible_parent)?;
|
||||
let (path, success) = cx.try_print_visible_def_path(visible_parent)?;
|
||||
prefix_success = success;
|
||||
Ok(printer)
|
||||
Ok(path)
|
||||
});
|
||||
if !prefix_success {
|
||||
return Ok((self.printer, false));
|
||||
return self.ok().map(|path| (path, false));
|
||||
};
|
||||
let actual_parent = self.tcx.parent(def_id);
|
||||
debug!(
|
||||
|
@ -445,7 +424,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
},
|
||||
};
|
||||
debug!("try_print_visible_def_path: symbol={:?}", symbol);
|
||||
Ok((self.path_append(|cx| Ok(cx.printer), &symbol)?, true))
|
||||
Ok((self.path_append(|cx| cx.ok(), &symbol)?, true))
|
||||
}
|
||||
|
||||
pub fn pretty_path_qualified(
|
||||
|
@ -475,7 +454,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
if let Some(trait_ref) = trait_ref {
|
||||
p!(write(" as "), print(trait_ref));
|
||||
}
|
||||
Ok(cx.printer)
|
||||
cx.ok()
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -498,7 +477,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
}
|
||||
p!(print(self_ty));
|
||||
|
||||
Ok(cx.printer)
|
||||
cx.ok()
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -556,7 +535,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
let projection0 = projections.next();
|
||||
|
||||
if arg0.is_none() && projection0.is_none() {
|
||||
return Ok(self.printer);
|
||||
return self.ok();
|
||||
}
|
||||
|
||||
self.generic_delimiters(|mut cx| {
|
||||
|
@ -568,7 +547,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
empty = false;
|
||||
Ok(())
|
||||
} else {
|
||||
write!(cx.printer, ", ")
|
||||
write!(cx, ", ")
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -598,11 +577,54 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
print(projection.ty));
|
||||
}
|
||||
|
||||
Ok(cx.printer)
|
||||
cx.ok()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// HACK(eddyb) boxed to avoid moving around a large struct by-value.
|
||||
pub struct FmtPrinter<F>(Box<FmtPrinterData<F>>);
|
||||
|
||||
pub struct FmtPrinterData<F> {
|
||||
fmt: F,
|
||||
|
||||
empty: bool,
|
||||
in_value: bool,
|
||||
|
||||
used_region_names: FxHashSet<InternedString>,
|
||||
region_index: usize,
|
||||
binder_depth: usize,
|
||||
|
||||
pub region_highlight_mode: RegionHighlightMode,
|
||||
}
|
||||
|
||||
impl<F> Deref for FmtPrinter<F> {
|
||||
type Target = FmtPrinterData<F>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<F> DerefMut for FmtPrinter<F> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<F> FmtPrinter<F> {
|
||||
pub fn new(fmt: F, ns: Namespace) -> Self {
|
||||
FmtPrinter(Box::new(FmtPrinterData {
|
||||
fmt,
|
||||
empty: true,
|
||||
in_value: ns == Namespace::ValueNS,
|
||||
used_region_names: Default::default(),
|
||||
region_index: 0,
|
||||
binder_depth: 0,
|
||||
region_highlight_mode: RegionHighlightMode::default(),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: fmt::Write> fmt::Write for FmtPrinter<F> {
|
||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||
self.empty &= s.is_empty();
|
||||
|
@ -629,17 +651,17 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
|
|||
if generics.as_ref().and_then(|g| g.parent).is_none() {
|
||||
let mut visible_path_success = false;
|
||||
self = self.nest(|cx| {
|
||||
let (printer, success) = cx.try_print_visible_def_path(def_id)?;
|
||||
let (path, success) = cx.try_print_visible_def_path(def_id)?;
|
||||
visible_path_success = success;
|
||||
Ok(printer)
|
||||
Ok(path)
|
||||
})?;
|
||||
if visible_path_success {
|
||||
return if let (Some(generics), Some(substs)) = (generics, substs) {
|
||||
let has_own_self = generics.has_self && generics.parent_count == 0;
|
||||
let params = &generics.params[has_own_self as usize..];
|
||||
self.path_generic_args(|cx| Ok(cx.printer), params, substs, projections)
|
||||
self.path_generic_args(|cx| cx.ok(), params, substs, projections)
|
||||
} else {
|
||||
Ok(self.printer)
|
||||
self.ok()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -693,14 +715,13 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
|
|||
if self.tcx.sess.rust_2018() {
|
||||
// We add the `crate::` keyword on Rust 2018, only when desired.
|
||||
if SHOULD_PREFIX_WITH_CRATE.with(|flag| flag.get()) {
|
||||
write!(self.printer, "{}", keywords::Crate.name())?;
|
||||
write!(self, "{}", keywords::Crate.name())?;
|
||||
}
|
||||
}
|
||||
Ok(self.printer)
|
||||
} else {
|
||||
write!(self.printer, "{}", self.tcx.crate_name(cnum))?;
|
||||
Ok(self.printer)
|
||||
write!(self, "{}", self.tcx.crate_name(cnum))?;
|
||||
}
|
||||
self.ok()
|
||||
}
|
||||
fn path_qualified(
|
||||
self: PrintCx<'_, '_, 'tcx, Self>,
|
||||
|
@ -719,15 +740,15 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
|
|||
trait_ref: Option<ty::TraitRef<'tcx>>,
|
||||
) -> Result<Self::Path, Self::Error> {
|
||||
self.pretty_path_append_impl(|cx| {
|
||||
let mut printer = print_prefix(cx)?;
|
||||
let mut path = print_prefix(cx)?;
|
||||
|
||||
// HACK(eddyb) this accounts for `generic_delimiters`
|
||||
// printing `::<` instead of `<` if `in_value` is set.
|
||||
if !printer.empty && !printer.in_value {
|
||||
write!(printer, "::")?;
|
||||
if !path.empty && !path.in_value {
|
||||
write!(path, "::")?;
|
||||
}
|
||||
|
||||
Ok(printer)
|
||||
Ok(path)
|
||||
}, self_ty, trait_ref)
|
||||
}
|
||||
fn path_append<'gcx, 'tcx>(
|
||||
|
@ -737,18 +758,18 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
|
|||
) -> Result<Self::Path, Self::Error>,
|
||||
text: &str,
|
||||
) -> Result<Self::Path, Self::Error> {
|
||||
let mut printer = print_prefix(self)?;
|
||||
let mut path = print_prefix(self)?;
|
||||
|
||||
// FIXME(eddyb) `text` should never be empty, but it
|
||||
// currently is for `extern { ... }` "foreign modules".
|
||||
if !text.is_empty() {
|
||||
if !printer.empty {
|
||||
write!(printer, "::")?;
|
||||
if !path.empty {
|
||||
write!(path, "::")?;
|
||||
}
|
||||
write!(printer, "{}", text)?;
|
||||
write!(path, "{}", text)?;
|
||||
}
|
||||
|
||||
Ok(printer)
|
||||
Ok(path)
|
||||
}
|
||||
fn path_generic_args<'gcx, 'tcx>(
|
||||
self: PrintCx<'_, 'gcx, 'tcx, Self>,
|
||||
|
@ -768,18 +789,11 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
|
|||
mut self: PrintCx<'a, 'gcx, 'tcx, Self>,
|
||||
f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, Self>) -> Result<Self, E>,
|
||||
) -> Result<PrintCx<'a, 'gcx, 'tcx, Self>, E> {
|
||||
let was_empty = std::mem::replace(&mut self.printer.empty, true);
|
||||
let mut printer = f(PrintCx {
|
||||
tcx: self.tcx,
|
||||
printer: self.printer,
|
||||
config: self.config,
|
||||
})?;
|
||||
printer.empty &= was_empty;
|
||||
Ok(PrintCx {
|
||||
tcx: self.tcx,
|
||||
printer,
|
||||
config: self.config,
|
||||
})
|
||||
let tcx = self.tcx;
|
||||
let was_empty = std::mem::replace(&mut self.empty, true);
|
||||
let mut inner = f(self)?;
|
||||
inner.empty &= was_empty;
|
||||
Ok(PrintCx::new(tcx, inner))
|
||||
}
|
||||
|
||||
fn print_value_path(
|
||||
|
@ -787,11 +801,11 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
|
|||
def_id: DefId,
|
||||
substs: Option<SubstsRef<'tcx>>,
|
||||
) -> Result<Self::Path, Self::Error> {
|
||||
let was_in_value = std::mem::replace(&mut self.printer.in_value, true);
|
||||
let mut printer = self.print_def_path(def_id, substs, iter::empty())?;
|
||||
printer.in_value = was_in_value;
|
||||
let was_in_value = std::mem::replace(&mut self.in_value, true);
|
||||
let mut path = self.print_def_path(def_id, substs, iter::empty())?;
|
||||
path.in_value = was_in_value;
|
||||
|
||||
Ok(printer)
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
fn in_binder<T>(
|
||||
|
@ -807,18 +821,18 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
|
|||
mut self: PrintCx<'_, 'gcx, 'tcx, Self>,
|
||||
f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, Self>) -> Result<Self, Self::Error>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
if !self.printer.empty && self.printer.in_value {
|
||||
write!(self.printer, "::<")?;
|
||||
if !self.empty && self.in_value {
|
||||
write!(self, "::<")?;
|
||||
} else {
|
||||
write!(self.printer, "<")?;
|
||||
write!(self, "<")?;
|
||||
}
|
||||
|
||||
let was_in_value = std::mem::replace(&mut self.printer.in_value, false);
|
||||
let mut printer = f(self)?;
|
||||
printer.in_value = was_in_value;
|
||||
let was_in_value = std::mem::replace(&mut self.in_value, false);
|
||||
let mut inner = f(self)?;
|
||||
inner.in_value = was_in_value;
|
||||
|
||||
write!(printer, ">")?;
|
||||
Ok(printer)
|
||||
write!(inner, ">")?;
|
||||
Ok(inner)
|
||||
}
|
||||
|
||||
fn always_print_region_in_paths(
|
||||
|
@ -832,7 +846,7 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
|
|||
self: &PrintCx<'_, '_, '_, Self>,
|
||||
region: ty::Region<'_>,
|
||||
) -> bool {
|
||||
let highlight = self.printer.region_highlight_mode;
|
||||
let highlight = self.region_highlight_mode;
|
||||
if highlight.region_highlighted(region).is_some() {
|
||||
return true;
|
||||
}
|
||||
|
@ -889,15 +903,15 @@ impl<F: fmt::Write> FmtPrinter<F> {
|
|||
define_scoped_cx!(self);
|
||||
|
||||
// Watch out for region highlights.
|
||||
let highlight = self.printer.region_highlight_mode;
|
||||
let highlight = self.region_highlight_mode;
|
||||
if let Some(n) = highlight.region_highlighted(region) {
|
||||
p!(write("'{}", n));
|
||||
return Ok(self.printer);
|
||||
return self.ok();
|
||||
}
|
||||
|
||||
if self.tcx.sess.verbose() {
|
||||
p!(write("{:?}", region));
|
||||
return Ok(self.printer);
|
||||
return self.ok();
|
||||
}
|
||||
|
||||
let identify_regions = self.tcx.sess.opts.debugging_opts.identify_regions;
|
||||
|
@ -918,7 +932,7 @@ impl<F: fmt::Write> FmtPrinter<F> {
|
|||
if let ty::BrNamed(_, name) = br {
|
||||
if name != "" && name != "'_" {
|
||||
p!(write("{}", name));
|
||||
return Ok(self.printer);
|
||||
return self.ok();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -958,7 +972,7 @@ impl<F: fmt::Write> FmtPrinter<F> {
|
|||
ty::ReClosureBound(vid) => p!(write("{:?}", vid)),
|
||||
}
|
||||
|
||||
Ok(self.printer)
|
||||
self.ok()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1056,7 +1070,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
// FIXME(eddyb) print this with `print_def_path`.
|
||||
if self.tcx.sess.verbose() {
|
||||
p!(write("Opaque({:?}, {:?})", def_id, substs));
|
||||
return Ok(self.printer);
|
||||
return self.ok();
|
||||
}
|
||||
|
||||
let def_key = self.tcx.def_key(def_id);
|
||||
|
@ -1072,7 +1086,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
}
|
||||
p!(write(">"));
|
||||
}
|
||||
return Ok(self.printer);
|
||||
return self.ok();
|
||||
}
|
||||
// Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
|
||||
// by looking up the projections associated with the def_id.
|
||||
|
@ -1216,7 +1230,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
}
|
||||
}
|
||||
|
||||
Ok(self.printer)
|
||||
self.ok()
|
||||
}
|
||||
|
||||
pub fn pretty_fn_sig(
|
||||
|
@ -1243,11 +1257,18 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
p!(write(" -> "), print(output));
|
||||
}
|
||||
|
||||
Ok(self.printer)
|
||||
self.ok()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pretty_in_binder<T>(mut self, value: &ty::Binder<T>) -> Result<P, fmt::Error>
|
||||
where T: Print<'tcx, P, Output = P, Error = fmt::Error> + TypeFoldable<'tcx>
|
||||
// HACK(eddyb) limited to `FmtPrinter` because of `binder_depth`,
|
||||
// `region_index` and `used_region_names`.
|
||||
impl<F: fmt::Write> FmtPrinter<F> {
|
||||
pub fn pretty_in_binder<T>(
|
||||
mut self: PrintCx<'_, '_, 'tcx, Self>,
|
||||
value: &ty::Binder<T>,
|
||||
) -> Result<Self, fmt::Error>
|
||||
where T: Print<'tcx, Self, Output = Self, Error = fmt::Error> + TypeFoldable<'tcx>
|
||||
{
|
||||
fn name_by_region_index(index: usize) -> InternedString {
|
||||
match index {
|
||||
|
@ -1262,13 +1283,13 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
// clearly differentiate between named and unnamed regions in
|
||||
// the output. We'll probably want to tweak this over time to
|
||||
// decide just how much information to give.
|
||||
if self.config.binder_depth == 0 {
|
||||
if self.binder_depth == 0 {
|
||||
self.prepare_late_bound_region_info(value);
|
||||
}
|
||||
|
||||
let mut empty = true;
|
||||
let mut start_or_continue = |cx: &mut Self, start: &str, cont: &str| {
|
||||
write!(cx.printer, "{}", if empty {
|
||||
write!(cx, "{}", if empty {
|
||||
empty = false;
|
||||
start
|
||||
} else {
|
||||
|
@ -1278,13 +1299,13 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
|
||||
define_scoped_cx!(self);
|
||||
|
||||
let old_region_index = self.config.region_index;
|
||||
let old_region_index = self.region_index;
|
||||
let mut region_index = old_region_index;
|
||||
let new_value = self.tcx.replace_late_bound_regions(value, |br| {
|
||||
let _ = start_or_continue(&mut self, "for<", ", ");
|
||||
let br = match br {
|
||||
ty::BrNamed(_, name) => {
|
||||
let _ = write!(self.printer, "{}", name);
|
||||
let _ = write!(self, "{}", name);
|
||||
br
|
||||
}
|
||||
ty::BrAnon(_) |
|
||||
|
@ -1293,11 +1314,11 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
let name = loop {
|
||||
let name = name_by_region_index(region_index);
|
||||
region_index += 1;
|
||||
if !self.is_name_used(&name) {
|
||||
if !self.used_region_names.contains(&name) {
|
||||
break name;
|
||||
}
|
||||
};
|
||||
let _ = write!(self.printer, "{}", name);
|
||||
let _ = write!(self, "{}", name);
|
||||
ty::BrNamed(DefId::local(CRATE_DEF_INDEX), name)
|
||||
}
|
||||
};
|
||||
|
@ -1305,25 +1326,20 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
}).0;
|
||||
start_or_continue(&mut self, "", "> ")?;
|
||||
|
||||
// Push current state to gcx, and restore after writing new_value.
|
||||
self.config.binder_depth += 1;
|
||||
self.config.region_index = region_index;
|
||||
let result = new_value.print(PrintCx {
|
||||
tcx: self.tcx,
|
||||
printer: self.printer,
|
||||
config: self.config,
|
||||
});
|
||||
self.config.region_index = old_region_index;
|
||||
self.config.binder_depth -= 1;
|
||||
result
|
||||
self.binder_depth += 1;
|
||||
self.region_index = region_index;
|
||||
let mut inner = new_value.print(self)?;
|
||||
inner.region_index = old_region_index;
|
||||
inner.binder_depth -= 1;
|
||||
Ok(inner)
|
||||
}
|
||||
|
||||
fn prepare_late_bound_region_info<T>(&mut self, value: &ty::Binder<T>)
|
||||
where T: TypeFoldable<'tcx>
|
||||
where T: TypeFoldable<'tcx>
|
||||
{
|
||||
|
||||
struct LateBoundRegionNameCollector(FxHashSet<InternedString>);
|
||||
impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector {
|
||||
struct LateBoundRegionNameCollector<'a>(&'a mut FxHashSet<InternedString>);
|
||||
impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector<'_> {
|
||||
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
|
||||
match *r {
|
||||
ty::ReLateBound(_, ty::BrNamed(_, name)) => {
|
||||
|
@ -1335,17 +1351,10 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
|
|||
}
|
||||
}
|
||||
|
||||
let mut collector = LateBoundRegionNameCollector(Default::default());
|
||||
self.used_region_names.clear();
|
||||
let mut collector = LateBoundRegionNameCollector(&mut self.used_region_names);
|
||||
value.visit_with(&mut collector);
|
||||
self.config.used_region_names = Some(collector.0);
|
||||
self.config.region_index = 0;
|
||||
}
|
||||
|
||||
fn is_name_used(&self, name: &InternedString) -> bool {
|
||||
match self.config.used_region_names {
|
||||
Some(ref names) => names.contains(name),
|
||||
None => false,
|
||||
}
|
||||
self.region_index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1377,10 +1386,10 @@ impl<T> LiftAndPrintToFmt<'tcx> for T
|
|||
tcx: TyCtxt<'_, '_, 'tcx>,
|
||||
f: &mut fmt::Formatter<'_>,
|
||||
) -> fmt::Result {
|
||||
PrintCx::with(tcx, FmtPrinter::new(f, Namespace::TypeNS), |cx| {
|
||||
cx.tcx.lift(self).expect("could not lift for printing").print(cx)?;
|
||||
Ok(())
|
||||
})
|
||||
tcx.lift(self)
|
||||
.expect("could not lift for printing")
|
||||
.print(PrintCx::new(tcx, FmtPrinter::new(f, Namespace::TypeNS)))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1391,10 +1400,8 @@ impl LiftAndPrintToFmt<'tcx> for ty::RegionKind {
|
|||
tcx: TyCtxt<'_, '_, 'tcx>,
|
||||
f: &mut fmt::Formatter<'_>,
|
||||
) -> fmt::Result {
|
||||
PrintCx::with(tcx, FmtPrinter::new(f, Namespace::TypeNS), |cx| {
|
||||
self.print(cx)?;
|
||||
Ok(())
|
||||
})
|
||||
self.print(PrintCx::new(tcx, FmtPrinter::new(f, Namespace::TypeNS)))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1427,7 +1434,7 @@ macro_rules! define_print_and_forward_display {
|
|||
define_scoped_cx!($cx);
|
||||
let _: () = $print;
|
||||
#[allow(unreachable_code)]
|
||||
Ok($cx.printer)
|
||||
$cx.ok()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1555,7 +1562,7 @@ define_print_and_forward_display! {
|
|||
ty::InferTy {
|
||||
if cx.tcx.sess.verbose() {
|
||||
p!(write("{:?}", self));
|
||||
return Ok(cx.printer);
|
||||
return cx.ok();
|
||||
}
|
||||
match *self {
|
||||
ty::TyVar(_) => p!(write("_")),
|
||||
|
|
|
@ -224,11 +224,10 @@ fn get_symbol_hash<'a, 'tcx>(
|
|||
}
|
||||
|
||||
fn def_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> ty::SymbolName {
|
||||
PrintCx::with(tcx, SymbolPath::new(tcx), |cx| {
|
||||
cx.print_def_path(def_id, None, iter::empty())
|
||||
.unwrap()
|
||||
.into_interned()
|
||||
})
|
||||
PrintCx::new(tcx, SymbolPath::new(tcx))
|
||||
.print_def_path(def_id, None, iter::empty())
|
||||
.unwrap()
|
||||
.into_interned()
|
||||
}
|
||||
|
||||
fn symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance<'tcx>) -> ty::SymbolName {
|
||||
|
@ -415,7 +414,7 @@ impl Printer for SymbolPath {
|
|||
self: PrintCx<'_, '_, '_, Self>,
|
||||
_region: ty::Region<'_>,
|
||||
) -> Result<Self::Region, Self::Error> {
|
||||
Ok(self.printer)
|
||||
self.ok()
|
||||
}
|
||||
|
||||
fn print_type(
|
||||
|
@ -440,8 +439,8 @@ impl Printer for SymbolPath {
|
|||
mut self: PrintCx<'_, '_, '_, Self>,
|
||||
cnum: CrateNum,
|
||||
) -> Result<Self::Path, Self::Error> {
|
||||
self.printer.write_str(&self.tcx.original_crate_name(cnum).as_str())?;
|
||||
Ok(self.printer)
|
||||
self.write_str(&self.tcx.original_crate_name(cnum).as_str())?;
|
||||
self.ok()
|
||||
}
|
||||
fn path_qualified(
|
||||
self: PrintCx<'_, '_, 'tcx, Self>,
|
||||
|
@ -524,10 +523,10 @@ impl PrettyPrinter for SymbolPath {
|
|||
mut self: PrintCx<'_, 'gcx, 'tcx, Self>,
|
||||
f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, Self>) -> Result<Self, Self::Error>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
write!(self.printer, "<")?;
|
||||
write!(self, "<")?;
|
||||
|
||||
let kept_within_component =
|
||||
mem::replace(&mut self.printer.keep_within_component, true);
|
||||
mem::replace(&mut self.keep_within_component, true);
|
||||
let mut path = f(self)?;
|
||||
path.keep_within_component = kept_within_component;
|
||||
|
||||
|
|
|
@ -2341,9 +2341,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
|
|||
_ => {}
|
||||
}
|
||||
|
||||
let _ = ty::print::PrintCx::with(self.infcx.tcx, printer, |cx| {
|
||||
ty.print(cx)
|
||||
});
|
||||
let _ = ty.print(ty::print::PrintCx::new(self.infcx.tcx, printer));
|
||||
s
|
||||
}
|
||||
|
||||
|
@ -2368,9 +2366,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
|
|||
_ => bug!("ty for annotation of borrow region is not a reference"),
|
||||
};
|
||||
|
||||
let _ = ty::print::PrintCx::with(self.infcx.tcx, printer, |cx| {
|
||||
region.print(cx)
|
||||
});
|
||||
let _ = region.print(ty::print::PrintCx::new(self.infcx.tcx, printer));
|
||||
s
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4312,9 +4312,9 @@ where F: Fn(DefId) -> Def {
|
|||
}
|
||||
}
|
||||
|
||||
let names = PrintCx::with(tcx, AbsolutePathPrinter, |cx| {
|
||||
cx.print_def_path(def_id, None, iter::empty()).unwrap()
|
||||
});
|
||||
let names = PrintCx::new(tcx, AbsolutePathPrinter)
|
||||
.print_def_path(def_id, None, iter::empty())
|
||||
.unwrap();
|
||||
|
||||
hir::Path {
|
||||
span: DUMMY_SP,
|
||||
|
|
Loading…
Add table
Reference in a new issue