From: Eduard-Mihai Burtescu Date: Mon, 14 Jan 2019 14:26:03 +0000 (+0200) Subject: rustc: centralize region printing in ty::RegionKind's Print impl. X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=fc914aa88a525eb6d78d0331fc59fa2d0a60b3b6;p=rust.git rustc: centralize region printing in ty::RegionKind's Print impl. --- diff --git a/src/librustc/infer/error_reporting/mod.rs b/src/librustc/infer/error_reporting/mod.rs index 6b1fea581e6..974b9c59ea4 100644 --- a/src/librustc/infer/error_reporting/mod.rs +++ b/src/librustc/infer/error_reporting/mod.rs @@ -223,7 +223,7 @@ fn msg_span_from_early_bound_and_free_regions( self.hir().span_by_hir_id(node), ), _ => ( - format!("the lifetime {} as defined on", fr.bound_region), + format!("the lifetime {} as defined on", region), cm.def_span(self.hir().span_by_hir_id(node)), ), }, @@ -1497,7 +1497,10 @@ fn report_inference_failure( var_origin: RegionVariableOrigin, ) -> DiagnosticBuilder<'tcx> { let br_string = |br: ty::BoundRegion| { - let mut s = br.to_string(); + let mut s = match br { + ty::BrNamed(_, name) => name.to_string(), + _ => String::new(), + }; if !s.is_empty() { s.push_str(" "); } diff --git a/src/librustc/ty/error.rs b/src/librustc/ty/error.rs index 342a6204d7f..4a3e814cf47 100644 --- a/src/librustc/ty/error.rs +++ b/src/librustc/ty/error.rs @@ -71,6 +71,13 @@ fn report_maybe_different(f: &mut fmt::Formatter<'_>, } } + let br_string = |br: ty::BoundRegion| { + match br { + ty::BrNamed(_, name) => format!(" {}", name), + _ => String::new(), + } + }; + match *self { CyclicTy(_) => write!(f, "cyclic type of infinite size"), Mismatch => write!(f, "types differ"), @@ -105,15 +112,13 @@ fn report_maybe_different(f: &mut fmt::Formatter<'_>, } RegionsInsufficientlyPolymorphic(br, _) => { write!(f, - "expected bound lifetime parameter{}{}, found concrete lifetime", - if br.is_named() { " " } else { "" }, - br) + "expected bound lifetime parameter{}, found concrete lifetime", + br_string(br)) } RegionsOverlyPolymorphic(br, _) => { write!(f, - "expected concrete lifetime, found bound lifetime parameter{}{}", - if br.is_named() { " " } else { "" }, - br) + "expected concrete lifetime, found bound lifetime parameter{}", + br_string(br)) } RegionsPlaceholderMismatch => { write!(f, "one type is more general than the other") diff --git a/src/librustc/ty/print.rs b/src/librustc/ty/print.rs index 28e21b28f7e..4e1fdf657bd 100644 --- a/src/librustc/ty/print.rs +++ b/src/librustc/ty/print.rs @@ -136,11 +136,6 @@ pub fn highlighting_bound_region( assert!(self.highlight_bound_region.is_none()); self.highlight_bound_region = Some((br, number)); } - - /// Returns `Some(N)` if the placeholder `p` is highlighted to print as "`'N`". - pub(crate) fn placeholder_highlight(&self, p: ty::PlaceholderRegion) -> Option { - self.region_highlighted(&ty::RePlaceholder(p)) - } } struct LateBoundRegionNameCollector(FxHashSet); diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index d4111e0375f..1ac6f3fea4c 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -2,7 +2,6 @@ use crate::hir::def_id::DefId; use crate::middle::region; use crate::ty::subst::{Kind, Subst, SubstsRef, UnpackedKind}; -use crate::ty::{BrAnon, BrEnv, BrFresh, BrNamed}; use crate::ty::{Bool, Char, Adt}; use crate::ty::{Error, Str, Array, Slice, Float, FnDef, FnPtr}; use crate::ty::{Param, Bound, RawPtr, Ref, Never, Tuple}; @@ -471,119 +470,34 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { } } -define_print! { - () ty::BoundRegion, (self, cx) { - display { - if cx.config.is_verbose { - return self.print_debug(cx); - } - - if let BrNamed(_, name) = *self { - if name != "" && name != "'_" { - p!(write("{}", name)); - return Ok(cx.printer); - } - } - - let highlight = cx.printer.region_highlight_mode(); - if let Some((region, counter)) = highlight.highlight_bound_region { - if *self == region { - p!(write("'{}", counter)); - } - } - } - debug { - match *self { - BrAnon(n) => p!(write("BrAnon({:?})", n)), - BrFresh(n) => p!(write("BrFresh({:?})", n)), - BrNamed(did, name) => { - p!(write("BrNamed({:?}:{:?}, {})", - did.krate, did.index, name)) - } - BrEnv => p!(write("BrEnv")), - } - } - } -} - -// HACK(eddyb) (see `ty::RegionKind::display_outputs_anything`) -// -// NB: this must be kept in sync with the printing logic above. -impl ty::BoundRegion { - fn display_outputs_anything

(&self, cx: &PrintCx<'_, '_, '_, P>) -> bool - where P: PrettyPrinter - { - if cx.config.is_verbose { - return true; - } - - if let BrNamed(_, name) = *self { - if name != "" && name != "'_" { - return true; - } - } - - let highlight = cx.printer.region_highlight_mode(); - if let Some((region, _)) = highlight.highlight_bound_region { - if *self == region { - return true; +impl fmt::Debug for ty::BoundRegion { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + ty::BrAnon(n) => write!(f, "BrAnon({:?})", n), + ty::BrFresh(n) => write!(f, "BrFresh({:?})", n), + ty::BrNamed(did, name) => { + write!(f, "BrNamed({:?}:{:?}, {})", + did.krate, did.index, name) } + ty::BrEnv => write!(f, "BrEnv"), } - - false } } define_print! { - () ty::PlaceholderRegion, (self, cx) { + () ty::RegionKind, (self, cx) { display { - if cx.config.is_verbose { - return self.print_debug(cx); - } - + // Watch out for region highlights. let highlight = cx.printer.region_highlight_mode(); - if let Some(counter) = highlight.placeholder_highlight(*self) { - p!(write("'{}", counter)); - } else { - p!(print_display(self.name)); + if let Some(n) = highlight.region_highlighted(self) { + p!(write("'{}", n)); + return Ok(cx.printer); } - } - } -} - -// HACK(eddyb) (see `ty::RegionKind::display_outputs_anything`) -// -// NB: this must be kept in sync with the printing logic above. -impl ty::PlaceholderRegion { - fn display_outputs_anything

(&self, cx: &PrintCx<'_, '_, '_, P>) -> bool - where P: PrettyPrinter - { - if cx.config.is_verbose { - return true; - } - - let highlight = cx.printer.region_highlight_mode(); - if highlight.placeholder_highlight(*self).is_some() { - return true; - } - self.name.display_outputs_anything(cx) - } -} - -define_print! { - () ty::RegionKind, (self, cx) { - display { if cx.config.is_verbose { return self.print_debug(cx); } - // Watch out for region highlights. - if let Some(n) = cx.printer.region_highlight_mode().region_highlighted(self) { - p!(write("'{}", n)); - return Ok(cx.printer); - } - // These printouts are concise. They do not contain all the information // the user might want to diagnose an error, but there is basically no way // to fit that into a short string. Hence the recommendation to use @@ -595,11 +509,20 @@ fn display_outputs_anything

(&self, cx: &PrintCx<'_, '_, '_, P>) -> bool } } ty::ReLateBound(_, br) | - ty::ReFree(ty::FreeRegion { bound_region: br, .. }) => { - p!(print_display(br)) - } - ty::RePlaceholder(p) => { - p!(print_display(p)) + ty::ReFree(ty::FreeRegion { bound_region: br, .. }) | + ty::RePlaceholder(ty::Placeholder { name: br, .. }) => { + if let ty::BrNamed(_, name) = br { + if name != "" && name != "'_" { + p!(write("{}", name)); + return Ok(cx.printer); + } + } + + if let Some((region, counter)) = highlight.highlight_bound_region { + if br == region { + p!(write("'{}", counter)); + } + } } ty::ReScope(scope) if cx.config.identify_regions => { match scope.data { @@ -619,11 +542,9 @@ fn display_outputs_anything

(&self, cx: &PrintCx<'_, '_, '_, P>) -> bool } } ty::ReVar(region_vid) if cx.config.identify_regions => { - p!(print_debug(region_vid)) - } - ty::ReVar(region_vid) => { - p!(print_display(region_vid)) + p!(write("{:?}", region_vid)); } + ty::ReVar(_) => {} ty::ReScope(_) | ty::ReErased => {} ty::ReStatic => p!(write("'static")), @@ -642,14 +563,11 @@ fn display_outputs_anything

(&self, cx: &PrintCx<'_, '_, '_, P>) -> bool } ty::ReClosureBound(ref vid) => { - p!(write("ReClosureBound({:?})", - vid)) + p!(write("ReClosureBound({:?})", vid)) } ty::ReLateBound(binder_id, ref bound_region) => { - p!(write("ReLateBound({:?}, ", binder_id), - print_debug(bound_region), - write(")")) + p!(write("ReLateBound({:?}, {:?})", binder_id, bound_region)) } ty::ReFree(ref fr) => p!(print_debug(fr)), @@ -661,11 +579,11 @@ fn display_outputs_anything

(&self, cx: &PrintCx<'_, '_, '_, P>) -> bool ty::ReStatic => p!(write("ReStatic")), ty::ReVar(ref vid) => { - p!(print_debug(vid)) + p!(write("{:?}", vid)); } ty::RePlaceholder(placeholder) => { - p!(write("RePlaceholder("), print_debug(placeholder), write(")")) + p!(write("RePlaceholder({:?})", placeholder)) } ty::ReEmpty => p!(write("ReEmpty")), @@ -687,11 +605,12 @@ impl ty::RegionKind { pub(crate) fn display_outputs_anything

(&self, cx: &PrintCx<'_, '_, '_, P>) -> bool where P: PrettyPrinter { - if cx.config.is_verbose { + let highlight = cx.printer.region_highlight_mode(); + if highlight.region_highlighted(self).is_some() { return true; } - if cx.printer.region_highlight_mode().region_highlighted(self).is_some() { + if cx.config.is_verbose { return true; } @@ -701,17 +620,27 @@ pub(crate) fn display_outputs_anything

(&self, cx: &PrintCx<'_, '_, '_, P>) -> } ty::ReLateBound(_, br) | - ty::ReFree(ty::FreeRegion { bound_region: br, .. }) => { - br.display_outputs_anything(cx) - } + ty::ReFree(ty::FreeRegion { bound_region: br, .. }) | + ty::RePlaceholder(ty::Placeholder { name: br, .. }) => { + if let ty::BrNamed(_, name) = br { + if name != "" && name != "'_" { + return true; + } + } - ty::RePlaceholder(p) => p.display_outputs_anything(cx), + if let Some((region, _)) = highlight.highlight_bound_region { + if br == region { + return true; + } + } + + false + } ty::ReScope(_) | ty::ReVar(_) if cx.config.identify_regions => true, - ty::ReVar(region_vid) => region_vid.display_outputs_anything(cx), - + ty::ReVar(_) | ty::ReScope(_) | ty::ReErased => false, @@ -788,48 +717,9 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { } } -define_print! { - () ty::RegionVid, (self, cx) { - display { - if cx.config.is_verbose { - return self.print_debug(cx); - } - - let highlight = cx.printer.region_highlight_mode(); - if let Some(counter) = highlight.region_highlighted(&ty::ReVar(*self)) { - p!(write("'{}", counter)); - } - } - debug { - // HACK(eddyb) this is duplicated from `display` printing, - // to keep NLL borrowck working even with `-Zverbose`. - let highlight = cx.printer.region_highlight_mode(); - if let Some(counter) = highlight.region_highlighted(&ty::ReVar(*self)) { - p!(write("'{}", counter)); - } else { - p!(write("'_#{}r", self.index())); - } - } - } -} - -// HACK(eddyb) (see `ty::RegionKind::display_outputs_anything`) -// -// NB: this must be kept in sync with the printing logic above. -impl ty::RegionVid { - fn display_outputs_anything

(&self, cx: &PrintCx<'_, '_, '_, P>) -> bool - where P: PrettyPrinter - { - if cx.config.is_verbose { - return true; - } - - let highlight = cx.printer.region_highlight_mode(); - if highlight.region_highlighted(&ty::ReVar(*self)).is_some() { - return true; - } - - false +impl fmt::Debug for ty::RegionVid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "'_#{}r", self.index()) } }