1 use crate::ty::{self, TyCtxt, TypeFoldable};
3 use rustc_data_structures::fx::FxHashSet;
4 use syntax::symbol::InternedString;
8 // FIXME(eddyb) this module uses `pub(crate)` for things used only
9 // from `ppaux` - when that is removed, they can be re-privatized.
11 struct LateBoundRegionNameCollector(FxHashSet<InternedString>);
12 impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector {
13 fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
15 ty::ReLateBound(_, ty::BrNamed(_, name)) => {
20 r.super_visit_with(self)
24 pub struct PrintCx<'a, 'gcx, 'tcx> {
25 pub(crate) tcx: TyCtxt<'a, 'gcx, 'tcx>,
26 pub(crate) is_debug: bool,
27 pub(crate) is_verbose: bool,
28 pub(crate) identify_regions: bool,
29 pub(crate) used_region_names: Option<FxHashSet<InternedString>>,
30 pub(crate) region_index: usize,
31 pub(crate) binder_depth: usize,
34 impl PrintCx<'a, 'gcx, 'tcx> {
35 pub(crate) fn with<R>(f: impl FnOnce(PrintCx<'_, '_, '_>) -> R) -> R {
40 is_verbose: tcx.sess.verbose(),
41 identify_regions: tcx.sess.opts.debugging_opts.identify_regions,
42 used_region_names: None,
48 pub(crate) fn prepare_late_bound_region_info<T>(&mut self, value: &ty::Binder<T>)
49 where T: TypeFoldable<'tcx>
51 let mut collector = LateBoundRegionNameCollector(Default::default());
52 value.visit_with(&mut collector);
53 self.used_region_names = Some(collector.0);
54 self.region_index = 0;
58 pub trait Print<'tcx> {
59 fn print<F: fmt::Write>(&self, f: &mut F, cx: &mut PrintCx<'_, '_, '_>) -> fmt::Result;
60 fn print_to_string(&self, cx: &mut PrintCx<'_, '_, '_>) -> String {
61 let mut result = String::new();
62 let _ = self.print(&mut result, cx);
65 fn print_display<F: fmt::Write>(&self, f: &mut F, cx: &mut PrintCx<'_, '_, '_>) -> fmt::Result {
66 let old_debug = cx.is_debug;
68 let result = self.print(f, cx);
69 cx.is_debug = old_debug;
72 fn print_display_to_string(&self, cx: &mut PrintCx<'_, '_, '_>) -> String {
73 let mut result = String::new();
74 let _ = self.print_display(&mut result, cx);
77 fn print_debug<F: fmt::Write>(&self, f: &mut F, cx: &mut PrintCx<'_, '_, '_>) -> fmt::Result {
78 let old_debug = cx.is_debug;
80 let result = self.print(f, cx);
81 cx.is_debug = old_debug;
84 fn print_debug_to_string(&self, cx: &mut PrintCx<'_, '_, '_>) -> String {
85 let mut result = String::new();
86 let _ = self.print_debug(&mut result, cx);