Skip to content

Commit c80c4d3

Browse files
committed
rustc: centralize region printing in ty::RegionKind's Print impl.
1 parent 400b50d commit c80c4d3

File tree

3 files changed

+61
-173
lines changed

3 files changed

+61
-173
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
224224
self.hir().span(node),
225225
),
226226
_ => (
227-
format!("the lifetime {} as defined on", fr.bound_region),
227+
format!("the lifetime {} as defined on", region),
228228
cm.def_span(self.hir().span(node)),
229229
),
230230
},
@@ -1495,7 +1495,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
14951495
var_origin: RegionVariableOrigin,
14961496
) -> DiagnosticBuilder<'tcx> {
14971497
let br_string = |br: ty::BoundRegion| {
1498-
let mut s = br.to_string();
1498+
let mut s = match br {
1499+
ty::BrNamed(_, name) => name.to_string(),
1500+
_ => String::new(),
1501+
};
14991502
if !s.is_empty() {
15001503
s.push_str(" ");
15011504
}

src/librustc/ty/print.rs

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

148143
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 hir::def::Namespace;
22
use hir::def_id::DefId;
33
use middle::region;
44
use ty::subst::{Kind, Subst, Substs, UnpackedKind};
5-
use ty::{BrAnon, BrEnv, BrFresh, BrNamed};
65
use ty::{Bool, Char, Adt};
76
use ty::{Error, Str, Array, Slice, Float, FnDef, FnPtr};
87
use ty::{Param, Bound, RawPtr, Ref, Never, Tuple};
@@ -469,119 +468,34 @@ define_print! {
469468
}
470469
}
471470

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

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

568-
self.name.display_outputs_anything(cx)
569-
}
570-
}
571-
572-
define_print! {
573-
() ty::RegionKind, (self, cx) {
574-
display {
575495
if cx.config.is_verbose {
576496
return self.print_debug(cx);
577497
}
578498

579-
// Watch out for region highlights.
580-
if let Some(n) = cx.printer.region_highlight_mode().region_highlighted(self) {
581-
p!(write("'{}", n));
582-
return Ok(cx.printer);
583-
}
584-
585499
// These printouts are concise. They do not contain all the information
586500
// the user might want to diagnose an error, but there is basically no way
587501
// to fit that into a short string. Hence the recommendation to use
@@ -593,11 +507,20 @@ define_print! {
593507
}
594508
}
595509
ty::ReLateBound(_, br) |
596-
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) => {
597-
p!(print_display(br))
598-
}
599-
ty::RePlaceholder(p) => {
600-
p!(print_display(p))
510+
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
511+
ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
512+
if let ty::BrNamed(_, name) = br {
513+
if name != "" && name != "'_" {
514+
p!(write("{}", name));
515+
return Ok(cx.printer);
516+
}
517+
}
518+
519+
if let Some((region, counter)) = highlight.highlight_bound_region {
520+
if br == region {
521+
p!(write("'{}", counter));
522+
}
523+
}
601524
}
602525
ty::ReScope(scope) if cx.config.identify_regions => {
603526
match scope.data {
@@ -617,11 +540,9 @@ define_print! {
617540
}
618541
}
619542
ty::ReVar(region_vid) if cx.config.identify_regions => {
620-
p!(print_debug(region_vid))
621-
}
622-
ty::ReVar(region_vid) => {
623-
p!(print_display(region_vid))
543+
p!(write("{:?}", region_vid));
624544
}
545+
ty::ReVar(_) => {}
625546
ty::ReScope(_) |
626547
ty::ReErased => {}
627548
ty::ReStatic => p!(write("'static")),
@@ -640,14 +561,11 @@ define_print! {
640561
}
641562

642563
ty::ReClosureBound(ref vid) => {
643-
p!(write("ReClosureBound({:?})",
644-
vid))
564+
p!(write("ReClosureBound({:?})", vid))
645565
}
646566

647567
ty::ReLateBound(binder_id, ref bound_region) => {
648-
p!(write("ReLateBound({:?}, ", binder_id),
649-
print_debug(bound_region),
650-
write(")"))
568+
p!(write("ReLateBound({:?}, {:?})", binder_id, bound_region))
651569
}
652570

653571
ty::ReFree(ref fr) => p!(print_debug(fr)),
@@ -659,11 +577,11 @@ define_print! {
659577
ty::ReStatic => p!(write("ReStatic")),
660578

661579
ty::ReVar(ref vid) => {
662-
p!(print_debug(vid))
580+
p!(write("{:?}", vid));
663581
}
664582

665583
ty::RePlaceholder(placeholder) => {
666-
p!(write("RePlaceholder("), print_debug(placeholder), write(")"))
584+
p!(write("RePlaceholder({:?})", placeholder))
667585
}
668586

669587
ty::ReEmpty => p!(write("ReEmpty")),
@@ -685,11 +603,12 @@ impl ty::RegionKind {
685603
pub(crate) fn display_outputs_anything<P>(&self, cx: &PrintCx<'_, '_, '_, P>) -> bool
686604
where P: PrettyPrinter
687605
{
688-
if cx.config.is_verbose {
606+
let highlight = cx.printer.region_highlight_mode();
607+
if highlight.region_highlighted(self).is_some() {
689608
return true;
690609
}
691610

692-
if cx.printer.region_highlight_mode().region_highlighted(self).is_some() {
611+
if cx.config.is_verbose {
693612
return true;
694613
}
695614

@@ -699,17 +618,27 @@ impl ty::RegionKind {
699618
}
700619

701620
ty::ReLateBound(_, br) |
702-
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) => {
703-
br.display_outputs_anything(cx)
704-
}
621+
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
622+
ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
623+
if let ty::BrNamed(_, name) = br {
624+
if name != "" && name != "'_" {
625+
return true;
626+
}
627+
}
705628

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

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

711-
ty::ReVar(region_vid) => region_vid.display_outputs_anything(cx),
712-
641+
ty::ReVar(_) |
713642
ty::ReScope(_) |
714643
ty::ReErased => false,
715644

@@ -780,48 +709,9 @@ impl fmt::Debug for ty::FloatVid {
780709
}
781710
}
782711

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

0 commit comments

Comments
 (0)