]> git.lizzy.rs Git - rust.git/commitdiff
rustc: split off most of ty::print::PrintCx's fields into a separate struct.
authorEduard-Mihai Burtescu <edy.burt@gmail.com>
Thu, 10 Jan 2019 11:26:35 +0000 (13:26 +0200)
committerEduard-Mihai Burtescu <edy.burt@gmail.com>
Fri, 15 Mar 2019 11:25:10 +0000 (13:25 +0200)
src/librustc/infer/error_reporting/mod.rs
src/librustc/mir/mod.rs
src/librustc/ty/print.rs
src/librustc/util/ppaux.rs
src/librustc_codegen_utils/symbol_names.rs
src/librustdoc/clean/mod.rs

index b777b0dc9f37f44617086987c4ecb8b57a4becd9..68b2d201fb00a071b8cf4623d6c15452add9253d 100644 (file)
@@ -498,8 +498,9 @@ fn path_generic_args<'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
index ea3668cec1544128d957d71f6dc6b18c25a9411e..7ee8eec11ee42002a6267e014378bf142bdc1784 100644 (file)
@@ -2369,8 +2369,8 @@ fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
                 };
 
                 // 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(' ');
index f179619b5f83cea6bc00dfe9dfdfc0a8f78dc532..d385d183e2b32ee18c1f6520176c051792b2dcee 100644 (file)
@@ -60,9 +60,7 @@ fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
     }
 }
 
-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 @@ fn deref(&self) -> &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 @@ fn print_display(
         &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 @@ pub fn def_path_str(self, def_id: DefId) -> String {
         let ns = self.guess_def_namespace(def_id);
         debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns);
         let mut s = String::new();
-        let _ = 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 @@ pub fn pretty_path_generic_args(
         });
 
         // 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| {
index d0d51e7f38c147741771ef4e4c92dee4767d0097..90300ecde2e5ce0f900912bab97cefd360e66652 100644 (file)
@@ -161,7 +161,7 @@ pub fn placeholder_highlight(&self, p: ty::PlaceholderRegion) -> Option<usize> {
 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 @@ impl<$($x)+, P: PrettyPrinter> Print<'tcx, P> for $target {
             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 @@ impl<P: PrettyPrinter> Print<'tcx, P> for $target {
             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 @@ fn name_by_region_index(index: usize) -> InternedString {
         // 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 @@ fn name_by_region_index(index: usize) -> InternedString {
         // 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 @@ fn name_by_region_index(index: usize) -> InternedString {
         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 @@ pub fn parameterized<F: fmt::Write>(
                 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 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 
 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 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 
 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 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 
 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 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 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 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 // 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 @@ fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool {
 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 @@ fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool {
 // 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 @@ fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool {
 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 @@ fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool {
                 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 @@ fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool {
                         )),
                     }
                 }
-                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 @@ fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool {
 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 @@ pub(crate) fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>
             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 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 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 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 // 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 @@ fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool {
 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 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
           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 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                     p!(write("Placeholder({:?})", placeholder))
                 }
                 Opaque(def_id, substs) => {
-                    if cx.is_verbose {
+                    if cx.config.is_verbose {
                         return p!(write("Opaque({:?}, {:?})", def_id, substs));
                     }
 
index c73c5950c0da515fcdea6cf584e5614eb7fad73f..8f4b1d1638a80e383768d4c067f28c63e4dfe6fc 100644 (file)
@@ -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 {
index 2a8db6455bf228717af2b95fd3980b9c04712a45..ffdf47db54723bdfea2f4b03ad664e4477a627d8 100644 (file)
@@ -4278,9 +4278,9 @@ fn path_generic_args(
         }
     }
 
-    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,