Skip to content

Commit

Permalink
Deduplicate some pretty printing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Dec 16, 2024
1 parent 4032b9d commit 1c7d54e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use rustc_span::symbol::{Ident, sym};
use rustc_span::{BytePos, DUMMY_SP, FileName, Span};
use tracing::{debug, instrument, warn};

use super::nice_region_error::placeholder_error::Highlighted;
use crate::error_reporting::TypeErrCtxt;
use crate::errors::{
AmbiguousImpl, AmbiguousReturn, AnnotationRequired, InferenceBadError,
Expand Down Expand Up @@ -281,6 +282,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
arg: GenericArg<'tcx>,
highlight: ty::print::RegionHighlightMode<'tcx>,
) -> InferenceDiagnosticsData {
let tcx = self.tcx;
match arg.unpack() {
GenericArgKind::Type(ty) => {
if let ty::Infer(ty::TyVar(ty_vid)) = *ty.kind() {
Expand All @@ -300,12 +302,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
}

let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS);
printer.region_highlight_mode = highlight;

ty.print(&mut printer).unwrap();
InferenceDiagnosticsData {
name: printer.into_buffer(),
name: Highlighted { highlight, ns: Namespace::TypeNS, tcx, value: ty }
.to_string(),
span: None,
kind: UnderspecifiedArgKind::Type { prefix: ty.prefix_string(self.tcx) },
parent: None,
Expand All @@ -324,12 +323,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}

debug_assert!(!origin.span.is_dummy());
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::ValueNS);
printer.region_highlight_mode = highlight;

ct.print(&mut printer).unwrap();
InferenceDiagnosticsData {
name: printer.into_buffer(),
name: Highlighted { highlight, ns: Namespace::ValueNS, tcx, value: ct }
.to_string(),
span: Some(origin.span),
kind: UnderspecifiedArgKind::Const { is_parameter: false },
parent: None,
Expand All @@ -341,12 +337,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
// FIXME: Ideally we should look into the generic constant
// to figure out which inference var is actually unresolved so that
// this path is unreachable.
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::ValueNS);
printer.region_highlight_mode = highlight;

ct.print(&mut printer).unwrap();
InferenceDiagnosticsData {
name: printer.into_buffer(),
name: Highlighted { highlight, ns: Namespace::ValueNS, tcx, value: ct }
.to_string(),
span: None,
kind: UnderspecifiedArgKind::Const { is_parameter: false },
parent: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ use crate::traits::{ObligationCause, ObligationCauseCode};
// HACK(eddyb) maybe move this in a more central location.
#[derive(Copy, Clone)]
pub struct Highlighted<'tcx, T> {
tcx: TyCtxt<'tcx>,
highlight: RegionHighlightMode<'tcx>,
value: T,
pub tcx: TyCtxt<'tcx>,
pub highlight: RegionHighlightMode<'tcx>,
pub value: T,
pub ns: Namespace,
}

impl<'tcx, T> IntoDiagArg for Highlighted<'tcx, T>
Expand All @@ -37,7 +38,7 @@ where

impl<'tcx, T> Highlighted<'tcx, T> {
fn map<U>(self, f: impl FnOnce(T) -> U) -> Highlighted<'tcx, U> {
Highlighted { tcx: self.tcx, highlight: self.highlight, value: f(self.value) }
Highlighted { tcx: self.tcx, highlight: self.highlight, value: f(self.value), ns: self.ns }
}
}

Expand All @@ -46,7 +47,7 @@ where
T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS);
let mut printer = ty::print::FmtPrinter::new(self.tcx, self.ns);
printer.region_highlight_mode = self.highlight;

self.value.print(&mut printer)?;
Expand Down Expand Up @@ -381,6 +382,7 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
tcx: self.tcx(),
highlight: RegionHighlightMode::default(),
value: trait_ref,
ns: Namespace::TypeNS,
};

let same_self_type = actual_trait_ref.self_ty() == expected_trait_ref.self_ty();
Expand Down

0 comments on commit 1c7d54e

Please sign in to comment.