rustc: move the formatter into ty::print::PrintCx.

This commit is contained in:
Eduard-Mihai Burtescu
2018-12-10 16:10:22 +02:00
parent 732b71a1ba
commit 08d27441ec
9 changed files with 448 additions and 429 deletions

View File

@@ -31,6 +31,7 @@
#![deny(rust_2018_idioms)] #![deny(rust_2018_idioms)]
#![allow(explicit_outlives_requirements)] #![allow(explicit_outlives_requirements)]
#![feature(arbitrary_self_types)]
#![feature(box_patterns)] #![feature(box_patterns)]
#![feature(box_syntax)] #![feature(box_syntax)]
#![feature(core_intrinsics)] #![feature(core_intrinsics)]

View File

@@ -2369,7 +2369,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
}; };
// When printing regions, add trailing space if necessary. // When printing regions, add trailing space if necessary.
ty::print::PrintCx::with(|cx| { ty::print::PrintCx::with(ty::print::FmtPrinter { fmt }, |cx| {
let region = if cx.is_verbose || cx.identify_regions { let region = if cx.is_verbose || cx.identify_regions {
let mut region = region.to_string(); let mut region = region.to_string();
if region.len() > 0 { if region.len() > 0 {
@@ -2380,7 +2380,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
// Do not even print 'static // Do not even print 'static
String::new() String::new()
}; };
write!(fmt, "&{}{}{:?}", region, kind_str, place) write!(cx.printer.fmt, "&{}{}{:?}", region, kind_str, place)
}) })
} }

View File

@@ -59,11 +59,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
/// root, unless with_forced_absolute_paths was used. /// root, unless with_forced_absolute_paths was used.
pub fn item_path_str(self, def_id: DefId) -> String { pub fn item_path_str(self, def_id: DefId) -> String {
debug!("item_path_str: def_id={:?}", def_id); debug!("item_path_str: def_id={:?}", def_id);
let mut cx = PrintCx::new(self);
if FORCE_ABSOLUTE.with(|force| force.get()) { if FORCE_ABSOLUTE.with(|force| force.get()) {
AbsolutePathPrinter::print_item_path(&mut cx, def_id) PrintCx::new(self, AbsolutePathPrinter).print_item_path(def_id)
} else { } else {
LocalPathPrinter::print_item_path(&mut cx, def_id) PrintCx::new(self, LocalPathPrinter).print_item_path(def_id)
} }
} }
@@ -76,26 +75,23 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
/// suitable for user output. It always begins with a crate identifier. /// suitable for user output. It always begins with a crate identifier.
pub fn absolute_item_path_str(self, def_id: DefId) -> String { pub fn absolute_item_path_str(self, def_id: DefId) -> String {
debug!("absolute_item_path_str: def_id={:?}", def_id); debug!("absolute_item_path_str: def_id={:?}", def_id);
let mut cx = PrintCx::new(self); PrintCx::new(self, AbsolutePathPrinter).print_item_path(def_id)
AbsolutePathPrinter::print_item_path(&mut cx, def_id)
} }
} }
impl PrintCx<'a, 'gcx, 'tcx> { impl<P: ItemPathPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
pub fn default_print_item_path<P>(&mut self, def_id: DefId) -> P::Path pub fn default_print_item_path(&mut self, def_id: DefId) -> P::Path {
where P: ItemPathPrinter
{
debug!("default_print_item_path: def_id={:?}", def_id); debug!("default_print_item_path: def_id={:?}", def_id);
let key = self.tcx.def_key(def_id); let key = self.tcx.def_key(def_id);
debug!("default_print_item_path: key={:?}", key); debug!("default_print_item_path: key={:?}", key);
match key.disambiguated_data.data { match key.disambiguated_data.data {
DefPathData::CrateRoot => { DefPathData::CrateRoot => {
assert!(key.parent.is_none()); assert!(key.parent.is_none());
P::path_crate(self, def_id.krate) self.path_crate(def_id.krate)
} }
DefPathData::Impl => { DefPathData::Impl => {
self.default_print_impl_path::<P>(def_id) self.default_print_impl_path(def_id)
} }
// Unclear if there is any value in distinguishing these. // Unclear if there is any value in distinguishing these.
@@ -121,20 +117,18 @@ impl PrintCx<'a, 'gcx, 'tcx> {
data @ DefPathData::ImplTrait | data @ DefPathData::ImplTrait |
data @ DefPathData::GlobalMetaData(..) => { data @ DefPathData::GlobalMetaData(..) => {
let parent_did = self.tcx.parent_def_id(def_id).unwrap(); let parent_did = self.tcx.parent_def_id(def_id).unwrap();
let path = P::print_item_path(self, parent_did); let path = self.print_item_path(parent_did);
P::path_append(path, &data.as_interned_str().as_symbol().as_str()) self.path_append(path, &data.as_interned_str().as_symbol().as_str())
}, },
DefPathData::StructCtor => { // present `X` instead of `X::{{constructor}}` DefPathData::StructCtor => { // present `X` instead of `X::{{constructor}}`
let parent_def_id = self.tcx.parent_def_id(def_id).unwrap(); let parent_def_id = self.tcx.parent_def_id(def_id).unwrap();
P::print_item_path(self, parent_def_id) self.print_item_path(parent_def_id)
} }
} }
} }
fn default_print_impl_path<P>(&mut self, impl_def_id: DefId) -> P::Path fn default_print_impl_path(&mut self, impl_def_id: DefId) -> P::Path {
where P: ItemPathPrinter
{
debug!("default_print_impl_path: impl_def_id={:?}", impl_def_id); debug!("default_print_impl_path: impl_def_id={:?}", impl_def_id);
let parent_def_id = self.tcx.parent_def_id(impl_def_id).unwrap(); let parent_def_id = self.tcx.parent_def_id(impl_def_id).unwrap();
@@ -147,7 +141,7 @@ impl PrintCx<'a, 'gcx, 'tcx> {
}; };
if !use_types { if !use_types {
return self.default_print_impl_path_fallback::<P>(impl_def_id); return self.default_print_impl_path_fallback(impl_def_id);
} }
// Decide whether to print the parent path for the impl. // Decide whether to print the parent path for the impl.
@@ -171,11 +165,11 @@ impl PrintCx<'a, 'gcx, 'tcx> {
// If the impl is not co-located with either self-type or // If the impl is not co-located with either self-type or
// trait-type, then fallback to a format that identifies // trait-type, then fallback to a format that identifies
// the module more clearly. // the module more clearly.
let path = P::print_item_path(self, parent_def_id); let path = self.print_item_path(parent_def_id);
if let Some(trait_ref) = impl_trait_ref { if let Some(trait_ref) = impl_trait_ref {
return P::path_append(path, &format!("<impl {} for {}>", trait_ref, self_ty)); return self.path_append(path, &format!("<impl {} for {}>", trait_ref, self_ty));
} else { } else {
return P::path_append(path, &format!("<impl {}>", self_ty)); return self.path_append(path, &format!("<impl {}>", self_ty));
} }
} }
@@ -184,7 +178,7 @@ impl PrintCx<'a, 'gcx, 'tcx> {
if let Some(trait_ref) = impl_trait_ref { if let Some(trait_ref) = impl_trait_ref {
// Trait impls. // Trait impls.
return P::path_impl(self, &format!("<{} as {}>", self_ty, trait_ref)); return self.path_impl(&format!("<{} as {}>", self_ty, trait_ref));
} }
// Inherent impls. Try to print `Foo::bar` for an inherent // Inherent impls. Try to print `Foo::bar` for an inherent
@@ -194,13 +188,13 @@ impl PrintCx<'a, 'gcx, 'tcx> {
ty::Adt(adt_def, substs) => { ty::Adt(adt_def, substs) => {
// FIXME(eddyb) always print without <> here. // FIXME(eddyb) always print without <> here.
if substs.types().next().is_none() { // ignore regions if substs.types().next().is_none() { // ignore regions
P::print_item_path(self, adt_def.did) self.print_item_path(adt_def.did)
} else { } else {
P::path_impl(self, &format!("<{}>", self_ty)) self.path_impl(&format!("<{}>", self_ty))
} }
} }
ty::Foreign(did) => P::print_item_path(self, did), ty::Foreign(did) => self.print_item_path(did),
ty::Bool | ty::Bool |
ty::Char | ty::Char |
@@ -208,28 +202,26 @@ impl PrintCx<'a, 'gcx, 'tcx> {
ty::Uint(_) | ty::Uint(_) |
ty::Float(_) | ty::Float(_) |
ty::Str => { ty::Str => {
P::path_impl(self, &self_ty.to_string()) self.path_impl(&self_ty.to_string())
} }
_ => { _ => {
P::path_impl(self, &format!("<{}>", self_ty)) self.path_impl(&format!("<{}>", self_ty))
} }
} }
} }
fn default_print_impl_path_fallback<P>(&mut self, impl_def_id: DefId) -> P::Path fn default_print_impl_path_fallback(&mut self, impl_def_id: DefId) -> P::Path {
where P: ItemPathPrinter
{
// If no type info is available, fall back to // If no type info is available, fall back to
// pretty printing some span information. This should // pretty printing some span information. This should
// only occur very early in the compiler pipeline. // only occur very early in the compiler pipeline.
// FIXME(eddyb) this should just be using `tcx.def_span(impl_def_id)` // FIXME(eddyb) this should just be using `tcx.def_span(impl_def_id)`
let parent_def_id = self.tcx.parent_def_id(impl_def_id).unwrap(); let parent_def_id = self.tcx.parent_def_id(impl_def_id).unwrap();
let path = P::print_item_path(self, parent_def_id); let path = self.print_item_path(parent_def_id);
let hir_id = self.tcx.hir().as_local_hir_id(impl_def_id).unwrap(); let hir_id = self.tcx.hir().as_local_hir_id(impl_def_id).unwrap();
let item = self.tcx.hir().expect_item_by_hir_id(hir_id); let item = self.tcx.hir().expect_item_by_hir_id(hir_id);
let span_str = self.tcx.sess.source_map().span_to_string(item.span); let span_str = self.tcx.sess.source_map().span_to_string(item.span);
P::path_append(path, &format!("<impl at {}>", span_str)) self.path_append(path, &format!("<impl at {}>", span_str))
} }
} }
@@ -296,13 +288,17 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
pub trait ItemPathPrinter: Sized { pub trait ItemPathPrinter: Sized {
type Path; type Path;
fn print_item_path(cx: &mut PrintCx<'_, '_, '_>, def_id: DefId) -> Self::Path { fn print_item_path(self: &mut PrintCx<'_, '_, '_, Self>, def_id: DefId) -> Self::Path {
cx.default_print_item_path::<Self>(def_id) self.default_print_item_path(def_id)
} }
fn path_crate(cx: &mut PrintCx<'_, '_, '_>, cnum: CrateNum) -> Self::Path; fn path_crate(self: &mut PrintCx<'_, '_, '_, Self>, cnum: CrateNum) -> Self::Path;
fn path_impl(cx: &mut PrintCx<'_, '_, '_>, text: &str) -> Self::Path; fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path;
fn path_append(path: Self::Path, text: &str) -> Self::Path; fn path_append(
self: &mut PrintCx<'_, '_, '_, Self>,
path: Self::Path,
text: &str,
) -> Self::Path;
} }
struct AbsolutePathPrinter; struct AbsolutePathPrinter;
@@ -310,13 +306,17 @@ struct AbsolutePathPrinter;
impl ItemPathPrinter for AbsolutePathPrinter { impl ItemPathPrinter for AbsolutePathPrinter {
type Path = String; type Path = String;
fn path_crate(cx: &mut PrintCx<'_, '_, '_>, cnum: CrateNum) -> Self::Path { fn path_crate(self: &mut PrintCx<'_, '_, '_, Self>, cnum: CrateNum) -> Self::Path {
cx.tcx.original_crate_name(cnum).to_string() self.tcx.original_crate_name(cnum).to_string()
} }
fn path_impl(_cx: &mut PrintCx<'_, '_, '_>, text: &str) -> Self::Path { fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path {
text.to_string() text.to_string()
} }
fn path_append(mut path: Self::Path, text: &str) -> Self::Path { fn path_append(
self: &mut PrintCx<'_, '_, '_, Self>,
mut path: Self::Path,
text: &str,
) -> Self::Path {
if !path.is_empty() { if !path.is_empty() {
path.push_str("::"); path.push_str("::");
} }
@@ -332,7 +332,7 @@ impl LocalPathPrinter {
/// from at least one local module and returns true. If the crate defining `def_id` is /// from at least one local module and returns true. If the crate defining `def_id` is
/// declared with an `extern crate`, the path is guaranteed to use the `extern crate`. /// declared with an `extern crate`, the path is guaranteed to use the `extern crate`.
fn try_print_visible_item_path( fn try_print_visible_item_path(
cx: &mut PrintCx<'_, '_, '_>, self: &mut PrintCx<'_, '_, '_, Self>,
def_id: DefId, def_id: DefId,
) -> Option<<Self as ItemPathPrinter>::Path> { ) -> Option<<Self as ItemPathPrinter>::Path> {
debug!("try_print_visible_item_path: def_id={:?}", def_id); debug!("try_print_visible_item_path: def_id={:?}", def_id);
@@ -343,7 +343,7 @@ impl LocalPathPrinter {
let cnum = def_id.krate; let cnum = def_id.krate;
if cnum == LOCAL_CRATE { if cnum == LOCAL_CRATE {
return Some(Self::path_crate(cx, cnum)); return Some(self.path_crate(cnum));
} }
// In local mode, when we encounter a crate other than // In local mode, when we encounter a crate other than
@@ -356,7 +356,7 @@ impl LocalPathPrinter {
// 2. for an extern inferred from a path or an indirect crate, // 2. for an extern inferred from a path or an indirect crate,
// where there is no explicit `extern crate`, we just prepend // where there is no explicit `extern crate`, we just prepend
// the crate name. // the crate name.
match *cx.tcx.extern_crate(def_id) { match *self.tcx.extern_crate(def_id) {
Some(ExternCrate { Some(ExternCrate {
src: ExternCrateSource::Extern(def_id), src: ExternCrateSource::Extern(def_id),
direct: true, direct: true,
@@ -365,14 +365,14 @@ impl LocalPathPrinter {
}) => { }) => {
debug!("try_print_visible_item_path: def_id={:?}", def_id); debug!("try_print_visible_item_path: def_id={:?}", def_id);
let path = if !span.is_dummy() { let path = if !span.is_dummy() {
Self::print_item_path(cx, def_id) self.print_item_path(def_id)
} else { } else {
Self::path_crate(cx, cnum) self.path_crate(cnum)
}; };
return Some(path); return Some(path);
} }
None => { None => {
return Some(Self::path_crate(cx, cnum)); return Some(self.path_crate(cnum));
} }
_ => {}, _ => {},
} }
@@ -382,9 +382,9 @@ impl LocalPathPrinter {
return None; return None;
} }
let visible_parent_map = cx.tcx.visible_parent_map(LOCAL_CRATE); let visible_parent_map = self.tcx.visible_parent_map(LOCAL_CRATE);
let mut cur_def_key = cx.tcx.def_key(def_id); let mut cur_def_key = self.tcx.def_key(def_id);
debug!("try_print_visible_item_path: cur_def_key={:?}", cur_def_key); debug!("try_print_visible_item_path: cur_def_key={:?}", cur_def_key);
// For a UnitStruct or TupleStruct we want the name of its parent rather than <unnamed>. // For a UnitStruct or TupleStruct we want the name of its parent rather than <unnamed>.
@@ -394,12 +394,12 @@ impl LocalPathPrinter {
index: cur_def_key.parent.expect("DefPathData::StructCtor missing a parent"), index: cur_def_key.parent.expect("DefPathData::StructCtor missing a parent"),
}; };
cur_def_key = cx.tcx.def_key(parent); cur_def_key = self.tcx.def_key(parent);
} }
let visible_parent = visible_parent_map.get(&def_id).cloned()?; let visible_parent = visible_parent_map.get(&def_id).cloned()?;
let path = Self::try_print_visible_item_path(cx, visible_parent)?; let path = self.try_print_visible_item_path(visible_parent)?;
let actual_parent = cx.tcx.parent(def_id); let actual_parent = self.tcx.parent(def_id);
let data = cur_def_key.disambiguated_data.data; let data = cur_def_key.disambiguated_data.data;
debug!( debug!(
@@ -441,7 +441,7 @@ impl LocalPathPrinter {
// have access to the re-exported name. // have access to the re-exported name.
DefPathData::Module(actual_name) | DefPathData::Module(actual_name) |
DefPathData::TypeNs(actual_name) if Some(visible_parent) != actual_parent => { DefPathData::TypeNs(actual_name) if Some(visible_parent) != actual_parent => {
cx.tcx.item_children(visible_parent) self.tcx.item_children(visible_parent)
.iter() .iter()
.find(|child| child.def.def_id() == def_id) .find(|child| child.def.def_id() == def_id)
.map(|child| child.ident.as_str()) .map(|child| child.ident.as_str())
@@ -451,7 +451,7 @@ impl LocalPathPrinter {
data.get_opt_name().map(|n| n.as_str()).unwrap_or_else(|| { data.get_opt_name().map(|n| n.as_str()).unwrap_or_else(|| {
// Re-exported `extern crate` (#43189). // Re-exported `extern crate` (#43189).
if let DefPathData::CrateRoot = data { if let DefPathData::CrateRoot = data {
cx.tcx.original_crate_name(def_id.krate).as_str() self.tcx.original_crate_name(def_id.krate).as_str()
} else { } else {
Symbol::intern("<unnamed>").as_str() Symbol::intern("<unnamed>").as_str()
} }
@@ -459,21 +459,21 @@ impl LocalPathPrinter {
}, },
}; };
debug!("try_print_visible_item_path: symbol={:?}", symbol); debug!("try_print_visible_item_path: symbol={:?}", symbol);
Some(Self::path_append(path, &symbol)) Some(self.path_append(path, &symbol))
} }
} }
impl ItemPathPrinter for LocalPathPrinter { impl ItemPathPrinter for LocalPathPrinter {
type Path = String; type Path = String;
fn print_item_path(cx: &mut PrintCx<'_, '_, '_>, def_id: DefId) -> Self::Path { fn print_item_path(self: &mut PrintCx<'_, '_, '_, Self>, def_id: DefId) -> Self::Path {
Self::try_print_visible_item_path(cx, def_id) self.try_print_visible_item_path(def_id)
.unwrap_or_else(|| cx.default_print_item_path::<Self>(def_id)) .unwrap_or_else(|| self.default_print_item_path(def_id))
} }
fn path_crate(cx: &mut PrintCx<'_, '_, '_>, cnum: CrateNum) -> Self::Path { fn path_crate(self: &mut PrintCx<'_, '_, '_, Self>, cnum: CrateNum) -> Self::Path {
if cnum == LOCAL_CRATE { if cnum == LOCAL_CRATE {
if cx.tcx.sess.rust_2018() { if self.tcx.sess.rust_2018() {
// We add the `crate::` keyword on Rust 2018, only when desired. // We add the `crate::` keyword on Rust 2018, only when desired.
if SHOULD_PREFIX_WITH_CRATE.with(|flag| flag.get()) { if SHOULD_PREFIX_WITH_CRATE.with(|flag| flag.get()) {
return keywords::Crate.name().to_string(); return keywords::Crate.name().to_string();
@@ -481,13 +481,17 @@ impl ItemPathPrinter for LocalPathPrinter {
} }
String::new() String::new()
} else { } else {
cx.tcx.crate_name(cnum).to_string() self.tcx.crate_name(cnum).to_string()
} }
} }
fn path_impl(_cx: &mut PrintCx<'_, '_, '_>, text: &str) -> Self::Path { fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path {
text.to_string() text.to_string()
} }
fn path_append(mut path: Self::Path, text: &str) -> Self::Path { fn path_append(
self: &mut PrintCx<'_, '_, '_, Self>,
mut path: Self::Path,
text: &str,
) -> Self::Path {
if !path.is_empty() { if !path.is_empty() {
path.push_str("::"); path.push_str("::");
} }

View File

@@ -4,6 +4,7 @@ use rustc_data_structures::fx::FxHashSet;
use syntax::symbol::InternedString; use syntax::symbol::InternedString;
use std::fmt; use std::fmt;
use std::ops::Deref;
// FIXME(eddyb) this module uses `pub(crate)` for things used only // FIXME(eddyb) this module uses `pub(crate)` for things used only
// from `ppaux` - when that is removed, they can be re-privatized. // from `ppaux` - when that is removed, they can be re-privatized.
@@ -21,8 +22,9 @@ impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector {
} }
} }
pub struct PrintCx<'a, 'gcx, 'tcx> { pub struct PrintCx<'a, 'gcx, 'tcx, P> {
pub tcx: TyCtxt<'a, 'gcx, 'tcx>, pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
pub printer: P,
pub(crate) is_debug: bool, pub(crate) is_debug: bool,
pub(crate) is_verbose: bool, pub(crate) is_verbose: bool,
pub(crate) identify_regions: bool, pub(crate) identify_regions: bool,
@@ -31,10 +33,20 @@ pub struct PrintCx<'a, 'gcx, 'tcx> {
pub(crate) binder_depth: usize, pub(crate) binder_depth: usize,
} }
impl PrintCx<'a, 'gcx, 'tcx> { // HACK(eddyb) this is solely for `self: &mut PrintCx<Self>`, e.g. to
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Self { // 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
}
}
impl<P> PrintCx<'a, 'gcx, 'tcx, P> {
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>, printer: P) -> Self {
PrintCx { PrintCx {
tcx, tcx,
printer,
is_debug: false, is_debug: false,
is_verbose: tcx.sess.verbose(), is_verbose: tcx.sess.verbose(),
identify_regions: tcx.sess.opts.debugging_opts.identify_regions, identify_regions: tcx.sess.opts.debugging_opts.identify_regions,
@@ -44,8 +56,8 @@ impl PrintCx<'a, 'gcx, 'tcx> {
} }
} }
pub(crate) fn with<R>(f: impl FnOnce(PrintCx<'_, '_, '_>) -> R) -> R { pub(crate) fn with<R>(printer: P, f: impl FnOnce(PrintCx<'_, '_, '_, P>) -> R) -> R {
ty::tls::with(|tcx| f(PrintCx::new(tcx))) ty::tls::with(|tcx| f(PrintCx::new(tcx, printer)))
} }
pub(crate) fn prepare_late_bound_region_info<T>(&mut self, value: &ty::Binder<T>) pub(crate) fn prepare_late_bound_region_info<T>(&mut self, value: &ty::Binder<T>)
where T: TypeFoldable<'tcx> where T: TypeFoldable<'tcx>
@@ -57,24 +69,26 @@ impl PrintCx<'a, 'gcx, 'tcx> {
} }
} }
pub trait Print<'tcx> { pub trait Print<'tcx, P> {
fn print<F: fmt::Write>(&self, f: &mut F, cx: &mut PrintCx<'_, '_, 'tcx>) -> fmt::Result; type Output;
fn print_display<F: fmt::Write>(
&self, fn print(&self, cx: &mut PrintCx<'_, '_, 'tcx, P>) -> Self::Output;
f: &mut F, fn print_display(&self, cx: &mut PrintCx<'_, '_, 'tcx, P>) -> Self::Output {
cx: &mut PrintCx<'_, '_, 'tcx>,
) -> fmt::Result {
let old_debug = cx.is_debug; let old_debug = cx.is_debug;
cx.is_debug = false; cx.is_debug = false;
let result = self.print(f, cx); let result = self.print(cx);
cx.is_debug = old_debug; cx.is_debug = old_debug;
result result
} }
fn print_debug<F: fmt::Write>(&self, f: &mut F, cx: &mut PrintCx<'_, '_, 'tcx>) -> fmt::Result { fn print_debug(&self, cx: &mut PrintCx<'_, '_, 'tcx, P>) -> Self::Output {
let old_debug = cx.is_debug; let old_debug = cx.is_debug;
cx.is_debug = true; cx.is_debug = true;
let result = self.print(f, cx); let result = self.print(cx);
cx.is_debug = old_debug; cx.is_debug = old_debug;
result result
} }
} }
pub struct FmtPrinter<F: fmt::Write> {
pub fmt: F,
}

File diff suppressed because it is too large Load Diff

View File

@@ -4,6 +4,7 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
#![feature(arbitrary_self_types)]
#![feature(box_patterns)] #![feature(box_patterns)]
#![feature(box_syntax)] #![feature(box_syntax)]
#![feature(custom_attribute)] #![feature(custom_attribute)]

View File

@@ -225,8 +225,7 @@ fn get_symbol_hash<'a, 'tcx>(
fn def_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> ty::SymbolName { fn def_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> ty::SymbolName {
item_path::with_forced_absolute_paths(|| { item_path::with_forced_absolute_paths(|| {
let mut cx = PrintCx::new(tcx); PrintCx::new(tcx, SymbolPathPrinter).print_item_path(def_id).into_interned()
SymbolPathPrinter::print_item_path(&mut cx, def_id).into_interned()
}) })
} }
@@ -401,17 +400,21 @@ struct SymbolPathPrinter;
impl ItemPathPrinter for SymbolPathPrinter { impl ItemPathPrinter for SymbolPathPrinter {
type Path = SymbolPath; type Path = SymbolPath;
fn path_crate(cx: &mut PrintCx<'_, '_, '_>, cnum: CrateNum) -> Self::Path { fn path_crate(self: &mut PrintCx<'_, '_, '_, Self>, cnum: CrateNum) -> Self::Path {
let mut path = SymbolPath::new(cx.tcx); let mut path = SymbolPath::new(self.tcx);
path.push(&cx.tcx.original_crate_name(cnum).as_str()); path.push(&self.tcx.original_crate_name(cnum).as_str());
path path
} }
fn path_impl(cx: &mut PrintCx<'_, '_, '_>, text: &str) -> Self::Path { fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path {
let mut path = SymbolPath::new(cx.tcx); let mut path = SymbolPath::new(self.tcx);
path.push(text); path.push(text);
path path
} }
fn path_append(mut path: Self::Path, text: &str) -> Self::Path { fn path_append(
self: &mut PrintCx<'_, '_, '_, Self>,
mut path: Self::Path,
text: &str,
) -> Self::Path {
path.push(text); path.push(text);
path path
} }

View File

@@ -4233,20 +4233,23 @@ where F: Fn(DefId) -> Def {
impl ItemPathPrinter for AbsolutePathPrinter { impl ItemPathPrinter for AbsolutePathPrinter {
type Path = Vec<String>; type Path = Vec<String>;
fn path_crate(cx: &mut PrintCx<'_, '_, '_>, cnum: CrateNum) -> Self::Path { fn path_crate(self: &mut PrintCx<'_, '_, '_, Self>, cnum: CrateNum) -> Self::Path {
vec![cx.tcx.original_crate_name(cnum).to_string()] vec![self.tcx.original_crate_name(cnum).to_string()]
} }
fn path_impl(_: &mut PrintCx<'_, '_, '_>, text: &str) -> Self::Path { fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path {
vec![text.to_string()] vec![text.to_string()]
} }
fn path_append(mut path: Self::Path, text: &str) -> Self::Path { fn path_append(
self: &mut PrintCx<'_, '_, '_, Self>,
mut path: Self::Path,
text: &str,
) -> Self::Path {
path.push(text.to_string()); path.push(text.to_string());
path path
} }
} }
let mut cx = PrintCx::new(tcx); let names = PrintCx::new(tcx, AbsolutePathPrinter).print_item_path(def_id);
let names = AbsolutePathPrinter::print_item_path(&mut cx, def_id);
hir::Path { hir::Path {
span: DUMMY_SP, span: DUMMY_SP,

View File

@@ -5,6 +5,7 @@
#![feature(bind_by_move_pattern_guards)] #![feature(bind_by_move_pattern_guards)]
#![feature(rustc_private)] #![feature(rustc_private)]
#![feature(arbitrary_self_types)]
#![feature(box_patterns)] #![feature(box_patterns)]
#![feature(box_syntax)] #![feature(box_syntax)]
#![feature(nll)] #![feature(nll)]