2022-02-16 10:56:01 +01:00
|
|
|
use crate::mir::interpret::{AllocRange, GlobalAlloc, Pointer, Provenance, Scalar};
|
Folding revamp.
This commit makes type folding more like the way chalk does it.
Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods.
- `fold_with` is the standard entry point, and defaults to calling
`super_fold_with`.
- `super_fold_with` does the actual work of traversing a type.
- For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead
calls into a `TypeFolder`, which can then call back into
`super_fold_with`.
With the new approach, `TypeFoldable` has `fold_with` and
`TypeSuperFoldable` has `super_fold_with`.
- `fold_with` is still the standard entry point, *and* it does the
actual work of traversing a type, for all types except types of
interest.
- `super_fold_with` is only implemented for the types of interest.
Benefits of the new model.
- I find it easier to understand. The distinction between types of
interest and other types is clearer, and `super_fold_with` doesn't
exist for most types.
- With the current model is easy to get confused and implement a
`super_fold_with` method that should be left defaulted. (Some of the
precursor commits fixed such cases.)
- With the current model it's easy to call `super_fold_with` within
`TypeFolder` impls where `fold_with` should be called. The new
approach makes this mistake impossible, and this commit fixes a number
of such cases.
- It's potentially faster, because it avoids the `fold_with` ->
`super_fold_with` call in all cases except types of interest. A lot of
the time the compile would inline those away, but not necessarily
always.
2022-06-02 11:38:15 +10:00
|
|
|
use crate::ty::{
|
2022-09-05 14:03:53 +10:00
|
|
|
self, ConstInt, DefIdTree, ParamConst, ScalarInt, Term, TermKind, Ty, TyCtxt, TypeFoldable,
|
2022-06-17 13:15:00 +01:00
|
|
|
TypeSuperFoldable, TypeSuperVisitable, TypeVisitable,
|
Folding revamp.
This commit makes type folding more like the way chalk does it.
Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods.
- `fold_with` is the standard entry point, and defaults to calling
`super_fold_with`.
- `super_fold_with` does the actual work of traversing a type.
- For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead
calls into a `TypeFolder`, which can then call back into
`super_fold_with`.
With the new approach, `TypeFoldable` has `fold_with` and
`TypeSuperFoldable` has `super_fold_with`.
- `fold_with` is still the standard entry point, *and* it does the
actual work of traversing a type, for all types except types of
interest.
- `super_fold_with` is only implemented for the types of interest.
Benefits of the new model.
- I find it easier to understand. The distinction between types of
interest and other types is clearer, and `super_fold_with` doesn't
exist for most types.
- With the current model is easy to get confused and implement a
`super_fold_with` method that should be left defaulted. (Some of the
precursor commits fixed such cases.)
- With the current model it's easy to call `super_fold_with` within
`TypeFolder` impls where `fold_with` should be called. The new
approach makes this mistake impossible, and this commit fixes a number
of such cases.
- It's potentially faster, because it avoids the `fold_with` ->
`super_fold_with` call in all cases except types of interest. A lot of
the time the compile would inline those away, but not necessarily
always.
2022-06-02 11:38:15 +10:00
|
|
|
};
|
2022-09-16 15:31:10 +02:00
|
|
|
use crate::ty::{GenericArg, GenericArgKind};
|
2019-03-29 10:52:09 +01:00
|
|
|
use rustc_apfloat::ieee::{Double, Single};
|
2022-05-11 16:36:26 -04:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
|
2021-03-23 12:41:26 +01:00
|
|
|
use rustc_data_structures::sso::SsoHashSet;
|
2020-03-24 09:09:42 +01:00
|
|
|
use rustc_hir as hir;
|
2020-09-02 10:40:56 +03:00
|
|
|
use rustc_hir::def::{self, CtorKind, DefKind, Namespace};
|
2022-04-15 19:27:53 +02:00
|
|
|
use rustc_hir::def_id::{DefId, DefIdSet, CRATE_DEF_ID, LOCAL_CRATE};
|
2020-08-31 18:11:44 +01:00
|
|
|
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
|
2020-09-02 10:40:56 +03:00
|
|
|
use rustc_session::config::TrimmedDefPaths;
|
2020-11-14 03:02:03 +01:00
|
|
|
use rustc_session::cstore::{ExternCrate, ExternCrateSource};
|
2020-04-19 13:00:18 +02:00
|
|
|
use rustc_span::symbol::{kw, Ident, Symbol};
|
2020-09-26 15:15:35 +02:00
|
|
|
use rustc_target::abi::Size;
|
2019-01-20 14:00:39 +02:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2019-01-18 21:33:31 +02:00
|
|
|
|
|
|
|
use std::cell::Cell;
|
2020-02-12 13:30:31 +01:00
|
|
|
use std::char;
|
2019-11-23 16:01:20 -08:00
|
|
|
use std::collections::BTreeMap;
|
2020-09-26 15:15:35 +02:00
|
|
|
use std::convert::TryFrom;
|
2019-01-18 21:33:31 +02:00
|
|
|
use std::fmt::{self, Write as _};
|
2021-03-08 15:32:41 -08:00
|
|
|
use std::iter;
|
2020-10-21 14:22:44 +02:00
|
|
|
use std::ops::{ControlFlow, 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 {
|
2020-09-30 10:07:15 -06:00
|
|
|
(@$lit:literal) => {
|
|
|
|
write!(scoped_cx!(), $lit)?
|
|
|
|
};
|
2019-01-25 12:28:43 +02:00
|
|
|
(@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
|
|
|
};
|
2020-09-30 10:07:15 -06:00
|
|
|
($($elem:tt $(($($args:tt)*))?),+) => {{
|
|
|
|
$(p!(@ $elem $(($($args)*))?);)+
|
2019-01-25 12:28:43 +02:00
|
|
|
}};
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
macro_rules! define_scoped_cx {
|
|
|
|
($cx:ident) => {
|
|
|
|
#[allow(unused_macros)]
|
|
|
|
macro_rules! scoped_cx {
|
|
|
|
() => {
|
|
|
|
$cx
|
2019-12-22 17:42:04 -05:00
|
|
|
};
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_local! {
|
2021-05-02 14:04:25 -04:00
|
|
|
static FORCE_IMPL_FILENAME_LINE: Cell<bool> = const { Cell::new(false) };
|
|
|
|
static SHOULD_PREFIX_WITH_CRATE: Cell<bool> = const { Cell::new(false) };
|
|
|
|
static NO_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
|
|
|
|
static NO_QUERIES: Cell<bool> = const { Cell::new(false) };
|
2021-09-20 20:22:55 +04:00
|
|
|
static NO_VISIBLE_PATH: Cell<bool> = const { Cell::new(false) };
|
2019-11-20 19:24:44 -05:00
|
|
|
}
|
|
|
|
|
2022-02-16 13:04:48 -05:00
|
|
|
macro_rules! define_helper {
|
|
|
|
($($(#[$a:meta])* fn $name:ident($helper:ident, $tl:ident);)+) => {
|
|
|
|
$(
|
|
|
|
#[must_use]
|
|
|
|
pub struct $helper(bool);
|
|
|
|
|
|
|
|
impl $helper {
|
|
|
|
pub fn new() -> $helper {
|
|
|
|
$helper($tl.with(|c| c.replace(true)))
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 21:33:31 +02:00
|
|
|
|
2022-02-16 13:04:48 -05:00
|
|
|
$(#[$a])*
|
|
|
|
pub macro $name($e:expr) {
|
|
|
|
{
|
|
|
|
let _guard = $helper::new();
|
|
|
|
$e
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 21:33:31 +02:00
|
|
|
|
2022-02-16 13:04:48 -05:00
|
|
|
impl Drop for $helper {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
$tl.with(|c| c.set(self.0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)+
|
|
|
|
}
|
2020-09-02 10:40:56 +03:00
|
|
|
}
|
|
|
|
|
2022-02-16 13:04:48 -05:00
|
|
|
define_helper!(
|
|
|
|
/// Avoids running any queries during any prints that occur
|
|
|
|
/// during the closure. This may alter the appearance of some
|
|
|
|
/// types (e.g. forcing verbose printing for opaque types).
|
|
|
|
/// This method is used during some queries (e.g. `explicit_item_bounds`
|
|
|
|
/// for opaque types), to ensure that any debug printing that
|
|
|
|
/// occurs during the query computation does not end up recursively
|
|
|
|
/// calling the same query.
|
|
|
|
fn with_no_queries(NoQueriesGuard, NO_QUERIES);
|
|
|
|
/// 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.
|
|
|
|
fn with_forced_impl_filename_line(ForcedImplGuard, FORCE_IMPL_FILENAME_LINE);
|
|
|
|
/// Adds the `crate::` prefix to paths where appropriate.
|
|
|
|
fn with_crate_prefix(CratePrefixGuard, SHOULD_PREFIX_WITH_CRATE);
|
|
|
|
/// Prevent path trimming if it is turned on. Path trimming affects `Display` impl
|
|
|
|
/// of various rustc types, for example `std::vec::Vec` would be trimmed to `Vec`,
|
|
|
|
/// if no other `Vec` is found.
|
|
|
|
fn with_no_trimmed_paths(NoTrimmedGuard, NO_TRIMMED_PATH);
|
|
|
|
/// Prevent selection of visible paths. `Display` impl of DefId will prefer
|
|
|
|
/// visible (public) reexports of types as paths.
|
|
|
|
fn with_no_visible_paths(NoVisibleGuard, NO_VISIBLE_PATH);
|
|
|
|
);
|
2021-09-20 20:22:55 +04:00
|
|
|
|
2019-01-18 21:33:31 +02:00
|
|
|
/// 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.
|
2022-01-28 11:25:15 +11:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct RegionHighlightMode<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
2019-01-18 21:33:31 +02:00
|
|
|
/// If enabled, when we see the selected region, use "`'N`"
|
|
|
|
/// instead of the ordinary behavior.
|
2022-01-28 11:25:15 +11:00
|
|
|
highlight_regions: [Option<(ty::Region<'tcx>, usize)>; 3],
|
2019-01-18 21:33:31 +02:00
|
|
|
|
|
|
|
/// If enabled, when printing a "free region" that originated from
|
2020-12-18 13:24:55 -05:00
|
|
|
/// the given `ty::BoundRegionKind`, print it as "`'1`". Free regions that would ordinarily
|
2019-01-18 21:33:31 +02:00
|
|
|
/// 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`.
|
2020-12-18 13:24:55 -05:00
|
|
|
highlight_bound_region: Option<(ty::BoundRegionKind, usize)>,
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2022-01-28 11:25:15 +11:00
|
|
|
impl<'tcx> RegionHighlightMode<'tcx> {
|
|
|
|
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
|
|
|
|
Self {
|
|
|
|
tcx,
|
|
|
|
highlight_regions: Default::default(),
|
|
|
|
highlight_bound_region: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 21:33:31 +02:00
|
|
|
/// If `region` and `number` are both `Some`, invokes
|
|
|
|
/// `highlighting_region`.
|
|
|
|
pub fn maybe_highlighting_region(
|
|
|
|
&mut self,
|
2022-01-28 11:25:15 +11:00
|
|
|
region: Option<ty::Region<'tcx>>,
|
2019-01-18 21:33:31 +02:00
|
|
|
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`.
|
2022-01-28 11:25:15 +11:00
|
|
|
pub fn highlighting_region(&mut self, region: ty::Region<'tcx>, number: usize) {
|
2019-01-18 21:33:31 +02:00
|
|
|
let num_slots = self.highlight_regions.len();
|
|
|
|
let first_avail_slot =
|
2020-02-26 23:03:45 +01:00
|
|
|
self.highlight_regions.iter_mut().find(|s| s.is_none()).unwrap_or_else(|| {
|
2019-01-18 21:33:31 +02:00
|
|
|
bug!("can only highlight {} placeholders at a time", num_slots,)
|
|
|
|
});
|
2022-01-28 11:25:15 +11:00
|
|
|
*first_avail_slot = Some((region, number));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience wrapper for `highlighting_region`.
|
|
|
|
pub fn highlighting_region_vid(&mut self, vid: ty::RegionVid, number: usize) {
|
2022-01-28 11:25:15 +11:00
|
|
|
self.highlighting_region(self.tcx.mk_region(ty::ReVar(vid)), number)
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `Some(n)` with the number to use for the given region, if any.
|
2022-06-19 00:20:27 -04:00
|
|
|
fn region_highlighted(&self, region: ty::Region<'tcx>) -> Option<usize> {
|
2020-04-24 20:03:45 -07:00
|
|
|
self.highlight_regions.iter().find_map(|h| match h {
|
2022-01-28 11:25:15 +11:00
|
|
|
Some((r, n)) if *r == region => Some(*n),
|
2020-04-24 20:03:45 -07:00
|
|
|
_ => None,
|
|
|
|
})
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2020-12-18 13:24:55 -05:00
|
|
|
pub fn highlighting_bound_region(&mut self, br: ty::BoundRegionKind, number: usize) {
|
2019-01-18 21:33:31 +02:00
|
|
|
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-06-14 00:48:52 +03:00
|
|
|
pub trait PrettyPrinter<'tcx>:
|
2019-06-14 01:32:15 +03:00
|
|
|
Printer<
|
|
|
|
'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-03-18 12:50:57 +02:00
|
|
|
Const = Self,
|
2019-06-14 01:32:15 +03:00
|
|
|
> + fmt::Write
|
2019-01-18 21:33:31 +02:00
|
|
|
{
|
|
|
|
/// 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-09-25 16:39:44 +01:00
|
|
|
substs: &'tcx [GenericArg<'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
|
|
|
}
|
|
|
|
|
2020-10-05 16:51:33 -04:00
|
|
|
fn in_binder<T>(self, value: &ty::Binder<'tcx, T>) -> Result<Self, Self::Error>
|
2019-06-14 01:32:15 +03:00
|
|
|
where
|
|
|
|
T: Print<'tcx, Self, Output = Self, Error = Self::Error> + TypeFoldable<'tcx>,
|
2019-01-20 04:56:48 +02:00
|
|
|
{
|
2020-06-24 23:40:33 +02:00
|
|
|
value.as_ref().skip_binder().print(self)
|
2019-01-20 04:56:48 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 21:27:15 -07:00
|
|
|
fn wrap_binder<T, F: FnOnce(&T, Self) -> Result<Self, fmt::Error>>(
|
2020-12-11 15:02:46 -05:00
|
|
|
self,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: &ty::Binder<'tcx, T>,
|
2020-12-11 15:02:46 -05:00
|
|
|
f: F,
|
|
|
|
) -> Result<Self, Self::Error>
|
|
|
|
where
|
|
|
|
T: Print<'tcx, Self, Output = Self, Error = Self::Error> + TypeFoldable<'tcx>,
|
|
|
|
{
|
|
|
|
f(value.as_ref().skip_binder(), self)
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Prints comma-separated elements.
|
2019-06-14 01:32:15 +03:00
|
|
|
fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, Self::Error>
|
|
|
|
where
|
|
|
|
T: Print<'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
|
|
|
}
|
|
|
|
|
2020-02-13 19:02:58 +01:00
|
|
|
/// Prints `{f: t}` or `{f as t}` depending on the `cast` argument
|
|
|
|
fn typed_value(
|
2020-01-27 18:49:05 +01:00
|
|
|
mut self,
|
|
|
|
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
|
|
|
|
t: impl FnOnce(Self) -> Result<Self, Self::Error>,
|
2020-02-26 19:36:10 +01:00
|
|
|
conversion: &str,
|
2020-01-27 18:49:05 +01:00
|
|
|
) -> Result<Self::Const, Self::Error> {
|
|
|
|
self.write_str("{")?;
|
|
|
|
self = f(self)?;
|
2020-02-26 19:36:10 +01:00
|
|
|
self.write_str(conversion)?;
|
2020-02-05 11:00:52 +01:00
|
|
|
self = t(self)?;
|
2020-01-27 18:49:05 +01:00
|
|
|
self.write_str("}")?;
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Prints `<...>` 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-09-06 03:57:44 +01:00
|
|
|
/// Returns `true` if the region should be printed in
|
|
|
|
/// optional positions, e.g., `&'a T` or `dyn Tr + 'b`.
|
2019-01-23 19:36:39 +02:00
|
|
|
/// This is typically the case for all non-`'_` regions.
|
2022-06-19 00:20:27 -04:00
|
|
|
fn should_print_region(&self, region: ty::Region<'tcx>) -> bool;
|
2019-01-18 21:33:31 +02:00
|
|
|
|
2020-03-06 12:13:55 +01:00
|
|
|
// Defaults (should not be overridden):
|
2019-01-18 21:33:31 +02:00
|
|
|
|
|
|
|
/// If possible, this returns a global path resolving to `def_id` that is visible
|
2019-09-06 03:57:44 +01:00
|
|
|
/// from at least one local module, and returns `true`. If the crate defining `def_id` is
|
2019-01-18 21:33:31 +02:00
|
|
|
/// declared with an `extern crate`, the path is guaranteed to use the `extern crate`.
|
2019-07-08 22:26:15 +02:00
|
|
|
fn try_print_visible_def_path(self, def_id: DefId) -> Result<(Self, bool), Self::Error> {
|
2021-09-20 20:22:55 +04:00
|
|
|
if NO_VISIBLE_PATH.with(|flag| flag.get()) {
|
|
|
|
return Ok((self, false));
|
|
|
|
}
|
|
|
|
|
2019-07-08 22:26:15 +02:00
|
|
|
let mut callers = Vec::new();
|
|
|
|
self.try_print_visible_def_path_recur(def_id, &mut callers)
|
|
|
|
}
|
|
|
|
|
2020-09-02 10:40:56 +03:00
|
|
|
/// Try to see if this path can be trimmed to a unique symbol name.
|
|
|
|
fn try_print_trimmed_def_path(
|
|
|
|
mut self,
|
|
|
|
def_id: DefId,
|
|
|
|
) -> Result<(Self::Path, bool), Self::Error> {
|
2022-07-06 07:44:47 -05:00
|
|
|
if !self.tcx().sess.opts.unstable_opts.trim_diagnostic_paths
|
2020-09-02 10:40:56 +03:00
|
|
|
|| matches!(self.tcx().sess.opts.trimmed_def_paths, TrimmedDefPaths::Never)
|
|
|
|
|| NO_TRIMMED_PATH.with(|flag| flag.get())
|
|
|
|
|| SHOULD_PREFIX_WITH_CRATE.with(|flag| flag.get())
|
|
|
|
{
|
|
|
|
return Ok((self, false));
|
|
|
|
}
|
|
|
|
|
2021-05-11 14:16:48 +02:00
|
|
|
match self.tcx().trimmed_def_paths(()).get(&def_id) {
|
2020-09-15 22:36:43 +02:00
|
|
|
None => Ok((self, false)),
|
2020-09-02 10:40:56 +03:00
|
|
|
Some(symbol) => {
|
2021-12-15 14:39:23 +11:00
|
|
|
self.write_str(symbol.as_str())?;
|
2020-09-15 22:36:43 +02:00
|
|
|
Ok((self, true))
|
2020-09-02 10:40:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 22:26:15 +02:00
|
|
|
/// Does the work of `try_print_visible_def_path`, building the
|
|
|
|
/// full definition path recursively before attempting to
|
|
|
|
/// post-process it into the valid and visible version that
|
|
|
|
/// accounts for re-exports.
|
|
|
|
///
|
2020-03-06 12:13:55 +01:00
|
|
|
/// This method should only be called by itself or
|
2019-07-08 22:26:15 +02:00
|
|
|
/// `try_print_visible_def_path`.
|
|
|
|
///
|
|
|
|
/// `callers` is a chain of visible_parent's leading to `def_id`,
|
|
|
|
/// to support cycle detection during recursion.
|
2021-11-30 17:19:37 -08:00
|
|
|
///
|
|
|
|
/// This method returns false if we can't print the visible path, so
|
|
|
|
/// `print_def_path` can fall back on the item's real definition path.
|
2019-07-08 22:26:15 +02:00
|
|
|
fn try_print_visible_def_path_recur(
|
2019-01-18 21:33:31 +02:00
|
|
|
mut self,
|
|
|
|
def_id: DefId,
|
2019-07-08 22:26:15 +02:00
|
|
|
callers: &mut Vec<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.
|
2022-04-15 19:27:53 +02:00
|
|
|
if let Some(cnum) = def_id.as_crate_root() {
|
2019-01-18 21:33:31 +02:00
|
|
|
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:
|
|
|
|
//
|
2019-09-06 03:57:44 +01:00
|
|
|
// 1. For a direct dependency, where user added an
|
2019-01-18 21:33:31 +02:00
|
|
|
// `extern crate` manually, we put the `extern
|
|
|
|
// crate` as the parent. So you wind up with
|
|
|
|
// something relative to the current crate.
|
2019-09-06 03:57:44 +01:00
|
|
|
// 2. For an extern inferred from a path or an indirect crate,
|
2019-01-18 21:33:31 +02:00
|
|
|
// where there is no explicit `extern crate`, we just prepend
|
|
|
|
// the crate name.
|
2018-12-01 17:27:12 +01:00
|
|
|
match self.tcx().extern_crate(def_id) {
|
2020-06-29 21:51:32 +03:00
|
|
|
Some(&ExternCrate { src, dependency_of, span, .. }) => match (src, dependency_of) {
|
|
|
|
(ExternCrateSource::Extern(def_id), LOCAL_CRATE) => {
|
2021-10-10 18:18:30 +03:00
|
|
|
// NOTE(eddyb) the only reason `span` might be dummy,
|
|
|
|
// that we're aware of, is that it's the `std`/`core`
|
|
|
|
// `extern crate` injected by default.
|
|
|
|
// FIXME(eddyb) find something better to key this on,
|
|
|
|
// or avoid ending up with `ExternCrateSource::Extern`,
|
|
|
|
// for the injected `std`/`core`.
|
|
|
|
if span.is_dummy() {
|
|
|
|
return Ok((self.path_crate(cnum)?, true));
|
|
|
|
}
|
|
|
|
|
2021-10-10 18:50:44 +03:00
|
|
|
// Disable `try_print_trimmed_def_path` behavior within
|
|
|
|
// the `print_def_path` call, to avoid infinite recursion
|
|
|
|
// in cases where the `extern crate foo` has non-trivial
|
|
|
|
// parents, e.g. it's nested in `impl foo::Trait for Bar`
|
|
|
|
// (see also issues #55779 and #87932).
|
2022-02-16 13:04:48 -05:00
|
|
|
self = with_no_visible_paths!(self.print_def_path(def_id, &[])?);
|
2021-10-10 18:50:44 +03:00
|
|
|
|
|
|
|
return Ok((self, true));
|
2020-06-29 21:51:32 +03:00
|
|
|
}
|
2020-07-01 20:05:51 +03:00
|
|
|
(ExternCrateSource::Path, LOCAL_CRATE) => {
|
2020-06-29 21:51:32 +03:00
|
|
|
return Ok((self.path_crate(cnum)?, true));
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
2019-01-18 21:33:31 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-05-11 14:16:48 +02:00
|
|
|
let visible_parent_map = self.tcx().visible_parent_map(());
|
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-09-06 03:57:44 +01:00
|
|
|
// For a constructor, we want the name of its parent rather than <unnamed>.
|
2020-03-22 13:36:56 +01:00
|
|
|
if let DefPathData::Ctor = cur_def_key.disambiguated_data.data {
|
|
|
|
let parent = DefId {
|
|
|
|
krate: def_id.krate,
|
|
|
|
index: cur_def_key
|
|
|
|
.parent
|
|
|
|
.expect("`DefPathData::Ctor` / `VariantData` missing a parent"),
|
|
|
|
};
|
2019-01-18 21:33:31 +02:00
|
|
|
|
2020-03-22 13:36:56 +01:00
|
|
|
cur_def_key = self.tcx().def_key(parent);
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some(visible_parent) = visible_parent_map.get(&def_id).cloned() else {
|
|
|
|
return Ok((self, false));
|
2019-01-18 21:33:31 +02:00
|
|
|
};
|
2021-11-30 17:19:37 -08:00
|
|
|
|
2022-04-25 22:08:45 +03:00
|
|
|
let actual_parent = self.tcx().opt_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).
|
|
|
|
//
|
2022-03-30 15:14:15 -04:00
|
|
|
// `std::os::unix` reexports the contents of `std::sys::unix::ext`. `std::sys` is
|
2019-01-18 21:33:31 +02:00
|
|
|
// 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 => {
|
2021-11-30 17:19:37 -08:00
|
|
|
// Item might be re-exported several times, but filter for the one
|
|
|
|
// that's public and whose identifier isn't `_`.
|
2019-02-03 12:59:37 +02:00
|
|
|
let reexport = self
|
|
|
|
.tcx()
|
2021-12-23 16:12:34 +08:00
|
|
|
.module_children(visible_parent)
|
2019-01-18 21:33:31 +02:00
|
|
|
.iter()
|
2021-11-30 17:19:37 -08:00
|
|
|
.filter(|child| child.res.opt_def_id() == Some(def_id))
|
|
|
|
.find(|child| child.vis.is_public() && child.ident.name != kw::Underscore)
|
2019-10-21 14:25:08 +11:00
|
|
|
.map(|child| child.ident.name);
|
2021-11-30 17:19:37 -08:00
|
|
|
|
|
|
|
if let Some(new_name) = reexport {
|
|
|
|
*name = new_name;
|
|
|
|
} else {
|
|
|
|
// There is no name that is public and isn't `_`, so bail.
|
|
|
|
return Ok((self, false));
|
2019-02-03 12:59:37 +02:00
|
|
|
}
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2019-02-03 12:59:37 +02:00
|
|
|
// Re-exported `extern crate` (#43189).
|
|
|
|
DefPathData::CrateRoot => {
|
2021-05-10 18:23:32 +02:00
|
|
|
data = DefPathData::TypeNs(self.tcx().crate_name(def_id.krate));
|
2019-02-03 12:59:37 +02:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
debug!("try_print_visible_def_path: data={:?}", data);
|
|
|
|
|
2021-11-30 17:19:37 -08:00
|
|
|
if callers.contains(&visible_parent) {
|
|
|
|
return Ok((self, false));
|
|
|
|
}
|
|
|
|
callers.push(visible_parent);
|
|
|
|
// 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_recur(visible_parent, callers)? {
|
|
|
|
(cx, false) => return Ok((cx, false)),
|
|
|
|
(cx, true) => self = cx,
|
|
|
|
}
|
|
|
|
callers.pop();
|
|
|
|
|
2019-02-03 12:59:37 +02:00
|
|
|
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.
|
2020-08-03 00:49:11 +02:00
|
|
|
match self_ty.kind() {
|
2019-01-18 21:33:31 +02:00
|
|
|
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 {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(" as ", print(trait_ref.print_only_trait_path()));
|
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);
|
|
|
|
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("impl ");
|
2019-01-18 21:33:31 +02:00
|
|
|
if let Some(trait_ref) = trait_ref {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(print(trait_ref.print_only_trait_path()), " 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);
|
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
match *ty.kind() {
|
2020-09-30 10:07:15 -06:00
|
|
|
ty::Bool => p!("bool"),
|
|
|
|
ty::Char => p!("char"),
|
2019-10-28 03:46:22 +01:00
|
|
|
ty::Int(t) => p!(write("{}", t.name_str())),
|
|
|
|
ty::Uint(t) => p!(write("{}", t.name_str())),
|
|
|
|
ty::Float(t) => p!(write("{}", t.name_str())),
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::RawPtr(ref tm) => {
|
|
|
|
p!(write(
|
|
|
|
"*{} ",
|
|
|
|
match tm.mutbl {
|
2019-12-16 17:28:40 +01:00
|
|
|
hir::Mutability::Mut => "mut",
|
|
|
|
hir::Mutability::Not => "const",
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
));
|
|
|
|
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) => {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("&");
|
2022-02-22 00:00:00 +00:00
|
|
|
if self.should_print_region(r) {
|
2020-10-02 15:08:01 -06:00
|
|
|
p!(print(r), " ");
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
p!(print(ty::TypeAndMut { ty, mutbl }))
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2020-09-30 10:07:15 -06:00
|
|
|
ty::Never => p!("!"),
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::Tuple(ref tys) => {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("(", comma_sep(tys.iter()));
|
2020-04-17 03:54:13 +03:00
|
|
|
if tys.len() == 1 {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(",");
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(")")
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::FnDef(def_id, substs) => {
|
2022-05-08 15:43:18 -04:00
|
|
|
let sig = self.tcx().bound_fn_sig(def_id).subst(self.tcx(), substs);
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(print(sig), " {{", print_value_path(def_id, substs), "}}");
|
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-05-30 15:33:13 -07:00
|
|
|
ty::Infer(infer_ty) => {
|
2020-12-12 15:25:55 +01:00
|
|
|
let verbose = self.tcx().sess.verbose();
|
2019-05-30 15:33:13 -07:00
|
|
|
if let ty::TyVar(ty_vid) = infer_ty {
|
2022-02-14 14:13:02 +01:00
|
|
|
if let Some(name) = self.ty_infer_name(ty_vid) {
|
2019-05-30 15:33:13 -07:00
|
|
|
p!(write("{}", name))
|
|
|
|
} else {
|
2020-12-12 15:25:55 +01:00
|
|
|
if verbose {
|
|
|
|
p!(write("{:?}", infer_ty))
|
|
|
|
} else {
|
|
|
|
p!(write("{}", infer_ty))
|
|
|
|
}
|
2019-05-30 15:33:13 -07:00
|
|
|
}
|
|
|
|
} else {
|
2022-06-14 17:34:37 +02:00
|
|
|
if verbose { p!(write("{:?}", infer_ty)) } else { p!(write("{}", infer_ty)) }
|
2019-05-30 15:33:13 -07:00
|
|
|
}
|
|
|
|
}
|
2020-09-30 10:07:15 -06:00
|
|
|
ty::Error(_) => p!("[type error]"),
|
2022-02-23 00:00:00 +00:00
|
|
|
ty::Param(ref param_ty) => p!(print(param_ty)),
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::Bound(debruijn, bound_ty) => match bound_ty.kind {
|
2020-03-13 10:48:41 +01:00
|
|
|
ty::BoundTyKind::Anon => self.pretty_print_bound_var(debruijn, bound_ty.var)?,
|
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) => {
|
2022-03-05 07:28:41 +11:00
|
|
|
p!(print_def_path(def.did(), substs));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2022-04-13 16:11:28 -07:00
|
|
|
ty::Dynamic(data, r, repr) => {
|
2022-02-22 00:00:00 +00:00
|
|
|
let print_r = self.should_print_region(r);
|
2019-01-18 21:33:31 +02:00
|
|
|
if print_r {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("(");
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2022-04-13 16:11:28 -07:00
|
|
|
match repr {
|
2022-08-29 03:53:33 +00:00
|
|
|
ty::Dyn => p!("dyn "),
|
|
|
|
ty::DynStar => p!("dyn* "),
|
2022-04-13 16:11:28 -07:00
|
|
|
}
|
|
|
|
p!(print(data));
|
2019-01-18 21:33:31 +02:00
|
|
|
if print_r {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(" + ", print(r), ")");
|
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
|
|
|
}
|
2022-09-08 00:45:09 +00:00
|
|
|
ty::Projection(ref data) => {
|
|
|
|
if self.tcx().def_kind(data.item_def_id) == DefKind::ImplTraitPlaceholder {
|
|
|
|
return self.pretty_print_opaque_impl_type(data.item_def_id, data.substs);
|
|
|
|
} else {
|
|
|
|
p!(print(data))
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 21:33:31 +02:00
|
|
|
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-11-20 19:24:44 -05:00
|
|
|
// We use verbose printing in 'NO_QUERIES' mode, to
|
|
|
|
// avoid needing to call `predicates_of`. This should
|
|
|
|
// only affect certain debug messages (e.g. messages printed
|
2020-03-29 16:41:09 +02:00
|
|
|
// from `rustc_middle::ty` during the computation of `tcx.predicates_of`),
|
2019-11-20 19:24:44 -05:00
|
|
|
// and should have no effect on any compiler output.
|
|
|
|
if self.tcx().sess.verbose() || NO_QUERIES.with(|q| q.get()) {
|
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
|
|
|
}
|
|
|
|
|
2022-04-25 22:08:45 +03:00
|
|
|
let parent = self.tcx().parent(def_id);
|
2022-02-14 16:10:22 +00:00
|
|
|
match self.tcx().def_kind(parent) {
|
|
|
|
DefKind::TyAlias | DefKind::AssocTy => {
|
|
|
|
if let ty::Opaque(d, _) = *self.tcx().type_of(parent).kind() {
|
|
|
|
if d == def_id {
|
|
|
|
// If the type alias directly starts with the `impl` of the
|
|
|
|
// opaque type we're printing, then skip the `::{opaque#1}`.
|
|
|
|
p!(print_def_path(parent, substs));
|
|
|
|
return Ok(self);
|
|
|
|
}
|
2022-02-07 15:50:42 +00:00
|
|
|
}
|
2022-02-14 16:10:22 +00:00
|
|
|
// Complex opaque type, e.g. `type Foo = (i32, impl Debug);`
|
|
|
|
p!(print_def_path(def_id, substs));
|
2022-02-07 15:50:42 +00:00
|
|
|
return Ok(self);
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2022-02-14 16:10:22 +00:00
|
|
|
_ => return self.pretty_print_opaque_impl_type(def_id, substs),
|
|
|
|
}
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2020-09-30 10:07:15 -06:00
|
|
|
ty::Str => p!("str"),
|
2019-01-18 21:33:31 +02:00
|
|
|
ty::Generator(did, substs, movability) => {
|
2020-09-08 10:43:06 -04:00
|
|
|
p!(write("["));
|
2019-11-09 18:06:57 +01:00
|
|
|
match movability {
|
2020-09-08 10:43:06 -04:00
|
|
|
hir::Movability::Movable => {}
|
2020-09-30 10:12:48 -06:00
|
|
|
hir::Movability::Static => p!("static "),
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 10:43:06 -04:00
|
|
|
if !self.tcx().sess.verbose() {
|
2020-09-30 10:12:48 -06:00
|
|
|
p!("generator");
|
2020-09-08 10:43:06 -04:00
|
|
|
// FIXME(eddyb) should use `def_span`.
|
|
|
|
if let Some(did) = did.as_local() {
|
2021-10-20 20:59:15 +02:00
|
|
|
let span = self.tcx().def_span(did);
|
2021-05-03 01:14:25 +01:00
|
|
|
p!(write(
|
|
|
|
"@{}",
|
|
|
|
// This may end up in stderr diagnostics but it may also be emitted
|
|
|
|
// into MIR. Hence we use the remapped path if available
|
|
|
|
self.tcx().sess.source_map().span_to_embeddable_string(span)
|
|
|
|
));
|
2020-09-08 10:43:06 -04:00
|
|
|
} else {
|
2020-10-22 14:40:09 +09:00
|
|
|
p!(write("@"), print_def_path(did, substs));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-09-08 10:43:06 -04:00
|
|
|
p!(print_def_path(did, substs));
|
2020-07-19 17:26:51 -04:00
|
|
|
p!(" upvar_tys=(");
|
|
|
|
if !substs.as_generator().is_valid() {
|
|
|
|
p!("unavailable");
|
|
|
|
} else {
|
|
|
|
self = self.comma_sep(substs.as_generator().upvar_tys())?;
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2020-07-19 17:26:51 -04:00
|
|
|
p!(")");
|
2019-01-18 21:33:31 +02:00
|
|
|
|
2021-06-12 00:00:00 +00:00
|
|
|
if substs.as_generator().is_valid() {
|
|
|
|
p!(" ", print(substs.as_generator().witness()));
|
|
|
|
}
|
2020-03-13 03:23:38 +02:00
|
|
|
}
|
|
|
|
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("]")
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
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) => {
|
2020-09-08 10:43:06 -04:00
|
|
|
p!(write("["));
|
|
|
|
if !self.tcx().sess.verbose() {
|
|
|
|
p!(write("closure"));
|
|
|
|
// FIXME(eddyb) should use `def_span`.
|
|
|
|
if let Some(did) = did.as_local() {
|
2022-07-06 07:44:47 -05:00
|
|
|
if self.tcx().sess.opts.unstable_opts.span_free_formats {
|
2020-09-30 10:12:48 -06:00
|
|
|
p!("@", print_def_path(did.to_def_id(), substs));
|
2020-09-08 10:43:06 -04:00
|
|
|
} else {
|
2021-10-20 20:59:15 +02:00
|
|
|
let span = self.tcx().def_span(did);
|
2021-05-03 01:14:25 +01:00
|
|
|
p!(write(
|
|
|
|
"@{}",
|
|
|
|
// This may end up in stderr diagnostics but it may also be emitted
|
|
|
|
// into MIR. Hence we use the remapped path if available
|
|
|
|
self.tcx().sess.source_map().span_to_embeddable_string(span)
|
|
|
|
));
|
2020-03-13 03:23:38 +02:00
|
|
|
}
|
2020-09-08 10:43:06 -04:00
|
|
|
} else {
|
2020-10-22 14:40:09 +09:00
|
|
|
p!(write("@"), print_def_path(did, substs));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-09-08 10:43:06 -04:00
|
|
|
p!(print_def_path(did, substs));
|
2020-07-19 17:26:51 -04:00
|
|
|
if !substs.as_closure().is_valid() {
|
|
|
|
p!(" closure_substs=(unavailable)");
|
2021-08-28 17:48:30 -04:00
|
|
|
p!(write(" substs={:?}", substs));
|
2020-07-19 17:26:51 -04:00
|
|
|
} else {
|
|
|
|
p!(" closure_kind_ty=", print(substs.as_closure().kind_ty()));
|
|
|
|
p!(
|
|
|
|
" closure_sig_as_fn_ptr_ty=",
|
|
|
|
print(substs.as_closure().sig_as_fn_ptr_ty())
|
|
|
|
);
|
|
|
|
p!(" upvar_tys=(");
|
|
|
|
self = self.comma_sep(substs.as_closure().upvar_tys())?;
|
|
|
|
p!(")");
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-30 10:12:48 -06:00
|
|
|
p!("]");
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
ty::Array(ty, sz) => {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("[", print(ty), "; ");
|
2019-10-07 10:59:46 -04:00
|
|
|
if self.tcx().sess.verbose() {
|
|
|
|
p!(write("{:?}", sz));
|
2022-06-10 11:18:06 +10:00
|
|
|
} else if let ty::ConstKind::Unevaluated(..) = sz.kind() {
|
2020-05-09 17:10:40 +02:00
|
|
|
// Do not try to evaluate unevaluated constants. If we are const evaluating an
|
2019-05-26 17:33:32 +02:00
|
|
|
// array length anon const, rustc will (with debug assertions) print the
|
|
|
|
// constant's path. Which will end up here again.
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("_");
|
2022-06-10 11:18:06 +10:00
|
|
|
} else if let Some(n) = sz.kind().try_to_bits(self.tcx().data_layout.pointer_size) {
|
2019-03-18 12:50:57 +02:00
|
|
|
p!(write("{}", n));
|
2022-06-10 11:18:06 +10:00
|
|
|
} else if let ty::ConstKind::Param(param) = sz.kind() {
|
2022-02-23 00:00:00 +00:00
|
|
|
p!(print(param));
|
2019-03-18 12:50:57 +02:00
|
|
|
} else {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("_");
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("]")
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2020-09-30 10:07:15 -06:00
|
|
|
ty::Slice(ty) => p!("[", print(ty), "]"),
|
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
|
|
|
}
|
|
|
|
|
2021-11-19 20:51:19 -08:00
|
|
|
fn pretty_print_opaque_impl_type(
|
|
|
|
mut self,
|
|
|
|
def_id: DefId,
|
|
|
|
substs: &'tcx ty::List<ty::GenericArg<'tcx>>,
|
|
|
|
) -> Result<Self::Type, Self::Error> {
|
2022-06-21 21:27:15 -07:00
|
|
|
let tcx = self.tcx();
|
2021-11-19 20:51:19 -08:00
|
|
|
|
|
|
|
// Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
|
|
|
|
// by looking up the projections associated with the def_id.
|
2022-06-21 21:27:15 -07:00
|
|
|
let bounds = tcx.bound_explicit_item_bounds(def_id);
|
2021-11-19 20:51:19 -08:00
|
|
|
|
2022-05-11 16:36:26 -04:00
|
|
|
let mut traits = FxIndexMap::default();
|
|
|
|
let mut fn_traits = FxIndexMap::default();
|
2021-11-19 20:51:19 -08:00
|
|
|
let mut is_sized = false;
|
|
|
|
|
2022-05-10 22:28:50 -04:00
|
|
|
for predicate in bounds.transpose_iter().map(|e| e.map_bound(|(p, _)| *p)) {
|
2022-06-21 21:27:15 -07:00
|
|
|
let predicate = predicate.subst(tcx, substs);
|
2021-11-19 20:51:19 -08:00
|
|
|
let bound_predicate = predicate.kind();
|
|
|
|
|
|
|
|
match bound_predicate.skip_binder() {
|
|
|
|
ty::PredicateKind::Trait(pred) => {
|
|
|
|
let trait_ref = bound_predicate.rebind(pred.trait_ref);
|
|
|
|
|
|
|
|
// Don't print + Sized, but rather + ?Sized if absent.
|
2022-06-21 21:27:15 -07:00
|
|
|
if Some(trait_ref.def_id()) == tcx.lang_items().sized_trait() {
|
2021-11-19 20:51:19 -08:00
|
|
|
is_sized = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.insert_trait_and_projection(trait_ref, None, &mut traits, &mut fn_traits);
|
|
|
|
}
|
|
|
|
ty::PredicateKind::Projection(pred) => {
|
|
|
|
let proj_ref = bound_predicate.rebind(pred);
|
2022-06-21 21:27:15 -07:00
|
|
|
let trait_ref = proj_ref.required_poly_trait_ref(tcx);
|
2021-11-19 20:51:19 -08:00
|
|
|
|
|
|
|
// Projection type entry -- the def-id for naming, and the ty.
|
2022-01-10 23:39:21 +00:00
|
|
|
let proj_ty = (proj_ref.projection_def_id(), proj_ref.term());
|
2021-11-19 20:51:19 -08:00
|
|
|
|
|
|
|
self.insert_trait_and_projection(
|
|
|
|
trait_ref,
|
|
|
|
Some(proj_ty),
|
|
|
|
&mut traits,
|
|
|
|
&mut fn_traits,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 10:21:24 -07:00
|
|
|
write!(self, "impl ")?;
|
2022-06-21 21:27:15 -07:00
|
|
|
|
2021-11-19 20:51:19 -08:00
|
|
|
let mut first = true;
|
|
|
|
// Insert parenthesis around (Fn(A, B) -> C) if the opaque ty has more than one other trait
|
|
|
|
let paren_needed = fn_traits.len() > 1 || traits.len() > 0 || !is_sized;
|
|
|
|
|
|
|
|
for (fn_once_trait_ref, entry) in fn_traits {
|
2022-06-22 10:21:24 -07:00
|
|
|
write!(self, "{}", if first { "" } else { " + " })?;
|
|
|
|
write!(self, "{}", if paren_needed { "(" } else { "" })?;
|
2022-06-21 21:27:15 -07:00
|
|
|
|
2022-06-22 10:21:24 -07:00
|
|
|
self = self.wrap_binder(&fn_once_trait_ref, |trait_ref, mut cx| {
|
|
|
|
define_scoped_cx!(cx);
|
2022-06-21 21:27:15 -07:00
|
|
|
// Get the (single) generic ty (the args) of this FnOnce trait ref.
|
|
|
|
let generics = tcx.generics_of(trait_ref.def_id);
|
|
|
|
let args = generics.own_substs_no_defaults(tcx, trait_ref.substs);
|
|
|
|
|
|
|
|
match (entry.return_ty, args[0].expect_ty()) {
|
|
|
|
// We can only print `impl Fn() -> ()` if we have a tuple of args and we recorded
|
|
|
|
// a return type.
|
|
|
|
(Some(return_ty), arg_tys) if matches!(arg_tys.kind(), ty::Tuple(_)) => {
|
|
|
|
let name = if entry.fn_trait_ref.is_some() {
|
|
|
|
"Fn"
|
|
|
|
} else if entry.fn_mut_trait_ref.is_some() {
|
|
|
|
"FnMut"
|
|
|
|
} else {
|
|
|
|
"FnOnce"
|
|
|
|
};
|
2021-11-19 20:51:19 -08:00
|
|
|
|
2022-06-21 21:27:15 -07:00
|
|
|
p!(write("{}(", name));
|
2021-11-19 20:51:19 -08:00
|
|
|
|
2022-06-21 21:27:15 -07:00
|
|
|
for (idx, ty) in arg_tys.tuple_fields().iter().enumerate() {
|
|
|
|
if idx > 0 {
|
|
|
|
p!(", ");
|
|
|
|
}
|
|
|
|
p!(print(ty));
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
|
|
|
|
2022-06-21 21:27:15 -07:00
|
|
|
p!(")");
|
2022-09-05 14:03:53 +10:00
|
|
|
if let Some(ty) = return_ty.skip_binder().ty() {
|
2022-06-21 21:27:15 -07:00
|
|
|
if !ty.is_unit() {
|
|
|
|
p!(" -> ", print(return_ty));
|
|
|
|
}
|
2022-01-10 23:39:21 +00:00
|
|
|
}
|
2022-06-21 21:27:15 -07:00
|
|
|
p!(write("{}", if paren_needed { ")" } else { "" }));
|
2021-11-19 20:51:19 -08:00
|
|
|
|
2022-06-21 21:27:15 -07:00
|
|
|
first = false;
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
2022-06-21 21:27:15 -07:00
|
|
|
// If we got here, we can't print as a `impl Fn(A, B) -> C`. Just record the
|
|
|
|
// trait_refs we collected in the OpaqueFnEntry as normal trait refs.
|
|
|
|
_ => {
|
|
|
|
if entry.has_fn_once {
|
|
|
|
traits.entry(fn_once_trait_ref).or_default().extend(
|
|
|
|
// Group the return ty with its def id, if we had one.
|
|
|
|
entry
|
|
|
|
.return_ty
|
|
|
|
.map(|ty| (tcx.lang_items().fn_once_output().unwrap(), ty)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if let Some(trait_ref) = entry.fn_mut_trait_ref {
|
|
|
|
traits.entry(trait_ref).or_default();
|
|
|
|
}
|
|
|
|
if let Some(trait_ref) = entry.fn_trait_ref {
|
|
|
|
traits.entry(trait_ref).or_default();
|
|
|
|
}
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
|
|
|
}
|
2022-06-21 21:27:15 -07:00
|
|
|
|
2022-06-22 10:21:24 -07:00
|
|
|
Ok(cx)
|
2022-06-21 21:27:15 -07:00
|
|
|
})?;
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print the rest of the trait types (that aren't Fn* family of traits)
|
|
|
|
for (trait_ref, assoc_items) in traits {
|
2022-06-22 10:21:24 -07:00
|
|
|
write!(self, "{}", if first { "" } else { " + " })?;
|
2021-11-19 20:51:19 -08:00
|
|
|
|
2022-06-22 10:21:24 -07:00
|
|
|
self = self.wrap_binder(&trait_ref, |trait_ref, mut cx| {
|
|
|
|
define_scoped_cx!(cx);
|
2022-06-21 21:27:15 -07:00
|
|
|
p!(print(trait_ref.print_only_trait_name()));
|
2021-11-19 20:51:19 -08:00
|
|
|
|
2022-06-21 21:27:15 -07:00
|
|
|
let generics = tcx.generics_of(trait_ref.def_id);
|
|
|
|
let args = generics.own_substs_no_defaults(tcx, trait_ref.substs);
|
2021-11-19 20:51:19 -08:00
|
|
|
|
2022-06-21 21:27:15 -07:00
|
|
|
if !args.is_empty() || !assoc_items.is_empty() {
|
|
|
|
let mut first = true;
|
|
|
|
|
|
|
|
for ty in args {
|
|
|
|
if first {
|
|
|
|
p!("<");
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
p!(", ");
|
|
|
|
}
|
|
|
|
p!(print(ty));
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
|
|
|
|
2022-06-21 21:27:15 -07:00
|
|
|
for (assoc_item_def_id, term) in assoc_items {
|
|
|
|
// Skip printing `<[generator@] as Generator<_>>::Return` from async blocks,
|
|
|
|
// unless we can find out what generator return type it comes from.
|
|
|
|
let term = if let Some(ty) = term.skip_binder().ty()
|
2022-09-08 02:34:22 +00:00
|
|
|
&& let ty::Projection(proj) = ty.kind()
|
|
|
|
&& let assoc = tcx.associated_item(proj.item_def_id)
|
|
|
|
&& assoc.trait_container(tcx) == tcx.lang_items().gen_trait()
|
|
|
|
&& assoc.name == rustc_span::sym::Return
|
2022-06-21 21:27:15 -07:00
|
|
|
{
|
|
|
|
if let ty::Generator(_, substs, _) = substs.type_at(0).kind() {
|
|
|
|
let return_ty = substs.as_generator().return_ty();
|
2022-09-08 02:42:00 +00:00
|
|
|
if !return_ty.is_ty_var() {
|
2022-06-21 21:27:15 -07:00
|
|
|
return_ty.into()
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2022-03-30 19:26:35 -07:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-21 21:27:15 -07:00
|
|
|
term.skip_binder()
|
|
|
|
};
|
2022-03-22 19:25:43 -07:00
|
|
|
|
2022-06-21 21:27:15 -07:00
|
|
|
if first {
|
|
|
|
p!("<");
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
p!(", ");
|
|
|
|
}
|
2022-03-22 19:25:43 -07:00
|
|
|
|
2022-06-21 21:27:15 -07:00
|
|
|
p!(write("{} = ", tcx.associated_item(assoc_item_def_id).name));
|
2021-11-21 21:15:57 -08:00
|
|
|
|
2022-09-05 14:03:53 +10:00
|
|
|
match term.unpack() {
|
|
|
|
TermKind::Ty(ty) => p!(print(ty)),
|
|
|
|
TermKind::Const(c) => p!(print(c)),
|
2022-06-21 21:27:15 -07:00
|
|
|
};
|
|
|
|
}
|
2021-11-19 20:51:19 -08:00
|
|
|
|
2022-06-21 21:27:15 -07:00
|
|
|
if !first {
|
|
|
|
p!(">");
|
|
|
|
}
|
2022-03-22 19:25:43 -07:00
|
|
|
}
|
2021-11-19 20:51:19 -08:00
|
|
|
|
2022-06-21 21:27:15 -07:00
|
|
|
first = false;
|
2022-06-22 10:21:24 -07:00
|
|
|
Ok(cx)
|
2022-06-21 21:27:15 -07:00
|
|
|
})?;
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if !is_sized {
|
2022-06-22 10:21:24 -07:00
|
|
|
write!(self, "{}?Sized", if first { "" } else { " + " })?;
|
2021-11-19 20:51:19 -08:00
|
|
|
} else if first {
|
2022-06-22 10:21:24 -07:00
|
|
|
write!(self, "Sized")?;
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert the trait ref and optionally a projection type associated with it into either the
|
|
|
|
/// traits map or fn_traits map, depending on if the trait is in the Fn* family of traits.
|
|
|
|
fn insert_trait_and_projection(
|
|
|
|
&mut self,
|
|
|
|
trait_ref: ty::PolyTraitRef<'tcx>,
|
2022-01-10 23:39:21 +00:00
|
|
|
proj_ty: Option<(DefId, ty::Binder<'tcx, Term<'tcx>>)>,
|
2022-05-11 16:36:26 -04:00
|
|
|
traits: &mut FxIndexMap<
|
2022-01-10 23:39:21 +00:00
|
|
|
ty::PolyTraitRef<'tcx>,
|
2022-05-11 16:36:26 -04:00
|
|
|
FxIndexMap<DefId, ty::Binder<'tcx, Term<'tcx>>>,
|
2022-01-10 23:39:21 +00:00
|
|
|
>,
|
2022-05-11 16:36:26 -04:00
|
|
|
fn_traits: &mut FxIndexMap<ty::PolyTraitRef<'tcx>, OpaqueFnEntry<'tcx>>,
|
2021-11-19 20:51:19 -08:00
|
|
|
) {
|
|
|
|
let trait_def_id = trait_ref.def_id();
|
|
|
|
|
|
|
|
// If our trait_ref is FnOnce or any of its children, project it onto the parent FnOnce
|
|
|
|
// super-trait ref and record it there.
|
|
|
|
if let Some(fn_once_trait) = self.tcx().lang_items().fn_once_trait() {
|
|
|
|
// If we have a FnOnce, then insert it into
|
|
|
|
if trait_def_id == fn_once_trait {
|
|
|
|
let entry = fn_traits.entry(trait_ref).or_default();
|
|
|
|
// Optionally insert the return_ty as well.
|
|
|
|
if let Some((_, ty)) = proj_ty {
|
|
|
|
entry.return_ty = Some(ty);
|
|
|
|
}
|
2021-11-23 10:34:01 -08:00
|
|
|
entry.has_fn_once = true;
|
2021-11-19 20:51:19 -08:00
|
|
|
return;
|
|
|
|
} else if Some(trait_def_id) == self.tcx().lang_items().fn_mut_trait() {
|
|
|
|
let super_trait_ref = crate::traits::util::supertraits(self.tcx(), trait_ref)
|
|
|
|
.find(|super_trait_ref| super_trait_ref.def_id() == fn_once_trait)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
fn_traits.entry(super_trait_ref).or_default().fn_mut_trait_ref = Some(trait_ref);
|
|
|
|
return;
|
|
|
|
} else if Some(trait_def_id) == self.tcx().lang_items().fn_trait() {
|
|
|
|
let super_trait_ref = crate::traits::util::supertraits(self.tcx(), trait_ref)
|
|
|
|
.find(|super_trait_ref| super_trait_ref.def_id() == fn_once_trait)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
fn_traits.entry(super_trait_ref).or_default().fn_trait_ref = Some(trait_ref);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, just group our traits and projection types.
|
|
|
|
traits.entry(trait_ref).or_default().extend(proj_ty);
|
|
|
|
}
|
|
|
|
|
2020-03-13 10:48:41 +01:00
|
|
|
fn pretty_print_bound_var(
|
|
|
|
&mut self,
|
|
|
|
debruijn: ty::DebruijnIndex,
|
|
|
|
var: ty::BoundVar,
|
|
|
|
) -> Result<(), Self::Error> {
|
|
|
|
if debruijn == ty::INNERMOST {
|
|
|
|
write!(self, "^{}", var.index())
|
|
|
|
} else {
|
|
|
|
write!(self, "^{}_{}", debruijn.index(), var.index())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 04:09:20 +09:00
|
|
|
fn ty_infer_name(&self, _: ty::TyVid) -> Option<Symbol> {
|
2022-02-14 14:13:02 +01:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-07-17 04:09:20 +09:00
|
|
|
fn const_infer_name(&self, _: ty::ConstVid<'tcx>) -> Option<Symbol> {
|
2019-05-30 15:33:13 -07:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:47:02 +02:00
|
|
|
fn pretty_print_dyn_existential(
|
|
|
|
mut self,
|
2020-10-05 16:51:33 -04:00
|
|
|
predicates: &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
|
2019-01-25 12:11:50 +02:00
|
|
|
) -> Result<Self::DynExistential, Self::Error> {
|
2019-01-24 20:47:02 +02:00
|
|
|
// Generate the main trait ref, including associated types.
|
|
|
|
let mut first = true;
|
|
|
|
|
|
|
|
if let Some(principal) = predicates.principal() {
|
2020-12-11 15:02:46 -05:00
|
|
|
self = self.wrap_binder(&principal, |principal, mut cx| {
|
|
|
|
define_scoped_cx!(cx);
|
|
|
|
p!(print_def_path(principal.def_id, &[]));
|
|
|
|
|
|
|
|
let mut resugared = false;
|
|
|
|
|
2022-03-30 15:14:15 -04:00
|
|
|
// Special-case `Fn(...) -> ...` and re-sugar it.
|
2020-12-11 15:02:46 -05:00
|
|
|
let fn_trait_kind = cx.tcx().fn_trait_kind_from_lang_item(principal.def_id);
|
|
|
|
if !cx.tcx().sess.verbose() && fn_trait_kind.is_some() {
|
2022-02-07 16:06:31 +01:00
|
|
|
if let ty::Tuple(tys) = principal.substs.type_at(0).kind() {
|
2020-12-11 15:02:46 -05:00
|
|
|
let mut projections = predicates.projection_bounds();
|
|
|
|
if let (Some(proj), None) = (projections.next(), projections.next()) {
|
2022-01-13 07:39:58 +00:00
|
|
|
p!(pretty_fn_sig(
|
2022-02-07 16:06:31 +01:00
|
|
|
tys,
|
2022-01-13 07:39:58 +00:00
|
|
|
false,
|
|
|
|
proj.skip_binder().term.ty().expect("Return type was a const")
|
|
|
|
));
|
2020-12-11 15:02:46 -05:00
|
|
|
resugared = true;
|
|
|
|
}
|
2019-01-24 20:47:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:02:46 -05:00
|
|
|
// 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.
|
|
|
|
let dummy_cx = cx.tcx().mk_ty_infer(ty::FreshTy(0));
|
|
|
|
let principal = principal.with_self_ty(cx.tcx(), dummy_cx);
|
|
|
|
|
2022-06-03 20:00:39 +02:00
|
|
|
let args = cx
|
|
|
|
.tcx()
|
|
|
|
.generics_of(principal.def_id)
|
|
|
|
.own_substs_no_defaults(cx.tcx(), principal.substs);
|
2020-12-11 15:02:46 -05:00
|
|
|
|
|
|
|
let mut projections = predicates.projection_bounds();
|
2019-01-24 20:47:02 +02:00
|
|
|
|
2022-09-11 14:37:55 +02:00
|
|
|
let mut args = args.iter().cloned();
|
2020-12-11 15:02:46 -05:00
|
|
|
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-24 20:47:02 +02:00
|
|
|
|
2020-12-11 15:02:46 -05:00
|
|
|
p!(generic_delimiters(|mut cx| {
|
|
|
|
cx = cx.comma_sep(args)?;
|
|
|
|
if arg0.is_some() && projection0.is_some() {
|
|
|
|
write!(cx, ", ")?;
|
|
|
|
}
|
|
|
|
cx.comma_sep(projections)
|
|
|
|
}));
|
|
|
|
}
|
2019-01-24 20:47:02 +02:00
|
|
|
}
|
2020-12-11 15:02:46 -05:00
|
|
|
Ok(cx)
|
|
|
|
})?;
|
|
|
|
|
2019-01-24 20:47:02 +02:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:02:46 -05:00
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
2019-01-24 20:47:02 +02:00
|
|
|
// Builtin bounds.
|
|
|
|
// FIXME(eddyb) avoid printing twice (needed to ensure
|
|
|
|
// that the auto traits are sorted *and* printed via cx).
|
2022-05-11 16:36:26 -04:00
|
|
|
let mut auto_traits: Vec<_> = predicates.auto_traits().collect();
|
2019-01-24 20:47:02 +02:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
2022-05-11 16:36:26 -04:00
|
|
|
// To avoid causing instabilities in compiletest
|
2019-01-24 20:47:02 +02:00
|
|
|
// output, sort the auto-traits alphabetically.
|
2022-05-11 16:36:26 -04:00
|
|
|
auto_traits.sort_by_cached_key(|did| self.tcx().def_path_str(*did));
|
2019-01-24 20:47:02 +02:00
|
|
|
|
2022-05-11 16:36:26 -04:00
|
|
|
for def_id in auto_traits {
|
2019-01-24 20:47:02 +02:00
|
|
|
if !first {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(" + ");
|
2019-01-24 20:47:02 +02:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("(", comma_sep(inputs.iter().copied()));
|
2020-04-17 03:54:13 +03:00
|
|
|
if c_variadic {
|
|
|
|
if !inputs.is_empty() {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(", ");
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("...");
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(")");
|
2019-01-18 21:33:31 +02:00
|
|
|
if !output.is_unit() {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(" -> ", 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-03-18 12:50:57 +02:00
|
|
|
|
2020-01-24 16:22:24 +00:00
|
|
|
fn pretty_print_const(
|
|
|
|
mut self,
|
2022-02-02 14:24:45 +11:00
|
|
|
ct: ty::Const<'tcx>,
|
2020-01-24 16:22:24 +00:00
|
|
|
print_ty: bool,
|
|
|
|
) -> Result<Self::Const, Self::Error> {
|
2019-03-18 12:50:57 +02:00
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
2019-10-07 10:59:46 -04:00
|
|
|
if self.tcx().sess.verbose() {
|
2022-06-10 11:18:06 +10:00
|
|
|
p!(write("Const({:?}: {:?})", ct.kind(), ct.ty()));
|
2019-10-07 10:59:46 -04:00
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
|
2020-01-24 16:22:24 +00:00
|
|
|
macro_rules! print_underscore {
|
|
|
|
() => {{
|
|
|
|
if print_ty {
|
2020-03-12 13:35:44 +01:00
|
|
|
self = self.typed_value(
|
|
|
|
|mut this| {
|
|
|
|
write!(this, "_")?;
|
|
|
|
Ok(this)
|
|
|
|
},
|
2022-02-02 14:24:45 +11:00
|
|
|
|this| this.print_type(ct.ty()),
|
2020-03-12 13:35:44 +01:00
|
|
|
": ",
|
|
|
|
)?;
|
|
|
|
} else {
|
|
|
|
write!(self, "_")?;
|
2020-01-24 16:22:24 +00:00
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2022-06-10 11:18:06 +10:00
|
|
|
match ct.kind() {
|
2022-09-22 12:34:23 +02:00
|
|
|
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }) => {
|
2022-01-12 03:19:52 +00:00
|
|
|
match self.tcx().def_kind(def.did) {
|
2022-03-29 17:11:12 +02:00
|
|
|
DefKind::Static(..) | DefKind::Const | DefKind::AssocConst => {
|
2022-01-12 03:19:52 +00:00
|
|
|
p!(print_value_path(def.did, substs))
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
if def.is_local() {
|
|
|
|
let span = self.tcx().def_span(def.did);
|
|
|
|
if let Ok(snip) = self.tcx().sess.source_map().span_to_snippet(span) {
|
|
|
|
p!(write("{}", snip))
|
2019-11-22 17:26:09 -03:00
|
|
|
} else {
|
2020-01-24 16:22:24 +00:00
|
|
|
print_underscore!()
|
2019-11-22 17:26:09 -03:00
|
|
|
}
|
2022-01-12 03:19:52 +00:00
|
|
|
} else {
|
|
|
|
print_underscore!()
|
2019-10-05 12:57:12 +13:00
|
|
|
}
|
|
|
|
}
|
2019-03-18 12:50:57 +02:00
|
|
|
}
|
2019-11-22 17:26:09 -03:00
|
|
|
}
|
2022-02-14 14:13:02 +01:00
|
|
|
ty::ConstKind::Infer(infer_ct) => {
|
|
|
|
match infer_ct {
|
|
|
|
ty::InferConst::Var(ct_vid)
|
|
|
|
if let Some(name) = self.const_infer_name(ct_vid) =>
|
|
|
|
p!(write("{}", name)),
|
|
|
|
_ => print_underscore!(),
|
|
|
|
}
|
|
|
|
}
|
2020-03-12 13:35:44 +01:00
|
|
|
ty::ConstKind::Param(ParamConst { name, .. }) => p!(write("{}", name)),
|
|
|
|
ty::ConstKind::Value(value) => {
|
2022-02-16 10:56:01 +01:00
|
|
|
return self.pretty_print_const_valtree(value, ct.ty(), print_ty);
|
2020-01-24 16:22:24 +00:00
|
|
|
}
|
2019-11-08 21:55:19 +01:00
|
|
|
|
2020-03-13 10:48:41 +01:00
|
|
|
ty::ConstKind::Bound(debruijn, bound_var) => {
|
|
|
|
self.pretty_print_bound_var(debruijn, bound_var)?
|
|
|
|
}
|
2020-03-13 10:55:57 +01:00
|
|
|
ty::ConstKind::Placeholder(placeholder) => p!(write("Placeholder({:?})", placeholder)),
|
2020-09-30 10:07:15 -06:00
|
|
|
ty::ConstKind::Error(_) => p!("[const error]"),
|
2019-11-08 21:55:19 +01:00
|
|
|
};
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2019-12-23 17:41:06 +01:00
|
|
|
fn pretty_print_const_scalar(
|
2021-03-02 14:54:10 +00:00
|
|
|
self,
|
2019-12-23 17:41:06 +01:00
|
|
|
scalar: Scalar,
|
2019-11-08 21:55:19 +01:00
|
|
|
ty: Ty<'tcx>,
|
2020-01-24 16:22:24 +00:00
|
|
|
print_ty: bool,
|
2021-03-02 14:54:10 +00:00
|
|
|
) -> Result<Self::Const, Self::Error> {
|
|
|
|
match scalar {
|
2021-07-12 20:29:05 +02:00
|
|
|
Scalar::Ptr(ptr, _size) => self.pretty_print_const_scalar_ptr(ptr, ty, print_ty),
|
2021-03-02 14:54:10 +00:00
|
|
|
Scalar::Int(int) => self.pretty_print_const_scalar_int(int, ty, print_ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pretty_print_const_scalar_ptr(
|
|
|
|
mut self,
|
|
|
|
ptr: Pointer,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
print_ty: bool,
|
2019-11-08 21:55:19 +01:00
|
|
|
) -> Result<Self::Const, Self::Error> {
|
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
2021-07-12 18:22:15 +02:00
|
|
|
let (alloc_id, offset) = ptr.into_parts();
|
2021-03-02 14:54:10 +00:00
|
|
|
match ty.kind() {
|
2019-12-23 17:41:06 +01:00
|
|
|
// Byte strings (&[u8; N])
|
2022-04-08 15:57:44 +00:00
|
|
|
ty::Ref(_, inner, _) => {
|
|
|
|
if let ty::Array(elem, len) = inner.kind() {
|
|
|
|
if let ty::Uint(ty::UintTy::U8) = elem.kind() {
|
2022-02-16 10:56:01 +01:00
|
|
|
if let ty::ConstKind::Value(ty::ValTree::Leaf(int)) = len.kind() {
|
2022-07-17 11:40:34 -04:00
|
|
|
match self.tcx().try_get_global_alloc(alloc_id) {
|
2022-04-08 15:57:44 +00:00
|
|
|
Some(GlobalAlloc::Memory(alloc)) => {
|
|
|
|
let len = int.assert_bits(self.tcx().data_layout.pointer_size);
|
|
|
|
let range =
|
|
|
|
AllocRange { start: offset, size: Size::from_bytes(len) };
|
|
|
|
if let Ok(byte_str) =
|
2022-08-27 14:54:02 -04:00
|
|
|
alloc.inner().get_bytes_strip_provenance(&self.tcx(), range)
|
2022-04-08 15:57:44 +00:00
|
|
|
{
|
|
|
|
p!(pretty_print_byte_str(byte_str))
|
|
|
|
} else {
|
|
|
|
p!("<too short allocation>")
|
|
|
|
}
|
|
|
|
}
|
2022-07-17 11:36:37 -04:00
|
|
|
// FIXME: for statics, vtables, and functions, we could in principle print more detail.
|
2022-04-08 15:57:44 +00:00
|
|
|
Some(GlobalAlloc::Static(def_id)) => {
|
|
|
|
p!(write("<static({:?})>", def_id))
|
|
|
|
}
|
|
|
|
Some(GlobalAlloc::Function(_)) => p!("<function>"),
|
2022-07-19 19:57:44 -04:00
|
|
|
Some(GlobalAlloc::VTable(..)) => p!("<vtable>"),
|
2022-04-08 15:57:44 +00:00
|
|
|
None => p!("<dangling pointer>"),
|
|
|
|
}
|
|
|
|
return Ok(self);
|
|
|
|
}
|
2020-05-25 08:52:16 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-08 15:57:44 +00:00
|
|
|
}
|
2021-03-02 14:54:10 +00:00
|
|
|
ty::FnPtr(_) => {
|
|
|
|
// FIXME: We should probably have a helper method to share code with the "Byte strings"
|
|
|
|
// printing above (which also has to handle pointers to all sorts of things).
|
2022-07-17 11:40:34 -04:00
|
|
|
if let Some(GlobalAlloc::Function(instance)) =
|
|
|
|
self.tcx().try_get_global_alloc(alloc_id)
|
2022-04-08 15:57:44 +00:00
|
|
|
{
|
|
|
|
self = self.typed_value(
|
|
|
|
|this| this.print_value_path(instance.def_id(), instance.substs),
|
|
|
|
|this| this.print_type(ty),
|
|
|
|
" as ",
|
|
|
|
)?;
|
|
|
|
return Ok(self);
|
2021-03-02 14:54:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-08 15:57:44 +00:00
|
|
|
_ => {}
|
2021-03-02 14:54:10 +00:00
|
|
|
}
|
2022-04-08 15:57:44 +00:00
|
|
|
// Any pointer values not covered by a branch above
|
|
|
|
self = self.pretty_print_const_pointer(ptr, ty, print_ty)?;
|
2021-03-02 14:54:10 +00:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pretty_print_const_scalar_int(
|
|
|
|
mut self,
|
|
|
|
int: ScalarInt,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
print_ty: bool,
|
|
|
|
) -> Result<Self::Const, Self::Error> {
|
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
|
|
|
match ty.kind() {
|
2019-12-23 17:41:06 +01:00
|
|
|
// Bool
|
2021-03-02 14:54:10 +00:00
|
|
|
ty::Bool if int == ScalarInt::FALSE => p!("false"),
|
|
|
|
ty::Bool if int == ScalarInt::TRUE => p!("true"),
|
2019-12-23 17:41:06 +01:00
|
|
|
// Float
|
2021-03-02 14:54:10 +00:00
|
|
|
ty::Float(ty::FloatTy::F32) => {
|
2020-09-26 15:15:35 +02:00
|
|
|
p!(write("{}f32", Single::try_from(int).unwrap()))
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2021-03-02 14:54:10 +00:00
|
|
|
ty::Float(ty::FloatTy::F64) => {
|
2020-09-26 15:15:35 +02:00
|
|
|
p!(write("{}f64", Double::try_from(int).unwrap()))
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-12-23 17:41:06 +01:00
|
|
|
// Int
|
2021-03-02 14:54:10 +00:00
|
|
|
ty::Uint(_) | ty::Int(_) => {
|
2020-09-26 15:15:35 +02:00
|
|
|
let int =
|
|
|
|
ConstInt::new(int, matches!(ty.kind(), ty::Int(_)), ty.is_ptr_sized_integral());
|
2022-06-14 17:34:37 +02:00
|
|
|
if print_ty { p!(write("{:#?}", int)) } else { p!(write("{:?}", int)) }
|
2019-10-05 12:57:12 +13:00
|
|
|
}
|
2019-12-23 17:41:06 +01:00
|
|
|
// Char
|
2021-03-02 14:54:10 +00:00
|
|
|
ty::Char if char::try_from(int).is_ok() => {
|
2020-09-26 15:15:35 +02:00
|
|
|
p!(write("{:?}", char::try_from(int).unwrap()))
|
2020-02-12 13:30:31 +01:00
|
|
|
}
|
2021-07-12 18:22:15 +02:00
|
|
|
// Pointer types
|
|
|
|
ty::Ref(..) | ty::RawPtr(_) | ty::FnPtr(_) => {
|
2020-09-26 15:15:35 +02:00
|
|
|
let data = int.assert_bits(self.tcx().data_layout.pointer_size);
|
2020-02-13 19:02:58 +01:00
|
|
|
self = self.typed_value(
|
|
|
|
|mut this| {
|
|
|
|
write!(this, "0x{:x}", data)?;
|
|
|
|
Ok(this)
|
|
|
|
},
|
|
|
|
|this| this.print_type(ty),
|
2020-02-26 19:36:10 +01:00
|
|
|
" as ",
|
2020-02-13 19:02:58 +01:00
|
|
|
)?;
|
2019-12-23 17:41:06 +01:00
|
|
|
}
|
|
|
|
// Nontrivial types with scalar bit representation
|
2021-03-02 14:54:10 +00:00
|
|
|
_ => {
|
2020-02-05 11:00:52 +01:00
|
|
|
let print = |mut this: Self| {
|
2020-09-26 15:15:35 +02:00
|
|
|
if int.size() == Size::ZERO {
|
2020-02-12 13:47:00 +01:00
|
|
|
write!(this, "transmute(())")?;
|
|
|
|
} else {
|
2020-09-26 15:15:35 +02:00
|
|
|
write!(this, "transmute(0x{:x})", int)?;
|
2020-02-12 13:47:00 +01:00
|
|
|
}
|
2020-02-05 11:00:52 +01:00
|
|
|
Ok(this)
|
|
|
|
};
|
|
|
|
self = if print_ty {
|
2020-02-26 19:36:10 +01:00
|
|
|
self.typed_value(print, |this| this.print_type(ty), ": ")?
|
2020-02-05 11:00:52 +01:00
|
|
|
} else {
|
|
|
|
print(self)?
|
|
|
|
};
|
2019-12-23 17:41:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is overridden for MIR printing because we only want to hide alloc ids from users, not
|
|
|
|
/// from MIR where it is actually useful.
|
2022-07-18 18:47:31 -04:00
|
|
|
fn pretty_print_const_pointer<Prov: Provenance>(
|
2020-02-05 11:00:52 +01:00
|
|
|
mut self,
|
2022-07-18 18:47:31 -04:00
|
|
|
_: Pointer<Prov>,
|
2019-12-23 17:41:06 +01:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
print_ty: bool,
|
|
|
|
) -> Result<Self::Const, Self::Error> {
|
2020-02-05 11:00:52 +01:00
|
|
|
if print_ty {
|
2020-02-13 19:02:58 +01:00
|
|
|
self.typed_value(
|
2020-02-05 11:00:52 +01:00
|
|
|
|mut this| {
|
|
|
|
this.write_str("&_")?;
|
|
|
|
Ok(this)
|
|
|
|
},
|
|
|
|
|this| this.print_type(ty),
|
2020-02-26 19:36:10 +01:00
|
|
|
": ",
|
2020-02-05 11:00:52 +01:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
self.write_str("&_")?;
|
|
|
|
Ok(self)
|
|
|
|
}
|
2019-12-23 17:41:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn pretty_print_byte_str(mut self, byte_str: &'tcx [u8]) -> Result<Self::Const, Self::Error> {
|
2022-08-19 18:53:27 +02:00
|
|
|
write!(self, "b\"{}\"", byte_str.escape_ascii())?;
|
2019-12-23 17:41:06 +01:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2022-02-16 10:56:01 +01:00
|
|
|
fn pretty_print_const_valtree(
|
2019-12-23 17:41:06 +01:00
|
|
|
mut self,
|
2022-02-16 10:56:01 +01:00
|
|
|
valtree: ty::ValTree<'tcx>,
|
2019-12-23 17:41:06 +01:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
print_ty: bool,
|
|
|
|
) -> Result<Self::Const, Self::Error> {
|
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
|
|
|
if self.tcx().sess.verbose() {
|
2022-02-16 10:56:01 +01:00
|
|
|
p!(write("ValTree({:?}: ", valtree), print(ty), ")");
|
2019-12-23 17:41:06 +01:00
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
let u8_type = self.tcx().types.u8;
|
2022-02-16 10:56:01 +01:00
|
|
|
match (valtree, ty.kind()) {
|
|
|
|
(ty::ValTree::Branch(_), ty::Ref(_, inner_ty, _)) => match inner_ty.kind() {
|
|
|
|
ty::Slice(t) if *t == u8_type => {
|
|
|
|
let bytes = valtree.try_to_raw_bytes(self.tcx(), ty).unwrap_or_else(|| {
|
|
|
|
bug!(
|
|
|
|
"expected to convert valtree {:?} to raw bytes for type {:?}",
|
|
|
|
valtree,
|
|
|
|
t
|
|
|
|
)
|
|
|
|
});
|
|
|
|
return self.pretty_print_byte_str(bytes);
|
2022-04-08 15:57:44 +00:00
|
|
|
}
|
2022-02-16 10:56:01 +01:00
|
|
|
ty::Str => {
|
|
|
|
let bytes = valtree.try_to_raw_bytes(self.tcx(), ty).unwrap_or_else(|| {
|
|
|
|
bug!("expected to convert valtree to raw bytes for type {:?}", ty)
|
|
|
|
});
|
|
|
|
p!(write("{:?}", String::from_utf8_lossy(bytes)));
|
|
|
|
return Ok(self);
|
|
|
|
}
|
2022-06-28 22:35:48 +02:00
|
|
|
_ => {
|
|
|
|
p!("&");
|
|
|
|
p!(pretty_print_const_valtree(valtree, *inner_ty, print_ty));
|
|
|
|
return Ok(self);
|
|
|
|
}
|
2022-02-16 10:56:01 +01:00
|
|
|
},
|
|
|
|
(ty::ValTree::Branch(_), ty::Array(t, _)) if *t == u8_type => {
|
2022-07-26 07:05:31 +00:00
|
|
|
let bytes = valtree.try_to_raw_bytes(self.tcx(), ty).unwrap_or_else(|| {
|
2022-02-16 10:56:01 +01:00
|
|
|
bug!("expected to convert valtree to raw bytes for type {:?}", t)
|
|
|
|
});
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("*");
|
2022-02-16 10:56:01 +01:00
|
|
|
p!(pretty_print_byte_str(bytes));
|
2022-04-08 15:57:44 +00:00
|
|
|
return Ok(self);
|
2019-12-23 17:41:06 +01:00
|
|
|
}
|
2020-04-17 03:55:39 +03:00
|
|
|
// Aggregates, printed as array/tuple/struct/variant construction syntax.
|
2022-06-02 19:42:29 +02:00
|
|
|
(ty::ValTree::Branch(_), ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) => {
|
2022-06-28 22:35:48 +02:00
|
|
|
let contents =
|
|
|
|
self.tcx().destructure_const(ty::Const::from_value(self.tcx(), valtree, ty));
|
2020-04-17 03:55:39 +03:00
|
|
|
let fields = contents.fields.iter().copied();
|
2020-08-03 00:49:11 +02:00
|
|
|
match *ty.kind() {
|
2020-04-17 03:55:39 +03:00
|
|
|
ty::Array(..) => {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("[", comma_sep(fields), "]");
|
2020-04-17 03:55:39 +03:00
|
|
|
}
|
|
|
|
ty::Tuple(..) => {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("(", comma_sep(fields));
|
2020-04-17 03:55:39 +03:00
|
|
|
if contents.fields.len() == 1 {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(",");
|
2020-04-17 03:55:39 +03:00
|
|
|
}
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(")");
|
2020-04-17 03:55:39 +03:00
|
|
|
}
|
2022-03-05 07:28:41 +11:00
|
|
|
ty::Adt(def, _) if def.variants().is_empty() => {
|
2021-07-16 14:12:18 +03:00
|
|
|
self = self.typed_value(
|
|
|
|
|mut this| {
|
|
|
|
write!(this, "unreachable()")?;
|
|
|
|
Ok(this)
|
|
|
|
},
|
|
|
|
|this| this.print_type(ty),
|
|
|
|
": ",
|
|
|
|
)?;
|
2020-06-18 10:37:59 +01:00
|
|
|
}
|
2020-04-17 03:55:39 +03:00
|
|
|
ty::Adt(def, substs) => {
|
2021-07-16 15:40:33 +03:00
|
|
|
let variant_idx =
|
|
|
|
contents.variant.expect("destructed const of adt without variant idx");
|
2022-03-05 07:28:41 +11:00
|
|
|
let variant_def = &def.variant(variant_idx);
|
2020-04-17 03:55:39 +03:00
|
|
|
p!(print_value_path(variant_def.def_id, substs));
|
|
|
|
match variant_def.ctor_kind {
|
|
|
|
CtorKind::Const => {}
|
|
|
|
CtorKind::Fn => {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("(", comma_sep(fields), ")");
|
2020-04-17 03:55:39 +03:00
|
|
|
}
|
|
|
|
CtorKind::Fictive => {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(" {{ ");
|
2020-04-17 03:55:39 +03:00
|
|
|
let mut first = true;
|
2021-03-08 15:32:41 -08:00
|
|
|
for (field_def, field) in iter::zip(&variant_def.fields, fields) {
|
2020-04-17 03:55:39 +03:00
|
|
|
if !first {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(", ");
|
2020-04-17 03:55:39 +03:00
|
|
|
}
|
2022-01-02 22:37:05 -05:00
|
|
|
p!(write("{}: ", field_def.name), print(field));
|
2020-04-17 03:55:39 +03:00
|
|
|
first = false;
|
|
|
|
}
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(" }}");
|
2020-04-17 03:55:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2022-04-08 15:57:44 +00:00
|
|
|
return Ok(self);
|
2020-04-17 03:55:39 +03:00
|
|
|
}
|
2022-08-09 19:12:14 +02:00
|
|
|
(ty::ValTree::Leaf(leaf), ty::Ref(_, inner_ty, _)) => {
|
|
|
|
p!(write("&"));
|
|
|
|
return self.pretty_print_const_scalar_int(leaf, *inner_ty, print_ty);
|
|
|
|
}
|
2022-02-16 10:56:01 +01:00
|
|
|
(ty::ValTree::Leaf(leaf), _) => {
|
|
|
|
return self.pretty_print_const_scalar_int(leaf, ty, print_ty);
|
2022-04-08 15:57:44 +00:00
|
|
|
}
|
2019-12-23 17:41:06 +01:00
|
|
|
// FIXME(oli-obk): also pretty print arrays and other aggregate constants by reading
|
|
|
|
// their fields instead of just dumping the memory.
|
2022-04-08 15:57:44 +00:00
|
|
|
_ => {}
|
2019-12-23 17:41:06 +01:00
|
|
|
}
|
2022-04-08 15:57:44 +00:00
|
|
|
|
|
|
|
// fallback
|
2022-06-03 20:42:35 +02:00
|
|
|
if valtree == ty::ValTree::zst() {
|
|
|
|
p!(write("<ZST>"));
|
|
|
|
} else {
|
2022-02-16 10:56:01 +01:00
|
|
|
p!(write("{:?}", valtree));
|
|
|
|
}
|
2022-04-08 15:57:44 +00:00
|
|
|
if print_ty {
|
|
|
|
p!(": ", print(ty));
|
|
|
|
}
|
|
|
|
Ok(self)
|
2019-03-18 12:50:57 +02:00
|
|
|
}
|
2022-08-26 01:25:56 +00:00
|
|
|
|
|
|
|
fn pretty_closure_as_impl(
|
|
|
|
mut self,
|
|
|
|
closure: ty::ClosureSubsts<'tcx>,
|
|
|
|
) -> Result<Self::Const, Self::Error> {
|
|
|
|
let sig = closure.sig();
|
|
|
|
let kind = closure.kind_ty().to_opt_closure_kind().unwrap_or(ty::ClosureKind::Fn);
|
|
|
|
|
|
|
|
write!(self, "impl ")?;
|
|
|
|
self.wrap_binder(&sig, |sig, mut cx| {
|
|
|
|
define_scoped_cx!(cx);
|
|
|
|
|
|
|
|
p!(print(kind), "(");
|
|
|
|
for (i, arg) in sig.inputs()[0].tuple_fields().iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
p!(", ");
|
|
|
|
}
|
|
|
|
p!(print(arg));
|
|
|
|
}
|
|
|
|
p!(")");
|
|
|
|
|
|
|
|
if !sig.output().is_unit() {
|
|
|
|
p!(" -> ", print(sig.output()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(cx)
|
|
|
|
})
|
|
|
|
}
|
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.
|
2022-02-18 16:15:29 -05:00
|
|
|
pub struct FmtPrinter<'a, 'tcx>(Box<FmtPrinterData<'a, 'tcx>>);
|
2019-01-18 21:33:31 +02:00
|
|
|
|
2022-02-18 16:15:29 -05:00
|
|
|
pub struct FmtPrinterData<'a, 'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2022-02-18 16:15:29 -05:00
|
|
|
fmt: String,
|
2019-01-25 12:11:50 +02:00
|
|
|
|
|
|
|
empty_path: bool,
|
|
|
|
in_value: bool,
|
2019-12-23 17:41:06 +01:00
|
|
|
pub print_alloc_ids: bool,
|
2019-01-25 12:11:50 +02:00
|
|
|
|
2022-09-15 18:46:40 +02:00
|
|
|
// set of all named (non-anonymous) region names
|
2019-10-18 13:22:50 +11:00
|
|
|
used_region_names: FxHashSet<Symbol>,
|
2022-09-15 18:46:40 +02:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
region_index: usize,
|
|
|
|
binder_depth: usize,
|
2020-09-15 18:22:24 -05:00
|
|
|
printed_type_count: usize,
|
2019-01-25 12:11:50 +02:00
|
|
|
|
2022-01-28 11:25:15 +11:00
|
|
|
pub region_highlight_mode: RegionHighlightMode<'tcx>,
|
2019-05-30 15:33:13 -07:00
|
|
|
|
2022-07-17 04:09:20 +09:00
|
|
|
pub ty_infer_name_resolver: Option<Box<dyn Fn(ty::TyVid) -> Option<Symbol> + 'a>>,
|
|
|
|
pub const_infer_name_resolver: Option<Box<dyn Fn(ty::ConstVid<'tcx>) -> Option<Symbol> + 'a>>,
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
2022-02-18 16:15:29 -05:00
|
|
|
impl<'a, 'tcx> Deref for FmtPrinter<'a, 'tcx> {
|
|
|
|
type Target = FmtPrinterData<'a, 'tcx>;
|
2019-01-25 12:11:50 +02:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 16:15:29 -05:00
|
|
|
impl DerefMut for FmtPrinter<'_, '_> {
|
2019-01-25 12:11:50 +02:00
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 16:15:29 -05:00
|
|
|
impl<'a, 'tcx> FmtPrinter<'a, 'tcx> {
|
|
|
|
pub fn new(tcx: TyCtxt<'tcx>, ns: Namespace) -> Self {
|
2019-01-25 12:11:50 +02:00
|
|
|
FmtPrinter(Box::new(FmtPrinterData {
|
|
|
|
tcx,
|
2022-02-19 14:49:35 -05:00
|
|
|
// Estimated reasonable capacity to allocate upfront based on a few
|
|
|
|
// benchmarks.
|
|
|
|
fmt: String::with_capacity(64),
|
2019-01-25 12:11:50 +02:00
|
|
|
empty_path: false,
|
|
|
|
in_value: ns == Namespace::ValueNS,
|
2019-12-23 17:41:06 +01:00
|
|
|
print_alloc_ids: false,
|
2019-01-25 12:11:50 +02:00
|
|
|
used_region_names: Default::default(),
|
|
|
|
region_index: 0,
|
|
|
|
binder_depth: 0,
|
2020-09-15 18:22:24 -05:00
|
|
|
printed_type_count: 0,
|
2022-01-28 11:25:15 +11:00
|
|
|
region_highlight_mode: RegionHighlightMode::new(tcx),
|
2022-02-14 14:13:02 +01:00
|
|
|
ty_infer_name_resolver: None,
|
|
|
|
const_infer_name_resolver: None,
|
2019-01-25 12:11:50 +02:00
|
|
|
}))
|
|
|
|
}
|
2022-02-18 16:15:29 -05:00
|
|
|
|
|
|
|
pub fn into_buffer(self) -> String {
|
|
|
|
self.0.fmt
|
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:59:02 +01:00
|
|
|
// 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(tcx: TyCtxt<'_>, def_id: DefId) -> Namespace {
|
|
|
|
match tcx.def_key(def_id).disambiguated_data.data {
|
|
|
|
DefPathData::TypeNs(..) | DefPathData::CrateRoot | DefPathData::ImplTrait => {
|
|
|
|
Namespace::TypeNS
|
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
|
2020-01-05 15:59:02 +01:00
|
|
|
DefPathData::ValueNs(..)
|
|
|
|
| DefPathData::AnonConst
|
|
|
|
| DefPathData::ClosureExpr
|
|
|
|
| DefPathData::Ctor => Namespace::ValueNS,
|
2019-05-03 22:45:36 +03:00
|
|
|
|
2020-01-05 15:59:02 +01:00
|
|
|
DefPathData::MacroNs(..) => Namespace::MacroNS,
|
2019-01-25 12:11:50 +02:00
|
|
|
|
2020-01-05 15:59:02 +01:00
|
|
|
_ => Namespace::TypeNS,
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
2020-01-05 15:59:02 +01:00
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'t> TyCtxt<'t> {
|
2019-01-25 12:11:50 +02:00
|
|
|
/// Returns a string identifying this `DefId`. This string is
|
|
|
|
/// suitable for user output.
|
|
|
|
pub fn def_path_str(self, def_id: DefId) -> String {
|
2019-11-23 16:01:20 -08:00
|
|
|
self.def_path_str_with_substs(def_id, &[])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn def_path_str_with_substs(self, def_id: DefId, substs: &'t [GenericArg<'t>]) -> String {
|
2020-01-05 15:59:02 +01:00
|
|
|
let ns = guess_def_namespace(self, def_id);
|
2019-01-25 12:11:50 +02:00
|
|
|
debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns);
|
2022-02-18 16:15:29 -05:00
|
|
|
FmtPrinter::new(self, ns).print_def_path(def_id, substs).unwrap().into_buffer()
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 16:15:29 -05:00
|
|
|
impl fmt::Write for FmtPrinter<'_, '_> {
|
2019-01-25 12:11:50 +02:00
|
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
2022-02-18 16:15:29 -05:00
|
|
|
self.fmt.push_str(s);
|
|
|
|
Ok(())
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 16:15:29 -05:00
|
|
|
impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
|
2019-01-25 12:11:50 +02:00
|
|
|
type Error = fmt::Error;
|
|
|
|
|
|
|
|
type Path = Self;
|
|
|
|
type Region = Self;
|
|
|
|
type Type = Self;
|
|
|
|
type DynExistential = Self;
|
2019-03-18 12:50:57 +02:00
|
|
|
type Const = Self;
|
2019-01-25 12:11:50 +02:00
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
|
2019-01-25 12:11:50 +02:00
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_def_path(
|
|
|
|
mut self,
|
|
|
|
def_id: DefId,
|
2019-09-25 16:39:44 +01:00
|
|
|
substs: &'tcx [GenericArg<'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() {
|
2020-09-02 10:40:56 +03:00
|
|
|
match self.try_print_trimmed_def_path(def_id)? {
|
|
|
|
(cx, true) => return Ok(cx),
|
|
|
|
(cx, false) => self = cx,
|
|
|
|
}
|
|
|
|
|
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);
|
2019-09-06 21:05:37 +01:00
|
|
|
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
|
|
|
|
};
|
2019-02-03 12:59:37 +02:00
|
|
|
|
2019-09-06 21:05:37 +01:00
|
|
|
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
|
|
|
|
2019-09-06 21:05:37 +01:00
|
|
|
self = self.print_def_path(parent_def_id, &[])?;
|
2019-02-03 12:59:37 +02:00
|
|
|
|
2019-09-06 21:05:37 +01:00
|
|
|
// HACK(eddyb) copy of `path_append` to avoid
|
|
|
|
// constructing a `DisambiguatedDefPathData`.
|
|
|
|
if !self.empty_path {
|
|
|
|
write!(self, "::")?;
|
2019-09-06 03:57:44 +01:00
|
|
|
}
|
2021-05-03 01:14:25 +01:00
|
|
|
write!(
|
|
|
|
self,
|
|
|
|
"<impl at {}>",
|
|
|
|
// This may end up in stderr diagnostics but it may also be emitted
|
|
|
|
// into MIR. Hence we use the remapped path if available
|
|
|
|
self.tcx.sess.source_map().span_to_embeddable_string(span)
|
|
|
|
)?;
|
2019-09-06 21:05:37 +01:00
|
|
|
self.empty_path = false;
|
|
|
|
|
|
|
|
return Ok(self);
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.default_print_def_path(def_id, substs)
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:20:27 -04:00
|
|
|
fn print_region(self, region: ty::Region<'tcx>) -> Result<Self::Region, Self::Error> {
|
2019-01-25 12:11:50 +02:00
|
|
|
self.pretty_print_region(region)
|
|
|
|
}
|
|
|
|
|
2020-09-15 18:22:24 -05:00
|
|
|
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
|
2021-07-04 13:02:51 -05:00
|
|
|
let type_length_limit = self.tcx.type_length_limit();
|
2021-03-23 12:41:26 +01:00
|
|
|
if type_length_limit.value_within_limit(self.printed_type_count) {
|
2020-09-15 18:22:24 -05:00
|
|
|
self.printed_type_count += 1;
|
|
|
|
self.pretty_print_type(ty)
|
|
|
|
} else {
|
|
|
|
write!(self, "...")?;
|
|
|
|
Ok(self)
|
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn print_dyn_existential(
|
|
|
|
self,
|
2020-10-05 16:51:33 -04:00
|
|
|
predicates: &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
|
2019-01-25 12:11:50 +02:00
|
|
|
) -> Result<Self::DynExistential, Self::Error> {
|
|
|
|
self.pretty_print_dyn_existential(predicates)
|
|
|
|
}
|
|
|
|
|
2022-02-02 14:24:45 +11:00
|
|
|
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
|
2022-07-19 02:25:14 +03:00
|
|
|
self.pretty_print_const(ct, false)
|
2019-03-18 12:50:57 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
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()) {
|
2019-05-11 17:41:37 +03:00
|
|
|
write!(self, "{}", kw::Crate)?;
|
2019-01-25 12:11:50 +02:00
|
|
|
self.empty_path = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
write!(self, "{}", self.tcx.crate_name(cnum))?;
|
|
|
|
self.empty_path = false;
|
|
|
|
}
|
|
|
|
Ok(self)
|
|
|
|
}
|
2019-09-06 03:57:44 +01:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
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)
|
|
|
|
}
|
2019-09-06 03:57:44 +01:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
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)?;
|
|
|
|
|
2021-12-17 16:45:15 +08:00
|
|
|
// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
|
|
|
|
if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
|
2020-03-22 13:36:56 +01:00
|
|
|
return Ok(self);
|
2019-02-03 12:59:37 +02:00
|
|
|
}
|
|
|
|
|
2020-09-01 00:42:30 +01:00
|
|
|
let name = disambiguated_data.data.name();
|
2021-12-17 16:45:15 +08:00
|
|
|
if !self.empty_path {
|
|
|
|
write!(self, "::")?;
|
|
|
|
}
|
2020-08-31 18:11:44 +01:00
|
|
|
|
2021-12-17 16:45:15 +08:00
|
|
|
if let DefPathDataName::Named(name) = name {
|
|
|
|
if Ident::with_dummy_span(name).is_raw_guess() {
|
|
|
|
write!(self, "r#")?;
|
2020-09-24 10:23:01 +01:00
|
|
|
}
|
2021-12-17 16:45:15 +08:00
|
|
|
}
|
2020-09-24 10:23:01 +01:00
|
|
|
|
2021-12-17 16:45:15 +08:00
|
|
|
let verbose = self.tcx.sess.verbose();
|
|
|
|
disambiguated_data.fmt_maybe_verbose(&mut self, verbose)?;
|
2019-02-03 12:59:37 +02:00
|
|
|
|
2021-12-17 16:45:15 +08:00
|
|
|
self.empty_path = false;
|
2019-01-25 12:11:50 +02:00
|
|
|
|
|
|
|
Ok(self)
|
|
|
|
}
|
2019-09-06 03:57:44 +01:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
fn path_generic_args(
|
|
|
|
mut self,
|
|
|
|
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
|
2019-09-25 16:39:44 +01:00
|
|
|
args: &[GenericArg<'tcx>],
|
2019-01-25 12:11:50 +02:00
|
|
|
) -> Result<Self::Path, Self::Error> {
|
|
|
|
self = print_prefix(self)?;
|
|
|
|
|
2022-09-11 14:37:55 +02:00
|
|
|
if args.first().is_some() {
|
2019-01-25 12:11:50 +02:00
|
|
|
if self.in_value {
|
|
|
|
write!(self, "::")?;
|
|
|
|
}
|
2022-09-11 14:37:55 +02:00
|
|
|
self.generic_delimiters(|cx| cx.comma_sep(args.iter().cloned()))
|
2019-01-25 12:11:50 +02:00
|
|
|
} else {
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 16:15:29 -05:00
|
|
|
impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
|
2022-07-17 04:09:20 +09:00
|
|
|
fn ty_infer_name(&self, id: ty::TyVid) -> Option<Symbol> {
|
2022-02-14 14:13:02 +01:00
|
|
|
self.0.ty_infer_name_resolver.as_ref().and_then(|func| func(id))
|
|
|
|
}
|
|
|
|
|
2022-07-17 04:09:20 +09:00
|
|
|
fn const_infer_name(&self, id: ty::ConstVid<'tcx>) -> Option<Symbol> {
|
2022-02-14 14:13:02 +01:00
|
|
|
self.0.const_infer_name_resolver.as_ref().and_then(|func| func(id))
|
2019-05-30 15:33:13 -07:00
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
fn print_value_path(
|
|
|
|
mut self,
|
|
|
|
def_id: DefId,
|
2019-09-25 16:39:44 +01:00
|
|
|
substs: &'tcx [GenericArg<'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)
|
|
|
|
}
|
|
|
|
|
2020-10-05 16:51:33 -04:00
|
|
|
fn in_binder<T>(self, value: &ty::Binder<'tcx, T>) -> Result<Self, Self::Error>
|
2019-06-14 01:32:15 +03:00
|
|
|
where
|
|
|
|
T: Print<'tcx, Self, Output = Self, Error = Self::Error> + TypeFoldable<'tcx>,
|
2019-01-25 12:11:50 +02:00
|
|
|
{
|
|
|
|
self.pretty_in_binder(value)
|
|
|
|
}
|
|
|
|
|
2022-06-21 21:27:15 -07:00
|
|
|
fn wrap_binder<T, C: FnOnce(&T, Self) -> Result<Self, Self::Error>>(
|
2020-12-11 15:02:46 -05:00
|
|
|
self,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: &ty::Binder<'tcx, T>,
|
2020-12-11 15:02:46 -05:00
|
|
|
f: C,
|
|
|
|
) -> Result<Self, Self::Error>
|
|
|
|
where
|
|
|
|
T: Print<'tcx, Self, Output = Self, Error = Self::Error> + TypeFoldable<'tcx>,
|
|
|
|
{
|
|
|
|
self.pretty_wrap_binder(value, f)
|
|
|
|
}
|
|
|
|
|
2020-02-13 19:02:58 +01:00
|
|
|
fn typed_value(
|
2020-01-27 18:49:05 +01:00
|
|
|
mut self,
|
|
|
|
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
|
|
|
|
t: impl FnOnce(Self) -> Result<Self, Self::Error>,
|
2020-02-26 19:36:10 +01:00
|
|
|
conversion: &str,
|
2020-01-27 18:49:05 +01:00
|
|
|
) -> Result<Self::Const, Self::Error> {
|
|
|
|
self.write_str("{")?;
|
|
|
|
self = f(self)?;
|
2020-02-26 19:36:10 +01:00
|
|
|
self.write_str(conversion)?;
|
2020-02-05 11:00:52 +01:00
|
|
|
let was_in_value = std::mem::replace(&mut self.in_value, false);
|
|
|
|
self = t(self)?;
|
|
|
|
self.in_value = was_in_value;
|
2020-01-27 18:49:05 +01:00
|
|
|
self.write_str("}")?;
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:20:27 -04:00
|
|
|
fn should_print_region(&self, region: ty::Region<'tcx>) -> bool {
|
2019-01-25 12:11:50 +02:00
|
|
|
let highlight = self.region_highlight_mode;
|
|
|
|
if highlight.region_highlighted(region).is_some() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.tcx.sess.verbose() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-06 07:44:47 -05:00
|
|
|
let identify_regions = self.tcx.sess.opts.unstable_opts.identify_regions;
|
2019-01-25 12:11:50 +02:00
|
|
|
|
|
|
|
match *region {
|
|
|
|
ty::ReEarlyBound(ref data) => {
|
2020-12-29 20:28:08 -05:00
|
|
|
data.name != kw::Empty && data.name != kw::UnderscoreLifetime
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
2020-10-26 14:18:31 -04:00
|
|
|
ty::ReLateBound(_, ty::BoundRegion { kind: br, .. })
|
2019-01-25 12:11:50 +02:00
|
|
|
| ty::ReFree(ty::FreeRegion { bound_region: br, .. })
|
|
|
|
| ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
|
|
|
|
if let ty::BrNamed(_, name) = br {
|
2020-12-29 20:28:08 -05:00
|
|
|
if name != kw::Empty && name != kw::UnderscoreLifetime {
|
2019-01-25 12:11:50 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((region, _)) = highlight.highlight_bound_region {
|
|
|
|
if br == region {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2020-05-19 20:26:24 +01:00
|
|
|
ty::ReVar(_) if identify_regions => true,
|
2019-01-25 12:11:50 +02:00
|
|
|
|
2020-05-19 20:26:24 +01:00
|
|
|
ty::ReVar(_) | ty::ReErased => false,
|
2019-01-25 12:11:50 +02:00
|
|
|
|
2022-06-26 15:40:45 -04:00
|
|
|
ty::ReStatic => true,
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-23 17:41:06 +01:00
|
|
|
|
2022-07-18 18:47:31 -04:00
|
|
|
fn pretty_print_const_pointer<Prov: Provenance>(
|
2019-12-23 17:41:06 +01:00
|
|
|
self,
|
2022-07-18 18:47:31 -04:00
|
|
|
p: Pointer<Prov>,
|
2019-12-23 17:41:06 +01:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
print_ty: bool,
|
|
|
|
) -> Result<Self::Const, Self::Error> {
|
2020-02-05 11:00:52 +01:00
|
|
|
let print = |mut this: Self| {
|
|
|
|
define_scoped_cx!(this);
|
|
|
|
if this.print_alloc_ids {
|
|
|
|
p!(write("{:?}", p));
|
|
|
|
} else {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("&_");
|
2020-02-05 11:00:52 +01:00
|
|
|
}
|
|
|
|
Ok(this)
|
|
|
|
};
|
|
|
|
if print_ty {
|
2020-02-26 19:36:10 +01:00
|
|
|
self.typed_value(print, |this| this.print_type(ty), ": ")
|
2020-02-05 11:00:52 +01:00
|
|
|
} else {
|
|
|
|
print(self)
|
|
|
|
}
|
2019-12-23 17:41:06 +01:00
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HACK(eddyb) limited to `FmtPrinter` because of `region_highlight_mode`.
|
2022-06-19 00:20:27 -04:00
|
|
|
impl<'tcx> FmtPrinter<'_, 'tcx> {
|
|
|
|
pub fn pretty_print_region(mut self, region: ty::Region<'tcx>) -> Result<Self, fmt::Error> {
|
2019-01-25 12:11:50 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-07-06 07:44:47 -05:00
|
|
|
let identify_regions = self.tcx.sess.opts.unstable_opts.identify_regions;
|
2019-01-25 12:11:50 +02:00
|
|
|
|
|
|
|
// 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) => {
|
2020-12-29 20:28:08 -05:00
|
|
|
if data.name != kw::Empty {
|
2019-01-25 12:11:50 +02:00
|
|
|
p!(write("{}", data.name));
|
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
}
|
2020-10-26 14:18:31 -04:00
|
|
|
ty::ReLateBound(_, ty::BoundRegion { kind: br, .. })
|
2019-01-25 12:11:50 +02:00
|
|
|
| ty::ReFree(ty::FreeRegion { bound_region: br, .. })
|
|
|
|
| ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
|
|
|
|
if let ty::BrNamed(_, name) = br {
|
2020-12-29 20:28:08 -05:00
|
|
|
if name != kw::Empty && name != kw::UnderscoreLifetime {
|
2019-01-25 12:11:50 +02:00
|
|
|
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::ReVar(region_vid) if identify_regions => {
|
|
|
|
p!(write("{:?}", region_vid));
|
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
ty::ReVar(_) => {}
|
2020-05-19 20:26:24 +01:00
|
|
|
ty::ReErased => {}
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::ReStatic => {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("'static");
|
2019-01-25 12:11:50 +02:00
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("'_");
|
2019-01-25 12:11:50 +02:00
|
|
|
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 03:18:05 -04:00
|
|
|
/// Folds through bound vars and placeholders, naming them
|
|
|
|
struct RegionFolder<'a, 'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
current_index: ty::DebruijnIndex,
|
|
|
|
region_map: BTreeMap<ty::BoundRegion, ty::Region<'tcx>>,
|
2022-09-29 18:33:58 +02:00
|
|
|
name: &'a mut (
|
|
|
|
dyn FnMut(
|
|
|
|
Option<ty::DebruijnIndex>,
|
|
|
|
ty::DebruijnIndex,
|
|
|
|
ty::BoundRegion,
|
|
|
|
) -> ty::Region<'tcx>
|
|
|
|
+ 'a
|
|
|
|
),
|
2021-07-18 03:18:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> ty::TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
|
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_binder<T: TypeFoldable<'tcx>>(
|
|
|
|
&mut self,
|
|
|
|
t: ty::Binder<'tcx, T>,
|
2021-12-01 00:55:57 +00:00
|
|
|
) -> ty::Binder<'tcx, T> {
|
2021-07-18 03:18:05 -04:00
|
|
|
self.current_index.shift_in(1);
|
|
|
|
let t = t.super_fold_with(self);
|
|
|
|
self.current_index.shift_out(1);
|
|
|
|
t
|
|
|
|
}
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
2021-07-18 03:18:05 -04:00
|
|
|
match *t.kind() {
|
|
|
|
_ if t.has_vars_bound_at_or_above(self.current_index) || t.has_placeholders() => {
|
|
|
|
return t.super_fold_with(self);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2021-12-01 00:55:57 +00:00
|
|
|
t
|
2021-07-18 03:18:05 -04:00
|
|
|
}
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
|
2021-07-18 03:18:05 -04:00
|
|
|
let name = &mut self.name;
|
|
|
|
let region = match *r {
|
2022-09-29 18:33:58 +02:00
|
|
|
ty::ReLateBound(db, br) => {
|
|
|
|
*self.region_map.entry(br).or_insert_with(|| name(Some(db), self.current_index, br))
|
|
|
|
}
|
2021-07-18 03:18:05 -04:00
|
|
|
ty::RePlaceholder(ty::PlaceholderRegion { name: kind, .. }) => {
|
|
|
|
// If this is an anonymous placeholder, don't rename. Otherwise, in some
|
|
|
|
// async fns, we get a `for<'r> Send` bound
|
|
|
|
match kind {
|
|
|
|
ty::BrAnon(_) | ty::BrEnv => r,
|
|
|
|
_ => {
|
|
|
|
// Index doesn't matter, since this is just for naming and these never get bound
|
|
|
|
let br = ty::BoundRegion { var: ty::BoundVar::from_u32(0), kind };
|
2022-09-29 18:33:58 +02:00
|
|
|
*self
|
|
|
|
.region_map
|
|
|
|
.entry(br)
|
|
|
|
.or_insert_with(|| name(None, self.current_index, br))
|
2021-07-18 03:18:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-01 00:55:57 +00:00
|
|
|
_ => return r,
|
2021-07-18 03:18:05 -04:00
|
|
|
};
|
|
|
|
if let ty::ReLateBound(debruijn1, br) = *region {
|
|
|
|
assert_eq!(debruijn1, ty::INNERMOST);
|
2021-12-01 00:55:57 +00:00
|
|
|
self.tcx.mk_region(ty::ReLateBound(self.current_index, br))
|
2021-07-18 03:18:05 -04:00
|
|
|
} else {
|
2021-12-01 00:55:57 +00:00
|
|
|
region
|
2021-07-18 03:18:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
// HACK(eddyb) limited to `FmtPrinter` because of `binder_depth`,
|
|
|
|
// `region_index` and `used_region_names`.
|
2022-02-18 16:15:29 -05:00
|
|
|
impl<'tcx> FmtPrinter<'_, 'tcx> {
|
2019-11-23 16:01:20 -08:00
|
|
|
pub fn name_all_regions<T>(
|
|
|
|
mut self,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: &ty::Binder<'tcx, T>,
|
2021-07-18 03:18:05 -04:00
|
|
|
) -> Result<(Self, T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>), fmt::Error>
|
2019-06-14 01:32:15 +03:00
|
|
|
where
|
|
|
|
T: Print<'tcx, Self, Output = Self, Error = fmt::Error> + TypeFoldable<'tcx>,
|
2019-01-25 12:11:50 +02:00
|
|
|
{
|
2022-09-15 18:46:40 +02:00
|
|
|
fn name_by_region_index(
|
|
|
|
index: usize,
|
|
|
|
available_names: &mut Vec<Symbol>,
|
|
|
|
num_available: usize,
|
|
|
|
) -> Symbol {
|
|
|
|
if let Some(name) = available_names.pop() {
|
|
|
|
name
|
|
|
|
} else {
|
2022-09-21 17:57:30 +02:00
|
|
|
Symbol::intern(&format!("'z{}", index - num_available))
|
2019-05-14 14:55:15 +10:00
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-15 18:46:40 +02:00
|
|
|
debug!("name_all_regions");
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
// Replace any anonymous late-bound regions with named
|
2019-08-31 16:41:13 +01:00
|
|
|
// variants, using new unique identifiers, so that we can
|
2019-01-25 12:11:50 +02:00
|
|
|
// 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 {
|
2022-09-15 18:46:40 +02:00
|
|
|
self.prepare_region_info(value);
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2022-09-15 18:46:40 +02:00
|
|
|
debug!("self.used_region_names: {:?}", &self.used_region_names);
|
|
|
|
|
2019-01-18 21:33:31 +02:00
|
|
|
let mut empty = true;
|
|
|
|
let mut start_or_continue = |cx: &mut Self, start: &str, cont: &str| {
|
2021-07-18 03:18:05 -04:00
|
|
|
let w = if empty {
|
|
|
|
empty = false;
|
|
|
|
start
|
|
|
|
} else {
|
|
|
|
cont
|
|
|
|
};
|
|
|
|
let _ = write!(cx, "{}", w);
|
|
|
|
};
|
|
|
|
let do_continue = |cx: &mut Self, cont: Symbol| {
|
|
|
|
let _ = write!(cx, "{}", cont);
|
2019-01-18 21:33:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
2022-09-21 17:57:30 +02:00
|
|
|
let possible_names =
|
|
|
|
('a'..='z').rev().map(|s| Symbol::intern(&format!("'{s}"))).collect::<Vec<_>>();
|
2022-09-15 18:46:40 +02:00
|
|
|
|
|
|
|
let mut available_names = possible_names
|
|
|
|
.into_iter()
|
|
|
|
.filter(|name| !self.used_region_names.contains(&name))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
debug!(?available_names);
|
|
|
|
let num_available = available_names.len();
|
|
|
|
|
2019-11-23 16:01:20 -08:00
|
|
|
let mut region_index = self.region_index;
|
2022-09-15 18:46:40 +02:00
|
|
|
let mut next_name = |this: &Self| {
|
2022-09-28 12:31:08 +02:00
|
|
|
let mut name;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
name = name_by_region_index(region_index, &mut available_names, num_available);
|
|
|
|
region_index += 1;
|
|
|
|
|
|
|
|
if !this.used_region_names.contains(&name) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 18:46:40 +02:00
|
|
|
|
|
|
|
name
|
2022-05-12 21:18:26 +02:00
|
|
|
};
|
|
|
|
|
2022-03-30 15:14:15 -04:00
|
|
|
// If we want to print verbosely, then print *all* binders, even if they
|
2021-04-05 00:10:09 -04:00
|
|
|
// aren't named. Eventually, we might just want this as the default, but
|
|
|
|
// this is not *quite* right and changes the ordering of some output
|
|
|
|
// anyways.
|
2021-07-18 03:18:05 -04:00
|
|
|
let (new_value, map) = if self.tcx().sess.verbose() {
|
2022-06-04 14:31:17 -04:00
|
|
|
let regions: Vec<_> = value
|
|
|
|
.bound_vars()
|
|
|
|
.into_iter()
|
|
|
|
.map(|var| {
|
|
|
|
let ty::BoundVariableKind::Region(var) = var else {
|
|
|
|
// This doesn't really matter because it doesn't get used,
|
|
|
|
// it's just an empty value
|
|
|
|
return ty::BrAnon(0);
|
|
|
|
};
|
|
|
|
match var {
|
|
|
|
ty::BrAnon(_) | ty::BrEnv => {
|
|
|
|
start_or_continue(&mut self, "for<", ", ");
|
|
|
|
let name = next_name(&self);
|
2022-09-15 18:46:40 +02:00
|
|
|
debug!(?name);
|
2022-06-04 14:31:17 -04:00
|
|
|
do_continue(&mut self, name);
|
|
|
|
ty::BrNamed(CRATE_DEF_ID.to_def_id(), name)
|
|
|
|
}
|
|
|
|
ty::BrNamed(def_id, kw::UnderscoreLifetime) => {
|
|
|
|
start_or_continue(&mut self, "for<", ", ");
|
|
|
|
let name = next_name(&self);
|
|
|
|
do_continue(&mut self, name);
|
|
|
|
ty::BrNamed(def_id, name)
|
|
|
|
}
|
|
|
|
ty::BrNamed(def_id, name) => {
|
|
|
|
start_or_continue(&mut self, "for<", ", ");
|
|
|
|
do_continue(&mut self, name);
|
|
|
|
ty::BrNamed(def_id, name)
|
|
|
|
}
|
2021-04-05 00:10:09 -04:00
|
|
|
}
|
2022-06-04 14:31:17 -04:00
|
|
|
})
|
|
|
|
.collect();
|
2021-07-18 03:18:05 -04:00
|
|
|
start_or_continue(&mut self, "", "> ");
|
2021-04-05 00:10:09 -04:00
|
|
|
|
|
|
|
self.tcx.replace_late_bound_regions(value.clone(), |br| {
|
2022-06-04 14:31:17 -04:00
|
|
|
let kind = regions[br.var.as_usize()];
|
2021-04-05 00:10:09 -04:00
|
|
|
self.tcx.mk_region(ty::ReLateBound(
|
|
|
|
ty::INNERMOST,
|
|
|
|
ty::BoundRegion { var: br.var, kind },
|
|
|
|
))
|
|
|
|
})
|
|
|
|
} else {
|
2021-07-18 03:18:05 -04:00
|
|
|
let tcx = self.tcx;
|
2022-09-29 18:33:58 +02:00
|
|
|
let mut name = |db: Option<ty::DebruijnIndex>,
|
|
|
|
binder_level: ty::DebruijnIndex,
|
|
|
|
br: ty::BoundRegion| {
|
|
|
|
let (name, kind) = match br.kind {
|
2021-04-05 00:10:09 -04:00
|
|
|
ty::BrAnon(_) | ty::BrEnv => {
|
2022-05-12 21:18:26 +02:00
|
|
|
let name = next_name(&self);
|
2022-09-29 18:33:58 +02:00
|
|
|
|
|
|
|
if let Some(db) = db {
|
|
|
|
if db > binder_level {
|
|
|
|
let kind = ty::BrNamed(CRATE_DEF_ID.to_def_id(), name);
|
|
|
|
return tcx.mk_region(ty::ReLateBound(
|
|
|
|
ty::INNERMOST,
|
|
|
|
ty::BoundRegion { var: br.var, kind },
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(name, ty::BrNamed(CRATE_DEF_ID.to_def_id(), name))
|
2021-04-05 00:10:09 -04:00
|
|
|
}
|
2022-05-12 21:18:26 +02:00
|
|
|
ty::BrNamed(def_id, kw::UnderscoreLifetime) => {
|
|
|
|
let name = next_name(&self);
|
2022-09-29 18:33:58 +02:00
|
|
|
|
|
|
|
if let Some(db) = db {
|
|
|
|
if db > binder_level {
|
|
|
|
let kind = ty::BrNamed(def_id, name);
|
|
|
|
return tcx.mk_region(ty::ReLateBound(
|
|
|
|
ty::INNERMOST,
|
|
|
|
ty::BoundRegion { var: br.var, kind },
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(name, ty::BrNamed(def_id, name))
|
2022-05-12 21:18:26 +02:00
|
|
|
}
|
|
|
|
ty::BrNamed(_, name) => {
|
2022-09-29 18:33:58 +02:00
|
|
|
if let Some(db) = db {
|
|
|
|
if db > binder_level {
|
|
|
|
let kind = br.kind;
|
|
|
|
return tcx.mk_region(ty::ReLateBound(
|
|
|
|
ty::INNERMOST,
|
|
|
|
ty::BoundRegion { var: br.var, kind },
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(name, br.kind)
|
2022-05-12 21:18:26 +02:00
|
|
|
}
|
2021-04-05 00:10:09 -04:00
|
|
|
};
|
2022-09-29 18:33:58 +02:00
|
|
|
|
|
|
|
start_or_continue(&mut self, "for<", ", ");
|
|
|
|
do_continue(&mut self, name);
|
2021-07-18 03:18:05 -04:00
|
|
|
tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BoundRegion { var: br.var, kind }))
|
|
|
|
};
|
|
|
|
let mut folder = RegionFolder {
|
|
|
|
tcx,
|
|
|
|
current_index: ty::INNERMOST,
|
|
|
|
name: &mut name,
|
|
|
|
region_map: BTreeMap::new(),
|
|
|
|
};
|
2021-12-01 00:55:57 +00:00
|
|
|
let new_value = value.clone().skip_binder().fold_with(&mut folder);
|
2021-07-18 03:18:05 -04:00
|
|
|
let region_map = folder.region_map;
|
|
|
|
start_or_continue(&mut self, "", "> ");
|
|
|
|
(new_value, region_map)
|
2021-04-05 00:10:09 -04:00
|
|
|
};
|
2019-01-18 21:33:31 +02:00
|
|
|
|
2019-01-20 19:46:47 +02:00
|
|
|
self.binder_depth += 1;
|
|
|
|
self.region_index = region_index;
|
2021-07-18 03:18:05 -04:00
|
|
|
Ok((self, new_value, map))
|
2019-11-23 16:01:20 -08:00
|
|
|
}
|
|
|
|
|
2020-10-05 16:51:33 -04:00
|
|
|
pub fn pretty_in_binder<T>(self, value: &ty::Binder<'tcx, T>) -> Result<Self, fmt::Error>
|
2019-11-23 16:01:20 -08:00
|
|
|
where
|
|
|
|
T: Print<'tcx, Self, Output = Self, Error = fmt::Error> + TypeFoldable<'tcx>,
|
|
|
|
{
|
|
|
|
let old_region_index = self.region_index;
|
2021-07-18 03:18:05 -04:00
|
|
|
let (new, new_value, _) = self.name_all_regions(value)?;
|
|
|
|
let mut inner = new_value.print(new)?;
|
2019-01-20 19:46:47 +02:00
|
|
|
inner.region_index = old_region_index;
|
|
|
|
inner.binder_depth -= 1;
|
|
|
|
Ok(inner)
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 21:27:15 -07:00
|
|
|
pub fn pretty_wrap_binder<T, C: FnOnce(&T, Self) -> Result<Self, fmt::Error>>(
|
2020-12-11 15:02:46 -05:00
|
|
|
self,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: &ty::Binder<'tcx, T>,
|
2020-12-11 15:02:46 -05:00
|
|
|
f: C,
|
|
|
|
) -> Result<Self, fmt::Error>
|
|
|
|
where
|
|
|
|
T: Print<'tcx, Self, Output = Self, Error = fmt::Error> + TypeFoldable<'tcx>,
|
|
|
|
{
|
|
|
|
let old_region_index = self.region_index;
|
2021-07-18 03:18:05 -04:00
|
|
|
let (new, new_value, _) = self.name_all_regions(value)?;
|
|
|
|
let mut inner = f(&new_value, new)?;
|
2020-12-11 15:02:46 -05:00
|
|
|
inner.region_index = old_region_index;
|
|
|
|
inner.binder_depth -= 1;
|
|
|
|
Ok(inner)
|
|
|
|
}
|
|
|
|
|
2022-09-15 18:46:40 +02:00
|
|
|
fn prepare_region_info<T>(&mut self, value: &ty::Binder<'tcx, T>)
|
2019-01-20 19:46:47 +02:00
|
|
|
where
|
2022-06-17 13:10:07 +01:00
|
|
|
T: TypeVisitable<'tcx>,
|
2019-01-20 04:56:48 +02:00
|
|
|
{
|
2022-09-15 18:46:40 +02:00
|
|
|
struct RegionNameCollector<'tcx> {
|
|
|
|
used_region_names: FxHashSet<Symbol>,
|
2021-03-23 12:41:26 +01:00
|
|
|
type_collector: SsoHashSet<Ty<'tcx>>,
|
|
|
|
}
|
|
|
|
|
2022-09-15 18:46:40 +02:00
|
|
|
impl<'tcx> RegionNameCollector<'tcx> {
|
|
|
|
fn new() -> Self {
|
|
|
|
RegionNameCollector {
|
|
|
|
used_region_names: Default::default(),
|
|
|
|
type_collector: SsoHashSet::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ty::visit::TypeVisitor<'tcx> for RegionNameCollector<'tcx> {
|
2021-03-23 12:41:26 +01:00
|
|
|
type BreakTy = ();
|
|
|
|
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2022-06-14 17:34:37 +02:00
|
|
|
trace!("address: {:p}", r.0.0);
|
2022-09-15 18:46:40 +02:00
|
|
|
|
|
|
|
// Collect all named lifetimes. These allow us to prevent duplication
|
|
|
|
// of already existing lifetime names when introducing names for
|
|
|
|
// anonymous late-bound regions.
|
|
|
|
if let Some(name) = r.get_name() {
|
2021-07-18 03:18:05 -04:00
|
|
|
self.used_region_names.insert(name);
|
2019-01-20 04:56:48 +02:00
|
|
|
}
|
2022-09-15 18:46:40 +02:00
|
|
|
|
2019-01-20 04:56:48 +02:00
|
|
|
r.super_visit_with(self)
|
|
|
|
}
|
2021-03-23 12:41:26 +01:00
|
|
|
|
|
|
|
// We collect types in order to prevent really large types from compiling for
|
|
|
|
// a really long time. See issue #83150 for why this is necessary.
|
|
|
|
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
|
|
|
let not_previously_inserted = self.type_collector.insert(ty);
|
|
|
|
if not_previously_inserted {
|
|
|
|
ty.super_visit_with(self)
|
|
|
|
} else {
|
|
|
|
ControlFlow::CONTINUE
|
|
|
|
}
|
|
|
|
}
|
2019-01-20 04:56:48 +02:00
|
|
|
}
|
|
|
|
|
2022-09-15 18:46:40 +02:00
|
|
|
let mut collector = RegionNameCollector::new();
|
2019-01-20 04:56:48 +02:00
|
|
|
value.visit_with(&mut collector);
|
2022-09-15 18:46:40 +02:00
|
|
|
self.used_region_names = collector.used_region_names;
|
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
|
|
|
|
2020-10-05 16:51:33 -04:00
|
|
|
impl<'tcx, T, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::Binder<'tcx, T>
|
2019-06-14 01:32:15 +03:00
|
|
|
where
|
|
|
|
T: Print<'tcx, P, Output = P, Error = P::Error> + TypeFoldable<'tcx>,
|
2019-01-20 04:56:48 +02:00
|
|
|
{
|
|
|
|
type Output = P;
|
|
|
|
type Error = P::Error;
|
2022-02-16 10:56:01 +01:00
|
|
|
|
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-06-14 01:32:15 +03:00
|
|
|
impl<'tcx, T, U, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::OutlivesPredicate<T, U>
|
|
|
|
where
|
|
|
|
T: Print<'tcx, P, Output = P, Error = P::Error>,
|
|
|
|
U: Print<'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);
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(print(self.0), ": ", print(self.1));
|
2019-01-25 12:11:50 +02:00
|
|
|
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),+) => {
|
2021-12-15 19:32:30 -05:00
|
|
|
// Some of the $ty arguments may not actually use 'tcx
|
|
|
|
$(#[allow(unused_lifetimes)] impl<'tcx> 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| {
|
2022-02-18 16:15:29 -05:00
|
|
|
let cx = tcx.lift(*self)
|
2019-01-25 12:11:50 +02:00
|
|
|
.expect("could not lift for printing")
|
2022-02-18 16:15:29 -05:00
|
|
|
.print(FmtPrinter::new(tcx, Namespace::TypeNS))?;
|
|
|
|
f.write_str(&cx.into_buffer())?;
|
2019-01-25 12:11:50 +02:00
|
|
|
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)+) => {
|
2019-06-14 00:48:52 +03:00
|
|
|
$(impl<'tcx, P: PrettyPrinter<'tcx>> Print<'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-11-21 21:01:14 +03:00
|
|
|
/// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
|
|
|
|
/// the trait path. That is, it will print `Trait<U>` instead of
|
|
|
|
/// `<T as Trait<U>>`.
|
2022-06-17 10:53:29 +01:00
|
|
|
#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift)]
|
2019-11-21 21:01:14 +03:00
|
|
|
pub struct TraitRefPrintOnlyTraitPath<'tcx>(ty::TraitRef<'tcx>);
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> fmt::Debug for TraitRefPrintOnlyTraitPath<'tcx> {
|
2019-11-21 21:01:14 +03:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
fmt::Display::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 14:48:54 +00:00
|
|
|
/// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
|
|
|
|
/// the trait name. That is, it will print `Trait` instead of
|
|
|
|
/// `<T as Trait<U>>`.
|
2022-06-17 10:53:29 +01:00
|
|
|
#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift)]
|
2021-09-28 14:48:54 +00:00
|
|
|
pub struct TraitRefPrintOnlyTraitName<'tcx>(ty::TraitRef<'tcx>);
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> fmt::Debug for TraitRefPrintOnlyTraitName<'tcx> {
|
2021-09-28 14:48:54 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
fmt::Display::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> ty::TraitRef<'tcx> {
|
2019-11-21 21:01:14 +03:00
|
|
|
pub fn print_only_trait_path(self) -> TraitRefPrintOnlyTraitPath<'tcx> {
|
|
|
|
TraitRefPrintOnlyTraitPath(self)
|
|
|
|
}
|
2021-09-28 14:48:54 +00:00
|
|
|
|
|
|
|
pub fn print_only_trait_name(self) -> TraitRefPrintOnlyTraitName<'tcx> {
|
|
|
|
TraitRefPrintOnlyTraitName(self)
|
|
|
|
}
|
2019-11-21 21:01:14 +03:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> ty::Binder<'tcx, ty::TraitRef<'tcx>> {
|
2020-10-05 16:51:33 -04:00
|
|
|
pub fn print_only_trait_path(self) -> ty::Binder<'tcx, TraitRefPrintOnlyTraitPath<'tcx>> {
|
2019-11-21 21:01:14 +03:00
|
|
|
self.map_bound(|tr| tr.print_only_trait_path())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:53:29 +01:00
|
|
|
#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift)]
|
2021-12-24 22:50:44 +08:00
|
|
|
pub struct TraitPredPrintModifiersAndPath<'tcx>(ty::TraitPredicate<'tcx>);
|
|
|
|
|
|
|
|
impl<'tcx> fmt::Debug for TraitPredPrintModifiersAndPath<'tcx> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
fmt::Display::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ty::TraitPredicate<'tcx> {
|
|
|
|
pub fn print_modifiers_and_trait_path(self) -> TraitPredPrintModifiersAndPath<'tcx> {
|
|
|
|
TraitPredPrintModifiersAndPath(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ty::PolyTraitPredicate<'tcx> {
|
|
|
|
pub fn print_modifiers_and_trait_path(
|
|
|
|
self,
|
|
|
|
) -> ty::Binder<'tcx, TraitPredPrintModifiersAndPath<'tcx>> {
|
|
|
|
self.map_bound(TraitPredPrintModifiersAndPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 01:25:56 +00:00
|
|
|
#[derive(Debug, Copy, Clone, TypeFoldable, TypeVisitable, Lift)]
|
|
|
|
pub struct PrintClosureAsImpl<'tcx> {
|
|
|
|
pub closure: ty::ClosureSubsts<'tcx>,
|
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
forward_display_to_print! {
|
2022-06-19 00:20:27 -04:00
|
|
|
ty::Region<'tcx>,
|
2019-01-25 12:11:50 +02:00
|
|
|
Ty<'tcx>,
|
2020-10-05 16:51:33 -04:00
|
|
|
&'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
|
2022-02-02 14:24:45 +11:00
|
|
|
ty::Const<'tcx>,
|
2019-01-25 12:11:50 +02:00
|
|
|
|
|
|
|
// HACK(eddyb) these are exhaustive instead of generic,
|
2019-06-14 00:48:52 +03:00
|
|
|
// because `for<'tcx>` isn't possible yet.
|
2020-10-05 16:51:33 -04:00
|
|
|
ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>,
|
|
|
|
ty::Binder<'tcx, ty::TraitRef<'tcx>>,
|
2021-10-07 11:29:01 +02:00
|
|
|
ty::Binder<'tcx, ty::ExistentialTraitRef<'tcx>>,
|
2020-10-05 16:51:33 -04:00
|
|
|
ty::Binder<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
|
2021-09-28 14:48:54 +00:00
|
|
|
ty::Binder<'tcx, TraitRefPrintOnlyTraitName<'tcx>>,
|
2020-10-05 16:51:33 -04:00
|
|
|
ty::Binder<'tcx, ty::FnSig<'tcx>>,
|
|
|
|
ty::Binder<'tcx, ty::TraitPredicate<'tcx>>,
|
2021-12-24 22:50:44 +08:00
|
|
|
ty::Binder<'tcx, TraitPredPrintModifiersAndPath<'tcx>>,
|
2020-10-05 16:51:33 -04:00
|
|
|
ty::Binder<'tcx, ty::SubtypePredicate<'tcx>>,
|
|
|
|
ty::Binder<'tcx, ty::ProjectionPredicate<'tcx>>,
|
|
|
|
ty::Binder<'tcx, ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>>,
|
|
|
|
ty::Binder<'tcx, ty::OutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>>>,
|
2019-01-25 12:11:50 +02:00
|
|
|
|
|
|
|
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>> {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("{{", comma_sep(self.iter()), "}}")
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::TypeAndMut<'tcx> {
|
2019-10-30 12:55:38 -07:00
|
|
|
p!(write("{}", self.mutbl.prefix_str()), print(self.ty))
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-11-21 21:01:14 +03:00
|
|
|
p!(print(trait_ref.print_only_trait_path()))
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:47:02 +02:00
|
|
|
ty::ExistentialProjection<'tcx> {
|
2022-01-12 21:15:51 -05:00
|
|
|
let name = cx.tcx().associated_item(self.item_def_id).name;
|
2022-01-13 07:39:58 +00:00
|
|
|
p!(write("{} = ", name), print(self.term))
|
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> {
|
2019-10-30 12:55:38 -07:00
|
|
|
p!(write("{}", self.unsafety.prefix_str()));
|
2019-01-20 14:00:39 +02:00
|
|
|
|
|
|
|
if self.abi != Abi::Rust {
|
|
|
|
p!(write("extern {} ", self.abi));
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("fn", pretty_fn_sig(self.inputs(), self.c_variadic, self.output()));
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::TraitRef<'tcx> {
|
2019-11-21 21:01:14 +03:00
|
|
|
p!(write("<{} as {}>", self.self_ty(), self.print_only_trait_path()))
|
|
|
|
}
|
|
|
|
|
|
|
|
TraitRefPrintOnlyTraitPath<'tcx> {
|
|
|
|
p!(print_def_path(self.0.def_id, self.0.substs));
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
|
2021-09-28 14:48:54 +00:00
|
|
|
TraitRefPrintOnlyTraitName<'tcx> {
|
|
|
|
p!(print_def_path(self.0.def_id, &[]));
|
|
|
|
}
|
|
|
|
|
2021-12-24 22:50:44 +08:00
|
|
|
TraitPredPrintModifiersAndPath<'tcx> {
|
|
|
|
if let ty::BoundConstness::ConstIfConst = self.0.constness {
|
|
|
|
p!("~const ")
|
|
|
|
}
|
|
|
|
|
|
|
|
if let ty::ImplPolarity::Negative = self.0.polarity {
|
|
|
|
p!("!")
|
|
|
|
}
|
|
|
|
|
|
|
|
p!(print(self.0.trait_ref.print_only_trait_path()));
|
|
|
|
}
|
|
|
|
|
2022-08-26 01:25:56 +00:00
|
|
|
PrintClosureAsImpl<'tcx> {
|
|
|
|
p!(pretty_closure_as_impl(self.closure))
|
|
|
|
}
|
|
|
|
|
2019-01-20 14:00:39 +02:00
|
|
|
ty::ParamTy {
|
|
|
|
p!(write("{}", self.name))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::ParamConst {
|
|
|
|
p!(write("{}", self.name))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::SubtypePredicate<'tcx> {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(print(self.a), " <: ", print(self.b))
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 07:06:16 -05:00
|
|
|
ty::CoercePredicate<'tcx> {
|
|
|
|
p!(print(self.a), " -> ", print(self.b))
|
|
|
|
}
|
|
|
|
|
2019-01-20 14:00:39 +02:00
|
|
|
ty::TraitPredicate<'tcx> {
|
2021-12-24 22:50:44 +08:00
|
|
|
p!(print(self.trait_ref.self_ty()), ": ");
|
2022-07-25 08:16:34 +00:00
|
|
|
if let ty::BoundConstness::ConstIfConst = self.constness && cx.tcx().features().const_trait_impl {
|
2021-12-24 22:50:44 +08:00
|
|
|
p!("~const ");
|
|
|
|
}
|
|
|
|
p!(print(self.trait_ref.print_only_trait_path()))
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::ProjectionPredicate<'tcx> {
|
2022-01-08 09:28:12 +00:00
|
|
|
p!(print(self.projection_ty), " == ", print(self.term))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::Term<'tcx> {
|
2022-09-05 14:03:53 +10:00
|
|
|
match self.unpack() {
|
|
|
|
ty::TermKind::Ty(ty) => p!(print(ty)),
|
|
|
|
ty::TermKind::Const(c) => p!(print(c)),
|
2022-01-08 09:28:12 +00:00
|
|
|
}
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-09-30 10:07:15 -06:00
|
|
|
ty::ClosureKind::Fn => p!("Fn"),
|
|
|
|
ty::ClosureKind::FnMut => p!("FnMut"),
|
|
|
|
ty::ClosureKind::FnOnce => p!("FnOnce"),
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::Predicate<'tcx> {
|
2021-01-07 11:20:28 -05:00
|
|
|
let binder = self.kind();
|
2020-12-23 13:16:25 -05:00
|
|
|
p!(print(binder))
|
2020-07-18 11:46:38 +02:00
|
|
|
}
|
|
|
|
|
2021-01-07 11:20:28 -05:00
|
|
|
ty::PredicateKind<'tcx> {
|
2020-07-18 11:46:38 +02:00
|
|
|
match *self {
|
2021-07-22 21:56:07 +08:00
|
|
|
ty::PredicateKind::Trait(ref data) => {
|
2020-07-18 11:46:38 +02:00
|
|
|
p!(print(data))
|
|
|
|
}
|
2021-01-07 11:20:28 -05:00
|
|
|
ty::PredicateKind::Subtype(predicate) => p!(print(predicate)),
|
2020-11-21 07:06:16 -05:00
|
|
|
ty::PredicateKind::Coerce(predicate) => p!(print(predicate)),
|
2021-01-07 11:20:28 -05:00
|
|
|
ty::PredicateKind::RegionOutlives(predicate) => p!(print(predicate)),
|
|
|
|
ty::PredicateKind::TypeOutlives(predicate) => p!(print(predicate)),
|
|
|
|
ty::PredicateKind::Projection(predicate) => p!(print(predicate)),
|
|
|
|
ty::PredicateKind::WellFormed(arg) => p!(print(arg), " well-formed"),
|
|
|
|
ty::PredicateKind::ObjectSafe(trait_def_id) => {
|
2020-10-02 15:08:01 -06:00
|
|
|
p!("the trait `", print_def_path(trait_def_id, &[]), "` is object-safe")
|
2020-07-18 11:46:38 +02:00
|
|
|
}
|
2021-01-07 11:20:28 -05:00
|
|
|
ty::PredicateKind::ClosureKind(closure_def_id, _closure_substs, kind) => {
|
2020-10-03 13:12:08 -06:00
|
|
|
p!("the closure `",
|
|
|
|
print_value_path(closure_def_id, &[]),
|
|
|
|
write("` implements the trait `{}`", kind))
|
2020-07-18 11:46:38 +02:00
|
|
|
}
|
2021-07-19 13:52:43 +02:00
|
|
|
ty::PredicateKind::ConstEvaluatable(uv) => {
|
2022-01-12 23:29:10 +00:00
|
|
|
p!("the constant `", print_value_path(uv.def.did, uv.substs), "` can be evaluated")
|
2020-07-18 11:46:38 +02:00
|
|
|
}
|
2021-01-07 11:20:28 -05:00
|
|
|
ty::PredicateKind::ConstEquate(c1, c2) => {
|
2020-10-02 15:08:01 -06:00
|
|
|
p!("the constant `", print(c1), "` equals `", print(c2), "`")
|
2020-06-18 20:41:43 +02:00
|
|
|
}
|
2021-01-07 11:20:28 -05:00
|
|
|
ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
|
2020-10-02 15:08:01 -06:00
|
|
|
p!("the type `", print(ty), "` is found in the environment")
|
2020-09-01 17:58:34 +02:00
|
|
|
}
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 16:39:44 +01:00
|
|
|
GenericArg<'tcx> {
|
2019-01-20 14:00:39 +02:00
|
|
|
match self.unpack() {
|
2019-09-25 16:39:44 +01:00
|
|
|
GenericArgKind::Lifetime(lt) => p!(print(lt)),
|
|
|
|
GenericArgKind::Type(ty) => p!(print(ty)),
|
|
|
|
GenericArgKind::Const(ct) => p!(print(ct)),
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-02 10:40:56 +03:00
|
|
|
|
|
|
|
fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, Namespace, DefId)) {
|
|
|
|
// Iterate all local crate items no matter where they are defined.
|
|
|
|
let hir = tcx.hir();
|
2022-04-03 15:50:33 -04:00
|
|
|
for id in hir.items() {
|
2022-04-29 16:45:48 -04:00
|
|
|
if matches!(tcx.def_kind(id.def_id), DefKind::Use) {
|
2022-04-07 14:04:07 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-04-03 15:50:33 -04:00
|
|
|
let item = hir.item(id);
|
2022-04-09 13:54:30 -04:00
|
|
|
if item.ident.name == kw::Empty {
|
2020-09-02 10:40:56 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-30 17:47:51 +01:00
|
|
|
let def_id = item.def_id.to_def_id();
|
|
|
|
let ns = tcx.def_kind(def_id).ns().unwrap_or(Namespace::TypeNS);
|
|
|
|
collect_fn(&item.ident, ns, def_id);
|
2020-09-02 10:40:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now take care of extern crate items.
|
|
|
|
let queue = &mut Vec::new();
|
|
|
|
let mut seen_defs: DefIdSet = Default::default();
|
|
|
|
|
2021-06-07 11:03:17 +02:00
|
|
|
for &cnum in tcx.crates(()).iter() {
|
2022-04-15 19:27:53 +02:00
|
|
|
let def_id = cnum.as_def_id();
|
2020-09-02 10:40:56 +03:00
|
|
|
|
|
|
|
// Ignore crates that are not direct dependencies.
|
|
|
|
match tcx.extern_crate(def_id) {
|
|
|
|
None => continue,
|
|
|
|
Some(extern_crate) => {
|
|
|
|
if !extern_crate.is_direct() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
queue.push(def_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate external crate defs but be mindful about visibility
|
|
|
|
while let Some(def) = queue.pop() {
|
2021-12-23 16:12:34 +08:00
|
|
|
for child in tcx.module_children(def).iter() {
|
2021-11-07 19:53:26 -08:00
|
|
|
if !child.vis.is_public() {
|
2020-09-02 10:40:56 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
match child.res {
|
|
|
|
def::Res::Def(DefKind::AssocTy, _) => {}
|
2021-01-28 18:01:36 +02:00
|
|
|
def::Res::Def(DefKind::TyAlias, _) => {}
|
2020-09-02 10:40:56 +03:00
|
|
|
def::Res::Def(defkind, def_id) => {
|
|
|
|
if let Some(ns) = defkind.ns() {
|
|
|
|
collect_fn(&child.ident, ns, def_id);
|
|
|
|
}
|
|
|
|
|
2021-12-18 20:07:58 +08:00
|
|
|
if matches!(defkind, DefKind::Mod | DefKind::Enum | DefKind::Trait)
|
|
|
|
&& seen_defs.insert(def_id)
|
|
|
|
{
|
2020-09-02 10:40:56 +03:00
|
|
|
queue.push(def_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The purpose of this function is to collect public symbols names that are unique across all
|
|
|
|
/// crates in the build. Later, when printing about types we can use those names instead of the
|
|
|
|
/// full exported path to them.
|
|
|
|
///
|
|
|
|
/// So essentially, if a symbol name can only be imported from one place for a type, and as
|
|
|
|
/// long as it was not glob-imported anywhere in the current crate, we can trim its printed
|
|
|
|
/// path and print only the name.
|
|
|
|
///
|
|
|
|
/// This has wide implications on error messages with types, for example, shortening
|
|
|
|
/// `std::vec::Vec` to just `Vec`, as long as there is no other `Vec` importable anywhere.
|
|
|
|
///
|
|
|
|
/// The implementation uses similar import discovery logic to that of 'use' suggestions.
|
2021-05-11 14:16:48 +02:00
|
|
|
fn trimmed_def_paths(tcx: TyCtxt<'_>, (): ()) -> FxHashMap<DefId, Symbol> {
|
2021-09-30 14:25:46 -04:00
|
|
|
let mut map: FxHashMap<DefId, Symbol> = FxHashMap::default();
|
2020-09-02 10:40:56 +03:00
|
|
|
|
|
|
|
if let TrimmedDefPaths::GoodPath = tcx.sess.opts.trimmed_def_paths {
|
|
|
|
// For good paths causing this bug, the `rustc_middle::ty::print::with_no_trimmed_paths`
|
|
|
|
// wrapper can be used to suppress this query, in exchange for full paths being formatted.
|
|
|
|
tcx.sess.delay_good_path_bug("trimmed_def_paths constructed");
|
|
|
|
}
|
|
|
|
|
|
|
|
let unique_symbols_rev: &mut FxHashMap<(Namespace, Symbol), Option<DefId>> =
|
|
|
|
&mut FxHashMap::default();
|
|
|
|
|
2021-04-04 14:40:35 +02:00
|
|
|
for symbol_set in tcx.resolutions(()).glob_map.values() {
|
2020-09-02 10:40:56 +03:00
|
|
|
for symbol in symbol_set {
|
|
|
|
unique_symbols_rev.insert((Namespace::TypeNS, *symbol), None);
|
|
|
|
unique_symbols_rev.insert((Namespace::ValueNS, *symbol), None);
|
|
|
|
unique_symbols_rev.insert((Namespace::MacroNS, *symbol), None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for_each_def(tcx, |ident, ns, def_id| {
|
|
|
|
use std::collections::hash_map::Entry::{Occupied, Vacant};
|
|
|
|
|
|
|
|
match unique_symbols_rev.entry((ns, ident.name)) {
|
|
|
|
Occupied(mut v) => match v.get() {
|
|
|
|
None => {}
|
|
|
|
Some(existing) => {
|
|
|
|
if *existing != def_id {
|
|
|
|
v.insert(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Vacant(v) => {
|
|
|
|
v.insert(Some(def_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for ((_, symbol), opt_def_id) in unique_symbols_rev.drain() {
|
2021-09-30 14:25:46 -04:00
|
|
|
use std::collections::hash_map::Entry::{Occupied, Vacant};
|
|
|
|
|
2020-09-02 10:40:56 +03:00
|
|
|
if let Some(def_id) = opt_def_id {
|
2021-09-30 14:25:46 -04:00
|
|
|
match map.entry(def_id) {
|
|
|
|
Occupied(mut v) => {
|
|
|
|
// A single DefId can be known under multiple names (e.g.,
|
|
|
|
// with a `pub use ... as ...;`). We need to ensure that the
|
|
|
|
// name placed in this map is chosen deterministically, so
|
|
|
|
// if we find multiple names (`symbol`) resolving to the
|
|
|
|
// same `def_id`, we prefer the lexicographically smallest
|
|
|
|
// name.
|
|
|
|
//
|
|
|
|
// Any stable ordering would be fine here though.
|
|
|
|
if *v.get() != symbol {
|
|
|
|
if v.get().as_str() > symbol.as_str() {
|
|
|
|
v.insert(symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Vacant(v) => {
|
|
|
|
v.insert(symbol);
|
|
|
|
}
|
|
|
|
}
|
2020-09-02 10:40:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
map
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn provide(providers: &mut ty::query::Providers) {
|
|
|
|
*providers = ty::query::Providers { trimmed_def_paths, ..*providers };
|
|
|
|
}
|
2021-11-19 20:51:19 -08:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct OpaqueFnEntry<'tcx> {
|
2021-11-23 10:34:01 -08:00
|
|
|
// The trait ref is already stored as a key, so just track if we have it as a real predicate
|
|
|
|
has_fn_once: bool,
|
2021-11-19 20:51:19 -08:00
|
|
|
fn_mut_trait_ref: Option<ty::PolyTraitRef<'tcx>>,
|
|
|
|
fn_trait_ref: Option<ty::PolyTraitRef<'tcx>>,
|
2022-01-10 23:39:21 +00:00
|
|
|
return_ty: Option<ty::Binder<'tcx, Term<'tcx>>>,
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|