2022-02-16 10:56:01 +01:00
|
|
|
use std::cell::Cell;
|
|
|
|
use std::fmt::{self, Write as _};
|
|
|
|
use std::iter;
|
|
|
|
use std::ops::{Deref, DerefMut};
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2024-11-02 19:33:00 -07:00
|
|
|
use rustc_abi::{ExternAbi, Size};
|
2023-11-24 23:35:05 +00:00
|
|
|
use rustc_apfloat::Float;
|
2024-06-18 19:33:21 -05:00
|
|
|
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
|
2022-05-11 16:36:26 -04:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
|
2024-03-21 14:02:13 +00:00
|
|
|
use rustc_data_structures::unord::UnordMap;
|
2020-03-24 09:09:42 +01:00
|
|
|
use rustc_hir as hir;
|
2022-11-22 17:19:19 +00:00
|
|
|
use rustc_hir::LangItem;
|
2020-09-02 10:40:56 +03:00
|
|
|
use rustc_hir::def::{self, CtorKind, DefKind, Namespace};
|
2023-12-21 12:27:04 +01:00
|
|
|
use rustc_hir::def_id::{CRATE_DEF_ID, DefIdMap, DefIdSet, LOCAL_CRATE, ModDefId};
|
2023-11-10 10:11:24 +08:00
|
|
|
use rustc_hir::definitions::{DefKey, DefPathDataName};
|
2024-05-10 14:59:56 -04:00
|
|
|
use rustc_macros::{Lift, extension};
|
2022-11-15 17:38:39 -08:00
|
|
|
use rustc_session::Limit;
|
2020-04-19 13:00:18 +02:00
|
|
|
use rustc_session::cstore::{ExternCrate, ExternCrateSource};
|
2024-12-13 10:29:23 +11:00
|
|
|
use rustc_span::{FileNameDisplayPreference, Ident, Symbol, kw, sym};
|
2024-07-06 12:33:03 -04:00
|
|
|
use rustc_type_ir::{Upcast as _, elaborate};
|
2022-10-22 20:18:16 +00:00
|
|
|
use smallvec::SmallVec;
|
2019-01-18 21:33:31 +02:00
|
|
|
|
|
|
|
// `pretty` is a separate module only for organization.
|
|
|
|
use super::*;
|
2022-02-16 10:56:01 +01:00
|
|
|
use crate::mir::interpret::{AllocRange, GlobalAlloc, Pointer, Provenance, Scalar};
|
2023-05-16 01:53:21 +02:00
|
|
|
use crate::query::{IntoQueryParam, Providers};
|
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::{
|
2024-11-11 17:36:18 +00:00
|
|
|
ConstInt, Expr, GenericArgKind, ParamConst, ScalarInt, Term, TermKind, TraitPredicate,
|
|
|
|
TypeFoldable, TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
|
2024-07-29 08:13:50 +10:00
|
|
|
};
|
2019-01-18 21:33:31 +02:00
|
|
|
|
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)) => {
|
2023-10-17 19:46:14 +02:00
|
|
|
$x.print(scoped_cx!())?
|
2019-01-18 21:33:31 +02:00
|
|
|
};
|
2019-01-25 12:28:43 +02:00
|
|
|
(@$method:ident($($arg:expr),*)) => {
|
2023-10-17 19:46:14 +02:00
|
|
|
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) => {
|
|
|
|
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) };
|
2022-12-06 20:13:31 -08:00
|
|
|
static FORCE_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
|
2024-03-03 04:37:33 +01:00
|
|
|
static REDUCED_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))
|
|
|
|
}
|
|
|
|
}
|
2023-06-27 09:07:14 +00:00
|
|
|
|
|
|
|
pub fn $name() -> bool {
|
|
|
|
$tl.with(|c| c.get())
|
|
|
|
}
|
2022-02-16 13:04:48 -05:00
|
|
|
)+
|
|
|
|
}
|
2020-09-02 10:40:56 +03:00
|
|
|
}
|
|
|
|
|
2022-02-16 13:04:48 -05:00
|
|
|
define_helper!(
|
2024-03-03 04:37:33 +01:00
|
|
|
/// Avoids running select queries during any prints that occur
|
2022-02-16 13:04:48 -05:00
|
|
|
/// 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.
|
2024-03-03 04:37:33 +01:00
|
|
|
fn with_reduced_queries(ReducedQueriesGuard, REDUCED_QUERIES);
|
2022-02-16 13:04:48 -05:00
|
|
|
/// 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);
|
2022-12-06 20:13:31 -08:00
|
|
|
fn with_forced_trimmed_paths(ForceTrimmedGuard, FORCE_TRIMMED_PATH);
|
2022-02-16 13:04:48 -05:00
|
|
|
/// 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
|
|
|
|
2024-03-03 04:37:33 +01:00
|
|
|
/// Avoids running any queries during prints.
|
|
|
|
pub macro with_no_queries($e:expr) {{
|
|
|
|
$crate::ty::print::with_reduced_queries!($crate::ty::print::with_forced_impl_filename_line!(
|
|
|
|
$crate::ty::print::with_no_trimmed_paths!($crate::ty::print::with_no_visible_paths!(
|
|
|
|
$crate::ty::print::with_forced_impl_filename_line!($e)
|
|
|
|
))
|
|
|
|
))
|
|
|
|
}}
|
|
|
|
|
2025-03-01 19:28:04 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum WrapBinderMode {
|
|
|
|
ForAll,
|
|
|
|
Unsafe,
|
|
|
|
}
|
|
|
|
impl WrapBinderMode {
|
|
|
|
pub fn start_str(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
WrapBinderMode::ForAll => "for<",
|
|
|
|
WrapBinderMode::Unsafe => "unsafe<",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2023-09-14 09:24:51 +10:00
|
|
|
#[derive(Copy, Clone, Default)]
|
2022-01-28 11:25:15 +11:00
|
|
|
pub struct RegionHighlightMode<'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> {
|
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`.
|
2023-09-14 09:24:51 +10:00
|
|
|
pub fn highlighting_region_vid(
|
|
|
|
&mut self,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
vid: ty::RegionVid,
|
|
|
|
number: usize,
|
|
|
|
) {
|
|
|
|
self.highlighting_region(ty::Region::new_var(tcx, 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.
|
2023-10-16 20:46:24 +02:00
|
|
|
pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
|
2019-01-18 21:33:31 +02:00
|
|
|
/// Like `print_def_path` but for value paths.
|
|
|
|
fn print_value_path(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2019-01-18 21:33:31 +02:00
|
|
|
def_id: DefId,
|
2023-07-11 22:35:29 +01:00
|
|
|
args: &'tcx [GenericArg<'tcx>],
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
2023-07-11 22:35:29 +01:00
|
|
|
self.print_def_path(def_id, args)
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2025-03-03 01:34:06 +00:00
|
|
|
fn print_in_binder<T>(&mut self, value: &ty::Binder<'tcx, T>) -> Result<(), PrintError>
|
2019-06-14 01:32:15 +03:00
|
|
|
where
|
2023-10-16 20:50:46 +02:00
|
|
|
T: Print<'tcx, Self> + TypeFoldable<TyCtxt<'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
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
fn wrap_binder<T, F: FnOnce(&T, &mut Self) -> Result<(), fmt::Error>>(
|
|
|
|
&mut self,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: &ty::Binder<'tcx, T>,
|
2025-03-01 19:28:04 +00:00
|
|
|
_mode: WrapBinderMode,
|
2020-12-11 15:02:46 -05:00
|
|
|
f: F,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError>
|
2020-12-11 15:02:46 -05:00
|
|
|
where
|
2025-02-23 04:46:51 +00:00
|
|
|
T: TypeFoldable<TyCtxt<'tcx>>,
|
2020-12-11 15:02:46 -05:00
|
|
|
{
|
|
|
|
f(value.as_ref().skip_binder(), self)
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Prints comma-separated elements.
|
2023-10-17 19:46:14 +02:00
|
|
|
fn comma_sep<T>(&mut self, mut elems: impl Iterator<Item = T>) -> Result<(), PrintError>
|
2019-06-14 01:32:15 +03:00
|
|
|
where
|
2023-10-16 20:50:46 +02:00
|
|
|
T: Print<'tcx, Self>,
|
2019-01-24 19:52:43 +02:00
|
|
|
{
|
|
|
|
if let Some(first) = elems.next() {
|
2023-10-17 19:46:14 +02:00
|
|
|
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(", ")?;
|
2023-10-17 19:46:14 +02:00
|
|
|
elem.print(self)?;
|
2019-01-24 19:52:43 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
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(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
|
|
|
f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
|
|
|
|
t: impl FnOnce(&mut Self) -> Result<(), PrintError>,
|
2020-02-26 19:36:10 +01:00
|
|
|
conversion: &str,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
2020-01-27 18:49:05 +01:00
|
|
|
self.write_str("{")?;
|
2023-10-17 19:46:14 +02:00
|
|
|
f(self)?;
|
2020-02-26 19:36:10 +01:00
|
|
|
self.write_str(conversion)?;
|
2023-10-17 19:46:14 +02:00
|
|
|
t(self)?;
|
2020-01-27 18:49:05 +01:00
|
|
|
self.write_str("}")?;
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2020-01-27 18:49:05 +01:00
|
|
|
}
|
|
|
|
|
2024-03-17 01:38:45 +11:00
|
|
|
/// Prints `(...)` around what `f` prints.
|
|
|
|
fn parenthesized(
|
|
|
|
&mut self,
|
|
|
|
f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
|
|
|
|
) -> Result<(), PrintError> {
|
|
|
|
self.write_str("(")?;
|
|
|
|
f(self)?;
|
|
|
|
self.write_str(")")?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Prints `(...)` around what `f` prints if `parenthesized` is true, otherwise just prints `f`.
|
|
|
|
fn maybe_parenthesized(
|
|
|
|
&mut self,
|
|
|
|
f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
|
|
|
|
parenthesized: bool,
|
|
|
|
) -> Result<(), PrintError> {
|
|
|
|
if parenthesized {
|
|
|
|
self.parenthesized(f)?;
|
|
|
|
} else {
|
|
|
|
f(self)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Prints `<...>` around what `f` prints.
|
2019-01-25 12:11:50 +02:00
|
|
|
fn generic_delimiters(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
|
|
|
f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
|
|
|
|
) -> Result<(), PrintError>;
|
2019-01-18 21:33:31 +02:00
|
|
|
|
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
|
|
|
|
2023-01-08 18:41:09 +00:00
|
|
|
fn reset_type_limit(&mut self) {}
|
|
|
|
|
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`.
|
2023-10-17 19:46:14 +02:00
|
|
|
fn try_print_visible_def_path(&mut self, def_id: DefId) -> Result<bool, PrintError> {
|
2023-11-26 23:23:44 +00:00
|
|
|
if with_no_visible_paths() {
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(false);
|
2021-09-20 20:22:55 +04:00
|
|
|
}
|
|
|
|
|
2019-07-08 22:26:15 +02:00
|
|
|
let mut callers = Vec::new();
|
|
|
|
self.try_print_visible_def_path_recur(def_id, &mut callers)
|
|
|
|
}
|
|
|
|
|
2022-12-06 20:13:31 -08:00
|
|
|
// Given a `DefId`, produce a short name. For types and traits, it prints *only* its name,
|
|
|
|
// For associated items on traits it prints out the trait's name and the associated item's name.
|
|
|
|
// For enum variants, if they have an unique name, then we only print the name, otherwise we
|
|
|
|
// print the enum name and the variant name. Otherwise, we do not print anything and let the
|
|
|
|
// caller use the `print_def_path` fallback.
|
2023-10-17 19:46:14 +02:00
|
|
|
fn force_print_trimmed_def_path(&mut self, def_id: DefId) -> Result<bool, PrintError> {
|
2022-12-06 20:13:31 -08:00
|
|
|
let key = self.tcx().def_key(def_id);
|
|
|
|
let visible_parent_map = self.tcx().visible_parent_map(());
|
|
|
|
let kind = self.tcx().def_kind(def_id);
|
|
|
|
|
|
|
|
let get_local_name = |this: &Self, name, def_id, key: DefKey| {
|
|
|
|
if let Some(visible_parent) = visible_parent_map.get(&def_id)
|
|
|
|
&& let actual_parent = this.tcx().opt_parent(def_id)
|
|
|
|
&& let DefPathData::TypeNs(_) = key.disambiguated_data.data
|
|
|
|
&& Some(*visible_parent) != actual_parent
|
|
|
|
{
|
|
|
|
this.tcx()
|
2023-04-26 20:53:51 +02:00
|
|
|
// FIXME(typed_def_id): Further propagate ModDefId
|
|
|
|
.module_children(ModDefId::new_unchecked(*visible_parent))
|
2022-12-06 20:13:31 -08:00
|
|
|
.iter()
|
|
|
|
.filter(|child| child.res.opt_def_id() == Some(def_id))
|
|
|
|
.find(|child| child.vis.is_public() && child.ident.name != kw::Underscore)
|
|
|
|
.map(|child| child.ident.name)
|
|
|
|
.unwrap_or(name)
|
|
|
|
} else {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let DefKind::Variant = kind
|
|
|
|
&& let Some(symbol) = self.tcx().trimmed_def_paths(()).get(&def_id)
|
|
|
|
{
|
|
|
|
// If `Assoc` is unique, we don't want to talk about `Trait::Assoc`.
|
2023-11-21 20:07:32 +01:00
|
|
|
self.write_str(get_local_name(self, *symbol, def_id, key).as_str())?;
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(true);
|
2022-12-06 20:13:31 -08:00
|
|
|
}
|
|
|
|
if let Some(symbol) = key.get_opt_name() {
|
|
|
|
if let DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy = kind
|
|
|
|
&& let Some(parent) = self.tcx().opt_parent(def_id)
|
|
|
|
&& let parent_key = self.tcx().def_key(parent)
|
|
|
|
&& let Some(symbol) = parent_key.get_opt_name()
|
|
|
|
{
|
|
|
|
// Trait
|
2023-11-21 20:07:32 +01:00
|
|
|
self.write_str(get_local_name(self, symbol, parent, parent_key).as_str())?;
|
2022-12-06 20:13:31 -08:00
|
|
|
self.write_str("::")?;
|
|
|
|
} else if let DefKind::Variant = kind
|
|
|
|
&& let Some(parent) = self.tcx().opt_parent(def_id)
|
|
|
|
&& let parent_key = self.tcx().def_key(parent)
|
|
|
|
&& let Some(symbol) = parent_key.get_opt_name()
|
|
|
|
{
|
|
|
|
// Enum
|
|
|
|
|
|
|
|
// For associated items and variants, we want the "full" path, namely, include
|
|
|
|
// the parent type in the path. For example, `Iterator::Item`.
|
2023-11-21 20:07:32 +01:00
|
|
|
self.write_str(get_local_name(self, symbol, parent, parent_key).as_str())?;
|
2022-12-06 20:13:31 -08:00
|
|
|
self.write_str("::")?;
|
|
|
|
} else if let DefKind::Struct
|
|
|
|
| DefKind::Union
|
|
|
|
| DefKind::Enum
|
|
|
|
| DefKind::Trait
|
2023-09-26 02:15:32 +00:00
|
|
|
| DefKind::TyAlias
|
|
|
|
| DefKind::Fn
|
|
|
|
| DefKind::Const
|
2024-02-23 23:12:20 +00:00
|
|
|
| DefKind::Static { .. } = kind
|
2022-12-06 20:13:31 -08:00
|
|
|
{
|
|
|
|
} else {
|
|
|
|
// If not covered above, like for example items out of `impl` blocks, fallback.
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(false);
|
2022-12-06 20:13:31 -08:00
|
|
|
}
|
2023-11-21 20:07:32 +01:00
|
|
|
self.write_str(get_local_name(self, symbol, def_id, key).as_str())?;
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(true);
|
2022-12-06 20:13:31 -08:00
|
|
|
}
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(false)
|
2022-12-06 20:13:31 -08:00
|
|
|
}
|
|
|
|
|
2020-09-02 10:40:56 +03:00
|
|
|
/// Try to see if this path can be trimmed to a unique symbol name.
|
2023-10-17 19:46:14 +02:00
|
|
|
fn try_print_trimmed_def_path(&mut self, def_id: DefId) -> Result<bool, PrintError> {
|
2024-01-10 11:08:06 +11:00
|
|
|
if with_forced_trimmed_paths() && self.force_print_trimmed_def_path(def_id)? {
|
|
|
|
return Ok(true);
|
2022-12-06 20:13:31 -08:00
|
|
|
}
|
2024-01-10 11:08:06 +11:00
|
|
|
if self.tcx().sess.opts.unstable_opts.trim_diagnostic_paths
|
2024-01-10 12:47:22 +11:00
|
|
|
&& self.tcx().sess.opts.trimmed_def_paths
|
2024-01-10 11:08:06 +11:00
|
|
|
&& !with_no_trimmed_paths()
|
|
|
|
&& !with_crate_prefix()
|
|
|
|
&& let Some(symbol) = self.tcx().trimmed_def_paths(()).get(&def_id)
|
2020-09-02 10:40:56 +03:00
|
|
|
{
|
2024-01-10 11:08:06 +11:00
|
|
|
write!(self, "{}", Ident::with_dummy_span(*symbol))?;
|
|
|
|
Ok(true)
|
|
|
|
} else {
|
|
|
|
Ok(false)
|
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(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2019-01-18 21:33:31 +02:00
|
|
|
def_id: DefId,
|
2019-07-08 22:26:15 +02:00
|
|
|
callers: &mut Vec<DefId>,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<bool, PrintError> {
|
2019-01-18 21:33:31 +02:00
|
|
|
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 {
|
2023-10-17 19:46:14 +02:00
|
|
|
self.path_crate(cnum)?;
|
|
|
|
return Ok(true);
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2024-08-17 12:50:12 -04:00
|
|
|
match self.tcx().extern_crate(cnum) {
|
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() {
|
2023-10-17 19:46:14 +02:00
|
|
|
self.path_crate(cnum)?;
|
|
|
|
return Ok(true);
|
2021-10-10 18:18:30 +03:00
|
|
|
}
|
|
|
|
|
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).
|
2023-10-17 19:46:14 +02:00
|
|
|
with_no_visible_paths!(self.print_def_path(def_id, &[])?);
|
2021-10-10 18:50:44 +03:00
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(true);
|
2020-06-29 21:51:32 +03:00
|
|
|
}
|
2020-07-01 20:05:51 +03:00
|
|
|
(ExternCrateSource::Path, LOCAL_CRATE) => {
|
2023-10-17 19:46:14 +02:00
|
|
|
self.path_crate(cnum)?;
|
|
|
|
return Ok(true);
|
2020-06-29 21:51:32 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
2019-01-18 21:33:31 +02:00
|
|
|
None => {
|
2023-10-17 19:46:14 +02:00
|
|
|
self.path_crate(cnum)?;
|
|
|
|
return Ok(true);
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if def_id.is_local() {
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(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 {
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(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()
|
2023-04-26 20:53:51 +02:00
|
|
|
// FIXME(typed_def_id): Further propagate ModDefId
|
|
|
|
.module_children(ModDefId::new_unchecked(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.
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(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) {
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(false);
|
2021-11-30 17:19:37 -08:00
|
|
|
}
|
|
|
|
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)? {
|
2023-10-17 19:46:14 +02:00
|
|
|
false => return Ok(false),
|
|
|
|
true => {}
|
2021-11-30 17:19:37 -08:00
|
|
|
}
|
|
|
|
callers.pop();
|
2023-10-17 19:46:14 +02:00
|
|
|
self.path_append(|_| Ok(()), &DisambiguatedDefPathData { data, disambiguator: 0 })?;
|
|
|
|
Ok(true)
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
fn pretty_path_qualified(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2019-01-18 21:33:31 +02:00
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
trait_ref: Option<ty::TraitRef<'tcx>>,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
self.generic_delimiters(|cx| {
|
2019-01-18 21:33:31 +02:00
|
|
|
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
|
|
|
}
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-01-18 21:33:31 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
fn pretty_path_append_impl(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
|
|
|
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
|
2019-01-18 21:33:31 +02:00
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
trait_ref: Option<ty::TraitRef<'tcx>>,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
|
|
|
print_prefix(self)?;
|
2019-01-18 21:33:31 +02:00
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
self.generic_delimiters(|cx| {
|
2019-01-18 21:33:31 +02:00
|
|
|
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
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-01-18 21:33:31 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
fn pretty_print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
|
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())),
|
2023-02-02 13:57:36 +00:00
|
|
|
ty::Pat(ty, pat) => {
|
|
|
|
p!("(", print(ty), ") is ", write("{pat:?}"))
|
|
|
|
}
|
2024-03-21 17:33:10 -04:00
|
|
|
ty::RawPtr(ty, mutbl) => {
|
2024-03-24 14:53:58 +01:00
|
|
|
p!(write("*{} ", mutbl.ptr_str()));
|
2024-03-21 17:33:10 -04:00
|
|
|
p!(print(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!("!"),
|
2023-11-21 20:07:32 +01:00
|
|
|
ty::Tuple(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
|
|
|
}
|
2023-07-11 22:35:29 +01:00
|
|
|
ty::FnDef(def_id, args) => {
|
2024-03-03 04:37:33 +01:00
|
|
|
if with_reduced_queries() {
|
2023-07-11 22:35:29 +01:00
|
|
|
p!(print_def_path(def_id, args));
|
2023-02-02 05:49:07 +00:00
|
|
|
} else {
|
2024-12-15 20:32:14 +00:00
|
|
|
let mut sig = self.tcx().fn_sig(def_id).instantiate(self.tcx(), args);
|
|
|
|
if self.tcx().codegen_fn_attrs(def_id).safe_target_features {
|
|
|
|
p!("#[target_features] ");
|
|
|
|
sig = sig.map_bound(|mut sig| {
|
|
|
|
sig.safety = hir::Safety::Safe;
|
|
|
|
sig
|
|
|
|
});
|
|
|
|
}
|
2023-07-11 22:35:29 +01:00
|
|
|
p!(print(sig), " {{", print_value_path(def_id, args), "}}");
|
2023-02-02 05:49:07 +00:00
|
|
|
}
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2024-08-08 17:18:20 +10:00
|
|
|
ty::FnPtr(ref sig_tys, hdr) => p!(print(sig_tys.with(hdr))),
|
2024-12-21 17:05:40 +00:00
|
|
|
ty::UnsafeBinder(ref bound_ty) => {
|
2025-03-01 19:28:04 +00:00
|
|
|
self.wrap_binder(bound_ty, WrapBinderMode::Unsafe, |ty, cx| {
|
|
|
|
cx.pretty_print_type(*ty)
|
|
|
|
})?;
|
2024-12-21 17:05:40 +00:00
|
|
|
}
|
2019-05-30 15:33:13 -07:00
|
|
|
ty::Infer(infer_ty) => {
|
2023-05-26 18:54:59 +01:00
|
|
|
if self.should_print_verbose() {
|
|
|
|
p!(write("{:?}", ty.kind()));
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2023-05-26 18:54:59 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-05-26 18:54:59 +01:00
|
|
|
p!(write("{}", infer_ty))
|
2019-05-30 15:33:13 -07:00
|
|
|
}
|
|
|
|
} else {
|
2023-05-26 18:54:59 +01:00
|
|
|
p!(write("{}", infer_ty))
|
2019-05-30 15:33:13 -07:00
|
|
|
}
|
|
|
|
}
|
2023-05-26 16:01:23 +01: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 {
|
2023-05-26 18:54:59 +01:00
|
|
|
ty::BoundTyKind::Anon => {
|
2023-10-17 19:46:14 +02:00
|
|
|
rustc_type_ir::debug_bound_var(self, debruijn, bound_ty.var)?
|
2023-05-26 18:54:59 +01:00
|
|
|
}
|
2023-03-23 00:07:55 +00:00
|
|
|
ty::BoundTyKind::Param(_, s) => match self.should_print_verbose() {
|
2023-05-26 18:54:59 +01:00
|
|
|
true => p!(write("{:?}", ty.kind())),
|
|
|
|
false => p!(write("{s}")),
|
2023-03-23 00:07:55 +00:00
|
|
|
},
|
2019-01-24 19:52:43 +02:00
|
|
|
},
|
2023-07-11 22:35:29 +01:00
|
|
|
ty::Adt(def, args) => {
|
|
|
|
p!(print_def_path(def.did(), args));
|
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
|
|
|
}
|
2023-03-07 12:03:11 +00:00
|
|
|
ty::Alias(ty::Projection | ty::Inherent | ty::Weak, ref data) => {
|
2024-02-27 16:11:35 +00:00
|
|
|
p!(print(data))
|
2022-09-08 00:45:09 +00:00
|
|
|
}
|
2023-04-06 21:12:17 -04:00
|
|
|
ty::Placeholder(placeholder) => match placeholder.bound.kind {
|
2023-05-26 18:54:59 +01:00
|
|
|
ty::BoundTyKind::Anon => p!(write("{placeholder:?}")),
|
|
|
|
ty::BoundTyKind::Param(_, name) => match self.should_print_verbose() {
|
|
|
|
true => p!(write("{:?}", ty.kind())),
|
|
|
|
false => p!(write("{name}")),
|
|
|
|
},
|
2023-02-17 21:21:27 +00:00
|
|
|
},
|
2023-07-11 22:35:29 +01:00
|
|
|
ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => {
|
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.
|
2023-12-19 12:39:58 -05:00
|
|
|
// [Unless `-Zverbose-internals` is used, e.g. in the output of
|
2023-09-07 15:14:40 +10:00
|
|
|
// `tests/ui/nll/ty-outlives/impl-trait-captures.rs`, for
|
|
|
|
// example.]
|
2023-02-02 05:49:07 +00:00
|
|
|
if self.should_print_verbose() {
|
|
|
|
// FIXME(eddyb) print this with `print_def_path`.
|
2023-09-07 15:14:40 +10:00
|
|
|
p!(write("Opaque({:?}, {})", def_id, args.print_as_list()));
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
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) {
|
2023-09-26 02:15:32 +00:00
|
|
|
DefKind::TyAlias | DefKind::AssocTy => {
|
2023-02-02 05:49:07 +00:00
|
|
|
// NOTE: I know we should check for NO_QUERIES here, but it's alright.
|
2023-02-02 15:02:21 -08:00
|
|
|
// `type_of` on a type alias or assoc type should never cause a cycle.
|
2022-12-13 11:07:42 +00:00
|
|
|
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id: d, .. }) =
|
2023-07-11 22:35:29 +01:00
|
|
|
*self.tcx().type_of(parent).instantiate_identity().kind()
|
2022-11-26 21:09:39 +00:00
|
|
|
{
|
2022-02-14 16:10:22 +00:00
|
|
|
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}`.
|
2023-07-11 22:35:29 +01:00
|
|
|
p!(print_def_path(parent, args));
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2022-02-14 16:10:22 +00:00
|
|
|
}
|
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);`
|
2023-07-11 22:35:29 +01:00
|
|
|
p!(print_def_path(def_id, args));
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2023-02-02 05:49:07 +00:00
|
|
|
_ => {
|
2024-03-03 04:37:33 +01:00
|
|
|
if with_reduced_queries() {
|
2023-02-02 05:49:07 +00:00
|
|
|
p!(print_def_path(def_id, &[]));
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2023-02-02 05:49:07 +00:00
|
|
|
} else {
|
2023-07-11 22:35:29 +01:00
|
|
|
return self.pretty_print_opaque_impl_type(def_id, args);
|
2023-02-02 05:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-14 16:10:22 +00:00
|
|
|
}
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2020-09-30 10:07:15 -06:00
|
|
|
ty::Str => p!("str"),
|
2023-12-21 01:52:10 +00:00
|
|
|
ty::Coroutine(did, args) => {
|
2024-03-22 17:43:53 -07:00
|
|
|
p!("{{");
|
2023-10-19 21:46:28 +00:00
|
|
|
let coroutine_kind = self.tcx().coroutine_kind(did).unwrap();
|
2023-12-25 16:56:12 +00:00
|
|
|
let should_print_movability = self.should_print_verbose()
|
|
|
|
|| matches!(coroutine_kind, hir::CoroutineKind::Coroutine(_));
|
2022-11-25 09:53:58 +01:00
|
|
|
|
|
|
|
if should_print_movability {
|
2023-12-26 22:43:11 +00:00
|
|
|
match coroutine_kind.movability() {
|
2022-11-25 09:53:58 +01:00
|
|
|
hir::Movability::Movable => {}
|
|
|
|
hir::Movability::Static => p!("static "),
|
|
|
|
}
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2022-10-30 17:38:49 +05:30
|
|
|
if !self.should_print_verbose() {
|
2023-10-19 21:46:28 +00:00
|
|
|
p!(write("{}", coroutine_kind));
|
2024-03-22 17:43:53 -07:00
|
|
|
if coroutine_kind.is_fn_like() {
|
|
|
|
// If we are printing an `async fn` coroutine type, then give the path
|
|
|
|
// of the fn, instead of its span, because that will in most cases be
|
|
|
|
// more helpful for the reader than just a source location.
|
|
|
|
//
|
|
|
|
// This will look like:
|
|
|
|
// {async fn body of some_fn()}
|
|
|
|
let did_of_the_fn_item = self.tcx().parent(did);
|
|
|
|
p!(" of ", print_def_path(did_of_the_fn_item, args), "()");
|
|
|
|
} else if let Some(local_did) = did.as_local() {
|
|
|
|
let span = self.tcx().def_span(local_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 {
|
2024-03-22 17:43:53 -07:00
|
|
|
p!("@", print_def_path(did, args));
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-07-11 22:35:29 +01:00
|
|
|
p!(print_def_path(did, args));
|
2024-01-23 15:10:23 +00:00
|
|
|
p!(
|
|
|
|
" upvar_tys=",
|
|
|
|
print(args.as_coroutine().tupled_upvars_ty()),
|
2024-12-25 01:08:59 +00:00
|
|
|
" resume_ty=",
|
|
|
|
print(args.as_coroutine().resume_ty()),
|
|
|
|
" yield_ty=",
|
|
|
|
print(args.as_coroutine().yield_ty()),
|
|
|
|
" return_ty=",
|
|
|
|
print(args.as_coroutine().return_ty()),
|
2024-01-23 15:10:23 +00:00
|
|
|
" witness=",
|
|
|
|
print(args.as_coroutine().witness())
|
|
|
|
);
|
2020-03-13 03:23:38 +02:00
|
|
|
}
|
|
|
|
|
2023-09-09 08:36:50 +02:00
|
|
|
p!("}}")
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2023-10-19 16:06:43 +00:00
|
|
|
ty::CoroutineWitness(did, args) => {
|
2023-09-09 08:36:50 +02:00
|
|
|
p!(write("{{"));
|
2023-12-19 12:39:58 -05:00
|
|
|
if !self.tcx().sess.verbose_internals() {
|
2023-10-19 21:46:28 +00:00
|
|
|
p!("coroutine witness");
|
2022-10-01 14:56:24 +02:00
|
|
|
if let Some(did) = did.as_local() {
|
|
|
|
let span = self.tcx().def_span(did);
|
|
|
|
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)
|
|
|
|
));
|
|
|
|
} else {
|
2023-07-11 22:35:29 +01:00
|
|
|
p!(write("@"), print_def_path(did, args));
|
2022-10-01 14:56:24 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-07-11 22:35:29 +01:00
|
|
|
p!(print_def_path(did, args));
|
2022-10-01 14:56:24 +02:00
|
|
|
}
|
|
|
|
|
2023-09-09 08:36:50 +02:00
|
|
|
p!("}}")
|
2022-10-01 14:56:24 +02:00
|
|
|
}
|
2023-07-11 22:35:29 +01:00
|
|
|
ty::Closure(did, args) => {
|
2023-09-09 08:36:50 +02:00
|
|
|
p!(write("{{"));
|
2022-10-30 17:38:49 +05:30
|
|
|
if !self.should_print_verbose() {
|
2020-09-08 10:43:06 -04:00
|
|
|
p!(write("closure"));
|
2025-01-31 03:26:56 +00:00
|
|
|
if self.should_truncate() {
|
|
|
|
write!(self, "@...}}")?;
|
|
|
|
return Ok(());
|
|
|
|
} else {
|
|
|
|
if let Some(did) = did.as_local() {
|
|
|
|
if self.tcx().sess.opts.unstable_opts.span_free_formats {
|
|
|
|
p!("@", print_def_path(did.to_def_id(), args));
|
2022-12-13 16:34:04 -08:00
|
|
|
} else {
|
2025-01-31 03:26:56 +00:00
|
|
|
let span = self.tcx().def_span(did);
|
|
|
|
let preference = if with_forced_trimmed_paths() {
|
|
|
|
FileNameDisplayPreference::Short
|
|
|
|
} else {
|
|
|
|
FileNameDisplayPreference::Remapped
|
|
|
|
};
|
|
|
|
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_string(span, preference)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p!(write("@"), print_def_path(did, args));
|
2020-03-13 03:23:38 +02:00
|
|
|
}
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-07-11 22:35:29 +01:00
|
|
|
p!(print_def_path(did, args));
|
2024-01-23 15:10:23 +00:00
|
|
|
p!(
|
|
|
|
" closure_kind_ty=",
|
|
|
|
print(args.as_closure().kind_ty()),
|
|
|
|
" closure_sig_as_fn_ptr_ty=",
|
|
|
|
print(args.as_closure().sig_as_fn_ptr_ty()),
|
|
|
|
" upvar_tys=",
|
|
|
|
print(args.as_closure().tupled_upvars_ty())
|
|
|
|
);
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2023-09-09 08:36:50 +02:00
|
|
|
p!("}}");
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
2024-01-24 18:01:56 +00:00
|
|
|
ty::CoroutineClosure(did, args) => {
|
|
|
|
p!(write("{{"));
|
|
|
|
if !self.should_print_verbose() {
|
2024-02-10 22:43:35 +00:00
|
|
|
match self.tcx().coroutine_kind(self.tcx().coroutine_for_closure(did)).unwrap()
|
|
|
|
{
|
|
|
|
hir::CoroutineKind::Desugared(
|
|
|
|
hir::CoroutineDesugaring::Async,
|
|
|
|
hir::CoroutineSource::Closure,
|
|
|
|
) => p!("async closure"),
|
|
|
|
hir::CoroutineKind::Desugared(
|
|
|
|
hir::CoroutineDesugaring::AsyncGen,
|
|
|
|
hir::CoroutineSource::Closure,
|
|
|
|
) => p!("async gen closure"),
|
|
|
|
hir::CoroutineKind::Desugared(
|
|
|
|
hir::CoroutineDesugaring::Gen,
|
|
|
|
hir::CoroutineSource::Closure,
|
|
|
|
) => p!("gen closure"),
|
|
|
|
_ => unreachable!(
|
|
|
|
"coroutine from coroutine-closure should have CoroutineSource::Closure"
|
|
|
|
),
|
|
|
|
}
|
2024-01-24 18:01:56 +00:00
|
|
|
if let Some(did) = did.as_local() {
|
|
|
|
if self.tcx().sess.opts.unstable_opts.span_free_formats {
|
|
|
|
p!("@", print_def_path(did.to_def_id(), args));
|
|
|
|
} else {
|
|
|
|
let span = self.tcx().def_span(did);
|
|
|
|
let preference = if with_forced_trimmed_paths() {
|
|
|
|
FileNameDisplayPreference::Short
|
|
|
|
} else {
|
|
|
|
FileNameDisplayPreference::Remapped
|
|
|
|
};
|
|
|
|
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_string(span, preference)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p!(write("@"), print_def_path(did, args));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p!(print_def_path(did, args));
|
|
|
|
p!(
|
|
|
|
" closure_kind_ty=",
|
|
|
|
print(args.as_coroutine_closure().kind_ty()),
|
|
|
|
" signature_parts_ty=",
|
|
|
|
print(args.as_coroutine_closure().signature_parts_ty()),
|
|
|
|
" upvar_tys=",
|
|
|
|
print(args.as_coroutine_closure().tupled_upvars_ty()),
|
|
|
|
" coroutine_captures_by_ref_ty=",
|
|
|
|
print(args.as_coroutine_closure().coroutine_captures_by_ref_ty()),
|
|
|
|
" coroutine_witness_ty=",
|
|
|
|
print(args.as_coroutine_closure().coroutine_witness_ty())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
p!("}}");
|
|
|
|
}
|
2023-01-14 18:32:17 +00:00
|
|
|
ty::Array(ty, sz) => p!("[", print(ty), "; ", print(sz), "]"),
|
2020-09-30 10:07:15 -06:00
|
|
|
ty::Slice(ty) => p!("[", print(ty), "]"),
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2021-11-19 20:51:19 -08:00
|
|
|
fn pretty_print_opaque_impl_type(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2021-11-19 20:51:19 -08:00
|
|
|
def_id: DefId,
|
2024-05-09 19:47:08 +00:00
|
|
|
args: ty::GenericArgsRef<'tcx>,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
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.
|
2023-04-17 16:43:46 -06:00
|
|
|
let bounds = tcx.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();
|
2023-12-27 17:57:55 +01:00
|
|
|
let mut has_sized_bound = false;
|
|
|
|
let mut has_negative_sized_bound = false;
|
2022-10-22 20:18:16 +00:00
|
|
|
let mut lifetimes = SmallVec::<[ty::Region<'tcx>; 1]>::new();
|
2021-11-19 20:51:19 -08:00
|
|
|
|
2023-07-17 17:49:47 +00:00
|
|
|
for (predicate, _) in bounds.iter_instantiated_copied(tcx, args) {
|
2021-11-19 20:51:19 -08:00
|
|
|
let bound_predicate = predicate.kind();
|
|
|
|
|
|
|
|
match bound_predicate.skip_binder() {
|
2023-06-19 20:46:46 +00:00
|
|
|
ty::ClauseKind::Trait(pred) => {
|
2023-12-27 17:57:55 +01:00
|
|
|
// Don't print `+ Sized`, but rather `+ ?Sized` if absent.
|
2024-11-11 17:42:35 +00:00
|
|
|
if tcx.is_lang_item(pred.def_id(), LangItem::Sized) {
|
2023-12-27 17:57:55 +01:00
|
|
|
match pred.polarity {
|
2024-03-21 15:45:28 -04:00
|
|
|
ty::PredicatePolarity::Positive => {
|
2023-12-27 17:57:55 +01:00
|
|
|
has_sized_bound = true;
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-21 15:45:28 -04:00
|
|
|
ty::PredicatePolarity::Negative => has_negative_sized_bound = true,
|
2023-12-27 17:57:55 +01:00
|
|
|
}
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
|
|
|
|
2023-12-27 17:57:55 +01:00
|
|
|
self.insert_trait_and_projection(
|
2024-11-11 17:36:18 +00:00
|
|
|
bound_predicate.rebind(pred),
|
2023-12-27 17:57:55 +01:00
|
|
|
None,
|
|
|
|
&mut traits,
|
|
|
|
&mut fn_traits,
|
|
|
|
);
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
2023-06-19 20:46:46 +00:00
|
|
|
ty::ClauseKind::Projection(pred) => {
|
2024-11-11 17:36:18 +00:00
|
|
|
let proj = bound_predicate.rebind(pred);
|
|
|
|
let trait_ref = proj.map_bound(|proj| TraitPredicate {
|
|
|
|
trait_ref: proj.projection_term.trait_ref(tcx),
|
|
|
|
polarity: ty::PredicatePolarity::Positive,
|
|
|
|
});
|
2021-11-19 20:51:19 -08:00
|
|
|
|
|
|
|
self.insert_trait_and_projection(
|
|
|
|
trait_ref,
|
2024-12-11 00:59:39 +00:00
|
|
|
Some((proj.item_def_id(), proj.term())),
|
2021-11-19 20:51:19 -08:00
|
|
|
&mut traits,
|
|
|
|
&mut fn_traits,
|
|
|
|
);
|
|
|
|
}
|
2023-06-19 20:46:46 +00:00
|
|
|
ty::ClauseKind::TypeOutlives(outlives) => {
|
2022-10-22 20:18:16 +00:00
|
|
|
lifetimes.push(outlives.1);
|
|
|
|
}
|
2021-11-19 20:51:19 -08:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2023-12-27 17:57:55 +01:00
|
|
|
let paren_needed = fn_traits.len() > 1 || traits.len() > 0 || !has_sized_bound;
|
2021-11-19 20:51:19 -08:00
|
|
|
|
2025-02-23 04:46:51 +00:00
|
|
|
for ((bound_args_and_self_ty, is_async), 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
|
|
|
|
2024-11-11 17:42:35 +00:00
|
|
|
let trait_def_id = if is_async {
|
|
|
|
tcx.async_fn_trait_kind_to_def_id(entry.kind).expect("expected AsyncFn lang items")
|
|
|
|
} else {
|
|
|
|
tcx.fn_trait_kind_to_def_id(entry.kind).expect("expected Fn lang items")
|
|
|
|
};
|
2021-11-19 20:51:19 -08:00
|
|
|
|
2024-11-11 17:36:18 +00:00
|
|
|
if let Some(return_ty) = entry.return_ty {
|
2025-03-01 19:28:04 +00:00
|
|
|
self.wrap_binder(
|
|
|
|
&bound_args_and_self_ty,
|
|
|
|
WrapBinderMode::ForAll,
|
|
|
|
|(args, _), cx| {
|
|
|
|
define_scoped_cx!(cx);
|
|
|
|
p!(write("{}", tcx.item_name(trait_def_id)));
|
|
|
|
p!("(");
|
|
|
|
|
|
|
|
for (idx, ty) in args.iter().enumerate() {
|
|
|
|
if idx > 0 {
|
|
|
|
p!(", ");
|
|
|
|
}
|
|
|
|
p!(print(ty));
|
2022-01-10 23:39:21 +00:00
|
|
|
}
|
2024-11-11 17:36:18 +00:00
|
|
|
|
2025-03-01 19:28:04 +00:00
|
|
|
p!(")");
|
|
|
|
if let Some(ty) = return_ty.skip_binder().as_type() {
|
|
|
|
if !ty.is_unit() {
|
|
|
|
p!(" -> ", print(return_ty));
|
|
|
|
}
|
2022-06-21 21:27:15 -07:00
|
|
|
}
|
2025-03-01 19:28:04 +00:00
|
|
|
p!(write("{}", if paren_needed { ")" } else { "" }));
|
2022-06-21 21:27:15 -07:00
|
|
|
|
2025-03-01 19:28:04 +00:00
|
|
|
first = false;
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
)?;
|
2024-11-11 17:36:18 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, render this like a regular trait.
|
|
|
|
traits.insert(
|
2025-02-23 04:46:51 +00:00
|
|
|
bound_args_and_self_ty.map_bound(|(args, self_ty)| ty::TraitPredicate {
|
2024-11-11 17:36:18 +00:00
|
|
|
polarity: ty::PredicatePolarity::Positive,
|
2025-02-23 04:46:51 +00:00
|
|
|
trait_ref: ty::TraitRef::new(
|
|
|
|
tcx,
|
|
|
|
trait_def_id,
|
|
|
|
[self_ty, Ty::new_tup(tcx, args)],
|
|
|
|
),
|
2024-11-11 17:36:18 +00:00
|
|
|
}),
|
|
|
|
FxIndexMap::default(),
|
|
|
|
);
|
|
|
|
}
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print the rest of the trait types (that aren't Fn* family of traits)
|
2024-11-11 17:36:18 +00:00
|
|
|
for (trait_pred, 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
|
|
|
|
2025-03-01 19:28:04 +00:00
|
|
|
self.wrap_binder(&trait_pred, WrapBinderMode::ForAll, |trait_pred, cx| {
|
2022-06-22 10:21:24 -07:00
|
|
|
define_scoped_cx!(cx);
|
2023-12-27 17:57:55 +01:00
|
|
|
|
2024-11-11 17:36:18 +00:00
|
|
|
if trait_pred.polarity == ty::PredicatePolarity::Negative {
|
2023-12-27 17:57:55 +01:00
|
|
|
p!("!");
|
|
|
|
}
|
2024-11-11 17:36:18 +00:00
|
|
|
p!(print(trait_pred.trait_ref.print_only_trait_name()));
|
2021-11-19 20:51:19 -08:00
|
|
|
|
2024-11-11 17:36:18 +00:00
|
|
|
let generics = tcx.generics_of(trait_pred.def_id());
|
|
|
|
let own_args = generics.own_args_no_defaults(tcx, trait_pred.trait_ref.args);
|
2021-11-19 20:51:19 -08:00
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
if !own_args.is_empty() || !assoc_items.is_empty() {
|
2022-06-21 21:27:15 -07:00
|
|
|
let mut first = true;
|
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
for ty in own_args {
|
2022-06-21 21:27:15 -07:00
|
|
|
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 {
|
2023-10-19 21:46:28 +00:00
|
|
|
// Skip printing `<{coroutine@} as Coroutine<_>>::Return` from async blocks,
|
|
|
|
// unless we can find out what coroutine return type it comes from.
|
2024-05-29 22:23:49 -04:00
|
|
|
let term = if let Some(ty) = term.skip_binder().as_type()
|
2022-11-26 21:51:55 +00:00
|
|
|
&& let ty::Alias(ty::Projection, proj) = ty.kind()
|
2022-11-26 21:21:20 +00:00
|
|
|
&& let Some(assoc) = tcx.opt_associated_item(proj.def_id)
|
2024-08-13 16:44:37 -04:00
|
|
|
&& assoc
|
|
|
|
.trait_container(tcx)
|
|
|
|
.is_some_and(|def_id| tcx.is_lang_item(def_id, LangItem::Coroutine))
|
2022-09-08 02:34:22 +00:00
|
|
|
&& assoc.name == rustc_span::sym::Return
|
2022-06-21 21:27:15 -07:00
|
|
|
{
|
2023-12-21 01:52:10 +00:00
|
|
|
if let ty::Coroutine(_, args) = args.type_at(0).kind() {
|
2023-10-19 21:46:28 +00:00
|
|
|
let return_ty = args.as_coroutine().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;
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2022-06-21 21:27:15 -07:00
|
|
|
})?;
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
|
|
|
|
2023-12-27 17:57:55 +01:00
|
|
|
let add_sized = has_sized_bound && (first || has_negative_sized_bound);
|
|
|
|
let add_maybe_sized = !has_sized_bound && !has_negative_sized_bound;
|
|
|
|
if add_sized || add_maybe_sized {
|
|
|
|
if !first {
|
|
|
|
write!(self, " + ")?;
|
|
|
|
}
|
|
|
|
if add_maybe_sized {
|
|
|
|
write!(self, "?")?;
|
|
|
|
}
|
2022-06-22 10:21:24 -07:00
|
|
|
write!(self, "Sized")?;
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
|
|
|
|
2023-11-26 23:23:44 +00:00
|
|
|
if !with_forced_trimmed_paths() {
|
2023-01-06 01:22:24 +00:00
|
|
|
for re in lifetimes {
|
|
|
|
write!(self, " + ")?;
|
2023-10-17 19:46:14 +02:00
|
|
|
self.print_region(re)?;
|
2023-01-06 01:22:24 +00:00
|
|
|
}
|
2022-10-22 20:18:16 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 09:01:57 +02:00
|
|
|
if self.tcx().features().return_type_notation()
|
2023-09-07 01:20:43 +00:00
|
|
|
&& let Some(ty::ImplTraitInTraitData::Trait { fn_def_id, .. }) =
|
|
|
|
self.tcx().opt_rpitit_info(def_id)
|
|
|
|
&& let ty::Alias(_, alias_ty) =
|
|
|
|
self.tcx().fn_sig(fn_def_id).skip_binder().output().skip_binder().kind()
|
|
|
|
&& alias_ty.def_id == def_id
|
2024-07-15 17:51:15 -04:00
|
|
|
&& let generics = self.tcx().generics_of(fn_def_id)
|
|
|
|
// FIXME(return_type_notation): We only support lifetime params for now.
|
|
|
|
&& generics.own_params.iter().all(|param| matches!(param.kind, ty::GenericParamDefKind::Lifetime))
|
2023-09-07 01:20:43 +00:00
|
|
|
{
|
2024-07-15 17:51:15 -04:00
|
|
|
let num_args = generics.count();
|
2023-09-07 01:20:43 +00:00
|
|
|
write!(self, " {{ ")?;
|
2023-10-17 19:46:14 +02:00
|
|
|
self.print_def_path(fn_def_id, &args[..num_args])?;
|
2024-07-15 17:51:15 -04:00
|
|
|
write!(self, "(..) }}")?;
|
2023-09-07 01:20:43 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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,
|
2024-11-11 17:36:18 +00:00
|
|
|
trait_pred: ty::PolyTraitPredicate<'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<
|
2024-11-11 17:36:18 +00:00
|
|
|
ty::PolyTraitPredicate<'tcx>,
|
2022-05-11 16:36:26 -04:00
|
|
|
FxIndexMap<DefId, ty::Binder<'tcx, Term<'tcx>>>,
|
2022-01-10 23:39:21 +00:00
|
|
|
>,
|
2024-11-11 17:42:35 +00:00
|
|
|
fn_traits: &mut FxIndexMap<
|
2025-02-23 04:46:51 +00:00
|
|
|
(ty::Binder<'tcx, (&'tcx ty::List<Ty<'tcx>>, Ty<'tcx>)>, bool),
|
2024-11-11 17:42:35 +00:00
|
|
|
OpaqueFnEntry<'tcx>,
|
|
|
|
>,
|
2021-11-19 20:51:19 -08:00
|
|
|
) {
|
2024-11-11 17:36:18 +00:00
|
|
|
let tcx = self.tcx();
|
|
|
|
let trait_def_id = trait_pred.def_id();
|
2021-11-19 20:51:19 -08:00
|
|
|
|
2024-11-11 17:42:35 +00:00
|
|
|
let fn_trait_and_async = if let Some(kind) = tcx.fn_trait_kind_from_def_id(trait_def_id) {
|
|
|
|
Some((kind, false))
|
|
|
|
} else if let Some(kind) = tcx.async_fn_trait_kind_from_def_id(trait_def_id) {
|
|
|
|
Some((kind, true))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2024-11-11 17:36:18 +00:00
|
|
|
if trait_pred.polarity() == ty::PredicatePolarity::Positive
|
2024-11-11 17:42:35 +00:00
|
|
|
&& let Some((kind, is_async)) = fn_trait_and_async
|
2024-11-11 17:36:18 +00:00
|
|
|
&& let ty::Tuple(types) = *trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
|
2023-12-27 17:57:55 +01:00
|
|
|
{
|
2024-11-11 17:36:18 +00:00
|
|
|
let entry = fn_traits
|
2025-02-23 04:46:51 +00:00
|
|
|
.entry((trait_pred.rebind((types, trait_pred.skip_binder().self_ty())), is_async))
|
2024-11-11 17:36:18 +00:00
|
|
|
.or_insert_with(|| OpaqueFnEntry { kind, return_ty: None });
|
|
|
|
if kind.extends(entry.kind) {
|
|
|
|
entry.kind = kind;
|
|
|
|
}
|
|
|
|
if let Some((proj_def_id, proj_ty)) = proj_ty
|
|
|
|
&& tcx.item_name(proj_def_id) == sym::Output
|
|
|
|
{
|
|
|
|
entry.return_ty = Some(proj_ty);
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
2024-11-11 17:36:18 +00:00
|
|
|
return;
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, just group our traits and projection types.
|
2024-11-11 17:36:18 +00:00
|
|
|
traits.entry(trait_pred).or_default().extend(proj_ty);
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|
|
|
|
|
2023-05-12 01:36:37 +02:00
|
|
|
fn pretty_print_inherent_projection(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2024-05-13 10:00:38 -04:00
|
|
|
alias_ty: ty::AliasTerm<'tcx>,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
2023-05-12 01:36:37 +02:00
|
|
|
let def_key = self.tcx().def_key(alias_ty.def_id);
|
|
|
|
self.path_generic_args(
|
|
|
|
|cx| {
|
|
|
|
cx.path_append(
|
|
|
|
|cx| cx.path_qualified(alias_ty.self_ty(), None),
|
|
|
|
&def_key.disambiguated_data,
|
|
|
|
)
|
|
|
|
},
|
2023-07-11 22:35:29 +01:00
|
|
|
&alias_ty.args[1..],
|
2023-05-12 01:36:37 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-24 20:13:36 +00:00
|
|
|
fn const_infer_name(&self, _: ty::ConstVid) -> Option<Symbol> {
|
2019-05-30 15:33:13 -07:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:47:02 +02:00
|
|
|
fn pretty_print_dyn_existential(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2022-11-19 03:28:56 +00:00
|
|
|
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
2019-01-24 20:47:02 +02:00
|
|
|
// Generate the main trait ref, including associated types.
|
|
|
|
let mut first = true;
|
|
|
|
|
2024-02-06 22:37:47 +01:00
|
|
|
if let Some(bound_principal) = predicates.principal() {
|
2025-03-01 19:28:04 +00:00
|
|
|
self.wrap_binder(&bound_principal, WrapBinderMode::ForAll, |principal, cx| {
|
2020-12-11 15:02:46 -05:00
|
|
|
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.
|
2022-11-22 18:12:12 +00:00
|
|
|
let fn_trait_kind = cx.tcx().fn_trait_kind_from_def_id(principal.def_id);
|
2022-10-30 17:38:49 +05:30
|
|
|
if !cx.should_print_verbose() && fn_trait_kind.is_some() {
|
2023-07-11 22:35:29 +01:00
|
|
|
if let ty::Tuple(tys) = principal.args.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,
|
2024-05-29 22:23:49 -04:00
|
|
|
proj.skip_binder().term.as_type().expect("Return type was a const")
|
2022-01-13 07:39:58 +00:00
|
|
|
));
|
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 {
|
2024-02-06 22:37:47 +01:00
|
|
|
let principal_with_self =
|
|
|
|
principal.with_self_ty(cx.tcx(), cx.tcx().types.trait_object_dummy_self);
|
2020-12-11 15:02:46 -05:00
|
|
|
|
2023-12-10 10:17:28 +00:00
|
|
|
let args = cx
|
2022-06-03 20:00:39 +02:00
|
|
|
.tcx()
|
2024-02-06 22:37:47 +01:00
|
|
|
.generics_of(principal_with_self.def_id)
|
|
|
|
.own_args_no_defaults(cx.tcx(), principal_with_self.args);
|
|
|
|
|
|
|
|
let bound_principal_with_self = bound_principal
|
|
|
|
.with_self_ty(cx.tcx(), cx.tcx().types.trait_object_dummy_self);
|
|
|
|
|
2024-07-06 12:33:03 -04:00
|
|
|
let clause: ty::Clause<'tcx> = bound_principal_with_self.upcast(cx.tcx());
|
|
|
|
let super_projections: Vec<_> = elaborate::elaborate(cx.tcx(), [clause])
|
|
|
|
.filter_only_self()
|
|
|
|
.filter_map(|clause| clause.as_projection_clause())
|
|
|
|
.collect();
|
2024-02-06 22:37:47 +01:00
|
|
|
|
|
|
|
let mut projections: Vec<_> = predicates
|
|
|
|
.projection_bounds()
|
2024-02-07 20:58:05 +01:00
|
|
|
.filter(|&proj| {
|
2024-02-06 22:37:47 +01:00
|
|
|
// Filter out projections that are implied by the super predicates.
|
|
|
|
let proj_is_implied = super_projections.iter().any(|&super_proj| {
|
2024-02-07 20:58:05 +01:00
|
|
|
let super_proj = super_proj.map_bound(|super_proj| {
|
|
|
|
ty::ExistentialProjection::erase_self_ty(cx.tcx(), super_proj)
|
|
|
|
});
|
|
|
|
|
|
|
|
// This function is sometimes called on types with erased and
|
|
|
|
// anonymized regions, but the super projections can still
|
|
|
|
// contain named regions. So we erase and anonymize everything
|
|
|
|
// here to compare the types modulo regions below.
|
|
|
|
let proj = cx.tcx().erase_regions(proj);
|
2024-02-06 22:37:47 +01:00
|
|
|
let proj = cx.tcx().anonymize_bound_vars(proj);
|
2024-02-07 20:58:05 +01:00
|
|
|
let super_proj = cx.tcx().erase_regions(super_proj);
|
2024-02-06 22:37:47 +01:00
|
|
|
let super_proj = cx.tcx().anonymize_bound_vars(super_proj);
|
|
|
|
|
|
|
|
proj == super_proj
|
|
|
|
});
|
2024-02-07 20:58:05 +01:00
|
|
|
!proj_is_implied
|
|
|
|
})
|
|
|
|
.map(|proj| {
|
2024-02-06 22:37:47 +01:00
|
|
|
// Skip the binder, because we don't want to print the binder in
|
|
|
|
// front of the associated item.
|
2024-02-07 20:58:05 +01:00
|
|
|
proj.skip_binder()
|
2024-02-06 22:37:47 +01:00
|
|
|
})
|
|
|
|
.collect();
|
2020-12-11 15:02:46 -05:00
|
|
|
|
2024-02-06 22:37:47 +01:00
|
|
|
projections
|
|
|
|
.sort_by_cached_key(|proj| cx.tcx().item_name(proj.def_id).to_string());
|
2019-01-24 20:47:02 +02:00
|
|
|
|
2023-09-07 06:57:08 +00:00
|
|
|
if !args.is_empty() || !projections.is_empty() {
|
2023-10-17 19:46:14 +02:00
|
|
|
p!(generic_delimiters(|cx| {
|
|
|
|
cx.comma_sep(args.iter().copied())?;
|
2023-09-07 06:57:08 +00:00
|
|
|
if !args.is_empty() && !projections.is_empty() {
|
2020-12-11 15:02:46 -05:00
|
|
|
write!(cx, ", ")?;
|
|
|
|
}
|
2023-09-07 06:57:08 +00:00
|
|
|
cx.comma_sep(projections.iter().copied())
|
2020-12-11 15:02:46 -05:00
|
|
|
}));
|
|
|
|
}
|
2019-01-24 20:47:02 +02:00
|
|
|
}
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2020-12-11 15:02:46 -05:00
|
|
|
})?;
|
|
|
|
|
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-10-30 19:39:07 +00:00
|
|
|
auto_traits.sort_by_cached_key(|did| with_no_trimmed_paths!(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
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-01-24 20:47:02 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
fn pretty_fn_sig(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2019-01-18 21:33:31 +02:00
|
|
|
inputs: &[Ty<'tcx>],
|
|
|
|
c_variadic: bool,
|
|
|
|
output: Ty<'tcx>,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
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
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
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(
|
2023-10-17 19:46:14 +02:00
|
|
|
&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,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
2019-03-18 12:50:57 +02:00
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
2022-10-30 17:38:49 +05:30
|
|
|
if self.should_print_verbose() {
|
2023-05-16 04:25:25 +01:00
|
|
|
p!(write("{:?}", ct));
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2019-10-07 10:59:46 -04:00
|
|
|
}
|
|
|
|
|
2022-06-10 11:18:06 +10:00
|
|
|
match ct.kind() {
|
2023-07-11 22:35:29 +01:00
|
|
|
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, args }) => {
|
2022-05-08 15:53:19 +02:00
|
|
|
match self.tcx().def_kind(def) {
|
2023-01-14 18:32:17 +00:00
|
|
|
DefKind::Const | DefKind::AssocConst => {
|
2023-07-11 22:35:29 +01:00
|
|
|
p!(print_value_path(def, args))
|
2022-01-12 03:19:52 +00:00
|
|
|
}
|
2023-01-14 18:32:17 +00:00
|
|
|
DefKind::AnonConst => {
|
2023-01-18 04:45:35 +00:00
|
|
|
if def.is_local()
|
2022-05-08 15:53:19 +02:00
|
|
|
&& let span = self.tcx().def_span(def)
|
2023-01-18 04:45:35 +00:00
|
|
|
&& let Ok(snip) = self.tcx().sess.source_map().span_to_snippet(span)
|
|
|
|
{
|
|
|
|
p!(write("{}", snip))
|
2022-01-12 03:19:52 +00:00
|
|
|
} else {
|
2023-01-18 04:45:35 +00:00
|
|
|
// Do not call `print_value_path` as if a parent of this anon const is an impl it will
|
|
|
|
// attempt to print out the impl trait ref i.e. `<T as Trait>::{constant#0}`. This would
|
|
|
|
// cause printing to enter an infinite recursion if the anon const is in the self type i.e.
|
|
|
|
// `impl<T: Default> Default for [T; 32 - 1 - 1 - 1] {`
|
|
|
|
// where we would try to print `<[T; /* print `constant#0` again */] as Default>::{constant#0}`
|
2022-05-08 15:53:19 +02:00
|
|
|
p!(write(
|
|
|
|
"{}::{}",
|
|
|
|
self.tcx().crate_name(def.krate),
|
|
|
|
self.tcx().def_path(def).to_string_no_crate_verbose()
|
|
|
|
))
|
2019-10-05 12:57:12 +13:00
|
|
|
}
|
|
|
|
}
|
2023-04-10 22:02:52 +02:00
|
|
|
defkind => bug!("`{:?}` has unexpected defkind {:?}", ct, defkind),
|
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))
|
|
|
|
}
|
2024-06-03 03:38:09 +01:00
|
|
|
_ => write!(self, "_")?,
|
2022-02-14 14:13:02 +01:00
|
|
|
},
|
2020-03-12 13:35:44 +01:00
|
|
|
ty::ConstKind::Param(ParamConst { name, .. }) => p!(write("{}", name)),
|
2025-01-27 04:30:00 +01:00
|
|
|
ty::ConstKind::Value(cv) => {
|
2025-02-02 11:46:21 +01:00
|
|
|
return self.pretty_print_const_valtree(cv, 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) => {
|
2023-10-17 19:46:14 +02:00
|
|
|
rustc_type_ir::debug_bound_var(self, debruijn, bound_var)?
|
2020-03-13 10:48:41 +01:00
|
|
|
}
|
2023-05-26 18:54:59 +01:00
|
|
|
ty::ConstKind::Placeholder(placeholder) => p!(write("{placeholder:?}")),
|
2022-07-27 07:27:52 +00:00
|
|
|
// FIXME(generic_const_exprs):
|
|
|
|
// write out some legible representation of an abstract const?
|
2024-03-17 01:38:45 +11:00
|
|
|
ty::ConstKind::Expr(expr) => self.pretty_print_const_expr(expr, print_ty)?,
|
2023-05-26 16:01:23 +01:00
|
|
|
ty::ConstKind::Error(_) => p!("{{const error}}"),
|
2019-11-08 21:55:19 +01:00
|
|
|
};
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-11-08 21:55:19 +01:00
|
|
|
}
|
|
|
|
|
2024-03-17 01:38:45 +11:00
|
|
|
fn pretty_print_const_expr(
|
|
|
|
&mut self,
|
|
|
|
expr: Expr<'tcx>,
|
|
|
|
print_ty: bool,
|
|
|
|
) -> Result<(), PrintError> {
|
|
|
|
define_scoped_cx!(self);
|
2024-06-04 02:24:57 +01:00
|
|
|
match expr.kind {
|
|
|
|
ty::ExprKind::Binop(op) => {
|
|
|
|
let (_, _, c1, c2) = expr.binop_args();
|
|
|
|
|
2024-12-19 18:24:07 +11:00
|
|
|
let precedence = |binop: crate::mir::BinOp| binop.to_hir_binop().precedence();
|
2024-03-17 01:38:45 +11:00
|
|
|
let op_precedence = precedence(op);
|
|
|
|
let formatted_op = op.to_hir_binop().as_str();
|
|
|
|
let (lhs_parenthesized, rhs_parenthesized) = match (c1.kind(), c2.kind()) {
|
|
|
|
(
|
2024-06-04 02:24:57 +01:00
|
|
|
ty::ConstKind::Expr(ty::Expr { kind: ty::ExprKind::Binop(lhs_op), .. }),
|
|
|
|
ty::ConstKind::Expr(ty::Expr { kind: ty::ExprKind::Binop(rhs_op), .. }),
|
2024-03-17 01:38:45 +11:00
|
|
|
) => (precedence(lhs_op) < op_precedence, precedence(rhs_op) < op_precedence),
|
2024-06-04 02:24:57 +01:00
|
|
|
(
|
|
|
|
ty::ConstKind::Expr(ty::Expr { kind: ty::ExprKind::Binop(lhs_op), .. }),
|
|
|
|
ty::ConstKind::Expr(_),
|
|
|
|
) => (precedence(lhs_op) < op_precedence, true),
|
|
|
|
(
|
|
|
|
ty::ConstKind::Expr(_),
|
|
|
|
ty::ConstKind::Expr(ty::Expr { kind: ty::ExprKind::Binop(rhs_op), .. }),
|
|
|
|
) => (true, precedence(rhs_op) < op_precedence),
|
2024-03-17 01:38:45 +11:00
|
|
|
(ty::ConstKind::Expr(_), ty::ConstKind::Expr(_)) => (true, true),
|
2024-06-04 02:24:57 +01:00
|
|
|
(
|
|
|
|
ty::ConstKind::Expr(ty::Expr { kind: ty::ExprKind::Binop(lhs_op), .. }),
|
|
|
|
_,
|
|
|
|
) => (precedence(lhs_op) < op_precedence, false),
|
|
|
|
(
|
|
|
|
_,
|
|
|
|
ty::ConstKind::Expr(ty::Expr { kind: ty::ExprKind::Binop(rhs_op), .. }),
|
|
|
|
) => (false, precedence(rhs_op) < op_precedence),
|
2024-03-17 01:38:45 +11:00
|
|
|
(ty::ConstKind::Expr(_), _) => (true, false),
|
|
|
|
(_, ty::ConstKind::Expr(_)) => (false, true),
|
|
|
|
_ => (false, false),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.maybe_parenthesized(
|
|
|
|
|this| this.pretty_print_const(c1, print_ty),
|
|
|
|
lhs_parenthesized,
|
|
|
|
)?;
|
|
|
|
p!(write(" {formatted_op} "));
|
|
|
|
self.maybe_parenthesized(
|
|
|
|
|this| this.pretty_print_const(c2, print_ty),
|
|
|
|
rhs_parenthesized,
|
|
|
|
)?;
|
|
|
|
}
|
2024-06-04 02:24:57 +01:00
|
|
|
ty::ExprKind::UnOp(op) => {
|
|
|
|
let (_, ct) = expr.unop_args();
|
|
|
|
|
2025-02-11 13:58:38 +11:00
|
|
|
use crate::mir::UnOp;
|
2024-03-17 01:38:45 +11:00
|
|
|
let formatted_op = match op {
|
|
|
|
UnOp::Not => "!",
|
|
|
|
UnOp::Neg => "-",
|
2024-04-21 16:11:01 -07:00
|
|
|
UnOp::PtrMetadata => "PtrMetadata",
|
2024-03-17 01:38:45 +11:00
|
|
|
};
|
|
|
|
let parenthesized = match ct.kind() {
|
2024-04-21 16:11:01 -07:00
|
|
|
_ if op == UnOp::PtrMetadata => true,
|
2024-06-04 02:24:57 +01:00
|
|
|
ty::ConstKind::Expr(ty::Expr { kind: ty::ExprKind::UnOp(c_op), .. }) => {
|
|
|
|
c_op != op
|
|
|
|
}
|
2024-03-17 01:38:45 +11:00
|
|
|
ty::ConstKind::Expr(_) => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
p!(write("{formatted_op}"));
|
|
|
|
self.maybe_parenthesized(
|
|
|
|
|this| this.pretty_print_const(ct, print_ty),
|
|
|
|
parenthesized,
|
|
|
|
)?
|
|
|
|
}
|
2024-06-04 02:24:57 +01:00
|
|
|
ty::ExprKind::FunctionCall => {
|
|
|
|
let (_, fn_def, fn_args) = expr.call_args();
|
|
|
|
|
|
|
|
write!(self, "(")?;
|
|
|
|
self.pretty_print_const(fn_def, print_ty)?;
|
|
|
|
p!(")(", comma_sep(fn_args), ")");
|
2024-03-17 01:38:45 +11:00
|
|
|
}
|
2024-06-04 02:24:57 +01:00
|
|
|
ty::ExprKind::Cast(kind) => {
|
|
|
|
let (_, value, to_ty) = expr.cast_args();
|
|
|
|
|
2024-03-17 01:38:45 +11:00
|
|
|
use ty::abstract_const::CastKind;
|
|
|
|
if kind == CastKind::As || (kind == CastKind::Use && self.should_print_verbose()) {
|
2024-06-04 02:24:57 +01:00
|
|
|
let parenthesized = match value.kind() {
|
|
|
|
ty::ConstKind::Expr(ty::Expr {
|
|
|
|
kind: ty::ExprKind::Cast { .. }, ..
|
|
|
|
}) => false,
|
2024-03-17 01:38:45 +11:00
|
|
|
ty::ConstKind::Expr(_) => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
self.maybe_parenthesized(
|
|
|
|
|this| {
|
|
|
|
this.typed_value(
|
2024-06-04 02:24:57 +01:00
|
|
|
|this| this.pretty_print_const(value, print_ty),
|
|
|
|
|this| this.pretty_print_type(to_ty),
|
2024-03-17 01:38:45 +11:00
|
|
|
" as ",
|
|
|
|
)
|
|
|
|
},
|
|
|
|
parenthesized,
|
|
|
|
)?;
|
|
|
|
} else {
|
2024-06-04 02:24:57 +01:00
|
|
|
self.pretty_print_const(value, print_ty)?
|
2024-03-17 01:38:45 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
fn pretty_print_const_scalar(
|
|
|
|
&mut self,
|
|
|
|
scalar: Scalar,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Result<(), PrintError> {
|
2021-03-02 14:54:10 +00:00
|
|
|
match scalar {
|
2023-06-28 09:43:31 +00:00
|
|
|
Scalar::Ptr(ptr, _size) => self.pretty_print_const_scalar_ptr(ptr, ty),
|
2023-07-05 15:58:19 +00:00
|
|
|
Scalar::Int(int) => {
|
|
|
|
self.pretty_print_const_scalar_int(int, ty, /* print_ty */ true)
|
|
|
|
}
|
2021-03-02 14:54:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pretty_print_const_scalar_ptr(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2021-03-02 14:54:10 +00:00
|
|
|
ptr: Pointer,
|
|
|
|
ty: Ty<'tcx>,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
2019-11-08 21:55:19 +01:00
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
2023-11-25 18:41:53 +01:00
|
|
|
let (prov, 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, _) => {
|
2025-02-07 19:33:58 +01:00
|
|
|
if let ty::Array(elem, ct_len) = inner.kind()
|
2025-01-27 04:30:00 +01:00
|
|
|
&& let ty::Uint(ty::UintTy::U8) = elem.kind()
|
2025-02-07 19:33:58 +01:00
|
|
|
&& let Some(len) = ct_len.try_to_target_usize(self.tcx())
|
2025-01-27 04:30:00 +01:00
|
|
|
{
|
|
|
|
match self.tcx().try_get_global_alloc(prov.alloc_id()) {
|
|
|
|
Some(GlobalAlloc::Memory(alloc)) => {
|
|
|
|
let range = AllocRange { start: offset, size: Size::from_bytes(len) };
|
|
|
|
if let Ok(byte_str) =
|
|
|
|
alloc.inner().get_bytes_strip_provenance(&self.tcx(), range)
|
|
|
|
{
|
|
|
|
p!(pretty_print_byte_str(byte_str))
|
|
|
|
} else {
|
|
|
|
p!("<too short allocation>")
|
2022-04-08 15:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
2025-01-27 04:30:00 +01:00
|
|
|
// FIXME: for statics, vtables, and functions, we could in principle print more detail.
|
|
|
|
Some(GlobalAlloc::Static(def_id)) => {
|
|
|
|
p!(write("<static({:?})>", def_id))
|
|
|
|
}
|
|
|
|
Some(GlobalAlloc::Function { .. }) => p!("<function>"),
|
|
|
|
Some(GlobalAlloc::VTable(..)) => p!("<vtable>"),
|
|
|
|
None => p!("<dangling pointer>"),
|
2020-05-25 08:52:16 +02:00
|
|
|
}
|
2025-01-27 04:30:00 +01:00
|
|
|
return Ok(());
|
2020-05-25 08:52:16 +02:00
|
|
|
}
|
2022-04-08 15:57:44 +00:00
|
|
|
}
|
2024-08-08 17:18:20 +10:00
|
|
|
ty::FnPtr(..) => {
|
2021-03-02 14:54:10 +00:00
|
|
|
// 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).
|
2024-07-02 21:05:22 +02:00
|
|
|
if let Some(GlobalAlloc::Function { instance, .. }) =
|
2023-11-25 18:41:53 +01:00
|
|
|
self.tcx().try_get_global_alloc(prov.alloc_id())
|
2022-04-08 15:57:44 +00:00
|
|
|
{
|
2023-10-17 19:46:14 +02:00
|
|
|
self.typed_value(
|
2023-07-11 22:35:29 +01:00
|
|
|
|this| this.print_value_path(instance.def_id(), instance.args),
|
2022-04-08 15:57:44 +00:00
|
|
|
|this| this.print_type(ty),
|
|
|
|
" as ",
|
|
|
|
)?;
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
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
|
2023-10-17 19:46:14 +02:00
|
|
|
self.pretty_print_const_pointer(ptr, ty)?;
|
|
|
|
Ok(())
|
2021-03-02 14:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn pretty_print_const_scalar_int(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2021-03-02 14:54:10 +00:00
|
|
|
int: ScalarInt,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
print_ty: bool,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
2021-03-02 14:54:10 +00:00
|
|
|
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
|
2024-07-01 18:18:10 -04:00
|
|
|
ty::Float(fty) => match fty {
|
|
|
|
ty::FloatTy::F16 => {
|
|
|
|
let val = Half::try_from(int).unwrap();
|
|
|
|
p!(write("{}{}f16", val, if val.is_finite() { "" } else { "_" }))
|
|
|
|
}
|
|
|
|
ty::FloatTy::F32 => {
|
|
|
|
let val = Single::try_from(int).unwrap();
|
|
|
|
p!(write("{}{}f32", val, if val.is_finite() { "" } else { "_" }))
|
|
|
|
}
|
|
|
|
ty::FloatTy::F64 => {
|
|
|
|
let val = Double::try_from(int).unwrap();
|
|
|
|
p!(write("{}{}f64", val, if val.is_finite() { "" } else { "_" }))
|
|
|
|
}
|
|
|
|
ty::FloatTy::F128 => {
|
|
|
|
let val = Quad::try_from(int).unwrap();
|
|
|
|
p!(write("{}{}f128", val, if val.is_finite() { "" } else { "_" }))
|
|
|
|
}
|
|
|
|
},
|
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
|
2024-08-08 17:18:20 +10:00
|
|
|
ty::Ref(..) | ty::RawPtr(_, _) | ty::FnPtr(..) => {
|
2024-06-08 16:13:45 +02:00
|
|
|
let data = int.to_bits(self.tcx().data_layout.pointer_size);
|
2023-10-17 19:46:14 +02:00
|
|
|
self.typed_value(
|
|
|
|
|this| {
|
2023-07-25 22:00:13 +02:00
|
|
|
write!(this, "0x{data:x}")?;
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2020-02-13 19:02:58 +01:00
|
|
|
},
|
|
|
|
|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
|
|
|
}
|
2025-01-29 09:20:53 +00:00
|
|
|
ty::Pat(base_ty, pat) if self.tcx().validate_scalar_in_layout(int, ty) => {
|
2025-01-27 15:19:49 +00:00
|
|
|
self.pretty_print_const_scalar_int(int, *base_ty, print_ty)?;
|
|
|
|
p!(write(" is {pat:?}"));
|
|
|
|
}
|
2019-12-23 17:41:06 +01:00
|
|
|
// Nontrivial types with scalar bit representation
|
2021-03-02 14:54:10 +00:00
|
|
|
_ => {
|
2023-10-17 19:46:14 +02:00
|
|
|
let print = |this: &mut 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 {
|
2023-07-25 22:00:13 +02:00
|
|
|
write!(this, "transmute(0x{int:x})")?;
|
2020-02-12 13:47:00 +01:00
|
|
|
}
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2020-02-05 11:00:52 +01:00
|
|
|
};
|
2023-10-17 19:46:14 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-12-23 17:41:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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>(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2022-07-18 18:47:31 -04:00
|
|
|
_: Pointer<Prov>,
|
2019-12-23 17:41:06 +01:00
|
|
|
ty: Ty<'tcx>,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
2023-06-28 09:43:31 +00:00
|
|
|
self.typed_value(
|
2023-10-17 19:46:14 +02:00
|
|
|
|this| {
|
2023-06-28 09:43:31 +00:00
|
|
|
this.write_str("&_")?;
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2023-06-28 09:43:31 +00:00
|
|
|
},
|
|
|
|
|this| this.print_type(ty),
|
|
|
|
": ",
|
|
|
|
)
|
2019-12-23 17:41:06 +01:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
fn pretty_print_byte_str(&mut self, byte_str: &'tcx [u8]) -> Result<(), PrintError> {
|
2022-08-19 18:53:27 +02:00
|
|
|
write!(self, "b\"{}\"", byte_str.escape_ascii())?;
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-12-23 17:41:06 +01:00
|
|
|
}
|
|
|
|
|
2022-02-16 10:56:01 +01:00
|
|
|
fn pretty_print_const_valtree(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2025-02-02 11:46:21 +01:00
|
|
|
cv: ty::Value<'tcx>,
|
2019-12-23 17:41:06 +01:00
|
|
|
print_ty: bool,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
2019-12-23 17:41:06 +01:00
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
2022-10-30 17:38:49 +05:30
|
|
|
if self.should_print_verbose() {
|
2025-02-02 11:46:21 +01:00
|
|
|
p!(write("ValTree({:?}: ", cv.valtree), print(cv.ty), ")");
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2019-12-23 17:41:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let u8_type = self.tcx().types.u8;
|
2025-02-07 19:33:58 +01:00
|
|
|
match (*cv.valtree, *cv.ty.kind()) {
|
|
|
|
(ty::ValTreeKind::Branch(_), ty::Ref(_, inner_ty, _)) => match inner_ty.kind() {
|
2022-02-16 10:56:01 +01:00
|
|
|
ty::Slice(t) if *t == u8_type => {
|
2025-02-03 18:33:27 +01:00
|
|
|
let bytes = cv.try_to_raw_bytes(self.tcx()).unwrap_or_else(|| {
|
2022-02-16 10:56:01 +01:00
|
|
|
bug!(
|
|
|
|
"expected to convert valtree {:?} to raw bytes for type {:?}",
|
2025-02-03 18:33:27 +01:00
|
|
|
cv.valtree,
|
2022-02-16 10:56:01 +01:00
|
|
|
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 => {
|
2025-02-03 18:33:27 +01:00
|
|
|
let bytes = cv.try_to_raw_bytes(self.tcx()).unwrap_or_else(|| {
|
|
|
|
bug!("expected to convert valtree to raw bytes for type {:?}", cv.ty)
|
2022-02-16 10:56:01 +01:00
|
|
|
});
|
|
|
|
p!(write("{:?}", String::from_utf8_lossy(bytes)));
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2022-02-16 10:56:01 +01:00
|
|
|
}
|
2022-06-28 22:35:48 +02:00
|
|
|
_ => {
|
2025-02-11 19:16:12 +00:00
|
|
|
let cv = ty::Value { valtree: cv.valtree, ty: inner_ty };
|
2022-06-28 22:35:48 +02:00
|
|
|
p!("&");
|
2025-02-02 11:46:21 +01:00
|
|
|
p!(pretty_print_const_valtree(cv, print_ty));
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2022-06-28 22:35:48 +02:00
|
|
|
}
|
2022-02-16 10:56:01 +01:00
|
|
|
},
|
2025-02-07 19:33:58 +01:00
|
|
|
(ty::ValTreeKind::Branch(_), ty::Array(t, _)) if t == u8_type => {
|
2025-02-03 18:33:27 +01:00
|
|
|
let bytes = cv.try_to_raw_bytes(self.tcx()).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));
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
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.
|
2025-02-07 19:33:58 +01:00
|
|
|
(ty::ValTreeKind::Branch(_), ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) => {
|
2025-02-02 11:46:21 +01:00
|
|
|
let contents = self.tcx().destructure_const(ty::Const::new_value(
|
|
|
|
self.tcx(),
|
|
|
|
cv.valtree,
|
|
|
|
cv.ty,
|
|
|
|
));
|
2020-04-17 03:55:39 +03:00
|
|
|
let fields = contents.fields.iter().copied();
|
2025-02-02 11:46:21 +01:00
|
|
|
match *cv.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() => {
|
2023-10-17 19:46:14 +02:00
|
|
|
self.typed_value(
|
|
|
|
|this| {
|
2021-07-16 14:12:18 +03:00
|
|
|
write!(this, "unreachable()")?;
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2021-07-16 14:12:18 +03:00
|
|
|
},
|
2025-02-02 11:46:21 +01:00
|
|
|
|this| this.print_type(cv.ty),
|
2021-07-16 14:12:18 +03:00
|
|
|
": ",
|
|
|
|
)?;
|
2020-06-18 10:37:59 +01:00
|
|
|
}
|
2023-07-11 22:35:29 +01:00
|
|
|
ty::Adt(def, args) => {
|
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);
|
2023-07-11 22:35:29 +01:00
|
|
|
p!(print_value_path(variant_def.def_id, args));
|
2022-10-25 20:15:15 +04:00
|
|
|
match variant_def.ctor_kind() {
|
|
|
|
Some(CtorKind::Const) => {}
|
|
|
|
Some(CtorKind::Fn) => {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("(", comma_sep(fields), ")");
|
2020-04-17 03:55:39 +03:00
|
|
|
}
|
2022-10-25 20:15:15 +04:00
|
|
|
None => {
|
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!(),
|
|
|
|
}
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2020-04-17 03:55:39 +03:00
|
|
|
}
|
2025-02-07 19:33:58 +01:00
|
|
|
(ty::ValTreeKind::Leaf(leaf), ty::Ref(_, inner_ty, _)) => {
|
2022-08-09 19:12:14 +02:00
|
|
|
p!(write("&"));
|
2025-02-07 19:33:58 +01:00
|
|
|
return self.pretty_print_const_scalar_int(*leaf, inner_ty, print_ty);
|
2022-08-09 19:12:14 +02:00
|
|
|
}
|
2025-02-07 19:33:58 +01:00
|
|
|
(ty::ValTreeKind::Leaf(leaf), _) => {
|
|
|
|
return self.pretty_print_const_scalar_int(*leaf, cv.ty, print_ty);
|
2022-04-08 15:57:44 +00:00
|
|
|
}
|
2025-02-11 19:16:12 +00:00
|
|
|
(_, ty::FnDef(def_id, args)) => {
|
|
|
|
// Never allowed today, but we still encounter them in invalid const args.
|
|
|
|
p!(print_value_path(def_id, args));
|
|
|
|
return Ok(());
|
|
|
|
}
|
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
|
2025-02-07 19:33:58 +01:00
|
|
|
if cv.valtree.is_zst() {
|
2022-06-03 20:42:35 +02:00
|
|
|
p!(write("<ZST>"));
|
|
|
|
} else {
|
2025-02-02 11:46:21 +01:00
|
|
|
p!(write("{:?}", cv.valtree));
|
2022-02-16 10:56:01 +01:00
|
|
|
}
|
2022-04-08 15:57:44 +00:00
|
|
|
if print_ty {
|
2025-02-02 11:46:21 +01:00
|
|
|
p!(": ", print(cv.ty));
|
2022-04-08 15:57:44 +00:00
|
|
|
}
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-03-18 12:50:57 +02:00
|
|
|
}
|
2022-08-26 01:25:56 +00:00
|
|
|
|
2024-05-31 14:13:46 -04:00
|
|
|
fn pretty_closure_as_impl(
|
|
|
|
&mut self,
|
|
|
|
closure: ty::ClosureArgs<TyCtxt<'tcx>>,
|
|
|
|
) -> Result<(), PrintError> {
|
2022-08-26 01:25:56 +00:00
|
|
|
let sig = closure.sig();
|
|
|
|
let kind = closure.kind_ty().to_opt_closure_kind().unwrap_or(ty::ClosureKind::Fn);
|
|
|
|
|
|
|
|
write!(self, "impl ")?;
|
2025-03-01 19:28:04 +00:00
|
|
|
self.wrap_binder(&sig, WrapBinderMode::ForAll, |sig, cx| {
|
2022-08-26 01:25:56 +00:00
|
|
|
define_scoped_cx!(cx);
|
|
|
|
|
2023-12-12 18:54:49 +00:00
|
|
|
p!(write("{kind}("));
|
2022-08-26 01:25:56 +00:00
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2022-08-26 01:25:56 +00:00
|
|
|
})
|
|
|
|
}
|
2022-10-30 17:38:49 +05:30
|
|
|
|
2023-12-31 18:23:23 +01:00
|
|
|
fn pretty_print_bound_constness(
|
|
|
|
&mut self,
|
2024-10-02 19:42:06 +08:00
|
|
|
constness: ty::BoundConstness,
|
2023-12-31 18:23:23 +01:00
|
|
|
) -> Result<(), PrintError> {
|
|
|
|
define_scoped_cx!(self);
|
|
|
|
|
2024-10-02 19:42:06 +08:00
|
|
|
match constness {
|
|
|
|
ty::BoundConstness::Const => {
|
|
|
|
p!("const ");
|
|
|
|
}
|
2024-10-29 23:42:59 +00:00
|
|
|
ty::BoundConstness::Maybe => {
|
2024-10-02 19:42:06 +08:00
|
|
|
p!("~const ");
|
|
|
|
}
|
2023-12-31 18:23:23 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-10-30 17:38:49 +05:30
|
|
|
fn should_print_verbose(&self) -> bool {
|
2023-12-19 12:39:58 -05:00
|
|
|
self.tcx().sess.verbose_internals()
|
2022-10-30 17:38:49 +05:30
|
|
|
}
|
2019-01-20 19:46:47 +02:00
|
|
|
}
|
2019-01-18 21:33:31 +02:00
|
|
|
|
2023-09-15 08:21:40 +02:00
|
|
|
pub(crate) fn pretty_print_const<'tcx>(
|
2023-09-14 23:33:42 +02:00
|
|
|
c: ty::Const<'tcx>,
|
|
|
|
fmt: &mut fmt::Formatter<'_>,
|
|
|
|
print_types: bool,
|
|
|
|
) -> fmt::Result {
|
|
|
|
ty::tls::with(|tcx| {
|
|
|
|
let literal = tcx.lift(c).unwrap();
|
|
|
|
let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS);
|
|
|
|
cx.print_alloc_ids = true;
|
2023-10-17 19:46:14 +02:00
|
|
|
cx.pretty_print_const(literal, print_types)?;
|
2023-09-14 23:33:42 +02:00
|
|
|
fmt.write_str(&cx.into_buffer())?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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,
|
2022-11-15 17:38:39 -08:00
|
|
|
type_length_limit: Limit,
|
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>>,
|
2023-10-24 20:13:36 +00:00
|
|
|
pub const_infer_name_resolver: Option<Box<dyn Fn(ty::ConstVid) -> 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 {
|
2024-03-03 04:37:33 +01:00
|
|
|
let limit =
|
|
|
|
if with_reduced_queries() { Limit::new(1048576) } else { tcx.type_length_limit() };
|
2023-06-27 09:07:14 +00:00
|
|
|
Self::new_with_limit(tcx, ns, limit)
|
2022-11-15 17:38:39 -08:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
pub fn print_string(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
ns: Namespace,
|
|
|
|
f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
|
|
|
|
) -> Result<String, PrintError> {
|
|
|
|
let mut c = FmtPrinter::new(tcx, ns);
|
|
|
|
f(&mut c)?;
|
|
|
|
Ok(c.into_buffer())
|
|
|
|
}
|
|
|
|
|
2022-11-15 17:38:39 -08:00
|
|
|
pub fn new_with_limit(tcx: TyCtxt<'tcx>, ns: Namespace, type_length_limit: Limit) -> 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-11-15 17:38:39 -08:00
|
|
|
type_length_limit,
|
2023-09-14 09:24:51 +10:00
|
|
|
region_highlight_mode: RegionHighlightMode::default(),
|
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 {
|
2023-12-03 12:29:59 +03:00
|
|
|
DefPathData::TypeNs(..) | DefPathData::CrateRoot | DefPathData::OpaqueTy => {
|
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
|
|
|
DefPathData::ValueNs(..)
|
|
|
|
| DefPathData::AnonConst
|
2023-12-03 12:29:59 +03:00
|
|
|
| DefPathData::Closure
|
2020-01-05 15:59:02 +01:00
|
|
|
| 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.
|
2023-02-16 09:25:11 +00:00
|
|
|
pub fn def_path_str(self, def_id: impl IntoQueryParam<DefId>) -> String {
|
2023-07-11 22:35:29 +01:00
|
|
|
self.def_path_str_with_args(def_id, &[])
|
2019-11-23 16:01:20 -08:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
pub fn def_path_str_with_args(
|
2023-02-16 09:25:11 +00:00
|
|
|
self,
|
|
|
|
def_id: impl IntoQueryParam<DefId>,
|
2023-07-11 22:35:29 +01:00
|
|
|
args: &'t [GenericArg<'t>],
|
2023-02-16 09:25:11 +00:00
|
|
|
) -> String {
|
|
|
|
let def_id = def_id.into_query_param();
|
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);
|
2023-10-17 19:46:14 +02:00
|
|
|
|
|
|
|
FmtPrinter::print_string(self, ns, |cx| cx.print_def_path(def_id, args)).unwrap()
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
2022-09-23 12:40:20 +01:00
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
pub fn value_path_str_with_args(
|
2023-02-16 09:25:11 +00:00
|
|
|
self,
|
|
|
|
def_id: impl IntoQueryParam<DefId>,
|
2023-07-11 22:35:29 +01:00
|
|
|
args: &'t [GenericArg<'t>],
|
2023-02-16 09:25:11 +00:00
|
|
|
) -> String {
|
|
|
|
let def_id = def_id.into_query_param();
|
2022-09-23 12:40:20 +01:00
|
|
|
let ns = guess_def_namespace(self, def_id);
|
|
|
|
debug!("value_path_str: def_id={:?}, ns={:?}", def_id, ns);
|
2023-10-17 19:46:14 +02:00
|
|
|
|
|
|
|
FmtPrinter::print_string(self, ns, |cx| cx.print_value_path(def_id, args)).unwrap()
|
2022-09-23 12:40:20 +01:00
|
|
|
}
|
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> {
|
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(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2019-01-25 12:11:50 +02:00
|
|
|
def_id: DefId,
|
2023-07-11 22:35:29 +01:00
|
|
|
args: &'tcx [GenericArg<'tcx>],
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
2023-07-11 22:35:29 +01:00
|
|
|
if args.is_empty() {
|
2020-09-02 10:40:56 +03:00
|
|
|
match self.try_print_trimmed_def_path(def_id)? {
|
2023-10-17 19:46:14 +02:00
|
|
|
true => return Ok(()),
|
|
|
|
false => {}
|
2020-09-02 10:40:56 +03:00
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
match self.try_print_visible_def_path(def_id)? {
|
2023-10-17 19:46:14 +02:00
|
|
|
true => return Ok(()),
|
|
|
|
false => {}
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2023-11-26 23:23:44 +00:00
|
|
|
let force_no_types = with_forced_impl_filename_line();
|
2019-09-06 21:05:37 +01:00
|
|
|
!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
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
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;
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
self.default_print_def_path(def_id, args)
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
fn print_region(&mut self, region: ty::Region<'tcx>) -> Result<(), PrintError> {
|
2019-01-25 12:11:50 +02:00
|
|
|
self.pretty_print_region(region)
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
|
2025-01-31 03:26:56 +00:00
|
|
|
match ty.kind() {
|
|
|
|
ty::Tuple(tys) if tys.len() == 0 && self.should_truncate() => {
|
|
|
|
// Don't truncate `()`.
|
|
|
|
self.printed_type_count += 1;
|
|
|
|
self.pretty_print_type(ty)
|
|
|
|
}
|
|
|
|
ty::Adt(..)
|
|
|
|
| ty::Foreign(_)
|
|
|
|
| ty::Pat(..)
|
|
|
|
| ty::RawPtr(..)
|
|
|
|
| ty::Ref(..)
|
|
|
|
| ty::FnDef(..)
|
|
|
|
| ty::FnPtr(..)
|
|
|
|
| ty::UnsafeBinder(..)
|
|
|
|
| ty::Dynamic(..)
|
|
|
|
| ty::Closure(..)
|
|
|
|
| ty::CoroutineClosure(..)
|
|
|
|
| ty::Coroutine(..)
|
|
|
|
| ty::CoroutineWitness(..)
|
|
|
|
| ty::Tuple(_)
|
|
|
|
| ty::Alias(..)
|
|
|
|
| ty::Param(_)
|
|
|
|
| ty::Bound(..)
|
|
|
|
| ty::Placeholder(_)
|
|
|
|
| ty::Error(_)
|
|
|
|
if self.should_truncate() =>
|
|
|
|
{
|
|
|
|
// We only truncate types that we know are likely to be much longer than 3 chars.
|
|
|
|
// There's no point in replacing `i32` or `!`.
|
|
|
|
write!(self, "...")?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.printed_type_count += 1;
|
|
|
|
self.pretty_print_type(ty)
|
|
|
|
}
|
2020-09-15 18:22:24 -05:00
|
|
|
}
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
2025-01-31 03:26:56 +00:00
|
|
|
fn should_truncate(&mut self) -> bool {
|
|
|
|
!self.type_length_limit.value_within_limit(self.printed_type_count)
|
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
fn print_dyn_existential(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2022-11-19 03:28:56 +00:00
|
|
|
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
2019-01-25 12:11:50 +02:00
|
|
|
self.pretty_print_dyn_existential(predicates)
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
|
2022-07-19 02:25:14 +03:00
|
|
|
self.pretty_print_const(ct, false)
|
2019-03-18 12:50:57 +02:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
|
2019-01-25 12:11:50 +02:00
|
|
|
self.empty_path = true;
|
|
|
|
if cnum == LOCAL_CRATE {
|
2023-07-16 18:59:36 +00:00
|
|
|
if self.tcx.sess.at_least_rust_2018() {
|
2019-01-25 12:11:50 +02:00
|
|
|
// We add the `crate::` keyword on Rust 2018, only when desired.
|
2023-11-26 23:23:44 +00:00
|
|
|
if with_crate_prefix() {
|
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;
|
|
|
|
}
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
2019-09-06 03:57:44 +01:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
fn path_qualified(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2019-01-25 12:11:50 +02:00
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
trait_ref: Option<ty::TraitRef<'tcx>>,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
|
|
|
self.pretty_path_qualified(self_ty, trait_ref)?;
|
2019-01-25 12:11:50 +02:00
|
|
|
self.empty_path = false;
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn path_append_impl(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
|
|
|
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
|
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>>,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
|
|
|
self.pretty_path_append_impl(
|
|
|
|
|cx| {
|
|
|
|
print_prefix(cx)?;
|
2019-01-25 12:11:50 +02:00
|
|
|
if !cx.empty_path {
|
|
|
|
write!(cx, "::")?;
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-01-25 12:11:50 +02:00
|
|
|
},
|
|
|
|
self_ty,
|
|
|
|
trait_ref,
|
|
|
|
)?;
|
|
|
|
self.empty_path = false;
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
2019-09-06 03:57:44 +01:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
fn path_append(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
|
|
|
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
|
2019-02-03 12:59:37 +02:00
|
|
|
disambiguated_data: &DisambiguatedDefPathData,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
|
|
|
print_prefix(self)?;
|
2019-01-25 12:11:50 +02:00
|
|
|
|
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 {
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
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
|
|
|
|
2022-10-30 17:38:49 +05:30
|
|
|
let verbose = self.should_print_verbose();
|
2023-10-17 19:46:14 +02:00
|
|
|
disambiguated_data.fmt_maybe_verbose(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
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
2019-09-06 03:57:44 +01:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
fn path_generic_args(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
|
|
|
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
|
2019-09-25 16:39:44 +01:00
|
|
|
args: &[GenericArg<'tcx>],
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
|
|
|
print_prefix(self)?;
|
2019-01-25 12:11:50 +02:00
|
|
|
|
2023-12-10 10:17:28 +00:00
|
|
|
if !args.is_empty() {
|
2019-01-25 12:11:50 +02:00
|
|
|
if self.in_value {
|
|
|
|
write!(self, "::")?;
|
|
|
|
}
|
2023-12-10 10:53:57 +00:00
|
|
|
self.generic_delimiters(|cx| cx.comma_sep(args.iter().copied()))
|
2019-01-25 12:11:50 +02:00
|
|
|
} else {
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2023-01-08 18:41:09 +00:00
|
|
|
fn reset_type_limit(&mut self) {
|
|
|
|
self.printed_type_count = 0;
|
|
|
|
}
|
|
|
|
|
2023-10-24 20:13:36 +00:00
|
|
|
fn const_infer_name(&self, id: ty::ConstVid) -> 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(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2019-01-25 12:11:50 +02:00
|
|
|
def_id: DefId,
|
2023-07-11 22:35:29 +01:00
|
|
|
args: &'tcx [GenericArg<'tcx>],
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
2019-01-25 12:11:50 +02:00
|
|
|
let was_in_value = std::mem::replace(&mut self.in_value, true);
|
2023-10-17 19:46:14 +02:00
|
|
|
self.print_def_path(def_id, args)?;
|
2019-01-25 12:11:50 +02:00
|
|
|
self.in_value = was_in_value;
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
2025-03-03 01:34:06 +00:00
|
|
|
fn print_in_binder<T>(&mut self, value: &ty::Binder<'tcx, T>) -> Result<(), PrintError>
|
2019-06-14 01:32:15 +03:00
|
|
|
where
|
2023-10-16 20:50:46 +02:00
|
|
|
T: Print<'tcx, Self> + TypeFoldable<TyCtxt<'tcx>>,
|
2019-01-25 12:11:50 +02:00
|
|
|
{
|
2025-03-03 01:34:06 +00:00
|
|
|
self.pretty_print_in_binder(value)
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
fn wrap_binder<T, C: FnOnce(&T, &mut Self) -> Result<(), PrintError>>(
|
|
|
|
&mut self,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: &ty::Binder<'tcx, T>,
|
2025-03-01 19:28:04 +00:00
|
|
|
mode: WrapBinderMode,
|
2020-12-11 15:02:46 -05:00
|
|
|
f: C,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError>
|
2020-12-11 15:02:46 -05:00
|
|
|
where
|
2025-02-23 04:46:51 +00:00
|
|
|
T: TypeFoldable<TyCtxt<'tcx>>,
|
2020-12-11 15:02:46 -05:00
|
|
|
{
|
2025-03-01 19:28:04 +00:00
|
|
|
self.pretty_wrap_binder(value, mode, f)
|
2020-12-11 15:02:46 -05:00
|
|
|
}
|
|
|
|
|
2020-02-13 19:02:58 +01:00
|
|
|
fn typed_value(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
|
|
|
f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
|
|
|
|
t: impl FnOnce(&mut Self) -> Result<(), PrintError>,
|
2020-02-26 19:36:10 +01:00
|
|
|
conversion: &str,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
2020-01-27 18:49:05 +01:00
|
|
|
self.write_str("{")?;
|
2023-10-17 19:46:14 +02:00
|
|
|
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);
|
2023-10-17 19:46:14 +02:00
|
|
|
t(self)?;
|
2020-02-05 11:00:52 +01:00
|
|
|
self.in_value = was_in_value;
|
2020-01-27 18:49:05 +01:00
|
|
|
self.write_str("}")?;
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2020-01-27 18:49:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
fn generic_delimiters(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
|
|
|
f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
|
|
|
|
) -> Result<(), PrintError> {
|
2019-01-25 12:11:50 +02:00
|
|
|
write!(self, "<")?;
|
|
|
|
|
|
|
|
let was_in_value = std::mem::replace(&mut self.in_value, false);
|
2023-10-17 19:46:14 +02:00
|
|
|
f(self)?;
|
|
|
|
self.in_value = was_in_value;
|
2019-01-25 12:11:50 +02:00
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
write!(self, ">")?;
|
|
|
|
Ok(())
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-10-30 17:38:49 +05:30
|
|
|
if self.should_print_verbose() {
|
2019-01-25 12:11:50 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-26 23:23:44 +00:00
|
|
|
if with_forced_trimmed_paths() {
|
2023-01-06 01:11:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-11-14 13:13:27 +00:00
|
|
|
ty::ReEarlyParam(ref data) => data.has_name(),
|
2019-01-25 12:11:50 +02:00
|
|
|
|
2024-12-06 14:46:28 +01:00
|
|
|
ty::ReLateParam(ty::LateParamRegion { kind, .. }) => kind.is_named(),
|
2023-11-13 14:00:05 +00:00
|
|
|
ty::ReBound(_, ty::BoundRegion { kind: br, .. })
|
2023-04-06 21:12:17 -04:00
|
|
|
| ty::RePlaceholder(ty::Placeholder {
|
|
|
|
bound: ty::BoundRegion { kind: br, .. }, ..
|
|
|
|
}) => {
|
2022-11-06 11:40:31 +00:00
|
|
|
if br.is_named() {
|
|
|
|
return true;
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-02-07 14:55:16 +00:00
|
|
|
ty::ReVar(_) | ty::ReErased | ty::ReError(_) => 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>(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2022-07-18 18:47:31 -04:00
|
|
|
p: Pointer<Prov>,
|
2019-12-23 17:41:06 +01:00
|
|
|
ty: Ty<'tcx>,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), PrintError> {
|
|
|
|
let print = |this: &mut Self| {
|
2020-02-05 11:00:52 +01:00
|
|
|
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
|
|
|
}
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2020-02-05 11:00:52 +01:00
|
|
|
};
|
2023-06-28 09:43:31 +00:00
|
|
|
self.typed_value(print, |this| this.print_type(ty), ": ")
|
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> {
|
2023-10-17 19:46:14 +02:00
|
|
|
pub fn pretty_print_region(&mut self, region: ty::Region<'tcx>) -> Result<(), 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));
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
2022-10-30 17:38:49 +05:30
|
|
|
if self.should_print_verbose() {
|
2019-01-25 12:11:50 +02:00
|
|
|
p!(write("{:?}", region));
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-11-16 20:34:16 +00:00
|
|
|
// These printouts are concise. They do not contain all the information
|
2019-01-25 12:11:50 +02:00
|
|
|
// the user might want to diagnose an error, but there is basically no way
|
2022-11-16 20:34:16 +00:00
|
|
|
// to fit that into a short string. Hence the recommendation to use
|
2019-01-25 12:11:50 +02:00
|
|
|
// `explain_region()` or `note_and_explain_region()`.
|
|
|
|
match *region {
|
2023-11-14 13:13:27 +00:00
|
|
|
ty::ReEarlyParam(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));
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
}
|
2024-12-06 14:46:28 +01:00
|
|
|
ty::ReLateParam(ty::LateParamRegion { kind, .. }) => {
|
|
|
|
if let Some(name) = kind.get_name() {
|
|
|
|
p!(write("{}", name));
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
2023-11-13 14:00:05 +00:00
|
|
|
ty::ReBound(_, ty::BoundRegion { kind: br, .. })
|
2023-04-06 21:12:17 -04:00
|
|
|
| ty::RePlaceholder(ty::Placeholder {
|
|
|
|
bound: ty::BoundRegion { kind: br, .. }, ..
|
|
|
|
}) => {
|
2024-11-03 22:06:03 +00:00
|
|
|
if let ty::BoundRegionKind::Named(_, name) = br
|
2022-11-06 11:40:31 +00:00
|
|
|
&& br.is_named()
|
|
|
|
{
|
|
|
|
p!(write("{}", name));
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((region, counter)) = highlight.highlight_bound_region {
|
|
|
|
if br == region {
|
|
|
|
p!(write("'{}", counter));
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::ReVar(region_vid) if identify_regions => {
|
|
|
|
p!(write("{:?}", region_vid));
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
ty::ReVar(_) => {}
|
2020-05-19 20:26:24 +01:00
|
|
|
ty::ReErased => {}
|
2023-02-07 14:55:16 +00:00
|
|
|
ty::ReError(_) => {}
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::ReStatic => {
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("'static");
|
2023-10-17 19:46:14 +02:00
|
|
|
return Ok(());
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:07:15 -06:00
|
|
|
p!("'_");
|
2019-01-25 12:11:50 +02:00
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2024-03-21 14:02:13 +00:00
|
|
|
region_map: UnordMap<ty::BoundRegion, ty::Region<'tcx>>,
|
2022-09-29 18:33:58 +02:00
|
|
|
name: &'a mut (
|
|
|
|
dyn FnMut(
|
2022-10-03 17:37:22 +02:00
|
|
|
Option<ty::DebruijnIndex>, // Debruijn index of the folded late-bound region
|
|
|
|
ty::DebruijnIndex, // Index corresponding to binder level
|
2022-09-29 18:33:58 +02:00
|
|
|
ty::BoundRegion,
|
|
|
|
) -> ty::Region<'tcx>
|
|
|
|
+ 'a
|
|
|
|
),
|
2021-07-18 03:18:05 -04:00
|
|
|
}
|
|
|
|
|
2023-02-22 02:18:40 +00:00
|
|
|
impl<'a, 'tcx> ty::TypeFolder<TyCtxt<'tcx>> for RegionFolder<'a, 'tcx> {
|
2024-06-18 19:13:54 -04:00
|
|
|
fn cx(&self) -> TyCtxt<'tcx> {
|
2021-07-18 03:18:05 -04:00
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
2023-02-22 02:18:40 +00:00
|
|
|
fn fold_binder<T: TypeFoldable<TyCtxt<'tcx>>>(
|
2021-07-18 03:18:05 -04:00
|
|
|
&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 {
|
2023-11-13 14:00:05 +00:00
|
|
|
ty::ReBound(db, br) if db >= self.current_index => {
|
2022-09-29 18:33:58 +02:00
|
|
|
*self.region_map.entry(br).or_insert_with(|| name(Some(db), self.current_index, br))
|
|
|
|
}
|
2023-04-06 21:12:17 -04:00
|
|
|
ty::RePlaceholder(ty::PlaceholderRegion {
|
|
|
|
bound: ty::BoundRegion { kind, .. },
|
|
|
|
..
|
|
|
|
}) => {
|
2021-07-18 03:18:05 -04:00
|
|
|
// If this is an anonymous placeholder, don't rename. Otherwise, in some
|
|
|
|
// async fns, we get a `for<'r> Send` bound
|
|
|
|
match kind {
|
2024-11-03 22:06:03 +00:00
|
|
|
ty::BoundRegionKind::Anon | ty::BoundRegionKind::ClosureEnv => r,
|
2021-07-18 03:18:05 -04:00
|
|
|
_ => {
|
|
|
|
// Index doesn't matter, since this is just for naming and these never get bound
|
2024-04-03 17:49:59 +03:00
|
|
|
let br = ty::BoundRegion { var: ty::BoundVar::ZERO, 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
|
|
|
};
|
2023-11-13 14:00:05 +00:00
|
|
|
if let ty::ReBound(debruijn1, br) = *region {
|
2021-07-18 03:18:05 -04:00
|
|
|
assert_eq!(debruijn1, ty::INNERMOST);
|
2023-11-13 14:00:05 +00:00
|
|
|
ty::Region::new_bound(self.tcx, 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>(
|
2023-10-17 19:46:14 +02:00
|
|
|
&mut self,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: &ty::Binder<'tcx, T>,
|
2025-03-01 19:28:04 +00:00
|
|
|
mode: WrapBinderMode,
|
2024-03-21 14:02:13 +00:00
|
|
|
) -> Result<(T, UnordMap<ty::BoundRegion, ty::Region<'tcx>>), fmt::Error>
|
2019-06-14 01:32:15 +03:00
|
|
|
where
|
2025-02-23 04:46:51 +00:00
|
|
|
T: TypeFoldable<TyCtxt<'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
|
|
|
}
|
|
|
|
|
2024-07-19 11:51:21 -04:00
|
|
|
debug!("self.used_region_names: {:?}", self.used_region_names);
|
2022-09-15 18:46:40 +02:00
|
|
|
|
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
|
|
|
|
};
|
2023-07-25 22:00:13 +02:00
|
|
|
let _ = write!(cx, "{w}");
|
2021-07-18 03:18:05 -04:00
|
|
|
};
|
|
|
|
let do_continue = |cx: &mut Self, cont: Symbol| {
|
2023-07-25 22:00:13 +02:00
|
|
|
let _ = write!(cx, "{cont}");
|
2019-01-18 21:33:31 +02:00
|
|
|
};
|
|
|
|
|
2022-11-18 10:30:47 +01:00
|
|
|
let possible_names = ('a'..='z').rev().map(|s| Symbol::intern(&format!("'{s}")));
|
2022-09-15 18:46:40 +02:00
|
|
|
|
|
|
|
let mut available_names = possible_names
|
2023-11-21 20:07:32 +01:00
|
|
|
.filter(|name| !self.used_region_names.contains(name))
|
2022-09-15 18:46:40 +02:00
|
|
|
.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.
|
2022-10-30 17:38:49 +05:30
|
|
|
let (new_value, map) = if self.should_print_verbose() {
|
2022-11-11 12:10:53 +00:00
|
|
|
for var in value.bound_vars().iter() {
|
2025-03-01 19:28:04 +00:00
|
|
|
start_or_continue(self, mode.start_str(), ", ");
|
2023-07-25 22:00:13 +02:00
|
|
|
write!(self, "{var:?}")?;
|
2022-11-11 12:10:53 +00:00
|
|
|
}
|
2025-03-01 19:28:04 +00:00
|
|
|
// Unconditionally render `unsafe<>`.
|
|
|
|
if value.bound_vars().is_empty() && mode == WrapBinderMode::Unsafe {
|
|
|
|
start_or_continue(self, mode.start_str(), "");
|
|
|
|
}
|
2023-10-17 19:46:14 +02:00
|
|
|
start_or_continue(self, "", "> ");
|
2024-03-21 14:02:13 +00:00
|
|
|
(value.clone().skip_binder(), UnordMap::default())
|
2021-04-05 00:10:09 -04:00
|
|
|
} else {
|
2021-07-18 03:18:05 -04:00
|
|
|
let tcx = self.tcx;
|
2022-10-03 17:37:22 +02:00
|
|
|
|
2023-11-26 23:23:44 +00:00
|
|
|
let trim_path = with_forced_trimmed_paths();
|
2022-10-03 17:37:22 +02:00
|
|
|
// Closure used in `RegionFolder` to create names for anonymous late-bound
|
|
|
|
// regions. We use two `DebruijnIndex`es (one for the currently folded
|
|
|
|
// late-bound region and the other for the binder level) to determine
|
|
|
|
// whether a name has already been created for the currently folded region,
|
|
|
|
// see issue #102392.
|
|
|
|
let mut name = |lifetime_idx: Option<ty::DebruijnIndex>,
|
|
|
|
binder_level_idx: ty::DebruijnIndex,
|
2022-09-29 18:33:58 +02:00
|
|
|
br: ty::BoundRegion| {
|
|
|
|
let (name, kind) = match br.kind {
|
2024-11-03 22:06:03 +00:00
|
|
|
ty::BoundRegionKind::Anon | ty::BoundRegionKind::ClosureEnv => {
|
2023-11-21 20:07:32 +01:00
|
|
|
let name = next_name(self);
|
2022-09-29 18:33:58 +02:00
|
|
|
|
2022-10-03 17:37:22 +02:00
|
|
|
if let Some(lt_idx) = lifetime_idx {
|
|
|
|
if lt_idx > binder_level_idx {
|
2024-11-03 22:06:03 +00:00
|
|
|
let kind =
|
|
|
|
ty::BoundRegionKind::Named(CRATE_DEF_ID.to_def_id(), name);
|
2023-11-13 14:00:05 +00:00
|
|
|
return ty::Region::new_bound(
|
2023-05-29 17:54:53 +00:00
|
|
|
tcx,
|
2022-09-29 18:33:58 +02:00
|
|
|
ty::INNERMOST,
|
|
|
|
ty::BoundRegion { var: br.var, kind },
|
2023-02-13 13:03:45 +11:00
|
|
|
);
|
2022-09-29 18:33:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-03 22:06:03 +00:00
|
|
|
(name, ty::BoundRegionKind::Named(CRATE_DEF_ID.to_def_id(), name))
|
2021-04-05 00:10:09 -04:00
|
|
|
}
|
2024-11-03 22:06:03 +00:00
|
|
|
ty::BoundRegionKind::Named(def_id, kw::UnderscoreLifetime | kw::Empty) => {
|
2023-11-21 20:07:32 +01:00
|
|
|
let name = next_name(self);
|
2022-09-29 18:33:58 +02:00
|
|
|
|
2022-10-03 17:37:22 +02:00
|
|
|
if let Some(lt_idx) = lifetime_idx {
|
|
|
|
if lt_idx > binder_level_idx {
|
2024-11-03 22:06:03 +00:00
|
|
|
let kind = ty::BoundRegionKind::Named(def_id, name);
|
2023-11-13 14:00:05 +00:00
|
|
|
return ty::Region::new_bound(
|
2023-05-29 17:54:53 +00:00
|
|
|
tcx,
|
2022-09-29 18:33:58 +02:00
|
|
|
ty::INNERMOST,
|
|
|
|
ty::BoundRegion { var: br.var, kind },
|
2023-02-13 13:03:45 +11:00
|
|
|
);
|
2022-09-29 18:33:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-03 22:06:03 +00:00
|
|
|
(name, ty::BoundRegionKind::Named(def_id, name))
|
2022-05-12 21:18:26 +02:00
|
|
|
}
|
2024-11-03 22:06:03 +00:00
|
|
|
ty::BoundRegionKind::Named(_, name) => {
|
2022-10-03 17:37:22 +02:00
|
|
|
if let Some(lt_idx) = lifetime_idx {
|
|
|
|
if lt_idx > binder_level_idx {
|
2022-09-29 18:33:58 +02:00
|
|
|
let kind = br.kind;
|
2023-11-13 14:00:05 +00:00
|
|
|
return ty::Region::new_bound(
|
2023-05-29 17:54:53 +00:00
|
|
|
tcx,
|
2022-09-29 18:33:58 +02:00
|
|
|
ty::INNERMOST,
|
|
|
|
ty::BoundRegion { var: br.var, kind },
|
2023-02-13 13:03:45 +11:00
|
|
|
);
|
2022-09-29 18:33:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(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
|
|
|
|
2025-03-01 19:28:04 +00:00
|
|
|
// Unconditionally render `unsafe<>`.
|
|
|
|
if !trim_path || mode == WrapBinderMode::Unsafe {
|
|
|
|
start_or_continue(self, mode.start_str(), ", ");
|
2023-10-17 19:46:14 +02:00
|
|
|
do_continue(self, name);
|
2023-01-06 01:03:52 +00:00
|
|
|
}
|
2023-11-13 14:00:05 +00:00
|
|
|
ty::Region::new_bound(tcx, ty::INNERMOST, ty::BoundRegion { var: br.var, kind })
|
2021-07-18 03:18:05 -04:00
|
|
|
};
|
|
|
|
let mut folder = RegionFolder {
|
|
|
|
tcx,
|
|
|
|
current_index: ty::INNERMOST,
|
|
|
|
name: &mut name,
|
2024-03-21 14:02:13 +00:00
|
|
|
region_map: UnordMap::default(),
|
2021-07-18 03:18:05 -04:00
|
|
|
};
|
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;
|
2025-03-01 19:28:04 +00:00
|
|
|
|
|
|
|
if mode == WrapBinderMode::Unsafe && region_map.is_empty() {
|
|
|
|
start_or_continue(self, mode.start_str(), "");
|
2023-01-06 01:03:52 +00:00
|
|
|
}
|
2025-03-01 19:28:04 +00:00
|
|
|
start_or_continue(self, "", "> ");
|
|
|
|
|
2021-07-18 03:18:05 -04:00
|
|
|
(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;
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok((new_value, map))
|
2019-11-23 16:01:20 -08:00
|
|
|
}
|
|
|
|
|
2025-03-03 01:34:06 +00:00
|
|
|
pub fn pretty_print_in_binder<T>(
|
2025-03-01 19:28:04 +00:00
|
|
|
&mut self,
|
|
|
|
value: &ty::Binder<'tcx, T>,
|
|
|
|
) -> Result<(), fmt::Error>
|
2019-11-23 16:01:20 -08:00
|
|
|
where
|
2023-10-16 20:50:46 +02:00
|
|
|
T: Print<'tcx, Self> + TypeFoldable<TyCtxt<'tcx>>,
|
2019-11-23 16:01:20 -08:00
|
|
|
{
|
|
|
|
let old_region_index = self.region_index;
|
2025-03-03 01:34:06 +00:00
|
|
|
let (new_value, _) = self.name_all_regions(value, WrapBinderMode::ForAll)?;
|
2023-10-17 19:46:14 +02:00
|
|
|
new_value.print(self)?;
|
|
|
|
self.region_index = old_region_index;
|
|
|
|
self.binder_depth -= 1;
|
|
|
|
Ok(())
|
2019-01-18 21:33:31 +02:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:46:14 +02:00
|
|
|
pub fn pretty_wrap_binder<T, C: FnOnce(&T, &mut Self) -> Result<(), fmt::Error>>(
|
|
|
|
&mut self,
|
2020-10-05 16:51:33 -04:00
|
|
|
value: &ty::Binder<'tcx, T>,
|
2025-03-01 19:28:04 +00:00
|
|
|
mode: WrapBinderMode,
|
2020-12-11 15:02:46 -05:00
|
|
|
f: C,
|
2023-10-17 19:46:14 +02:00
|
|
|
) -> Result<(), fmt::Error>
|
2020-12-11 15:02:46 -05:00
|
|
|
where
|
2025-02-23 04:46:51 +00:00
|
|
|
T: TypeFoldable<TyCtxt<'tcx>>,
|
2020-12-11 15:02:46 -05:00
|
|
|
{
|
|
|
|
let old_region_index = self.region_index;
|
2025-03-01 19:28:04 +00:00
|
|
|
let (new_value, _) = self.name_all_regions(value, mode)?;
|
2023-10-17 19:46:14 +02:00
|
|
|
f(&new_value, self)?;
|
|
|
|
self.region_index = old_region_index;
|
|
|
|
self.binder_depth -= 1;
|
|
|
|
Ok(())
|
2020-12-11 15:02:46 -05:00
|
|
|
}
|
|
|
|
|
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
|
2023-02-22 02:18:40 +00:00
|
|
|
T: TypeVisitable<TyCtxt<'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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 02:18:40 +00:00
|
|
|
impl<'tcx> ty::visit::TypeVisitor<TyCtxt<'tcx>> for RegionNameCollector<'tcx> {
|
2024-02-24 17:22:28 -05:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) {
|
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
|
|
|
}
|
|
|
|
}
|
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.
|
2024-02-24 17:22:28 -05:00
|
|
|
fn visit_ty(&mut self, ty: Ty<'tcx>) {
|
2021-03-23 12:41:26 +01:00
|
|
|
let not_previously_inserted = self.type_collector.insert(ty);
|
|
|
|
if not_previously_inserted {
|
|
|
|
ty.super_visit_with(self)
|
|
|
|
}
|
|
|
|
}
|
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
|
2023-10-16 20:50:46 +02:00
|
|
|
T: Print<'tcx, P> + TypeFoldable<TyCtxt<'tcx>>,
|
2019-01-20 04:56:48 +02:00
|
|
|
{
|
2023-10-17 19:46:14 +02:00
|
|
|
fn print(&self, cx: &mut P) -> Result<(), PrintError> {
|
2025-03-03 01:34:06 +00:00
|
|
|
cx.print_in_binder(self)
|
2019-01-20 04:56:48 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-20 14:00:39 +02:00
|
|
|
|
2024-05-21 11:07:54 -04:00
|
|
|
impl<'tcx, T, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::OutlivesPredicate<'tcx, T>
|
2019-06-14 01:32:15 +03:00
|
|
|
where
|
2023-10-16 20:50:46 +02:00
|
|
|
T: Print<'tcx, P>,
|
2019-01-20 14:00:39 +02:00
|
|
|
{
|
2023-10-17 19:46:14 +02:00
|
|
|
fn print(&self, cx: &mut P) -> Result<(), PrintError> {
|
2019-01-25 12:11:50 +02:00
|
|
|
define_scoped_cx!(cx);
|
2020-09-30 10:07:15 -06:00
|
|
|
p!(print(self.0), ": ", print(self.1));
|
2023-10-17 19:46:14 +02:00
|
|
|
Ok(())
|
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>>`.
|
Teach structured errors to display short `Ty`
Make it so that every structured error annotated with `#[derive(Diagnostic)]` that has a field of type `Ty<'_>`, the printing of that value into a `String` will look at the thread-local storage `TyCtxt` in order to shorten to a length appropriate with the terminal width. When this happen, the resulting error will have a note with the file where the full type name was written to.
```
error[E0618]: expected function, found `((..., ..., ..., ...), ..., ..., ...)``
--> long.rs:7:5
|
6 | fn foo(x: D) { //~ `x` has type `(...
| - `x` has type `((..., ..., ..., ...), ..., ..., ...)`
7 | x(); //~ ERROR expected function, found `(...
| ^--
| |
| call expression requires function
|
= note: the full name for the type has been written to 'long.long-type-14182675702747116984.txt'
= note: consider using `--verbose` to print the full type name to the console
```
2025-02-18 01:15:59 +00:00
|
|
|
#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift, Hash)]
|
2019-11-21 21:01:14 +03:00
|
|
|
pub struct TraitRefPrintOnlyTraitPath<'tcx>(ty::TraitRef<'tcx>);
|
|
|
|
|
2024-03-05 16:53:24 +11:00
|
|
|
impl<'tcx> rustc_errors::IntoDiagArg for TraitRefPrintOnlyTraitPath<'tcx> {
|
Teach structured errors to display short `Ty`
Make it so that every structured error annotated with `#[derive(Diagnostic)]` that has a field of type `Ty<'_>`, the printing of that value into a `String` will look at the thread-local storage `TyCtxt` in order to shorten to a length appropriate with the terminal width. When this happen, the resulting error will have a note with the file where the full type name was written to.
```
error[E0618]: expected function, found `((..., ..., ..., ...), ..., ..., ...)``
--> long.rs:7:5
|
6 | fn foo(x: D) { //~ `x` has type `(...
| - `x` has type `((..., ..., ..., ...), ..., ..., ...)`
7 | x(); //~ ERROR expected function, found `(...
| ^--
| |
| call expression requires function
|
= note: the full name for the type has been written to 'long.long-type-14182675702747116984.txt'
= note: consider using `--verbose` to print the full type name to the console
```
2025-02-18 01:15:59 +00:00
|
|
|
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
|
|
|
|
ty::tls::with(|tcx| {
|
|
|
|
let trait_ref = tcx.short_string(self, path);
|
|
|
|
rustc_errors::DiagArgValue::Str(std::borrow::Cow::Owned(trait_ref))
|
|
|
|
})
|
2023-05-03 21:09:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-24 22:09:59 +00:00
|
|
|
/// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
|
|
|
|
/// the trait path, and additionally tries to "sugar" `Fn(...)` trait bounds.
|
Teach structured errors to display short `Ty`
Make it so that every structured error annotated with `#[derive(Diagnostic)]` that has a field of type `Ty<'_>`, the printing of that value into a `String` will look at the thread-local storage `TyCtxt` in order to shorten to a length appropriate with the terminal width. When this happen, the resulting error will have a note with the file where the full type name was written to.
```
error[E0618]: expected function, found `((..., ..., ..., ...), ..., ..., ...)``
--> long.rs:7:5
|
6 | fn foo(x: D) { //~ `x` has type `(...
| - `x` has type `((..., ..., ..., ...), ..., ..., ...)`
7 | x(); //~ ERROR expected function, found `(...
| ^--
| |
| call expression requires function
|
= note: the full name for the type has been written to 'long.long-type-14182675702747116984.txt'
= note: consider using `--verbose` to print the full type name to the console
```
2025-02-18 01:15:59 +00:00
|
|
|
#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift, Hash)]
|
2023-11-24 22:09:59 +00:00
|
|
|
pub struct TraitRefPrintSugared<'tcx>(ty::TraitRef<'tcx>);
|
|
|
|
|
2024-03-05 16:53:24 +11:00
|
|
|
impl<'tcx> rustc_errors::IntoDiagArg for TraitRefPrintSugared<'tcx> {
|
Teach structured errors to display short `Ty`
Make it so that every structured error annotated with `#[derive(Diagnostic)]` that has a field of type `Ty<'_>`, the printing of that value into a `String` will look at the thread-local storage `TyCtxt` in order to shorten to a length appropriate with the terminal width. When this happen, the resulting error will have a note with the file where the full type name was written to.
```
error[E0618]: expected function, found `((..., ..., ..., ...), ..., ..., ...)``
--> long.rs:7:5
|
6 | fn foo(x: D) { //~ `x` has type `(...
| - `x` has type `((..., ..., ..., ...), ..., ..., ...)`
7 | x(); //~ ERROR expected function, found `(...
| ^--
| |
| call expression requires function
|
= note: the full name for the type has been written to 'long.long-type-14182675702747116984.txt'
= note: consider using `--verbose` to print the full type name to the console
```
2025-02-18 01:15:59 +00:00
|
|
|
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
|
|
|
|
ty::tls::with(|tcx| {
|
|
|
|
let trait_ref = tcx.short_string(self, path);
|
|
|
|
rustc_errors::DiagArgValue::Str(std::borrow::Cow::Owned(trait_ref))
|
|
|
|
})
|
2023-11-24 22:09:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> fmt::Debug for TraitRefPrintSugared<'tcx> {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-10 14:59:56 -04:00
|
|
|
#[extension(pub trait PrintTraitRefExt<'tcx>)]
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> ty::TraitRef<'tcx> {
|
2024-05-10 14:59:56 -04:00
|
|
|
fn print_only_trait_path(self) -> TraitRefPrintOnlyTraitPath<'tcx> {
|
2019-11-21 21:01:14 +03:00
|
|
|
TraitRefPrintOnlyTraitPath(self)
|
|
|
|
}
|
2021-09-28 14:48:54 +00:00
|
|
|
|
2024-05-10 14:59:56 -04:00
|
|
|
fn print_trait_sugared(self) -> TraitRefPrintSugared<'tcx> {
|
2023-11-24 22:09:59 +00:00
|
|
|
TraitRefPrintSugared(self)
|
|
|
|
}
|
|
|
|
|
2024-05-10 14:59:56 -04:00
|
|
|
fn print_only_trait_name(self) -> TraitRefPrintOnlyTraitName<'tcx> {
|
2021-09-28 14:48:54 +00:00
|
|
|
TraitRefPrintOnlyTraitName(self)
|
|
|
|
}
|
2019-11-21 21:01:14 +03:00
|
|
|
}
|
|
|
|
|
2024-05-20 12:57:07 -04:00
|
|
|
#[extension(pub trait PrintPolyTraitRefExt<'tcx>)]
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> ty::Binder<'tcx, ty::TraitRef<'tcx>> {
|
2024-05-20 12:57:07 -04:00
|
|
|
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())
|
|
|
|
}
|
2023-11-24 22:09:59 +00:00
|
|
|
|
2024-05-20 12:57:07 -04:00
|
|
|
fn print_trait_sugared(self) -> ty::Binder<'tcx, TraitRefPrintSugared<'tcx>> {
|
2023-11-24 22:09:59 +00:00
|
|
|
self.map_bound(|tr| tr.print_trait_sugared())
|
|
|
|
}
|
2019-11-21 21:01:14 +03:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-11 02:03:53 -04:00
|
|
|
#[extension(pub trait PrintTraitPredicateExt<'tcx>)]
|
2021-12-24 22:50:44 +08:00
|
|
|
impl<'tcx> ty::TraitPredicate<'tcx> {
|
2024-05-11 02:03:53 -04:00
|
|
|
fn print_modifiers_and_trait_path(self) -> TraitPredPrintModifiersAndPath<'tcx> {
|
2021-12-24 22:50:44 +08:00
|
|
|
TraitPredPrintModifiersAndPath(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-31 03:26:56 +00:00
|
|
|
#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift, Hash)]
|
2024-10-20 18:33:59 +00:00
|
|
|
pub struct TraitPredPrintWithBoundConstness<'tcx>(
|
|
|
|
ty::TraitPredicate<'tcx>,
|
|
|
|
Option<ty::BoundConstness>,
|
|
|
|
);
|
2024-10-02 19:42:06 +08:00
|
|
|
|
|
|
|
impl<'tcx> fmt::Debug for TraitPredPrintWithBoundConstness<'tcx> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
fmt::Display::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-20 12:57:07 -04:00
|
|
|
#[extension(pub trait PrintPolyTraitPredicateExt<'tcx>)]
|
2021-12-24 22:50:44 +08:00
|
|
|
impl<'tcx> ty::PolyTraitPredicate<'tcx> {
|
2024-05-20 12:57:07 -04:00
|
|
|
fn print_modifiers_and_trait_path(
|
2021-12-24 22:50:44 +08:00
|
|
|
self,
|
|
|
|
) -> ty::Binder<'tcx, TraitPredPrintModifiersAndPath<'tcx>> {
|
|
|
|
self.map_bound(TraitPredPrintModifiersAndPath)
|
|
|
|
}
|
2024-10-02 19:42:06 +08:00
|
|
|
|
|
|
|
fn print_with_bound_constness(
|
|
|
|
self,
|
2024-10-20 18:33:59 +00:00
|
|
|
constness: Option<ty::BoundConstness>,
|
2024-10-02 19:42:06 +08:00
|
|
|
) -> ty::Binder<'tcx, TraitPredPrintWithBoundConstness<'tcx>> {
|
|
|
|
self.map_bound(|trait_pred| TraitPredPrintWithBoundConstness(trait_pred, constness))
|
|
|
|
}
|
2021-12-24 22:50:44 +08:00
|
|
|
}
|
|
|
|
|
2023-04-17 07:16:25 +10:00
|
|
|
#[derive(Debug, Copy, Clone, Lift)]
|
2022-08-26 01:25:56 +00:00
|
|
|
pub struct PrintClosureAsImpl<'tcx> {
|
2024-05-31 14:13:46 -04:00
|
|
|
pub closure: ty::ClosureArgs<TyCtxt<'tcx>>,
|
2022-08-26 01:25:56 +00:00
|
|
|
}
|
|
|
|
|
2023-10-28 20:22:47 +02:00
|
|
|
macro_rules! forward_display_to_print {
|
|
|
|
($($ty:ty),+) => {
|
|
|
|
// Some of the $ty arguments may not actually use 'tcx
|
|
|
|
$(#[allow(unused_lifetimes)] impl<'tcx> fmt::Display for $ty {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
ty::tls::with(|tcx| {
|
|
|
|
let mut cx = FmtPrinter::new(tcx, Namespace::TypeNS);
|
|
|
|
tcx.lift(*self)
|
|
|
|
.expect("could not lift for printing")
|
|
|
|
.print(&mut cx)?;
|
|
|
|
f.write_str(&cx.into_buffer())?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})+
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! define_print {
|
|
|
|
(($self:ident, $cx:ident): $($ty:ty $print:block)+) => {
|
|
|
|
$(impl<'tcx, P: PrettyPrinter<'tcx>> Print<'tcx, P> for $ty {
|
|
|
|
fn print(&$self, $cx: &mut P) -> Result<(), PrintError> {
|
|
|
|
define_scoped_cx!($cx);
|
|
|
|
let _: () = $print;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
})+
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! define_print_and_forward_display {
|
|
|
|
(($self:ident, $cx:ident): $($ty:ty $print:block)+) => {
|
|
|
|
define_print!(($self, $cx): $($ty $print)*);
|
|
|
|
forward_display_to_print!($($ty),+);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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>,
|
2022-11-19 03:28:56 +00:00
|
|
|
&'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
|
2024-05-21 11:07:54 -04:00
|
|
|
ty::Const<'tcx>
|
2019-01-25 12:11:50 +02:00
|
|
|
}
|
|
|
|
|
2023-10-20 20:43:33 +00:00
|
|
|
define_print! {
|
|
|
|
(self, cx):
|
|
|
|
|
2024-05-15 13:54:37 -04:00
|
|
|
ty::FnSig<'tcx> {
|
2024-05-17 14:17:48 -03:00
|
|
|
p!(write("{}", self.safety.prefix_str()));
|
2024-05-15 13:54:37 -04:00
|
|
|
|
2024-11-02 19:33:00 -07:00
|
|
|
if self.abi != ExternAbi::Rust {
|
2024-05-15 13:54:37 -04:00
|
|
|
p!(write("extern {} ", self.abi));
|
|
|
|
}
|
|
|
|
|
|
|
|
p!("fn", pretty_fn_sig(self.inputs(), self.c_variadic, self.output()));
|
|
|
|
}
|
|
|
|
|
2024-05-10 14:59:56 -04:00
|
|
|
ty::TraitRef<'tcx> {
|
|
|
|
p!(write("<{} as {}>", self.self_ty(), self.print_only_trait_path()))
|
|
|
|
}
|
|
|
|
|
2024-05-13 12:40:08 -04:00
|
|
|
ty::AliasTy<'tcx> {
|
|
|
|
let alias_term: ty::AliasTerm<'tcx> = (*self).into();
|
|
|
|
p!(print(alias_term))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::AliasTerm<'tcx> {
|
|
|
|
match self.kind(cx.tcx()) {
|
|
|
|
ty::AliasTermKind::InherentTy => p!(pretty_print_inherent_projection(*self)),
|
|
|
|
ty::AliasTermKind::ProjectionTy
|
|
|
|
| ty::AliasTermKind::WeakTy
|
|
|
|
| ty::AliasTermKind::OpaqueTy
|
|
|
|
| ty::AliasTermKind::UnevaluatedConst
|
|
|
|
| ty::AliasTermKind::ProjectionConst => {
|
|
|
|
// If we're printing verbosely, or don't want to invoke queries
|
|
|
|
// (`is_impl_trait_in_trait`), then fall back to printing the def path.
|
|
|
|
// This is likely what you want if you're debugging the compiler anyways.
|
|
|
|
if !(cx.should_print_verbose() || with_reduced_queries())
|
|
|
|
&& cx.tcx().is_impl_trait_in_trait(self.def_id)
|
|
|
|
{
|
|
|
|
return cx.pretty_print_opaque_impl_type(self.def_id, self.args);
|
|
|
|
} else {
|
|
|
|
p!(print_def_path(self.def_id, self.args));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-11 02:03:53 -04:00
|
|
|
ty::TraitPredicate<'tcx> {
|
|
|
|
p!(print(self.trait_ref.self_ty()), ": ");
|
|
|
|
if let ty::PredicatePolarity::Negative = self.polarity {
|
|
|
|
p!("!");
|
|
|
|
}
|
|
|
|
p!(print(self.trait_ref.print_trait_sugared()))
|
|
|
|
}
|
|
|
|
|
2024-10-20 19:49:11 +00:00
|
|
|
ty::HostEffectPredicate<'tcx> {
|
2024-10-29 23:42:59 +00:00
|
|
|
let constness = match self.constness {
|
|
|
|
ty::BoundConstness::Const => { "const" }
|
|
|
|
ty::BoundConstness::Maybe => { "~const" }
|
2024-10-20 19:49:11 +00:00
|
|
|
};
|
|
|
|
p!(print(self.trait_ref.self_ty()), ": {constness} ");
|
|
|
|
p!(print(self.trait_ref.print_trait_sugared()))
|
|
|
|
}
|
|
|
|
|
2023-12-12 19:07:19 +00:00
|
|
|
ty::TypeAndMut<'tcx> {
|
|
|
|
p!(write("{}", self.mutbl.prefix_str()), print(self.ty))
|
|
|
|
}
|
|
|
|
|
2023-10-20 20:43:33 +00:00
|
|
|
ty::ClauseKind<'tcx> {
|
|
|
|
match *self {
|
|
|
|
ty::ClauseKind::Trait(ref data) => {
|
|
|
|
p!(print(data))
|
|
|
|
}
|
|
|
|
ty::ClauseKind::RegionOutlives(predicate) => p!(print(predicate)),
|
|
|
|
ty::ClauseKind::TypeOutlives(predicate) => p!(print(predicate)),
|
|
|
|
ty::ClauseKind::Projection(predicate) => p!(print(predicate)),
|
2024-10-20 19:49:11 +00:00
|
|
|
ty::ClauseKind::HostEffect(predicate) => p!(print(predicate)),
|
2023-10-20 20:43:33 +00:00
|
|
|
ty::ClauseKind::ConstArgHasType(ct, ty) => {
|
|
|
|
p!("the constant `", print(ct), "` has type `", print(ty), "`")
|
|
|
|
},
|
|
|
|
ty::ClauseKind::WellFormed(arg) => p!(print(arg), " well-formed"),
|
|
|
|
ty::ClauseKind::ConstEvaluatable(ct) => {
|
|
|
|
p!("the constant `", print(ct), "` can be evaluated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::PredicateKind<'tcx> {
|
|
|
|
match *self {
|
|
|
|
ty::PredicateKind::Clause(data) => {
|
|
|
|
p!(print(data))
|
|
|
|
}
|
|
|
|
ty::PredicateKind::Subtype(predicate) => p!(print(predicate)),
|
|
|
|
ty::PredicateKind::Coerce(predicate) => p!(print(predicate)),
|
2024-09-25 10:38:40 +02:00
|
|
|
ty::PredicateKind::DynCompatible(trait_def_id) => {
|
|
|
|
p!("the trait `", print_def_path(trait_def_id, &[]), "` is dyn-compatible")
|
2023-10-20 20:43:33 +00:00
|
|
|
}
|
|
|
|
ty::PredicateKind::ConstEquate(c1, c2) => {
|
|
|
|
p!("the constant `", print(c1), "` equals `", print(c2), "`")
|
|
|
|
}
|
|
|
|
ty::PredicateKind::Ambiguous => p!("ambiguous"),
|
2023-12-07 18:20:27 +01:00
|
|
|
ty::PredicateKind::NormalizesTo(data) => p!(print(data)),
|
2023-10-20 20:43:33 +00:00
|
|
|
ty::PredicateKind::AliasRelate(t1, t2, dir) => p!(print(t1), write(" {} ", dir), print(t2)),
|
|
|
|
}
|
|
|
|
}
|
2019-01-20 14:00:39 +02:00
|
|
|
|
2024-05-11 18:14:44 -04: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) => {
|
|
|
|
p!(print_def_path(def_id, &[]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2024-02-09 20:10:39 +00:00
|
|
|
let dummy_self = Ty::new_fresh(cx.tcx(), 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-11-26 21:21:20 +00:00
|
|
|
let name = cx.tcx().associated_item(self.def_id).name;
|
2024-08-22 05:30:08 +02:00
|
|
|
// The args don't contain the self ty (as it has been erased) but the corresp.
|
|
|
|
// generics do as the trait always has a self ty param. We need to offset.
|
|
|
|
let args = &self.args[cx.tcx().generics_of(self.def_id).parent_count - 1..];
|
|
|
|
p!(path_generic_args(|cx| write!(cx, "{name}"), args), " = ", print(self.term))
|
2019-01-24 20:47:02 +02:00
|
|
|
}
|
|
|
|
|
2024-05-11 12:46:11 -04:00
|
|
|
ty::ProjectionPredicate<'tcx> {
|
2024-05-13 10:00:38 -04:00
|
|
|
p!(print(self.projection_term), " == ");
|
2024-05-11 12:46:11 -04:00
|
|
|
cx.reset_type_limit();
|
|
|
|
p!(print(self.term))
|
|
|
|
}
|
2024-05-11 13:51:25 -04:00
|
|
|
|
|
|
|
ty::SubtypePredicate<'tcx> {
|
|
|
|
p!(print(self.a), " <: ");
|
|
|
|
cx.reset_type_limit();
|
|
|
|
p!(print(self.b))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::CoercePredicate<'tcx> {
|
|
|
|
p!(print(self.a), " -> ");
|
|
|
|
cx.reset_type_limit();
|
|
|
|
p!(print(self.b))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::NormalizesTo<'tcx> {
|
|
|
|
p!(print(self.alias), " normalizes-to ");
|
|
|
|
cx.reset_type_limit();
|
|
|
|
p!(print(self.term))
|
|
|
|
}
|
2024-05-11 12:46:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
define_print_and_forward_display! {
|
|
|
|
(self, cx):
|
|
|
|
|
|
|
|
&'tcx ty::List<Ty<'tcx>> {
|
|
|
|
p!("{{", comma_sep(self.iter()), "}}")
|
|
|
|
}
|
|
|
|
|
2019-11-21 21:01:14 +03:00
|
|
|
TraitRefPrintOnlyTraitPath<'tcx> {
|
2023-07-11 22:35:29 +01:00
|
|
|
p!(print_def_path(self.0.def_id, self.0.args));
|
2019-01-20 14:00:39 +02:00
|
|
|
}
|
|
|
|
|
2023-11-24 22:09:59 +00:00
|
|
|
TraitRefPrintSugared<'tcx> {
|
2024-03-03 04:37:33 +01:00
|
|
|
if !with_reduced_queries()
|
2024-11-11 17:42:35 +00:00
|
|
|
&& cx.tcx().trait_def(self.0.def_id).paren_sugar
|
2023-11-24 22:09:59 +00:00
|
|
|
&& let ty::Tuple(args) = self.0.args.type_at(1).kind()
|
|
|
|
{
|
2024-11-11 17:42:35 +00:00
|
|
|
p!(write("{}", cx.tcx().item_name(self.0.def_id)), "(");
|
2023-11-24 22:09:59 +00:00
|
|
|
for (i, arg) in args.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
p!(", ");
|
|
|
|
}
|
|
|
|
p!(print(arg));
|
|
|
|
}
|
|
|
|
p!(")");
|
|
|
|
} else {
|
|
|
|
p!(print_def_path(self.0.def_id, self.0.args));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
2024-03-21 15:45:28 -04:00
|
|
|
if let ty::PredicatePolarity::Negative = self.0.polarity {
|
2021-12-24 22:50:44 +08:00
|
|
|
p!("!")
|
|
|
|
}
|
2024-05-29 21:42:40 +00:00
|
|
|
p!(print(self.0.trait_ref.print_trait_sugared()));
|
2021-12-24 22:50:44 +08:00
|
|
|
}
|
|
|
|
|
2024-10-02 19:42:06 +08:00
|
|
|
TraitPredPrintWithBoundConstness<'tcx> {
|
|
|
|
p!(print(self.0.trait_ref.self_ty()), ": ");
|
2024-10-20 18:33:59 +00:00
|
|
|
if let Some(constness) = self.1 {
|
|
|
|
p!(pretty_print_bound_constness(constness));
|
|
|
|
}
|
2024-10-02 19:42:06 +08:00
|
|
|
if let ty::PredicatePolarity::Negative = self.0.polarity {
|
|
|
|
p!("!");
|
|
|
|
}
|
|
|
|
p!(print(self.0.trait_ref.print_trait_sugared()))
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2022-01-08 09:28:12 +00:00
|
|
|
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::Predicate<'tcx> {
|
2023-10-20 20:43:33 +00:00
|
|
|
p!(print(self.kind()))
|
2020-07-18 11:46:38 +02:00
|
|
|
}
|
|
|
|
|
2023-06-22 18:17:13 +00:00
|
|
|
ty::Clause<'tcx> {
|
|
|
|
p!(print(self.kind()))
|
|
|
|
}
|
|
|
|
|
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.
|
2025-02-03 10:45:49 +11:00
|
|
|
for id in tcx.hir_free_items() {
|
2022-10-27 14:02:18 +11:00
|
|
|
if matches!(tcx.def_kind(id.owner_id), DefKind::Use) {
|
2022-04-07 14:04:07 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2025-02-03 10:45:49 +11:00
|
|
|
let item = tcx.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;
|
|
|
|
}
|
|
|
|
|
2022-10-27 14:02:18 +11:00
|
|
|
let def_id = item.owner_id.to_def_id();
|
2021-01-30 17:47:51 +01:00
|
|
|
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();
|
|
|
|
|
2024-06-06 09:45:50 +00:00
|
|
|
for &cnum in tcx.crates(()).iter() {
|
2020-09-02 10:40:56 +03:00
|
|
|
// Ignore crates that are not direct dependencies.
|
2024-08-17 12:50:12 -04:00
|
|
|
match tcx.extern_crate(cnum) {
|
2020-09-02 10:40:56 +03:00
|
|
|
None => continue,
|
|
|
|
Some(extern_crate) => {
|
|
|
|
if !extern_crate.is_direct() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-17 12:50:12 -04:00
|
|
|
queue.push(cnum.as_def_id());
|
2020-09-02 10:40:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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, _) => {}
|
2023-09-26 02:15:32 +00: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.
|
2022-12-22 12:57:47 -06:00
|
|
|
///
|
2024-05-23 03:50:43 +02:00
|
|
|
/// See also [`with_no_trimmed_paths!`].
|
2023-11-19 17:26:24 +01:00
|
|
|
// this is pub to be able to intra-doc-link it
|
2023-12-21 12:27:04 +01:00
|
|
|
pub fn trimmed_def_paths(tcx: TyCtxt<'_>, (): ()) -> DefIdMap<Symbol> {
|
2024-01-10 12:47:22 +11:00
|
|
|
// Trimming paths is expensive and not optimized, since we expect it to only be used for error
|
2024-02-12 16:48:45 +11:00
|
|
|
// reporting. Record the fact that we did it, so we can abort if we later found it was
|
|
|
|
// unnecessary.
|
2024-01-10 12:47:22 +11:00
|
|
|
//
|
2024-02-12 16:48:45 +11:00
|
|
|
// The `rustc_middle::ty::print::with_no_trimmed_paths` wrapper can be used to suppress this
|
|
|
|
// checking, in exchange for full paths being formatted.
|
|
|
|
tcx.sess.record_trimmed_def_paths();
|
2020-09-02 10:40:56 +03:00
|
|
|
|
2024-01-10 10:55:08 +11:00
|
|
|
// Once constructed, unique namespace+symbol pairs will have a `Some(_)` entry, while
|
|
|
|
// non-unique pairs will have a `None` entry.
|
2020-09-02 10:40:56 +03:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-01-10 10:55:08 +11:00
|
|
|
// Put the symbol from all the unique namespace+symbol pairs into `map`.
|
2024-01-10 12:47:22 +11:00
|
|
|
let mut map: DefIdMap<Symbol> = Default::default();
|
2020-09-02 10:40:56 +03:00
|
|
|
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.
|
2024-09-11 13:32:53 -04:00
|
|
|
if *v.get() != symbol && v.get().as_str() > symbol.as_str() {
|
|
|
|
v.insert(symbol);
|
2021-09-30 14:25:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Vacant(v) => {
|
|
|
|
v.insert(symbol);
|
|
|
|
}
|
|
|
|
}
|
2020-09-02 10:40:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
map
|
|
|
|
}
|
|
|
|
|
2023-05-15 06:24:45 +02:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
|
|
|
*providers = Providers { trimmed_def_paths, ..*providers };
|
2020-09-02 10:40:56 +03:00
|
|
|
}
|
2021-11-19 20:51:19 -08:00
|
|
|
|
|
|
|
pub struct OpaqueFnEntry<'tcx> {
|
2024-11-11 17:36:18 +00:00
|
|
|
kind: ty::ClosureKind,
|
2022-01-10 23:39:21 +00:00
|
|
|
return_ty: Option<ty::Binder<'tcx, Term<'tcx>>>,
|
2021-11-19 20:51:19 -08:00
|
|
|
}
|