rustc: centralize region printing in ty::RegionKind's Print impl.

This commit is contained in:
Eduard-Mihai Burtescu
2019-01-14 16:26:03 +02:00
parent cafd83d1d5
commit fc914aa88a
4 changed files with 73 additions and 180 deletions

View File

@@ -223,7 +223,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.hir().span_by_hir_id(node), 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)), cm.def_span(self.hir().span_by_hir_id(node)),
), ),
}, },
@@ -1497,7 +1497,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
var_origin: RegionVariableOrigin, var_origin: RegionVariableOrigin,
) -> DiagnosticBuilder<'tcx> { ) -> DiagnosticBuilder<'tcx> {
let br_string = |br: ty::BoundRegion| { 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() { if !s.is_empty() {
s.push_str(" "); s.push_str(" ");
} }

View File

@@ -71,6 +71,13 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
} }
} }
let br_string = |br: ty::BoundRegion| {
match br {
ty::BrNamed(_, name) => format!(" {}", name),
_ => String::new(),
}
};
match *self { match *self {
CyclicTy(_) => write!(f, "cyclic type of infinite size"), CyclicTy(_) => write!(f, "cyclic type of infinite size"),
Mismatch => write!(f, "types differ"), Mismatch => write!(f, "types differ"),
@@ -105,15 +112,13 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
} }
RegionsInsufficientlyPolymorphic(br, _) => { RegionsInsufficientlyPolymorphic(br, _) => {
write!(f, write!(f,
"expected bound lifetime parameter{}{}, found concrete lifetime", "expected bound lifetime parameter{}, found concrete lifetime",
if br.is_named() { " " } else { "" }, br_string(br))
br)
} }
RegionsOverlyPolymorphic(br, _) => { RegionsOverlyPolymorphic(br, _) => {
write!(f, write!(f,
"expected concrete lifetime, found bound lifetime parameter{}{}", "expected concrete lifetime, found bound lifetime parameter{}",
if br.is_named() { " " } else { "" }, br_string(br))
br)
} }
RegionsPlaceholderMismatch => { RegionsPlaceholderMismatch => {
write!(f, "one type is more general than the other") write!(f, "one type is more general than the other")

View File

@@ -136,11 +136,6 @@ impl RegionHighlightMode {
assert!(self.highlight_bound_region.is_none()); assert!(self.highlight_bound_region.is_none());
self.highlight_bound_region = Some((br, number)); 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<usize> {
self.region_highlighted(&ty::RePlaceholder(p))
}
} }
struct LateBoundRegionNameCollector(FxHashSet<InternedString>); struct LateBoundRegionNameCollector(FxHashSet<InternedString>);

View File

@@ -2,7 +2,6 @@ use crate::hir::def::Namespace;
use crate::hir::def_id::DefId; use crate::hir::def_id::DefId;
use crate::middle::region; use crate::middle::region;
use crate::ty::subst::{Kind, Subst, SubstsRef, UnpackedKind}; use crate::ty::subst::{Kind, Subst, SubstsRef, UnpackedKind};
use crate::ty::{BrAnon, BrEnv, BrFresh, BrNamed};
use crate::ty::{Bool, Char, Adt}; use crate::ty::{Bool, Char, Adt};
use crate::ty::{Error, Str, Array, Slice, Float, FnDef, FnPtr}; use crate::ty::{Error, Str, Array, Slice, Float, FnDef, FnPtr};
use crate::ty::{Param, Bound, RawPtr, Ref, Never, Tuple}; use crate::ty::{Param, Bound, RawPtr, Ref, Never, Tuple};
@@ -471,119 +470,34 @@ define_print! {
} }
} }
define_print! { impl fmt::Debug for ty::BoundRegion {
() ty::BoundRegion, (self, cx) { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
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 { match *self {
BrAnon(n) => p!(write("BrAnon({:?})", n)), ty::BrAnon(n) => write!(f, "BrAnon({:?})", n),
BrFresh(n) => p!(write("BrFresh({:?})", n)), ty::BrFresh(n) => write!(f, "BrFresh({:?})", n),
BrNamed(did, name) => { ty::BrNamed(did, name) => {
p!(write("BrNamed({:?}:{:?}, {})", write!(f, "BrNamed({:?}:{:?}, {})",
did.krate, did.index, name)) did.krate, did.index, name)
} }
BrEnv => p!(write("BrEnv")), ty::BrEnv => write!(f, "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<P>(&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;
}
}
false
}
}
define_print! {
() ty::PlaceholderRegion, (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.placeholder_highlight(*self) {
p!(write("'{}", counter));
} else {
p!(print_display(self.name));
}
}
}
}
// 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<P>(&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! { define_print! {
() ty::RegionKind, (self, cx) { () ty::RegionKind, (self, cx) {
display { display {
if cx.config.is_verbose {
return self.print_debug(cx);
}
// Watch out for region highlights. // Watch out for region highlights.
if let Some(n) = cx.printer.region_highlight_mode().region_highlighted(self) { let highlight = cx.printer.region_highlight_mode();
if let Some(n) = highlight.region_highlighted(self) {
p!(write("'{}", n)); p!(write("'{}", n));
return Ok(cx.printer); return Ok(cx.printer);
} }
if cx.config.is_verbose {
return self.print_debug(cx);
}
// These printouts are concise. They do not contain all the information // 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 // 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 // to fit that into a short string. Hence the recommendation to use
@@ -595,11 +509,20 @@ define_print! {
} }
} }
ty::ReLateBound(_, br) | ty::ReLateBound(_, br) |
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) => { ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
p!(print_display(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::RePlaceholder(p) => {
p!(print_display(p))
} }
ty::ReScope(scope) if cx.config.identify_regions => { ty::ReScope(scope) if cx.config.identify_regions => {
match scope.data { match scope.data {
@@ -619,11 +542,9 @@ define_print! {
} }
} }
ty::ReVar(region_vid) if cx.config.identify_regions => { ty::ReVar(region_vid) if cx.config.identify_regions => {
p!(print_debug(region_vid)) p!(write("{:?}", region_vid));
}
ty::ReVar(region_vid) => {
p!(print_display(region_vid))
} }
ty::ReVar(_) => {}
ty::ReScope(_) | ty::ReScope(_) |
ty::ReErased => {} ty::ReErased => {}
ty::ReStatic => p!(write("'static")), ty::ReStatic => p!(write("'static")),
@@ -642,14 +563,11 @@ define_print! {
} }
ty::ReClosureBound(ref vid) => { ty::ReClosureBound(ref vid) => {
p!(write("ReClosureBound({:?})", p!(write("ReClosureBound({:?})", vid))
vid))
} }
ty::ReLateBound(binder_id, ref bound_region) => { ty::ReLateBound(binder_id, ref bound_region) => {
p!(write("ReLateBound({:?}, ", binder_id), p!(write("ReLateBound({:?}, {:?})", binder_id, bound_region))
print_debug(bound_region),
write(")"))
} }
ty::ReFree(ref fr) => p!(print_debug(fr)), ty::ReFree(ref fr) => p!(print_debug(fr)),
@@ -661,11 +579,11 @@ define_print! {
ty::ReStatic => p!(write("ReStatic")), ty::ReStatic => p!(write("ReStatic")),
ty::ReVar(ref vid) => { ty::ReVar(ref vid) => {
p!(print_debug(vid)) p!(write("{:?}", vid));
} }
ty::RePlaceholder(placeholder) => { ty::RePlaceholder(placeholder) => {
p!(write("RePlaceholder("), print_debug(placeholder), write(")")) p!(write("RePlaceholder({:?})", placeholder))
} }
ty::ReEmpty => p!(write("ReEmpty")), ty::ReEmpty => p!(write("ReEmpty")),
@@ -687,11 +605,12 @@ impl ty::RegionKind {
pub(crate) fn display_outputs_anything<P>(&self, cx: &PrintCx<'_, '_, '_, P>) -> bool pub(crate) fn display_outputs_anything<P>(&self, cx: &PrintCx<'_, '_, '_, P>) -> bool
where P: PrettyPrinter where P: PrettyPrinter
{ {
if cx.config.is_verbose { let highlight = cx.printer.region_highlight_mode();
if highlight.region_highlighted(self).is_some() {
return true; return true;
} }
if cx.printer.region_highlight_mode().region_highlighted(self).is_some() { if cx.config.is_verbose {
return true; return true;
} }
@@ -701,17 +620,27 @@ impl ty::RegionKind {
} }
ty::ReLateBound(_, br) | ty::ReLateBound(_, br) |
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) => { ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
br.display_outputs_anything(cx) 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::ReScope(_) |
ty::ReVar(_) if cx.config.identify_regions => true, ty::ReVar(_) if cx.config.identify_regions => true,
ty::ReVar(region_vid) => region_vid.display_outputs_anything(cx), ty::ReVar(_) |
ty::ReScope(_) | ty::ReScope(_) |
ty::ReErased => false, ty::ReErased => false,
@@ -788,48 +717,9 @@ impl fmt::Debug for ty::FloatVid {
} }
} }
define_print! { impl fmt::Debug for ty::RegionVid {
() ty::RegionVid, (self, cx) { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
display { write!(f, "'_#{}r", self.index())
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<P>(&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
} }
} }