diff options
| author | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2019-01-10 13:26:35 +0200 |
|---|---|---|
| committer | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2019-03-15 13:25:10 +0200 |
| commit | 37e918526a7107bc3c8de48f9a535b0100da40f5 (patch) | |
| tree | 579d87126d04c47e8e00cadca7286204c937456a | |
| parent | 5616ca857dd05e5e62b4bfcd11bd1ea0f2e22f5e (diff) | |
| download | rust-37e918526a7107bc3c8de48f9a535b0100da40f5.tar.gz rust-37e918526a7107bc3c8de48f9a535b0100da40f5.zip | |
rustc: split off most of ty::print::PrintCx's fields into a separate struct.
| -rw-r--r-- | src/librustc/infer/error_reporting/mod.rs | 5 | ||||
| -rw-r--r-- | src/librustc/mir/mod.rs | 4 | ||||
| -rw-r--r-- | src/librustc/ty/print.rs | 69 | ||||
| -rw-r--r-- | src/librustc/util/ppaux.rs | 61 | ||||
| -rw-r--r-- | src/librustc_codegen_utils/symbol_names.rs | 7 | ||||
| -rw-r--r-- | src/librustdoc/clean/mod.rs | 6 |
6 files changed, 86 insertions, 66 deletions
diff --git a/src/librustc/infer/error_reporting/mod.rs b/src/librustc/infer/error_reporting/mod.rs index b777b0dc9f3..68b2d201fb0 100644 --- a/src/librustc/infer/error_reporting/mod.rs +++ b/src/librustc/infer/error_reporting/mod.rs @@ -498,8 +498,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { // module we could have false positives if !(did1.is_local() || did2.is_local()) && did1.krate != did2.krate { let abs_path = |def_id| { - PrintCx::new(self.tcx, AbsolutePathPrinter) - .print_def_path(def_id, None, Namespace::TypeNS, iter::empty()) + PrintCx::with(self.tcx, AbsolutePathPrinter, |mut cx| { + cx.print_def_path(def_id, None, Namespace::TypeNS, iter::empty()) + }) }; // We compare strings because DefPath can be different diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index ea3668cec15..7ee8eec11ee 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -2369,8 +2369,8 @@ impl<'tcx> Debug for Rvalue<'tcx> { }; // When printing regions, add trailing space if necessary. - ty::print::PrintCx::with(ty::print::FmtPrinter { fmt }, |cx| { - let region = if cx.is_verbose || cx.identify_regions { + ty::print::PrintCx::with_tls_tcx(ty::print::FmtPrinter { fmt }, |cx| { + let region = if cx.config.is_verbose || cx.config.identify_regions { let mut region = region.to_string(); if region.len() > 0 { region.push(' '); diff --git a/src/librustc/ty/print.rs b/src/librustc/ty/print.rs index f179619b5f8..d385d183e2b 100644 --- a/src/librustc/ty/print.rs +++ b/src/librustc/ty/print.rs @@ -60,9 +60,7 @@ impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector { } } -pub struct PrintCx<'a, 'gcx, 'tcx, P> { - pub tcx: TyCtxt<'a, 'gcx, 'tcx>, - pub printer: P, +pub(crate) struct PrintConfig { pub(crate) is_debug: bool, pub(crate) is_verbose: bool, pub(crate) identify_regions: bool, @@ -71,6 +69,25 @@ pub struct PrintCx<'a, 'gcx, 'tcx, P> { pub(crate) binder_depth: usize, } +impl PrintConfig { + pub(crate) fn new(tcx: TyCtxt<'_, '_, '_>) -> Self { + PrintConfig { + is_debug: false, + is_verbose: tcx.sess.verbose(), + identify_regions: tcx.sess.opts.debugging_opts.identify_regions, + used_region_names: None, + region_index: 0, + binder_depth: 0, + } + } +} + +pub struct PrintCx<'a, 'gcx, 'tcx, P> { + pub tcx: TyCtxt<'a, 'gcx, 'tcx>, + pub printer: P, + pub(crate) config: &'a mut PrintConfig, +} + // HACK(eddyb) this is solely for `self: &mut PrintCx<Self>`, e.g. to // implement traits on the printer and call the methods on the context. impl<P> Deref for PrintCx<'_, '_, '_, P> { @@ -80,30 +97,29 @@ impl<P> Deref for PrintCx<'_, '_, '_, P> { } } -impl<P> PrintCx<'a, 'gcx, 'tcx, P> { - pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>, printer: P) -> Self { - PrintCx { +impl<'a, 'gcx, 'tcx, P> PrintCx<'a, 'gcx, 'tcx, P> { + pub fn with<R>( + tcx: TyCtxt<'a, 'gcx, 'tcx>, + printer: P, + f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, P>) -> R, + ) -> R { + f(PrintCx { tcx, printer, - is_debug: false, - is_verbose: tcx.sess.verbose(), - identify_regions: tcx.sess.opts.debugging_opts.identify_regions, - used_region_names: None, - region_index: 0, - binder_depth: 0, - } + config: &mut PrintConfig::new(tcx), + }) } - pub(crate) fn with<R>(printer: P, f: impl FnOnce(PrintCx<'_, '_, '_, P>) -> R) -> R { - ty::tls::with(|tcx| f(PrintCx::new(tcx, printer))) + pub(crate) fn with_tls_tcx<R>(printer: P, f: impl FnOnce(PrintCx<'_, '_, '_, P>) -> R) -> R { + ty::tls::with(|tcx| PrintCx::with(tcx, printer, f)) } pub(crate) fn prepare_late_bound_region_info<T>(&mut self, value: &ty::Binder<T>) where T: TypeFoldable<'tcx> { let mut collector = LateBoundRegionNameCollector(Default::default()); value.visit_with(&mut collector); - self.used_region_names = Some(collector.0); - self.region_index = 0; + self.config.used_region_names = Some(collector.0); + self.config.region_index = 0; } } @@ -116,17 +132,17 @@ pub trait Print<'tcx, P> { &self, cx: &mut PrintCx<'_, '_, 'tcx, P>, ) -> Result<Self::Output, Self::Error> { - let old_debug = cx.is_debug; - cx.is_debug = false; + let old_debug = cx.config.is_debug; + cx.config.is_debug = false; let result = self.print(cx); - cx.is_debug = old_debug; + cx.config.is_debug = old_debug; result } fn print_debug(&self, cx: &mut PrintCx<'_, '_, 'tcx, P>) -> Result<Self::Output, Self::Error> { - let old_debug = cx.is_debug; - cx.is_debug = true; + let old_debug = cx.config.is_debug; + cx.config.is_debug = true; let result = self.print(cx); - cx.is_debug = old_debug; + cx.config.is_debug = old_debug; result } } @@ -215,8 +231,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { let ns = self.guess_def_namespace(def_id); debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns); let mut s = String::new(); - let _ = PrintCx::new(self, FmtPrinter { fmt: &mut s }) - .print_def_path(def_id, None, ns, iter::empty()); + let _ = PrintCx::with(self, FmtPrinter { fmt: &mut s }, |mut cx| { + cx.print_def_path(def_id, None, ns, iter::empty()) + }); s } } @@ -613,7 +630,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> { }); // Don't print args that are the defaults of their respective parameters. - let num_supplied_defaults = if self.is_verbose { + let num_supplied_defaults = if self.config.is_verbose { 0 } else { params.iter().rev().take_while(|param| { diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index d0d51e7f38c..90300ecde2e 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -161,7 +161,7 @@ impl RegionHighlightMode { macro_rules! gen_display_debug_body { ( $with:path ) => { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - PrintCx::with(FmtPrinter { fmt: f }, |mut cx| { + PrintCx::with_tls_tcx(FmtPrinter { fmt: f }, |mut cx| { $with(&cx.tcx.lift(self).expect("could not lift for printing"), &mut cx) }) } @@ -197,7 +197,7 @@ macro_rules! gen_print_impl { type Error = fmt::Error; fn print(&$self, $cx: &mut PrintCx<'_, '_, 'tcx, P>) -> fmt::Result { define_scoped_cx!($cx); - if $cx.is_debug $dbg + if $cx.config.is_debug $dbg else $disp } } @@ -208,7 +208,7 @@ macro_rules! gen_print_impl { type Error = fmt::Error; fn print(&$self, $cx: &mut PrintCx<'_, '_, 'tcx, P>) -> fmt::Result { define_scoped_cx!($cx); - if $cx.is_debug $dbg + if $cx.config.is_debug $dbg else $disp } } @@ -316,7 +316,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> { // clearly differentiate between named and unnamed regions in // the output. We'll probably want to tweak this over time to // decide just how much information to give. - if self.binder_depth == 0 { + if self.config.binder_depth == 0 { self.prepare_late_bound_region_info(value); } @@ -337,7 +337,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> { // is disallowed (name resolution thinks `scoped_cx!` is ambiguous). define_scoped_cx!(self); - let old_region_index = self.region_index; + let old_region_index = self.config.region_index; let mut region_index = old_region_index; let new_value = self.tcx.replace_late_bound_regions(value, |br| { let _ = start_or_continue(self, "for<", ", "); @@ -365,16 +365,16 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> { start_or_continue(self, "", "> ")?; // Push current state to gcx, and restore after writing new_value. - self.binder_depth += 1; - self.region_index = region_index; + self.config.binder_depth += 1; + self.config.region_index = region_index; let result = new_value.print_display(self); - self.region_index = old_region_index; - self.binder_depth -= 1; + self.config.region_index = old_region_index; + self.config.binder_depth -= 1; result } fn is_name_used(&self, name: &InternedString) -> bool { - match self.used_region_names { + match self.config.used_region_names { Some(ref names) => names.contains(name), None => false, } @@ -387,7 +387,7 @@ pub fn parameterized<F: fmt::Write>( substs: SubstsRef<'_>, ns: Namespace, ) -> fmt::Result { - PrintCx::with(FmtPrinter { fmt: f }, |mut cx| { + PrintCx::with_tls_tcx(FmtPrinter { fmt: f }, |mut cx| { let substs = cx.tcx.lift(&substs).expect("could not lift for printing"); let _ = cx.print_def_path(did, Some(substs), ns, iter::empty())?; Ok(()) @@ -404,8 +404,9 @@ define_print! { let mut resugared_principal = false; // Special-case `Fn(...) -> ...` and resugar it. - if !cx.is_verbose && cx.tcx.lang_items().fn_trait_kind(principal.def_id).is_some() { - if let Tuple(ref args) = principal.substs.type_at(0).sty { + let fn_trait_kind = cx.tcx.lang_items().fn_trait_kind(principal.def_id); + if !cx.config.is_verbose && fn_trait_kind.is_some() { + if let ty::Tuple(ref args) = principal.substs.type_at(0).sty { let mut projections = self.projection_bounds(); if let (Some(proj), None) = (projections.next(), projections.next()) { let _ = cx.print_def_path( @@ -486,7 +487,7 @@ impl fmt::Debug for ty::GenericParamDef { impl fmt::Debug for ty::TraitDef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - PrintCx::with(FmtPrinter { fmt: f }, |mut cx| { + PrintCx::with_tls_tcx(FmtPrinter { fmt: f }, |mut cx| { let _ = cx.print_def_path( self.def_id, None, @@ -500,7 +501,7 @@ impl fmt::Debug for ty::TraitDef { impl fmt::Debug for ty::AdtDef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - PrintCx::with(FmtPrinter { fmt: f }, |mut cx| { + PrintCx::with_tls_tcx(FmtPrinter { fmt: f }, |mut cx| { let _ = cx.print_def_path( self.did, None, @@ -522,7 +523,7 @@ impl<'tcx> fmt::Debug for ty::ClosureUpvar<'tcx> { impl fmt::Debug for ty::UpvarId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - PrintCx::with(FmtPrinter { fmt: f }, |mut cx| { + PrintCx::with_tls_tcx(FmtPrinter { fmt: f }, |mut cx| { define_scoped_cx!(cx); p!(write("UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, @@ -592,7 +593,7 @@ define_print! { define_print! { () ty::BoundRegion, (self, cx) { display { - if cx.is_verbose { + if cx.config.is_verbose { return self.print_debug(cx); } @@ -630,7 +631,7 @@ define_print! { // NB: this must be kept in sync with the printing logic above. impl ty::BoundRegion { fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool { - if cx.is_verbose { + if cx.config.is_verbose { return true; } @@ -654,7 +655,7 @@ impl ty::BoundRegion { define_print! { () ty::PlaceholderRegion, (self, cx) { display { - if cx.is_verbose { + if cx.config.is_verbose { return self.print_debug(cx); } @@ -673,7 +674,7 @@ define_print! { // NB: this must be kept in sync with the printing logic above. impl ty::PlaceholderRegion { fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool { - if cx.is_verbose { + if cx.config.is_verbose { return true; } @@ -689,7 +690,7 @@ impl ty::PlaceholderRegion { define_print! { () ty::RegionKind, (self, cx) { display { - if cx.is_verbose { + if cx.config.is_verbose { return self.print_debug(cx); } @@ -717,7 +718,7 @@ define_print! { ty::RePlaceholder(p) => { p!(print_display(p)) } - ty::ReScope(scope) if cx.identify_regions => { + ty::ReScope(scope) if cx.config.identify_regions => { match scope.data { region::ScopeData::Node => p!(write("'{}s", scope.item_local_id().as_usize())), @@ -734,7 +735,7 @@ define_print! { )), } } - ty::ReVar(region_vid) if cx.identify_regions => { + ty::ReVar(region_vid) if cx.config.identify_regions => { p!(print_debug(region_vid)) } ty::ReVar(region_vid) => { @@ -801,7 +802,7 @@ define_print! { impl ty::RegionKind { // HACK(eddyb) `pub(crate)` only for `ty::print`. pub(crate) fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool { - if cx.is_verbose { + if cx.config.is_verbose { return true; } @@ -822,7 +823,7 @@ impl ty::RegionKind { ty::RePlaceholder(p) => p.display_outputs_anything(cx), ty::ReScope(_) | - ty::ReVar(_) if cx.identify_regions => true, + ty::ReVar(_) if cx.config.identify_regions => true, ty::ReVar(region_vid) => region_vid.display_outputs_anything(cx), @@ -905,7 +906,7 @@ impl fmt::Debug for ty::FloatVid { define_print! { () ty::RegionVid, (self, cx) { display { - if cx.is_verbose { + if cx.config.is_verbose { return self.print_debug(cx); } @@ -934,7 +935,7 @@ define_print! { // NB: this must be kept in sync with the printing logic above. impl ty::RegionVid { fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool { - if cx.is_verbose { + if cx.config.is_verbose { return true; } @@ -950,7 +951,7 @@ impl ty::RegionVid { define_print! { () ty::InferTy, (self, cx) { display { - if cx.is_verbose { + if cx.config.is_verbose { return self.print_debug(cx); } match *self { @@ -997,7 +998,7 @@ impl fmt::Debug for ty::FloatVarValue { for<'a> <T as ty::Lift<'a>>::Lifted: fmt::Display + TypeFoldable<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - PrintCx::with(|cx| cx.in_binder(cx.tcx.lift(self) + PrintCx::with_tls_tcx(|cx| cx.in_binder(cx.tcx.lift(self) .expect("could not lift for printing"))) } }*/ @@ -1146,7 +1147,7 @@ define_print! { p!(write("Placeholder({:?})", placeholder)) } Opaque(def_id, substs) => { - if cx.is_verbose { + if cx.config.is_verbose { return p!(write("Opaque({:?}, {:?})", def_id, substs)); } diff --git a/src/librustc_codegen_utils/symbol_names.rs b/src/librustc_codegen_utils/symbol_names.rs index c73c5950c0d..8f4b1d1638a 100644 --- a/src/librustc_codegen_utils/symbol_names.rs +++ b/src/librustc_codegen_utils/symbol_names.rs @@ -225,9 +225,10 @@ fn get_symbol_hash<'a, 'tcx>( } fn def_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> ty::SymbolName { - let mut cx = PrintCx::new(tcx, SymbolPath::new(tcx)); - let _ = cx.print_def_path(def_id, None, Namespace::ValueNS, iter::empty()); - cx.printer.into_interned() + PrintCx::with(tcx, SymbolPath::new(tcx), |mut cx| { + let _ = cx.print_def_path(def_id, None, Namespace::ValueNS, iter::empty()); + cx.printer.into_interned() + }) } fn symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance<'tcx>) -> ty::SymbolName { diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 2a8db6455bf..ffdf47db547 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -4278,9 +4278,9 @@ where F: Fn(DefId) -> Def { } } - let names = PrintCx::new(tcx, AbsolutePathPrinter) - .print_def_path(def_id, None, Namespace::TypeNS, iter::empty()) - .unwrap(); + let names = PrintCx::with(tcx, AbsolutePathPrinter, |mut cx| { + cx.print_def_path(def_id, None, Namespace::TypeNS, iter::empty()).unwrap() + }); hir::Path { span: DUMMY_SP, |
