Skip to content

Commit fc914aa

Browse files
committed
rustc: centralize region printing in ty::RegionKind's Print impl.
1 parent cafd83d commit fc914aa

File tree

4 files changed

+72
-179
lines changed

4 files changed

+72
-179
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
223223
self.hir().span_by_hir_id(node),
224224
),
225225
_ => (
226-
format!("the lifetime {} as defined on", fr.bound_region),
226+
format!("the lifetime {} as defined on", region),
227227
cm.def_span(self.hir().span_by_hir_id(node)),
228228
),
229229
},
@@ -1497,7 +1497,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
14971497
var_origin: RegionVariableOrigin,
14981498
) -> DiagnosticBuilder<'tcx> {
14991499
let br_string = |br: ty::BoundRegion| {
1500-
let mut s = br.to_string();
1500+
let mut s = match br {
1501+
ty::BrNamed(_, name) => name.to_string(),
1502+
_ => String::new(),
1503+
};
15011504
if !s.is_empty() {
15021505
s.push_str(" ");
15031506
}

src/librustc/ty/error.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
7171
}
7272
}
7373

74+
let br_string = |br: ty::BoundRegion| {
75+
match br {
76+
ty::BrNamed(_, name) => format!(" {}", name),
77+
_ => String::new(),
78+
}
79+
};
80+
7481
match *self {
7582
CyclicTy(_) => write!(f, "cyclic type of infinite size"),
7683
Mismatch => write!(f, "types differ"),
@@ -105,15 +112,13 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
105112
}
106113
RegionsInsufficientlyPolymorphic(br, _) => {
107114
write!(f,
108-
"expected bound lifetime parameter{}{}, found concrete lifetime",
109-
if br.is_named() { " " } else { "" },
110-
br)
115+
"expected bound lifetime parameter{}, found concrete lifetime",
116+
br_string(br))
111117
}
112118
RegionsOverlyPolymorphic(br, _) => {
113119
write!(f,
114-
"expected concrete lifetime, found bound lifetime parameter{}{}",
115-
if br.is_named() { " " } else { "" },
116-
br)
120+
"expected concrete lifetime, found bound lifetime parameter{}",
121+
br_string(br))
117122
}
118123
RegionsPlaceholderMismatch => {
119124
write!(f, "one type is more general than the other")

src/librustc/ty/print.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,6 @@ impl RegionHighlightMode {
136136
assert!(self.highlight_bound_region.is_none());
137137
self.highlight_bound_region = Some((br, number));
138138
}
139-
140-
/// Returns `Some(N)` if the placeholder `p` is highlighted to print as "`'N`".
141-
pub(crate) fn placeholder_highlight(&self, p: ty::PlaceholderRegion) -> Option<usize> {
142-
self.region_highlighted(&ty::RePlaceholder(p))
143-
}
144139
}
145140

146141
struct LateBoundRegionNameCollector(FxHashSet<InternedString>);

src/librustc/util/ppaux.rs

Lines changed: 56 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::hir::def::Namespace;
22
use crate::hir::def_id::DefId;
33
use crate::middle::region;
44
use crate::ty::subst::{Kind, Subst, SubstsRef, UnpackedKind};
5-
use crate::ty::{BrAnon, BrEnv, BrFresh, BrNamed};
65
use crate::ty::{Bool, Char, Adt};
76
use crate::ty::{Error, Str, Array, Slice, Float, FnDef, FnPtr};
87
use crate::ty::{Param, Bound, RawPtr, Ref, Never, Tuple};
@@ -471,119 +470,34 @@ define_print! {
471470
}
472471
}
473472

474-
define_print! {
475-
() ty::BoundRegion, (self, cx) {
476-
display {
477-
if cx.config.is_verbose {
478-
return self.print_debug(cx);
479-
}
480-
481-
if let BrNamed(_, name) = *self {
482-
if name != "" && name != "'_" {
483-
p!(write("{}", name));
484-
return Ok(cx.printer);
485-
}
486-
}
487-
488-
let highlight = cx.printer.region_highlight_mode();
489-
if let Some((region, counter)) = highlight.highlight_bound_region {
490-
if *self == region {
491-
p!(write("'{}", counter));
492-
}
493-
}
494-
}
495-
debug {
496-
match *self {
497-
BrAnon(n) => p!(write("BrAnon({:?})", n)),
498-
BrFresh(n) => p!(write("BrFresh({:?})", n)),
499-
BrNamed(did, name) => {
500-
p!(write("BrNamed({:?}:{:?}, {})",
501-
did.krate, did.index, name))
502-
}
503-
BrEnv => p!(write("BrEnv")),
504-
}
505-
}
506-
}
507-
}
508-
509-
// HACK(eddyb) (see `ty::RegionKind::display_outputs_anything`)
510-
//
511-
// NB: this must be kept in sync with the printing logic above.
512-
impl ty::BoundRegion {
513-
fn display_outputs_anything<P>(&self, cx: &PrintCx<'_, '_, '_, P>) -> bool
514-
where P: PrettyPrinter
515-
{
516-
if cx.config.is_verbose {
517-
return true;
518-
}
519-
520-
if let BrNamed(_, name) = *self {
521-
if name != "" && name != "'_" {
522-
return true;
523-
}
524-
}
525-
526-
let highlight = cx.printer.region_highlight_mode();
527-
if let Some((region, _)) = highlight.highlight_bound_region {
528-
if *self == region {
529-
return true;
473+
impl fmt::Debug for ty::BoundRegion {
474+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
475+
match *self {
476+
ty::BrAnon(n) => write!(f, "BrAnon({:?})", n),
477+
ty::BrFresh(n) => write!(f, "BrFresh({:?})", n),
478+
ty::BrNamed(did, name) => {
479+
write!(f, "BrNamed({:?}:{:?}, {})",
480+
did.krate, did.index, name)
530481
}
482+
ty::BrEnv => write!(f, "BrEnv"),
531483
}
532-
533-
false
534484
}
535485
}
536486

537487
define_print! {
538-
() ty::PlaceholderRegion, (self, cx) {
488+
() ty::RegionKind, (self, cx) {
539489
display {
540-
if cx.config.is_verbose {
541-
return self.print_debug(cx);
542-
}
543-
490+
// Watch out for region highlights.
544491
let highlight = cx.printer.region_highlight_mode();
545-
if let Some(counter) = highlight.placeholder_highlight(*self) {
546-
p!(write("'{}", counter));
547-
} else {
548-
p!(print_display(self.name));
492+
if let Some(n) = highlight.region_highlighted(self) {
493+
p!(write("'{}", n));
494+
return Ok(cx.printer);
549495
}
550-
}
551-
}
552-
}
553-
554-
// HACK(eddyb) (see `ty::RegionKind::display_outputs_anything`)
555-
//
556-
// NB: this must be kept in sync with the printing logic above.
557-
impl ty::PlaceholderRegion {
558-
fn display_outputs_anything<P>(&self, cx: &PrintCx<'_, '_, '_, P>) -> bool
559-
where P: PrettyPrinter
560-
{
561-
if cx.config.is_verbose {
562-
return true;
563-
}
564-
565-
let highlight = cx.printer.region_highlight_mode();
566-
if highlight.placeholder_highlight(*self).is_some() {
567-
return true;
568-
}
569496

570-
self.name.display_outputs_anything(cx)
571-
}
572-
}
573-
574-
define_print! {
575-
() ty::RegionKind, (self, cx) {
576-
display {
577497
if cx.config.is_verbose {
578498
return self.print_debug(cx);
579499
}
580500

581-
// Watch out for region highlights.
582-
if let Some(n) = cx.printer.region_highlight_mode().region_highlighted(self) {
583-
p!(write("'{}", n));
584-
return Ok(cx.printer);
585-
}
586-
587501
// These printouts are concise. They do not contain all the information
588502
// the user might want to diagnose an error, but there is basically no way
589503
// to fit that into a short string. Hence the recommendation to use
@@ -595,11 +509,20 @@ define_print! {
595509
}
596510
}
597511
ty::ReLateBound(_, br) |
598-
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) => {
599-
p!(print_display(br))
600-
}
601-
ty::RePlaceholder(p) => {
602-
p!(print_display(p))
512+
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
513+
ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
514+
if let ty::BrNamed(_, name) = br {
515+
if name != "" && name != "'_" {
516+
p!(write("{}", name));
517+
return Ok(cx.printer);
518+
}
519+
}
520+
521+
if let Some((region, counter)) = highlight.highlight_bound_region {
522+
if br == region {
523+
p!(write("'{}", counter));
524+
}
525+
}
603526
}
604527
ty::ReScope(scope) if cx.config.identify_regions => {
605528
match scope.data {
@@ -619,11 +542,9 @@ define_print! {
619542
}
620543
}
621544
ty::ReVar(region_vid) if cx.config.identify_regions => {
622-
p!(print_debug(region_vid))
623-
}
624-
ty::ReVar(region_vid) => {
625-
p!(print_display(region_vid))
545+
p!(write("{:?}", region_vid));
626546
}
547+
ty::ReVar(_) => {}
627548
ty::ReScope(_) |
628549
ty::ReErased => {}
629550
ty::ReStatic => p!(write("'static")),
@@ -642,14 +563,11 @@ define_print! {
642563
}
643564

644565
ty::ReClosureBound(ref vid) => {
645-
p!(write("ReClosureBound({:?})",
646-
vid))
566+
p!(write("ReClosureBound({:?})", vid))
647567
}
648568

649569
ty::ReLateBound(binder_id, ref bound_region) => {
650-
p!(write("ReLateBound({:?}, ", binder_id),
651-
print_debug(bound_region),
652-
write(")"))
570+
p!(write("ReLateBound({:?}, {:?})", binder_id, bound_region))
653571
}
654572

655573
ty::ReFree(ref fr) => p!(print_debug(fr)),
@@ -661,11 +579,11 @@ define_print! {
661579
ty::ReStatic => p!(write("ReStatic")),
662580

663581
ty::ReVar(ref vid) => {
664-
p!(print_debug(vid))
582+
p!(write("{:?}", vid));
665583
}
666584

667585
ty::RePlaceholder(placeholder) => {
668-
p!(write("RePlaceholder("), print_debug(placeholder), write(")"))
586+
p!(write("RePlaceholder({:?})", placeholder))
669587
}
670588

671589
ty::ReEmpty => p!(write("ReEmpty")),
@@ -687,11 +605,12 @@ impl ty::RegionKind {
687605
pub(crate) fn display_outputs_anything<P>(&self, cx: &PrintCx<'_, '_, '_, P>) -> bool
688606
where P: PrettyPrinter
689607
{
690-
if cx.config.is_verbose {
608+
let highlight = cx.printer.region_highlight_mode();
609+
if highlight.region_highlighted(self).is_some() {
691610
return true;
692611
}
693612

694-
if cx.printer.region_highlight_mode().region_highlighted(self).is_some() {
613+
if cx.config.is_verbose {
695614
return true;
696615
}
697616

@@ -701,17 +620,27 @@ impl ty::RegionKind {
701620
}
702621

703622
ty::ReLateBound(_, br) |
704-
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) => {
705-
br.display_outputs_anything(cx)
706-
}
623+
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
624+
ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
625+
if let ty::BrNamed(_, name) = br {
626+
if name != "" && name != "'_" {
627+
return true;
628+
}
629+
}
707630

708-
ty::RePlaceholder(p) => p.display_outputs_anything(cx),
631+
if let Some((region, _)) = highlight.highlight_bound_region {
632+
if br == region {
633+
return true;
634+
}
635+
}
636+
637+
false
638+
}
709639

710640
ty::ReScope(_) |
711641
ty::ReVar(_) if cx.config.identify_regions => true,
712642

713-
ty::ReVar(region_vid) => region_vid.display_outputs_anything(cx),
714-
643+
ty::ReVar(_) |
715644
ty::ReScope(_) |
716645
ty::ReErased => false,
717646

@@ -788,48 +717,9 @@ impl fmt::Debug for ty::FloatVid {
788717
}
789718
}
790719

791-
define_print! {
792-
() ty::RegionVid, (self, cx) {
793-
display {
794-
if cx.config.is_verbose {
795-
return self.print_debug(cx);
796-
}
797-
798-
let highlight = cx.printer.region_highlight_mode();
799-
if let Some(counter) = highlight.region_highlighted(&ty::ReVar(*self)) {
800-
p!(write("'{}", counter));
801-
}
802-
}
803-
debug {
804-
// HACK(eddyb) this is duplicated from `display` printing,
805-
// to keep NLL borrowck working even with `-Zverbose`.
806-
let highlight = cx.printer.region_highlight_mode();
807-
if let Some(counter) = highlight.region_highlighted(&ty::ReVar(*self)) {
808-
p!(write("'{}", counter));
809-
} else {
810-
p!(write("'_#{}r", self.index()));
811-
}
812-
}
813-
}
814-
}
815-
816-
// HACK(eddyb) (see `ty::RegionKind::display_outputs_anything`)
817-
//
818-
// NB: this must be kept in sync with the printing logic above.
819-
impl ty::RegionVid {
820-
fn display_outputs_anything<P>(&self, cx: &PrintCx<'_, '_, '_, P>) -> bool
821-
where P: PrettyPrinter
822-
{
823-
if cx.config.is_verbose {
824-
return true;
825-
}
826-
827-
let highlight = cx.printer.region_highlight_mode();
828-
if highlight.region_highlighted(&ty::ReVar(*self)).is_some() {
829-
return true;
830-
}
831-
832-
false
720+
impl fmt::Debug for ty::RegionVid {
721+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
722+
write!(f, "'_#{}r", self.index())
833723
}
834724
}
835725

0 commit comments

Comments
 (0)