2019-01-18 21:33:31 +02:00
|
|
|
use crate::hir;
|
|
|
|
use crate::hir::def::Namespace;
|
2019-02-03 12:59:37 +02:00
|
|
|
use crate::hir::map::{DefPathData, DisambiguatedDefPathData};
|
2019-01-18 21:33:31 +02:00
|
|
|
use crate::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
|
|
|
|
use crate::middle::cstore::{ExternCrate, ExternCrateSource};
|
|
|
|
use crate::middle::region;
|
|
|
|
use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable};
|
2019-01-29 07:21:11 +02:00
|
|
|
use crate::ty::subst::{Kind, Subst, UnpackedKind};
|
2019-01-18 21:33:31 +02:00
|
|
|
use crate::mir::interpret::ConstValue;
|
|
|
|
use syntax::symbol::{keywords, Symbol};
|
|
|
|
|
2019-01-20 14:00:39 +02:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2019-01-18 21:33:31 +02:00
|
|
|
use syntax::symbol::InternedString;
|
|
|
|
|
|
|
|
use std::cell::Cell;
|
|
|
|
use std::fmt::{self, Write as _};
|
2019-01-20 19:46:47 +02:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2019-01-18 21:33:31 +02:00
|
|
|
|
|
|
|
// `pretty` is a separate module only for organization.
|
|
|
|
use super::*;
|
|
|
|
|
2019-01-25 12:28:43 +02:00
|
|
|
macro_rules! p {
|
|
|
|
(@write($($data:expr),+)) => {
|
2019-01-20 19:46:47 +02:00
|
|
|
write!(scoped_cx!(), $($data),+)?
|
2019-01-18 21:33:31 +02:00
|
|
|
};
|
2019-01-25 12:28:43 +02:00
|
|
|
(@print($x:expr)) => {
|
|
|
|
scoped_cx!() = $x.print(scoped_cx!())?
|
2019-01-18 21:33:31 +02:00
|
|
|
};
|
2019-01-25 12:28:43 +02:00
|
|
|
(@$method:ident($($arg:expr),*)) => {
|
|
|
|
scoped_cx!() = scoped_cx!().$method($($arg),*)?
|
2019-01-18 21:33:31 +02:00
|
|
|
};
|
2019-01-25 12:28:43 +02:00
|
|
|
($($kind:ident $data:tt),+) => {{
|
|
|
|
$(p!(@$kind $data);)+
|
|
|
|
}};
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
macro_rules! define_scoped_cx {
|
|
|
|
($cx:ident) => {
|
|
|
|
#[allow(unused_macros)]
|
|
|
|
macro_rules! scoped_cx {
|
|
|
|
() => ($cx)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_local! {
|
|
|
|
static FORCE_IMPL_FILENAME_LINE: Cell<bool> = Cell::new(false);
|
|
|
|
static SHOULD_PREFIX_WITH_CRATE: Cell<bool> = Cell::new(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Force us to name impls with just the filename/line number. We
|
|
|
|
/// normally try to use types. But at some points, notably while printing
|
|
|
|
/// cycle errors, this can result in extra or suboptimal error output,
|
|
|
|
/// so this variable disables that check.
|
|
|
|
pub fn with_forced_impl_filename_line<F: FnOnce() -> R, R>(f: F) -> R {
|
|
|
|
FORCE_IMPL_FILENAME_LINE.with(|force| {
|
|
|
|
let old = force.get();
|
|
|
|
force.set(true);
|
|
|
|
let result = f();
|
|
|
|
force.set(old);
|
|
|
|
result
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds the `crate::` prefix to paths where appropriate.
|
|
|
|
pub fn with_crate_prefix<F: FnOnce() -> R, R>(f: F) -> R {
|
|
|
|
SHOULD_PREFIX_WITH_CRATE.with(|flag| {
|
|
|
|
let old = flag.get();
|
|
|
|
flag.set(true);
|
|
|
|
let result = f();
|
|
|
|
flag.set(old);
|
|
|
|
result
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The "region highlights" are used to control region printing during
|
|
|
|
/// specific error messages. When a "region highlight" is enabled, it
|
|
|
|
/// gives an alternate way to print specific regions. For now, we
|
|
|
|
/// always print those regions using a number, so something like "`'0`".
|
|
|
|
///
|
|
|
|
/// Regions not selected by the region highlight mode are presently
|
|
|
|
/// unaffected.
|
|
|
|
#[derive(Copy, Clone, Default)]
|
|
|
|
pub struct RegionHighlightMode {
|
|
|
|
/// If enabled, when we see the selected region, use "`'N`"
|
|
|
|
/// instead of the ordinary behavior.
|
|
|
|
highlight_regions: [Option<(ty::RegionKind, usize)>; 3],
|
|
|
|
|
|
|
|
/// If enabled, when printing a "free region" that originated from
|
|
|
|
/// the given `ty::BoundRegion`, print it as "`'1`". Free regions that would ordinarily
|
|
|
|
/// have names print as normal.
|
|
|
|
///
|
|
|
|
/// This is used when you have a signature like `fn foo(x: &u32,
|
|
|
|
/// y: &'a u32)` and we want to give a name to the region of the
|
|
|
|
/// reference `x`.
|
|
|
|
highlight_bound_region: Option<(ty::BoundRegion, usize)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RegionHighlightMode {
|
|
|
|
/// If `region` and `number` are both `Some`, invokes
|
|
|
|
/// `highlighting_region`.
|
|
|
|
pub fn maybe_highlighting_region(
|
|
|
|
&mut self,
|
|
|
|
region: Option<ty::Region<'_>>,
|
|
|
|
number: Option<usize>,
|
|
|
|
) {
|
|
|
|
if let Some(k) = region {
|
|
|
|
if let Some(n) = number {
|
|
|
|
self.highlighting_region(k, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Highlights the region inference variable `vid` as `'N`.
|
|
|
|
pub fn highlighting_region(
|
|
|
|
&mut self,
|
|
|
|
region: ty::Region<'_>,
|
|
|
|
number: usize,
|
|
|
|
) {
|
|
|
|
let num_slots = self.highlight_regions.len();
|
|
|
|
let first_avail_slot = self.highlight_regions.iter_mut()
|
|
|
|
.filter(|s| s.is_none())
|
|
|
|
.next()
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
bug!(
|
|
|
|
"can only highlight {} placeholders at a time",
|
|
|
|
num_slots,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
*first_avail_slot = Some((*region, number));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience wrapper for `highlighting_region`.
|
|
|
|
pub fn highlighting_region_vid(
|
|
|
|
&mut self,
|
|
|
|
vid: ty::RegionVid,
|
|
|
|
number: usize,
|
|
|
|
) {
|
|
|
|
self.highlighting_region(&ty::ReVar(vid), number)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `Some(n)` with the number to use for the given region, if any.
|
|
|
|
fn region_highlighted(&self, region: ty::Region<'_>) -> Option<usize> {
|
|
|
|
self
|
|
|
|
.highlight_regions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|h| match h {
|
|
|
|
Some((r, n)) if r == region => Some(*n),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.next()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Highlight the given bound region.
|
|
|
|
/// We can only highlight one bound region at a time. See
|
|
|
|
/// the field `highlight_bound_region` for more detailed notes.
|
|
|
|
pub fn highlighting_bound_region(
|
|
|
|
&mut self,
|
|
|
|
br: ty::BoundRegion,
|
|
|
|
number: usize,
|
|
|
|
) {
|
|
|
|
assert!(self.highlight_bound_region.is_none());
|
|
|
|
self.highlight_bound_region = Some((br, number));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Trait for printers that pretty-print using `fmt::Write` to the printer.
|
2019-01-25 12:11:50 +02:00
|
|
|
pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
|
|
|
|
Printer<'gcx, 'tcx,
|
2019-01-18 21:33:31 +02:00
|
|
|
Error = fmt::Error,
|
|
|
|
Path = Self,
|
|
|
|
Region = Self,
|
|
|
|
Type = Self,
|
2019-01-24 20:47:02 +02:00
|
|
|
DynExistential = Self,
|
2019-01-18 21:33:31 +02:00
|
|
|
> +
|
|
|
|
fmt::Write
|
|
|
|
{
|
|
|
|
/// Like `print_def_path` but for value paths.
|
|
|
|
fn print_value_path(
|
2019-01-25 12:11:50 +02:00
|
|
|
self,
|
2019-01-18 21:33:31 +02:00
|
|
|
def_id: DefId,
|
2019-01-29 07:21:11 +02:00
|
|
|
substs: &'tcx [Kind<'tcx>],
|
2019-01-18 21:33:31 +02:00
|
|
|
) -> Result<Self::Path, Self::Error> {
|
2019-01-24 20:47:02 +02:00
|
|
|
self.print_def_path(def_id, substs)
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-20 04:56:48 +02:00
|
|
|
fn in_binder<T>(
|
2019-01-25 12:11:50 +02:00
|
|
|
self,
|
2019-01-20 04:56:48 +02:00
|
|
|
value: &ty::Binder<T>,
|
|
|
|
) -> Result<Self, Self::Error>
|
2019-01-25 12:11:50 +02:00
|
|
|
where T: Print<'gcx, 'tcx, Self, Output = Self, Error = Self::Error> + TypeFoldable<'tcx>
|
2019-01-20 04:56:48 +02:00
|
|
|
{
|
|
|
|
value.skip_binder().print(self)
|
|
|
|
}
|
|
|
|
|
2019-01-24 19:52:43 +02:00
|
|
|
/// Print comma-separated elements.
|
|
|
|
fn comma_sep<T>(
|
2019-01-25 12:11:50 +02:00
|
|
|
mut self,
|
2019-01-24 19:52:43 +02:00
|
|
|
mut elems: impl Iterator<Item = T>,
|
|
|
|
) -> Result<Self, Self::Error>
|
2019-01-25 12:11:50 +02:00
|
|
|
where T: Print<'gcx, 'tcx, Self, Output = Self, Error = Self::Error>
|
2019-01-24 19:52:43 +02:00
|
|
|
{
|
|
|
|
if let Some(first) = elems.next() {
|
2019-01-25 12:11:50 +02:00
|
|
|
self = first.print(self)?;
|
2019-01-24 19:52:43 +02:00
|
|
|
for elem in elems {
|
2019-01-24 20:47:02 +02:00
|
|
|
self.write_str(", ")?;
|
2019-01-25 12:11:50 +02:00
|
|
|
self = elem.print(self)?;
|
2019-01-24 19:52:43 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
Ok(self)
|
2019-01-24 19:52:43 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 21:33:31 +02:00
|
|
|
/// Print `<...>` around what `f` prints.
|
2019-01-25 12:11:50 +02:00
|
|
|
fn generic_delimiters(
|
|
|
|
self,
|
|
|
|
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
|
2019-01-18 21:33:31 +02:00
|
|
|
) -> Result<Self, Self::Error>;
|
|
|
|
|
2019-01-23 19:36:39 +02:00
|
|
|
/// Return `true` if the region should be printed in
|
|
|
|
/// optional positions, e.g. `&'a T` or `dyn Tr + 'b`.
|
|
|
|
/// This is typically the case for all non-`'_` regions.
|
|
|
|
fn region_should_not_be_omitted(
|
2019-01-25 12:11:50 +02:00
|
|
|
&self,
|
2019-01-18 21:33:31 +02:00
|
|
|
region: ty::Region<'_>,
|
|
|
|
) -> bool;
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
// Defaults (should not be overriden):
|
2019-01-18 21:33:31 +02:00
|
|
|
|
|
|
|
/// 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
|
|
|
|
/// declared with an `extern crate`, the path is guaranteed to use the `extern crate`.
|
|
|
|
fn try_print_visible_def_path(
|
|
|
|
mut self,
|
|
|
|
def_id: DefId,
|
2019-01-25 12:11:50 +02:00
|
|
|
) -> Result<(Self, bool), Self::Error> {
|
2019-01-18 21:33:31 +02:00
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
|
|
|
debug!("try_print_visible_def_path: def_id={:?}", def_id);
|
|
|
|
|
|
|
|
// If `def_id` is a direct or injected extern crate, return the
|
|
|
|
// path to the crate followed by the path to the item within the crate.
|
|
|
|
if def_id.index == CRATE_DEF_INDEX {
|
|
|
|
let cnum = def_id.krate;
|
|
|
|
|
|
|
|
if cnum == LOCAL_CRATE {
|
|
|
|
return Ok((self.path_crate(cnum)?, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
// In local mode, when we encounter a crate other than
|
|
|
|
// LOCAL_CRATE, execution proceeds in one of two ways:
|
|
|
|
//
|
|
|
|
// 1. for a direct dependency, where user added an
|
|
|
|
// `extern crate` manually, we put the `extern
|
|
|
|
// crate` as the parent. So you wind up with
|
|
|
|
// something relative to the current crate.
|
|
|
|
// 2. for an extern inferred from a path or an indirect crate,
|
|
|
|
// where there is no explicit `extern crate`, we just prepend
|
|
|
|
// the crate name.
|
2019-01-25 12:11:50 +02:00
|
|
|
match *self.tcx().extern_crate(def_id) {
|
2019-01-18 21:33:31 +02:00
|
|
|
Some(ExternCrate {
|
|
|
|
src: ExternCrateSource::Extern(def_id),
|
|
|
|
direct: true,
|
|
|
|
span,
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
debug!("try_print_visible_def_path: def_id={:?}", def_id);
|
|
|
|
return Ok((if !span.is_dummy() {
|
2019-01-29 07:21:11 +02:00
|
|
|
self.print_def_path(def_id, &[])?
|
2019-01-18 21:33:31 +02:00
|
|
|
} else {
|
|
|
|
self.path_crate(cnum)?
|
|
|
|
}, true));
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
return Ok((self.path_crate(cnum)?, true));
|
|
|
|
}
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if def_id.is_local() {
|
2019-01-25 12:11:50 +02:00
|
|
|
return Ok((self, false));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
let visible_parent_map = self.tcx().visible_parent_map(LOCAL_CRATE);
|
2019-01-18 21:33:31 +02:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
let mut cur_def_key = self.tcx().def_key(def_id);
|
2019-01-18 21:33:31 +02:00
|
|
|
debug!("try_print_visible_def_path: cur_def_key={:?}", cur_def_key);
|
|
|
|
|
2019-03-24 17:49:58 +03:00
|
|
|
// For a constructor we want the name of its parent rather than <unnamed>.
|
2019-03-21 23:38:50 +01:00
|
|
|
match cur_def_key.disambiguated_data.data {
|
2019-03-24 17:49:58 +03:00
|
|
|
DefPathData::Ctor => {
|
2019-03-21 23:38:50 +01:00
|
|
|
let parent = DefId {
|
|
|
|
krate: def_id.krate,
|
|
|
|
index: cur_def_key.parent
|
2019-03-24 17:49:58 +03:00
|
|
|
.expect("DefPathData::Ctor/VariantData missing a parent"),
|
2019-03-21 23:38:50 +01:00
|
|
|
};
|
2019-01-18 21:33:31 +02:00
|
|
|
|
2019-03-21 23:38:50 +01:00
|
|
|
cur_def_key = self.tcx().def_key(parent);
|
|
|
|
},
|
|
|
|
_ => {},
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let visible_parent = match visible_parent_map.get(&def_id).cloned() {
|
|
|
|
Some(parent) => parent,
|
2019-01-25 12:11:50 +02:00
|
|
|
None => return Ok((self, false)),
|
2019-01-18 21:33:31 +02:00
|
|
|
};
|
2019-01-25 12:11:50 +02:00
|
|
|
// HACK(eddyb) this bypasses `path_append`'s prefix printing to avoid
|
|
|
|
// knowing ahead of time whether the entire path will succeed or not.
|
|
|
|
// To support printers that do not implement `PrettyPrinter`, a `Vec` or
|
|
|
|
// linked list on the stack would need to be built, before any printing.
|
|
|
|
match self.try_print_visible_def_path(visible_parent)? {
|
|
|
|
(cx, false) => return Ok((cx, false)),
|
|
|
|
(cx, true) => self = cx,
|
|
|
|
}
|
|
|
|
let actual_parent = self.tcx().parent(def_id);
|
2019-01-18 21:33:31 +02:00
|
|
|
debug!(
|
|
|
|
"try_print_visible_def_path: visible_parent={:?} actual_parent={:?}",
|
|
|
|
visible_parent, actual_parent,
|
|
|
|
);
|
|
|
|
|
2019-02-03 12:59:37 +02:00
|
|
|
let mut data = cur_def_key.disambiguated_data.data;
|
2019-01-18 21:33:31 +02:00
|
|
|
debug!(
|
|
|
|
"try_print_visible_def_path: data={:?} visible_parent={:?} actual_parent={:?}",
|
|
|
|
data, visible_parent, actual_parent,
|
|
|
|
);
|
|
|
|
|
2019-02-03 12:59:37 +02:00
|
|
|
match data {
|
2019-01-18 21:33:31 +02:00
|
|
|
// In order to output a path that could actually be imported (valid and visible),
|
|
|
|
// we need to handle re-exports correctly.
|
|
|
|
//
|
|
|
|
// For example, take `std::os::unix::process::CommandExt`, this trait is actually
|
|
|
|
// defined at `std::sys::unix::ext::process::CommandExt` (at time of writing).
|
|
|
|
//
|
|
|
|
// `std::os::unix` rexports the contents of `std::sys::unix::ext`. `std::sys` is
|
|
|
|
// private so the "true" path to `CommandExt` isn't accessible.
|
|
|
|
//
|
|
|
|
// In this case, the `visible_parent_map` will look something like this:
|
|
|
|
//
|
|
|
|
// (child) -> (parent)
|
|
|
|
// `std::sys::unix::ext::process::CommandExt` -> `std::sys::unix::ext::process`
|
|
|
|
// `std::sys::unix::ext::process` -> `std::sys::unix::ext`
|
|
|
|
// `std::sys::unix::ext` -> `std::os`
|
|
|
|
//
|
|
|
|
// This is correct, as the visible parent of `std::sys::unix::ext` is in fact
|
|
|
|
// `std::os`.
|
|
|
|
//
|
|
|
|
// When printing the path to `CommandExt` and looking at the `cur_def_key` that
|
|
|
|
// corresponds to `std::sys::unix::ext`, we would normally print `ext` and then go
|
|
|
|
// to the parent - resulting in a mangled path like
|
|
|
|
// `std::os::ext::process::CommandExt`.
|
|
|
|
//
|
|
|
|
// Instead, we must detect that there was a re-export and instead print `unix`
|
|
|
|
// (which is the name `std::sys::unix::ext` was re-exported as in `std::os`). To
|
|
|
|
// do this, we compare the parent of `std::sys::unix::ext` (`std::sys::unix`) with
|
|
|
|
// the visible parent (`std::os`). If these do not match, then we iterate over
|
|
|
|
// the children of the visible parent (as was done when computing
|
|
|
|
// `visible_parent_map`), looking for the specific child we currently have and then
|
|
|
|
// have access to the re-exported name.
|
2019-02-03 12:59:37 +02:00
|
|
|
DefPathData::TypeNs(ref mut name) if Some(visible_parent) != actual_parent => {
|
|
|
|
let reexport = self.tcx().item_children(visible_parent)
|
2019-01-18 21:33:31 +02:00
|
|
|
.iter()
|
2019-04-20 19:36:05 +03:00
|
|
|
.find(|child| child.res.def_id() == def_id)
|
2019-02-03 12:59:37 +02:00
|
|
|
.map(|child| child.ident.as_interned_str());
|
|
|
|
if let Some(reexport) = reexport {
|
|
|
|
*name = reexport;
|
|
|
|
}
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2019-02-03 12:59:37 +02:00
|
|
|
// Re-exported `extern crate` (#43189).
|
|
|
|
DefPathData::CrateRoot => {
|
2019-05-03 21:51:24 +03:00
|
|
|
data = DefPathData::TypeNs(
|
2019-02-03 12:59:37 +02:00
|
|
|
self.tcx().original_crate_name(def_id.krate).as_interned_str(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
debug!("try_print_visible_def_path: data={:?}", data);
|
|
|
|
|
|
|
|
Ok((self.path_append(Ok, &DisambiguatedDefPathData {
|
|
|
|
data,
|
|
|
|
disambiguator: 0,
|
|
|
|
})?, true))
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
fn pretty_path_qualified(
|
2019-01-18 21:33:31 +02:00
|
|
|
self,
|
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
trait_ref: Option<ty::TraitRef<'tcx>>,
|
2019-01-25 12:11:50 +02:00
|
|
|
) -> Result<Self::Path, Self::Error> {
|
2019-01-18 21:33:31 +02:00
|
|
|
if trait_ref.is_none() {
|
|
|
|
// Inherent impls. Try to print `Foo::bar` for an inherent
|
|
|
|
// impl on `Foo`, but fallback to `<Foo>::bar` if self-type is
|
|
|
|
// anything other than a simple path.
|
|
|
|
match self_ty.sty {
|
|
|
|
ty::Adt(..) | ty::Foreign(_) |
|
|
|
|
ty::Bool | ty::Char | ty::Str |
|
|
|
|
ty::Int(_) | ty::Uint(_) | ty::Float(_) => {
|
2019-01-19 03:25:51 +02:00
|
|
|
return self_ty.print(self);
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.generic_delimiters(|mut cx| {
|
|
|
|
define_scoped_cx!(cx);
|
|
|
|
|
2019-01-19 03:25:51 +02:00
|
|
|
p!(print(self_ty));
|
2019-01-18 21:33:31 +02:00
|
|
|
if let Some(trait_ref) = trait_ref {
|
2019-01-19 03:25:51 +02:00
|
|
|
p!(write(" as "), print(trait_ref));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
Ok(cx)
|
2019-01-18 21:33:31 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
fn pretty_path_append_impl(
|
2019-01-18 21:33:31 +02:00
|
|
|
mut self,
|
2019-01-25 12:11:50 +02:00
|
|
|
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
|
2019-01-18 21:33:31 +02:00
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
trait_ref: Option<ty::TraitRef<'tcx>>,
|
2019-01-25 12:11:50 +02:00
|
|
|
) -> Result<Self::Path, Self::Error> {
|
|
|
|
self = print_prefix(self)?;
|
2019-01-18 21:33:31 +02:00
|
|
|
|
|
|
|
self.generic_delimiters(|mut cx| {
|
|
|
|
define_scoped_cx!(cx);
|
|
|
|
|
|
|
|
p!(write("impl "));
|
|
|
|
if let Some(trait_ref) = trait_ref {
|
2019-01-19 03:25:51 +02:00
|
|
|
p!(print(trait_ref), write(" for "));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2019-01-19 03:25:51 +02:00
|
|
|
p!(print(self_ty));
|
2019-01-18 21:33:31 +02:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
Ok(cx)
|
2019-01-18 21:33:31 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
fn pretty_print_type(
|
|
|
|
mut self,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Result<Self::Type, Self::Error> {
|
2019-01-25 02:34:19 +02:00
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
match ty.sty {
|
|
|
|
ty::Bool => p!(write("bool")),
|
|
|
|
ty::Char => p!(write("char")),
|
|
|
|
ty::Int(t) => p!(write("{}", t.ty_to_string())),
|
|
|
|
ty::Uint(t) => p!(write("{}", t.ty_to_string())),
|
|
|
|
ty::Float(t) => p!(write("{}", t.ty_to_string())),
|
|
|
|
ty::RawPtr(ref tm) => {
|
|
|
|
p!(write("*{} ", match tm.mutbl {
|
|
|
|
hir::MutMutable => "mut",
|
|
|
|
hir::MutImmutable => "const",
|
|
|
|
}));
|
|
|
|
p!(print(tm.ty))
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::Ref(r, ty, mutbl) => {
|
|
|
|
p!(write("&"));
|
|
|
|
if self.region_should_not_be_omitted(r) {
|
|
|
|
p!(print(r), write(" "));
|
|
|
|
}
|
|
|
|
p!(print(ty::TypeAndMut { ty, mutbl }))
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::Never => p!(write("!")),
|
|
|
|
ty::Tuple(ref tys) => {
|
|
|
|
p!(write("("));
|
|
|
|
let mut tys = tys.iter();
|
|
|
|
if let Some(&ty) = tys.next() {
|
|
|
|
p!(print(ty), write(","));
|
|
|
|
if let Some(&ty) = tys.next() {
|
|
|
|
p!(write(" "), print(ty));
|
|
|
|
for &ty in tys {
|
|
|
|
p!(write(", "), print(ty));
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
p!(write(")"))
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::FnDef(def_id, substs) => {
|
|
|
|
let sig = self.tcx().fn_sig(def_id).subst(self.tcx(), substs);
|
2019-01-29 07:21:11 +02:00
|
|
|
p!(print(sig), write(" {{"), print_value_path(def_id, substs), write("}}"));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::FnPtr(ref bare_fn) => {
|
|
|
|
p!(print(bare_fn))
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::Infer(infer_ty) => p!(write("{}", infer_ty)),
|
|
|
|
ty::Error => p!(write("[type error]")),
|
|
|
|
ty::Param(ref param_ty) => p!(write("{}", param_ty)),
|
|
|
|
ty::Bound(debruijn, bound_ty) => {
|
|
|
|
match bound_ty.kind {
|
|
|
|
ty::BoundTyKind::Anon => {
|
|
|
|
if debruijn == ty::INNERMOST {
|
|
|
|
p!(write("^{}", bound_ty.var.index()))
|
|
|
|
} else {
|
|
|
|
p!(write("^{}_{}", debruijn.index(), bound_ty.var.index()))
|
|
|
|
}
|
|
|
|
}
|
2019-01-24 20:47:02 +02:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::BoundTyKind::Param(p) => p!(write("{}", p)),
|
|
|
|
}
|
2019-01-24 19:52:43 +02:00
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::Adt(def, substs) => {
|
2019-01-29 07:21:11 +02:00
|
|
|
p!(print_def_path(def.did, substs));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
ty::Dynamic(data, r) => {
|
2019-01-23 19:36:39 +02:00
|
|
|
let print_r = self.region_should_not_be_omitted(r);
|
2019-01-18 21:33:31 +02:00
|
|
|
if print_r {
|
|
|
|
p!(write("("));
|
|
|
|
}
|
|
|
|
p!(write("dyn "), print(data));
|
|
|
|
if print_r {
|
2019-01-19 03:25:51 +02:00
|
|
|
p!(write(" + "), print(r), write(")"));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::Foreign(def_id) => {
|
2019-01-29 07:21:11 +02:00
|
|
|
p!(print_def_path(def_id, &[]));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
ty::Projection(ref data) => p!(print(data)),
|
|
|
|
ty::UnnormalizedProjection(ref data) => {
|
|
|
|
p!(write("Unnormalized("), print(data), write(")"))
|
|
|
|
}
|
|
|
|
ty::Placeholder(placeholder) => {
|
|
|
|
p!(write("Placeholder({:?})", placeholder))
|
|
|
|
}
|
|
|
|
ty::Opaque(def_id, substs) => {
|
2019-01-19 03:25:51 +02:00
|
|
|
// FIXME(eddyb) print this with `print_def_path`.
|
2019-01-25 12:11:50 +02:00
|
|
|
if self.tcx().sess.verbose() {
|
2019-01-18 21:33:31 +02:00
|
|
|
p!(write("Opaque({:?}, {:?})", def_id, substs));
|
2019-01-25 12:11:50 +02:00
|
|
|
return Ok(self);
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
let def_key = self.tcx().def_key(def_id);
|
2019-01-18 21:33:31 +02:00
|
|
|
if let Some(name) = def_key.disambiguated_data.data.get_opt_name() {
|
|
|
|
p!(write("{}", name));
|
|
|
|
let mut substs = substs.iter();
|
|
|
|
// FIXME(eddyb) print this with `print_def_path`.
|
|
|
|
if let Some(first) = substs.next() {
|
|
|
|
p!(write("::<"));
|
2019-01-19 03:25:51 +02:00
|
|
|
p!(print(first));
|
2019-01-18 21:33:31 +02:00
|
|
|
for subst in substs {
|
2019-01-19 03:25:51 +02:00
|
|
|
p!(write(", "), print(subst));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
p!(write(">"));
|
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
return Ok(self);
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
// Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
|
|
|
|
// by looking up the projections associated with the def_id.
|
2019-01-25 12:11:50 +02:00
|
|
|
let bounds = self.tcx().predicates_of(def_id).instantiate(self.tcx(), substs);
|
2019-01-18 21:33:31 +02:00
|
|
|
|
|
|
|
let mut first = true;
|
|
|
|
let mut is_sized = false;
|
|
|
|
p!(write("impl"));
|
|
|
|
for predicate in bounds.predicates {
|
|
|
|
if let Some(trait_ref) = predicate.to_opt_poly_trait_ref() {
|
|
|
|
// Don't print +Sized, but rather +?Sized if absent.
|
2019-01-25 12:11:50 +02:00
|
|
|
if Some(trait_ref.def_id()) == self.tcx().lang_items().sized_trait() {
|
2019-01-18 21:33:31 +02:00
|
|
|
is_sized = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
p!(
|
|
|
|
write("{}", if first { " " } else { "+" }),
|
|
|
|
print(trait_ref));
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !is_sized {
|
|
|
|
p!(write("{}?Sized", if first { " " } else { "+" }));
|
|
|
|
} else if first {
|
|
|
|
p!(write(" Sized"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::Str => p!(write("str")),
|
|
|
|
ty::Generator(did, substs, movability) => {
|
2019-01-25 12:11:50 +02:00
|
|
|
let upvar_tys = substs.upvar_tys(did, self.tcx());
|
|
|
|
let witness = substs.witness(did, self.tcx());
|
2019-01-18 21:33:31 +02:00
|
|
|
if movability == hir::GeneratorMovability::Movable {
|
|
|
|
p!(write("[generator"));
|
|
|
|
} else {
|
|
|
|
p!(write("[static generator"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(eddyb) should use `def_span`.
|
2019-01-25 12:11:50 +02:00
|
|
|
if let Some(hir_id) = self.tcx().hir().as_local_hir_id(did) {
|
|
|
|
p!(write("@{:?}", self.tcx().hir().span_by_hir_id(hir_id)));
|
2019-01-18 21:33:31 +02:00
|
|
|
let mut sep = " ";
|
2019-01-25 12:11:50 +02:00
|
|
|
for (freevar, upvar_ty) in self.tcx().freevars(did)
|
2019-01-18 21:33:31 +02:00
|
|
|
.as_ref()
|
|
|
|
.map_or(&[][..], |fv| &fv[..])
|
|
|
|
.iter()
|
|
|
|
.zip(upvar_tys)
|
|
|
|
{
|
|
|
|
p!(
|
|
|
|
write("{}{}:",
|
|
|
|
sep,
|
2019-04-03 09:07:45 +02:00
|
|
|
self.tcx().hir().name_by_hir_id(freevar.var_id())),
|
2019-01-18 21:33:31 +02:00
|
|
|
print(upvar_ty));
|
|
|
|
sep = ", ";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// cross-crate closure types should only be
|
|
|
|
// visible in codegen bug reports, I imagine.
|
|
|
|
p!(write("@{:?}", did));
|
|
|
|
let mut sep = " ";
|
|
|
|
for (index, upvar_ty) in upvar_tys.enumerate() {
|
|
|
|
p!(
|
|
|
|
write("{}{}:", sep, index),
|
|
|
|
print(upvar_ty));
|
|
|
|
sep = ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p!(write(" "), print(witness), write("]"))
|
|
|
|
},
|
|
|
|
ty::GeneratorWitness(types) => {
|
2019-01-25 12:28:43 +02:00
|
|
|
p!(in_binder(&types));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
ty::Closure(did, substs) => {
|
2019-01-25 12:11:50 +02:00
|
|
|
let upvar_tys = substs.upvar_tys(did, self.tcx());
|
2019-01-18 21:33:31 +02:00
|
|
|
p!(write("[closure"));
|
|
|
|
|
|
|
|
// FIXME(eddyb) should use `def_span`.
|
2019-01-25 12:11:50 +02:00
|
|
|
if let Some(hir_id) = self.tcx().hir().as_local_hir_id(did) {
|
|
|
|
if self.tcx().sess.opts.debugging_opts.span_free_formats {
|
2019-01-18 21:33:31 +02:00
|
|
|
p!(write("@{:?}", hir_id));
|
|
|
|
} else {
|
2019-01-25 12:11:50 +02:00
|
|
|
p!(write("@{:?}", self.tcx().hir().span_by_hir_id(hir_id)));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
let mut sep = " ";
|
2019-01-25 12:11:50 +02:00
|
|
|
for (freevar, upvar_ty) in self.tcx().freevars(did)
|
2019-01-18 21:33:31 +02:00
|
|
|
.as_ref()
|
|
|
|
.map_or(&[][..], |fv| &fv[..])
|
|
|
|
.iter()
|
|
|
|
.zip(upvar_tys)
|
|
|
|
{
|
|
|
|
p!(
|
|
|
|
write("{}{}:",
|
|
|
|
sep,
|
2019-04-03 09:07:45 +02:00
|
|
|
self.tcx().hir().name_by_hir_id(freevar.var_id())),
|
2019-01-18 21:33:31 +02:00
|
|
|
print(upvar_ty));
|
|
|
|
sep = ", ";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// cross-crate closure types should only be
|
|
|
|
// visible in codegen bug reports, I imagine.
|
|
|
|
p!(write("@{:?}", did));
|
|
|
|
let mut sep = " ";
|
|
|
|
for (index, upvar_ty) in upvar_tys.enumerate() {
|
|
|
|
p!(
|
|
|
|
write("{}{}:", sep, index),
|
|
|
|
print(upvar_ty));
|
|
|
|
sep = ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
if self.tcx().sess.verbose() {
|
2019-01-18 21:33:31 +02:00
|
|
|
p!(write(
|
|
|
|
" closure_kind_ty={:?} closure_sig_ty={:?}",
|
2019-01-25 12:11:50 +02:00
|
|
|
substs.closure_kind_ty(did, self.tcx()),
|
|
|
|
substs.closure_sig_ty(did, self.tcx())
|
2019-01-18 21:33:31 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
p!(write("]"))
|
|
|
|
},
|
|
|
|
ty::Array(ty, sz) => {
|
|
|
|
p!(write("["), print(ty), write("; "));
|
2019-03-14 10:19:31 +01:00
|
|
|
match sz.val {
|
|
|
|
ConstValue::Unevaluated(..) |
|
|
|
|
ConstValue::Infer(..) => p!(write("_")),
|
|
|
|
ConstValue::Param(ParamConst { name, .. }) =>
|
|
|
|
p!(write("{}", name)),
|
|
|
|
_ => p!(write("{}", sz.unwrap_usize(self.tcx()))),
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
p!(write("]"))
|
|
|
|
}
|
|
|
|
ty::Slice(ty) => {
|
|
|
|
p!(write("["), print(ty), write("]"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
Ok(self)
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:47:02 +02:00
|
|
|
fn pretty_print_dyn_existential(
|
|
|
|
mut self,
|
|
|
|
predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
|
2019-01-25 12:11:50 +02:00
|
|
|
) -> Result<Self::DynExistential, Self::Error> {
|
2019-01-24 20:47:02 +02:00
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
|
|
|
// Generate the main trait ref, including associated types.
|
|
|
|
let mut first = true;
|
|
|
|
|
|
|
|
if let Some(principal) = predicates.principal() {
|
2019-01-29 07:21:11 +02:00
|
|
|
p!(print_def_path(principal.def_id, &[]));
|
2019-01-24 20:47:02 +02:00
|
|
|
|
|
|
|
let mut resugared = false;
|
|
|
|
|
|
|
|
// Special-case `Fn(...) -> ...` and resugar it.
|
2019-01-25 12:11:50 +02:00
|
|
|
let fn_trait_kind = self.tcx().lang_items().fn_trait_kind(principal.def_id);
|
|
|
|
if !self.tcx().sess.verbose() && fn_trait_kind.is_some() {
|
2019-01-24 20:47:02 +02:00
|
|
|
if let ty::Tuple(ref args) = principal.substs.type_at(0).sty {
|
|
|
|
let mut projections = predicates.projection_bounds();
|
|
|
|
if let (Some(proj), None) = (projections.next(), projections.next()) {
|
2019-04-26 00:27:33 +01:00
|
|
|
let tys: Vec<_> = args.iter().map(|k| k.expect_ty()).collect();
|
|
|
|
p!(pretty_fn_sig(&tys, false, proj.ty));
|
2019-01-24 20:47:02 +02:00
|
|
|
resugared = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK(eddyb) this duplicates `FmtPrinter`'s `path_generic_args`,
|
|
|
|
// in order to place the projections inside the `<...>`.
|
|
|
|
if !resugared {
|
|
|
|
// Use a type that can't appear in defaults of type parameters.
|
2019-03-18 20:55:19 +00:00
|
|
|
let dummy_self = self.tcx().mk_ty_infer(ty::FreshTy(0));
|
2019-01-25 12:11:50 +02:00
|
|
|
let principal = principal.with_self_ty(self.tcx(), dummy_self);
|
2019-01-24 20:47:02 +02:00
|
|
|
|
|
|
|
let args = self.generic_args_to_print(
|
2019-01-25 12:11:50 +02:00
|
|
|
self.tcx().generics_of(principal.def_id),
|
2019-01-24 20:47:02 +02:00
|
|
|
principal.substs,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Don't print `'_` if there's no unerased regions.
|
|
|
|
let print_regions = args.iter().any(|arg| {
|
|
|
|
match arg.unpack() {
|
|
|
|
UnpackedKind::Lifetime(r) => *r != ty::ReErased,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let mut args = args.iter().cloned().filter(|arg| {
|
|
|
|
match arg.unpack() {
|
|
|
|
UnpackedKind::Lifetime(_) => print_regions,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let mut projections = predicates.projection_bounds();
|
|
|
|
|
|
|
|
let arg0 = args.next();
|
|
|
|
let projection0 = projections.next();
|
|
|
|
if arg0.is_some() || projection0.is_some() {
|
|
|
|
let args = arg0.into_iter().chain(args);
|
|
|
|
let projections = projection0.into_iter().chain(projections);
|
|
|
|
|
2019-01-25 12:28:43 +02:00
|
|
|
p!(generic_delimiters(|mut cx| {
|
2019-01-25 12:11:50 +02:00
|
|
|
cx = cx.comma_sep(args)?;
|
2019-01-24 20:47:02 +02:00
|
|
|
if arg0.is_some() && projection0.is_some() {
|
|
|
|
write!(cx, ", ")?;
|
|
|
|
}
|
|
|
|
cx.comma_sep(projections)
|
2019-01-25 12:28:43 +02:00
|
|
|
}));
|
2019-01-24 20:47:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Builtin bounds.
|
|
|
|
// FIXME(eddyb) avoid printing twice (needed to ensure
|
|
|
|
// that the auto traits are sorted *and* printed via cx).
|
|
|
|
let mut auto_traits: Vec<_> = predicates.auto_traits().map(|did| {
|
2019-01-25 12:11:50 +02:00
|
|
|
(self.tcx().def_path_str(did), did)
|
2019-01-24 20:47:02 +02:00
|
|
|
}).collect();
|
|
|
|
|
|
|
|
// The auto traits come ordered by `DefPathHash`. While
|
|
|
|
// `DefPathHash` is *stable* in the sense that it depends on
|
|
|
|
// neither the host nor the phase of the moon, it depends
|
|
|
|
// "pseudorandomly" on the compiler version and the target.
|
|
|
|
//
|
|
|
|
// To avoid that causing instabilities in compiletest
|
|
|
|
// output, sort the auto-traits alphabetically.
|
|
|
|
auto_traits.sort();
|
|
|
|
|
|
|
|
for (_, def_id) in auto_traits {
|
|
|
|
if !first {
|
|
|
|
p!(write(" + "));
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
|
2019-01-29 07:21:11 +02:00
|
|
|
p!(print_def_path(def_id, &[]));
|
2019-01-24 20:47:02 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
Ok(self)
|
2019-01-24 20:47:02 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
fn pretty_fn_sig(
|
2019-01-18 21:33:31 +02:00
|
|
|
mut self,
|
|
|
|
inputs: &[Ty<'tcx>],
|
|
|
|
c_variadic: bool,
|
|
|
|
output: Ty<'tcx>,
|
2019-01-25 12:11:50 +02:00
|
|
|
) -> Result<Self, Self::Error> {
|
2019-01-18 21:33:31 +02:00
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
|
|
|
p!(write("("));
|
|
|
|
let mut inputs = inputs.iter();
|
|
|
|
if let Some(&ty) = inputs.next() {
|
2019-01-19 03:25:51 +02:00
|
|
|
p!(print(ty));
|
2019-01-18 21:33:31 +02:00
|
|
|
for &ty in inputs {
|
2019-01-19 03:25:51 +02:00
|
|
|
p!(write(", "), print(ty));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
if c_variadic {
|
|
|
|
p!(write(", ..."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p!(write(")"));
|
|
|
|
if !output.is_unit() {
|
2019-01-19 03:25:51 +02:00
|
|
|
p!(write(" -> "), print(output));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
Ok(self)
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2019-01-20 19:46:47 +02:00
|
|
|
}
|
2019-01-18 21:33:31 +02:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
// HACK(eddyb) boxed to avoid moving around a large struct by-value.
|
|
|
|
pub struct FmtPrinter<'a, 'gcx, 'tcx, F>(Box<FmtPrinterData<'a, 'gcx, 'tcx, F>>);
|
2019-01-18 21:33:31 +02:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
pub struct FmtPrinterData<'a, 'gcx, 'tcx, F> {
|
|
|
|
tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
fmt: F,
|
|
|
|
|
|
|
|
empty_path: 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<'a, 'gcx, 'tcx, F> {
|
|
|
|
type Target = FmtPrinterData<'a, 'gcx, 'tcx, 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<'a, 'gcx, 'tcx, F> {
|
|
|
|
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>, fmt: F, ns: Namespace) -> Self {
|
|
|
|
FmtPrinter(Box::new(FmtPrinterData {
|
|
|
|
tcx,
|
|
|
|
fmt,
|
|
|
|
empty_path: false,
|
|
|
|
in_value: ns == Namespace::ValueNS,
|
|
|
|
used_region_names: Default::default(),
|
|
|
|
region_index: 0,
|
|
|
|
binder_depth: 0,
|
|
|
|
region_highlight_mode: RegionHighlightMode::default(),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TyCtxt<'_, '_, '_> {
|
|
|
|
// 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?)
|
|
|
|
fn guess_def_namespace(self, def_id: DefId) -> Namespace {
|
|
|
|
match self.def_key(def_id).disambiguated_data.data {
|
|
|
|
DefPathData::ValueNs(..) |
|
|
|
|
DefPathData::AnonConst |
|
|
|
|
DefPathData::ClosureExpr |
|
2019-03-24 17:49:58 +03:00
|
|
|
DefPathData::Ctor => Namespace::ValueNS,
|
2019-01-25 12:11:50 +02:00
|
|
|
|
|
|
|
DefPathData::MacroDef(..) => Namespace::MacroNS,
|
|
|
|
|
|
|
|
_ => Namespace::TypeNS,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a string identifying this `DefId`. This string is
|
|
|
|
/// suitable for user output.
|
|
|
|
pub fn def_path_str(self, def_id: DefId) -> String {
|
|
|
|
let ns = self.guess_def_namespace(def_id);
|
|
|
|
debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns);
|
|
|
|
let mut s = String::new();
|
|
|
|
let _ = FmtPrinter::new(self, &mut s, ns)
|
2019-01-29 07:21:11 +02:00
|
|
|
.print_def_path(def_id, &[]);
|
2019-01-25 12:11:50 +02:00
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: fmt::Write> fmt::Write for FmtPrinter<'_, '_, '_, F> {
|
|
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
|
|
|
self.fmt.write_str(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> {
|
|
|
|
type Error = fmt::Error;
|
|
|
|
|
|
|
|
type Path = Self;
|
|
|
|
type Region = Self;
|
|
|
|
type Type = Self;
|
|
|
|
type DynExistential = Self;
|
|
|
|
|
|
|
|
fn tcx(&'a self) -> TyCtxt<'a, 'gcx, 'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_def_path(
|
|
|
|
mut self,
|
|
|
|
def_id: DefId,
|
2019-01-29 07:21:11 +02:00
|
|
|
substs: &'tcx [Kind<'tcx>],
|
2019-01-25 12:11:50 +02:00
|
|
|
) -> Result<Self::Path, Self::Error> {
|
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
2019-01-29 07:21:11 +02:00
|
|
|
if substs.is_empty() {
|
2019-01-25 12:11:50 +02:00
|
|
|
match self.try_print_visible_def_path(def_id)? {
|
2019-01-29 07:21:11 +02:00
|
|
|
(cx, true) => return Ok(cx),
|
2019-01-25 12:11:50 +02:00
|
|
|
(cx, false) => self = cx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let key = self.tcx.def_key(def_id);
|
|
|
|
if let DefPathData::Impl = key.disambiguated_data.data {
|
|
|
|
// Always use types for non-local impls, where types are always
|
|
|
|
// available, and filename/line-number is mostly uninteresting.
|
|
|
|
let use_types =
|
|
|
|
!def_id.is_local() || {
|
|
|
|
// Otherwise, use filename/line-number if forced.
|
|
|
|
let force_no_types = FORCE_IMPL_FILENAME_LINE.with(|f| f.get());
|
|
|
|
!force_no_types
|
|
|
|
};
|
|
|
|
|
|
|
|
if !use_types {
|
|
|
|
// If no type info is available, fall back to
|
|
|
|
// pretty printing some span information. This should
|
|
|
|
// only occur very early in the compiler pipeline.
|
|
|
|
let parent_def_id = DefId { index: key.parent.unwrap(), ..def_id };
|
|
|
|
let span = self.tcx.def_span(def_id);
|
2019-02-03 12:59:37 +02:00
|
|
|
|
|
|
|
self = self.print_def_path(parent_def_id, &[])?;
|
|
|
|
|
|
|
|
// HACK(eddyb) copy of `path_append` to avoid
|
|
|
|
// constructing a `DisambiguatedDefPathData`.
|
|
|
|
if !self.empty_path {
|
|
|
|
write!(self, "::")?;
|
|
|
|
}
|
|
|
|
write!(self, "<impl at {:?}>", span)?;
|
|
|
|
self.empty_path = false;
|
|
|
|
|
|
|
|
return Ok(self);
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.default_print_def_path(def_id, substs)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_region(
|
|
|
|
self,
|
|
|
|
region: ty::Region<'_>,
|
|
|
|
) -> Result<Self::Region, Self::Error> {
|
|
|
|
self.pretty_print_region(region)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_type(
|
|
|
|
self,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Result<Self::Type, Self::Error> {
|
|
|
|
self.pretty_print_type(ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_dyn_existential(
|
|
|
|
self,
|
|
|
|
predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
|
|
|
|
) -> Result<Self::DynExistential, Self::Error> {
|
|
|
|
self.pretty_print_dyn_existential(predicates)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path_crate(
|
|
|
|
mut self,
|
|
|
|
cnum: CrateNum,
|
|
|
|
) -> Result<Self::Path, Self::Error> {
|
|
|
|
self.empty_path = true;
|
|
|
|
if cnum == LOCAL_CRATE {
|
|
|
|
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, "{}", keywords::Crate.name())?;
|
|
|
|
self.empty_path = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
write!(self, "{}", self.tcx.crate_name(cnum))?;
|
|
|
|
self.empty_path = false;
|
|
|
|
}
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
fn path_qualified(
|
|
|
|
mut self,
|
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
trait_ref: Option<ty::TraitRef<'tcx>>,
|
|
|
|
) -> Result<Self::Path, Self::Error> {
|
|
|
|
self = self.pretty_path_qualified(self_ty, trait_ref)?;
|
|
|
|
self.empty_path = false;
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path_append_impl(
|
|
|
|
mut self,
|
|
|
|
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
|
2019-02-03 12:59:37 +02:00
|
|
|
_disambiguated_data: &DisambiguatedDefPathData,
|
2019-01-25 12:11:50 +02:00
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
trait_ref: Option<ty::TraitRef<'tcx>>,
|
|
|
|
) -> Result<Self::Path, Self::Error> {
|
|
|
|
self = self.pretty_path_append_impl(|mut cx| {
|
|
|
|
cx = print_prefix(cx)?;
|
|
|
|
if !cx.empty_path {
|
|
|
|
write!(cx, "::")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(cx)
|
|
|
|
}, self_ty, trait_ref)?;
|
|
|
|
self.empty_path = false;
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
fn path_append(
|
|
|
|
mut self,
|
|
|
|
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
|
2019-02-03 12:59:37 +02:00
|
|
|
disambiguated_data: &DisambiguatedDefPathData,
|
2019-01-25 12:11:50 +02:00
|
|
|
) -> Result<Self::Path, Self::Error> {
|
|
|
|
self = print_prefix(self)?;
|
|
|
|
|
2019-02-03 12:59:37 +02:00
|
|
|
// Skip `::{{constructor}}` on tuple/unit structs.
|
|
|
|
match disambiguated_data.data {
|
2019-03-24 17:49:58 +03:00
|
|
|
DefPathData::Ctor => return Ok(self),
|
2019-02-03 12:59:37 +02:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(eddyb) `name` should never be empty, but it
|
2019-01-25 12:11:50 +02:00
|
|
|
// currently is for `extern { ... }` "foreign modules".
|
2019-02-03 12:59:37 +02:00
|
|
|
let name = disambiguated_data.data.as_interned_str().as_str();
|
|
|
|
if !name.is_empty() {
|
2019-01-25 12:11:50 +02:00
|
|
|
if !self.empty_path {
|
|
|
|
write!(self, "::")?;
|
|
|
|
}
|
2019-02-03 12:59:37 +02:00
|
|
|
write!(self, "{}", name)?;
|
|
|
|
|
|
|
|
// FIXME(eddyb) this will print e.g. `{{closure}}#3`, but it
|
|
|
|
// might be nicer to use something else, e.g. `{closure#3}`.
|
|
|
|
let dis = disambiguated_data.disambiguator;
|
|
|
|
let print_dis =
|
|
|
|
disambiguated_data.data.get_opt_name().is_none() ||
|
|
|
|
dis != 0 && self.tcx.sess.verbose();
|
|
|
|
if print_dis {
|
|
|
|
write!(self, "#{}", dis)?;
|
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
self.empty_path = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
fn path_generic_args(
|
|
|
|
mut self,
|
|
|
|
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
|
|
|
|
args: &[Kind<'tcx>],
|
|
|
|
) -> Result<Self::Path, Self::Error> {
|
|
|
|
self = print_prefix(self)?;
|
|
|
|
|
|
|
|
// Don't print `'_` if there's no unerased regions.
|
|
|
|
let print_regions = args.iter().any(|arg| {
|
|
|
|
match arg.unpack() {
|
|
|
|
UnpackedKind::Lifetime(r) => *r != ty::ReErased,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let args = args.iter().cloned().filter(|arg| {
|
|
|
|
match arg.unpack() {
|
|
|
|
UnpackedKind::Lifetime(_) => print_regions,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if args.clone().next().is_some() {
|
|
|
|
if self.in_value {
|
|
|
|
write!(self, "::")?;
|
|
|
|
}
|
|
|
|
self.generic_delimiters(|cx| cx.comma_sep(args))
|
|
|
|
} else {
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: fmt::Write> PrettyPrinter<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> {
|
|
|
|
fn print_value_path(
|
|
|
|
mut self,
|
|
|
|
def_id: DefId,
|
2019-01-29 07:21:11 +02:00
|
|
|
substs: &'tcx [Kind<'tcx>],
|
2019-01-25 12:11:50 +02:00
|
|
|
) -> Result<Self::Path, Self::Error> {
|
|
|
|
let was_in_value = std::mem::replace(&mut self.in_value, true);
|
|
|
|
self = self.print_def_path(def_id, substs)?;
|
|
|
|
self.in_value = was_in_value;
|
|
|
|
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn in_binder<T>(
|
|
|
|
self,
|
|
|
|
value: &ty::Binder<T>,
|
|
|
|
) -> Result<Self, Self::Error>
|
|
|
|
where T: Print<'gcx, 'tcx, Self, Output = Self, Error = Self::Error> + TypeFoldable<'tcx>
|
|
|
|
{
|
|
|
|
self.pretty_in_binder(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generic_delimiters(
|
|
|
|
mut self,
|
|
|
|
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
|
|
|
|
) -> Result<Self, Self::Error> {
|
|
|
|
write!(self, "<")?;
|
|
|
|
|
|
|
|
let was_in_value = std::mem::replace(&mut self.in_value, false);
|
|
|
|
let mut inner = f(self)?;
|
|
|
|
inner.in_value = was_in_value;
|
|
|
|
|
|
|
|
write!(inner, ">")?;
|
|
|
|
Ok(inner)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn region_should_not_be_omitted(
|
|
|
|
&self,
|
|
|
|
region: ty::Region<'_>,
|
|
|
|
) -> bool {
|
|
|
|
let highlight = self.region_highlight_mode;
|
|
|
|
if highlight.region_highlighted(region).is_some() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.tcx.sess.verbose() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let identify_regions = self.tcx.sess.opts.debugging_opts.identify_regions;
|
|
|
|
|
|
|
|
match *region {
|
|
|
|
ty::ReEarlyBound(ref data) => {
|
|
|
|
data.name != "" && data.name != "'_"
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::ReLateBound(_, br) |
|
|
|
|
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
|
|
|
|
ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
|
|
|
|
if let ty::BrNamed(_, name) = br {
|
|
|
|
if name != "" && name != "'_" {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((region, _)) = highlight.highlight_bound_region {
|
|
|
|
if br == region {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::ReScope(_) |
|
|
|
|
ty::ReVar(_) if identify_regions => true,
|
|
|
|
|
|
|
|
ty::ReVar(_) |
|
|
|
|
ty::ReScope(_) |
|
|
|
|
ty::ReErased => false,
|
|
|
|
|
|
|
|
ty::ReStatic |
|
|
|
|
ty::ReEmpty |
|
|
|
|
ty::ReClosureBound(_) => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK(eddyb) limited to `FmtPrinter` because of `region_highlight_mode`.
|
|
|
|
impl<F: fmt::Write> FmtPrinter<'_, '_, '_, F> {
|
|
|
|
pub fn pretty_print_region(
|
|
|
|
mut self,
|
|
|
|
region: ty::Region<'_>,
|
|
|
|
) -> Result<Self, fmt::Error> {
|
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
|
|
|
// Watch out for region highlights.
|
|
|
|
let highlight = self.region_highlight_mode;
|
|
|
|
if let Some(n) = highlight.region_highlighted(region) {
|
|
|
|
p!(write("'{}", n));
|
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.tcx.sess.verbose() {
|
|
|
|
p!(write("{:?}", region));
|
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
let identify_regions = self.tcx.sess.opts.debugging_opts.identify_regions;
|
|
|
|
|
|
|
|
// These printouts are concise. They do not contain all the information
|
|
|
|
// the user might want to diagnose an error, but there is basically no way
|
|
|
|
// to fit that into a short string. Hence the recommendation to use
|
|
|
|
// `explain_region()` or `note_and_explain_region()`.
|
|
|
|
match *region {
|
|
|
|
ty::ReEarlyBound(ref data) => {
|
|
|
|
if data.name != "" {
|
|
|
|
p!(write("{}", data.name));
|
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::ReLateBound(_, br) |
|
|
|
|
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
|
|
|
|
ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
|
|
|
|
if let ty::BrNamed(_, name) = br {
|
|
|
|
if name != "" && name != "'_" {
|
|
|
|
p!(write("{}", name));
|
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((region, counter)) = highlight.highlight_bound_region {
|
|
|
|
if br == region {
|
|
|
|
p!(write("'{}", counter));
|
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::ReScope(scope) if identify_regions => {
|
|
|
|
match scope.data {
|
|
|
|
region::ScopeData::Node =>
|
|
|
|
p!(write("'{}s", scope.item_local_id().as_usize())),
|
|
|
|
region::ScopeData::CallSite =>
|
|
|
|
p!(write("'{}cs", scope.item_local_id().as_usize())),
|
|
|
|
region::ScopeData::Arguments =>
|
|
|
|
p!(write("'{}as", scope.item_local_id().as_usize())),
|
|
|
|
region::ScopeData::Destruction =>
|
|
|
|
p!(write("'{}ds", scope.item_local_id().as_usize())),
|
|
|
|
region::ScopeData::Remainder(first_statement_index) => p!(write(
|
|
|
|
"'{}_{}rs",
|
|
|
|
scope.item_local_id().as_usize(),
|
|
|
|
first_statement_index.index()
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
ty::ReVar(region_vid) if identify_regions => {
|
|
|
|
p!(write("{:?}", region_vid));
|
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
ty::ReVar(_) => {}
|
|
|
|
ty::ReScope(_) |
|
|
|
|
ty::ReErased => {}
|
|
|
|
ty::ReStatic => {
|
|
|
|
p!(write("'static"));
|
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
ty::ReEmpty => {
|
|
|
|
p!(write("'<empty>"));
|
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The user should never encounter these in unsubstituted form.
|
|
|
|
ty::ReClosureBound(vid) => {
|
|
|
|
p!(write("{:?}", vid));
|
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p!(write("'_"));
|
|
|
|
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK(eddyb) limited to `FmtPrinter` because of `binder_depth`,
|
|
|
|
// `region_index` and `used_region_names`.
|
|
|
|
impl<F: fmt::Write> FmtPrinter<'_, 'gcx, 'tcx, F> {
|
|
|
|
pub fn pretty_in_binder<T>(
|
|
|
|
mut self,
|
|
|
|
value: &ty::Binder<T>,
|
|
|
|
) -> Result<Self, fmt::Error>
|
|
|
|
where T: Print<'gcx, 'tcx, Self, Output = Self, Error = fmt::Error> + TypeFoldable<'tcx>
|
|
|
|
{
|
|
|
|
fn name_by_region_index(index: usize) -> InternedString {
|
|
|
|
match index {
|
|
|
|
0 => Symbol::intern("'r"),
|
|
|
|
1 => Symbol::intern("'s"),
|
|
|
|
i => Symbol::intern(&format!("'t{}", i-2)),
|
|
|
|
}.as_interned_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace any anonymous late-bound regions with named
|
|
|
|
// variants, using gensym'd identifiers, so that we can
|
|
|
|
// clearly differentiate between named and unnamed regions in
|
2019-01-18 21:33:31 +02:00
|
|
|
// the output. We'll probably want to tweak this over time to
|
|
|
|
// decide just how much information to give.
|
2019-01-20 19:46:47 +02:00
|
|
|
if self.binder_depth == 0 {
|
2019-01-18 21:33:31 +02:00
|
|
|
self.prepare_late_bound_region_info(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut empty = true;
|
|
|
|
let mut start_or_continue = |cx: &mut Self, start: &str, cont: &str| {
|
2019-01-20 19:46:47 +02:00
|
|
|
write!(cx, "{}", if empty {
|
2019-01-18 21:33:31 +02:00
|
|
|
empty = false;
|
|
|
|
start
|
|
|
|
} else {
|
|
|
|
cont
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
2019-01-20 19:46:47 +02:00
|
|
|
let old_region_index = self.region_index;
|
2019-01-18 21:33:31 +02:00
|
|
|
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) => {
|
2019-01-20 19:46:47 +02:00
|
|
|
let _ = write!(self, "{}", name);
|
2019-01-18 21:33:31 +02:00
|
|
|
br
|
|
|
|
}
|
|
|
|
ty::BrAnon(_) |
|
|
|
|
ty::BrFresh(_) |
|
|
|
|
ty::BrEnv => {
|
|
|
|
let name = loop {
|
|
|
|
let name = name_by_region_index(region_index);
|
|
|
|
region_index += 1;
|
2019-01-20 19:46:47 +02:00
|
|
|
if !self.used_region_names.contains(&name) {
|
2019-01-18 21:33:31 +02:00
|
|
|
break name;
|
|
|
|
}
|
|
|
|
};
|
2019-01-20 19:46:47 +02:00
|
|
|
let _ = write!(self, "{}", name);
|
2019-01-18 21:33:31 +02:00
|
|
|
ty::BrNamed(DefId::local(CRATE_DEF_INDEX), name)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.tcx.mk_region(ty::ReLateBound(ty::INNERMOST, br))
|
|
|
|
}).0;
|
|
|
|
start_or_continue(&mut self, "", "> ")?;
|
|
|
|
|
2019-01-20 19:46:47 +02:00
|
|
|
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)
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-20 04:56:48 +02:00
|
|
|
fn prepare_late_bound_region_info<T>(&mut self, value: &ty::Binder<T>)
|
2019-01-20 19:46:47 +02:00
|
|
|
where T: TypeFoldable<'tcx>
|
2019-01-20 04:56:48 +02:00
|
|
|
{
|
|
|
|
|
2019-01-20 19:46:47 +02:00
|
|
|
struct LateBoundRegionNameCollector<'a>(&'a mut FxHashSet<InternedString>);
|
|
|
|
impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector<'_> {
|
2019-01-20 04:56:48 +02:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
|
|
|
|
match *r {
|
|
|
|
ty::ReLateBound(_, ty::BrNamed(_, name)) => {
|
|
|
|
self.0.insert(name);
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
r.super_visit_with(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 19:46:47 +02:00
|
|
|
self.used_region_names.clear();
|
|
|
|
let mut collector = LateBoundRegionNameCollector(&mut self.used_region_names);
|
2019-01-20 04:56:48 +02:00
|
|
|
value.visit_with(&mut collector);
|
2019-01-20 19:46:47 +02:00
|
|
|
self.region_index = 0;
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-20 04:56:48 +02:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
impl<'gcx: 'tcx, 'tcx, T, P: PrettyPrinter<'gcx, 'tcx>> Print<'gcx, 'tcx, P>
|
|
|
|
for ty::Binder<T>
|
|
|
|
where T: Print<'gcx, 'tcx, P, Output = P, Error = P::Error> + TypeFoldable<'tcx>
|
2019-01-20 04:56:48 +02:00
|
|
|
{
|
|
|
|
type Output = P;
|
|
|
|
type Error = P::Error;
|
2019-01-25 12:11:50 +02:00
|
|
|
fn print(&self, cx: P) -> Result<Self::Output, Self::Error> {
|
2019-01-20 04:56:48 +02:00
|
|
|
cx.in_binder(self)
|
|
|
|
}
|
|
|
|
}
|
2019-01-20 14:00:39 +02:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
impl<'gcx: 'tcx, 'tcx, T, U, P: PrettyPrinter<'gcx, 'tcx>> Print<'gcx, 'tcx, P>
|
|
|
|
for ty::OutlivesPredicate<T, U>
|
|
|
|
where T: Print<'gcx, 'tcx, P, Output = P, Error = P::Error>,
|
|
|
|
U: Print<'gcx, 'tcx, P, Output = P, Error = P::Error>,
|
2019-01-20 14:00:39 +02:00
|
|
|
{
|
2019-01-25 12:11:50 +02:00
|
|
|
type Output = P;
|
|
|
|
type Error = P::Error;
|
|
|
|
fn print(&self, mut cx: P) -> Result<Self::Output, Self::Error> {
|
|
|
|
define_scoped_cx!(cx);
|
|
|
|
p!(print(self.0), write(" : "), print(self.1));
|
|
|
|
Ok(cx)
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! forward_display_to_print {
|
2019-01-25 12:11:50 +02:00
|
|
|
($($ty:ty),+) => {
|
|
|
|
$(impl fmt::Display for $ty {
|
2019-01-20 14:00:39 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::tls::with(|tcx| {
|
|
|
|
tcx.lift(self)
|
|
|
|
.expect("could not lift for printing")
|
|
|
|
.print(FmtPrinter::new(tcx, f, Namespace::TypeNS))?;
|
|
|
|
Ok(())
|
|
|
|
})
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
})+
|
2019-01-20 14:00:39 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! define_print_and_forward_display {
|
2019-01-25 12:11:50 +02:00
|
|
|
(($self:ident, $cx:ident): $($ty:ty $print:block)+) => {
|
|
|
|
$(impl<'gcx: 'tcx, 'tcx, P: PrettyPrinter<'gcx, 'tcx>> Print<'gcx, 'tcx, P> for $ty {
|
2019-01-20 14:00:39 +02:00
|
|
|
type Output = P;
|
|
|
|
type Error = fmt::Error;
|
2019-01-25 12:11:50 +02:00
|
|
|
fn print(&$self, $cx: P) -> Result<Self::Output, Self::Error> {
|
2019-01-20 14:00:39 +02:00
|
|
|
#[allow(unused_mut)]
|
|
|
|
let mut $cx = $cx;
|
|
|
|
define_scoped_cx!($cx);
|
|
|
|
let _: () = $print;
|
|
|
|
#[allow(unreachable_code)]
|
2019-01-25 12:11:50 +02:00
|
|
|
Ok($cx)
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
})+
|
2019-01-20 14:00:39 +02:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
forward_display_to_print!($($ty),+);
|
2019-01-20 14:00:39 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
// HACK(eddyb) this is separate because `ty::RegionKind` doesn't need lifting.
|
|
|
|
impl fmt::Display for ty::RegionKind {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
ty::tls::with(|tcx| {
|
|
|
|
self.print(FmtPrinter::new(tcx, f, Namespace::TypeNS))?;
|
|
|
|
Ok(())
|
|
|
|
})
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
forward_display_to_print! {
|
|
|
|
Ty<'tcx>,
|
|
|
|
&'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
|
|
|
|
|
|
|
|
// HACK(eddyb) these are exhaustive instead of generic,
|
|
|
|
// because `for<'gcx: 'tcx, 'tcx>` isn't possible yet.
|
|
|
|
ty::Binder<&'tcx ty::List<ty::ExistentialPredicate<'tcx>>>,
|
|
|
|
ty::Binder<ty::TraitRef<'tcx>>,
|
|
|
|
ty::Binder<ty::FnSig<'tcx>>,
|
|
|
|
ty::Binder<ty::TraitPredicate<'tcx>>,
|
|
|
|
ty::Binder<ty::SubtypePredicate<'tcx>>,
|
|
|
|
ty::Binder<ty::ProjectionPredicate<'tcx>>,
|
|
|
|
ty::Binder<ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>>,
|
|
|
|
ty::Binder<ty::OutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>>>,
|
|
|
|
|
|
|
|
ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>,
|
|
|
|
ty::OutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>>
|
|
|
|
}
|
|
|
|
|
2019-01-20 14:00:39 +02:00
|
|
|
define_print_and_forward_display! {
|
|
|
|
(self, cx):
|
|
|
|
|
|
|
|
&'tcx ty::List<Ty<'tcx>> {
|
|
|
|
p!(write("{{"));
|
|
|
|
let mut tys = self.iter();
|
|
|
|
if let Some(&ty) = tys.next() {
|
|
|
|
p!(print(ty));
|
|
|
|
for &ty in tys {
|
|
|
|
p!(write(", "), print(ty));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p!(write("}}"))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::TypeAndMut<'tcx> {
|
|
|
|
p!(write("{}", if self.mutbl == hir::MutMutable { "mut " } else { "" }),
|
|
|
|
print(self.ty))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::ExistentialTraitRef<'tcx> {
|
2019-01-24 20:47:02 +02:00
|
|
|
// Use a type that can't appear in defaults of type parameters.
|
2019-03-18 20:55:19 +00:00
|
|
|
let dummy_self = cx.tcx().mk_ty_infer(ty::FreshTy(0));
|
2019-01-25 12:11:50 +02:00
|
|
|
let trait_ref = self.with_self_ty(cx.tcx(), dummy_self);
|
2019-01-20 14:00:39 +02:00
|
|
|
p!(print(trait_ref))
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:47:02 +02:00
|
|
|
ty::ExistentialProjection<'tcx> {
|
2019-01-25 12:11:50 +02:00
|
|
|
let name = cx.tcx().associated_item(self.item_def_id).ident;
|
2019-02-04 00:48:16 +02:00
|
|
|
p!(write("{} = ", name), print(self.ty))
|
2019-01-24 20:47:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::ExistentialPredicate<'tcx> {
|
|
|
|
match *self {
|
|
|
|
ty::ExistentialPredicate::Trait(x) => p!(print(x)),
|
|
|
|
ty::ExistentialPredicate::Projection(x) => p!(print(x)),
|
|
|
|
ty::ExistentialPredicate::AutoTrait(def_id) => {
|
2019-01-29 07:21:11 +02:00
|
|
|
p!(print_def_path(def_id, &[]));
|
2019-01-24 20:47:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 14:00:39 +02:00
|
|
|
ty::FnSig<'tcx> {
|
|
|
|
if self.unsafety == hir::Unsafety::Unsafe {
|
|
|
|
p!(write("unsafe "));
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.abi != Abi::Rust {
|
|
|
|
p!(write("extern {} ", self.abi));
|
|
|
|
}
|
|
|
|
|
2019-01-25 12:28:43 +02:00
|
|
|
p!(write("fn"), pretty_fn_sig(self.inputs(), self.c_variadic, self.output()));
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::InferTy {
|
2019-01-25 12:11:50 +02:00
|
|
|
if cx.tcx().sess.verbose() {
|
2019-01-20 14:00:39 +02:00
|
|
|
p!(write("{:?}", self));
|
2019-01-25 12:11:50 +02:00
|
|
|
return Ok(cx);
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
match *self {
|
|
|
|
ty::TyVar(_) => p!(write("_")),
|
|
|
|
ty::IntVar(_) => p!(write("{}", "{integer}")),
|
|
|
|
ty::FloatVar(_) => p!(write("{}", "{float}")),
|
|
|
|
ty::FreshTy(v) => p!(write("FreshTy({})", v)),
|
|
|
|
ty::FreshIntTy(v) => p!(write("FreshIntTy({})", v)),
|
|
|
|
ty::FreshFloatTy(v) => p!(write("FreshFloatTy({})", v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::TraitRef<'tcx> {
|
2019-01-29 07:21:11 +02:00
|
|
|
p!(print_def_path(self.def_id, self.substs));
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
|
2019-03-14 10:19:31 +01:00
|
|
|
&'tcx ty::Const<'tcx> {
|
|
|
|
match self.val {
|
|
|
|
ConstValue::Unevaluated(..) |
|
2019-01-20 14:00:39 +02:00
|
|
|
ConstValue::Infer(..) => p!(write("_")),
|
|
|
|
ConstValue::Param(ParamConst { name, .. }) => p!(write("{}", name)),
|
|
|
|
_ => p!(write("{:?}", self)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::ParamTy {
|
|
|
|
p!(write("{}", self.name))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::ParamConst {
|
|
|
|
p!(write("{}", self.name))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::SubtypePredicate<'tcx> {
|
|
|
|
p!(print(self.a), write(" <: "), print(self.b))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::TraitPredicate<'tcx> {
|
|
|
|
p!(print(self.trait_ref.self_ty()), write(": "), print(self.trait_ref))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::ProjectionPredicate<'tcx> {
|
|
|
|
p!(print(self.projection_ty), write(" == "), print(self.ty))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::ProjectionTy<'tcx> {
|
2019-01-29 07:21:11 +02:00
|
|
|
p!(print_def_path(self.item_def_id, self.substs));
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::ClosureKind {
|
|
|
|
match *self {
|
|
|
|
ty::ClosureKind::Fn => p!(write("Fn")),
|
|
|
|
ty::ClosureKind::FnMut => p!(write("FnMut")),
|
|
|
|
ty::ClosureKind::FnOnce => p!(write("FnOnce")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::Predicate<'tcx> {
|
|
|
|
match *self {
|
|
|
|
ty::Predicate::Trait(ref data) => p!(print(data)),
|
|
|
|
ty::Predicate::Subtype(ref predicate) => p!(print(predicate)),
|
|
|
|
ty::Predicate::RegionOutlives(ref predicate) => p!(print(predicate)),
|
|
|
|
ty::Predicate::TypeOutlives(ref predicate) => p!(print(predicate)),
|
|
|
|
ty::Predicate::Projection(ref predicate) => p!(print(predicate)),
|
|
|
|
ty::Predicate::WellFormed(ty) => p!(print(ty), write(" well-formed")),
|
|
|
|
ty::Predicate::ObjectSafe(trait_def_id) => {
|
2019-01-25 12:28:43 +02:00
|
|
|
p!(write("the trait `"),
|
2019-01-29 07:21:11 +02:00
|
|
|
print_def_path(trait_def_id, &[]),
|
2019-01-25 12:28:43 +02:00
|
|
|
write("` is object-safe"))
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
ty::Predicate::ClosureKind(closure_def_id, _closure_substs, kind) => {
|
2019-01-25 12:28:43 +02:00
|
|
|
p!(write("the closure `"),
|
2019-01-29 07:21:11 +02:00
|
|
|
print_value_path(closure_def_id, &[]),
|
2019-01-25 12:28:43 +02:00
|
|
|
write("` implements the trait `{}`", kind))
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
ty::Predicate::ConstEvaluatable(def_id, substs) => {
|
2019-01-25 12:28:43 +02:00
|
|
|
p!(write("the constant `"),
|
2019-01-29 07:21:11 +02:00
|
|
|
print_value_path(def_id, substs),
|
2019-01-25 12:28:43 +02:00
|
|
|
write("` can be evaluated"))
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Kind<'tcx> {
|
|
|
|
match self.unpack() {
|
|
|
|
UnpackedKind::Lifetime(lt) => p!(print(lt)),
|
|
|
|
UnpackedKind::Type(ty) => p!(print(ty)),
|
|
|
|
UnpackedKind::Const(ct) => p!(print(ct)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|