1
Fork 0

Deduplicate some pretty printing logic

This commit is contained in:
Oli Scherer 2024-12-16 15:17:23 +00:00
parent 4032b9ddbd
commit 1c7d54eb7b
2 changed files with 15 additions and 20 deletions

View file

@ -21,6 +21,7 @@ use rustc_span::symbol::{Ident, sym};
use rustc_span::{BytePos, DUMMY_SP, FileName, Span}; use rustc_span::{BytePos, DUMMY_SP, FileName, Span};
use tracing::{debug, instrument, warn}; use tracing::{debug, instrument, warn};
use super::nice_region_error::placeholder_error::Highlighted;
use crate::error_reporting::TypeErrCtxt; use crate::error_reporting::TypeErrCtxt;
use crate::errors::{ use crate::errors::{
AmbiguousImpl, AmbiguousReturn, AnnotationRequired, InferenceBadError, AmbiguousImpl, AmbiguousReturn, AnnotationRequired, InferenceBadError,
@ -281,6 +282,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
arg: GenericArg<'tcx>, arg: GenericArg<'tcx>,
highlight: ty::print::RegionHighlightMode<'tcx>, highlight: ty::print::RegionHighlightMode<'tcx>,
) -> InferenceDiagnosticsData { ) -> InferenceDiagnosticsData {
let tcx = self.tcx;
match arg.unpack() { match arg.unpack() {
GenericArgKind::Type(ty) => { GenericArgKind::Type(ty) => {
if let ty::Infer(ty::TyVar(ty_vid)) = *ty.kind() { if let ty::Infer(ty::TyVar(ty_vid)) = *ty.kind() {
@ -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 { InferenceDiagnosticsData {
name: printer.into_buffer(), name: Highlighted { highlight, ns: Namespace::TypeNS, tcx, value: ty }
.to_string(),
span: None, span: None,
kind: UnderspecifiedArgKind::Type { prefix: ty.prefix_string(self.tcx) }, kind: UnderspecifiedArgKind::Type { prefix: ty.prefix_string(self.tcx) },
parent: None, parent: None,
@ -324,12 +323,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
} }
debug_assert!(!origin.span.is_dummy()); 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 { InferenceDiagnosticsData {
name: printer.into_buffer(), name: Highlighted { highlight, ns: Namespace::ValueNS, tcx, value: ct }
.to_string(),
span: Some(origin.span), span: Some(origin.span),
kind: UnderspecifiedArgKind::Const { is_parameter: false }, kind: UnderspecifiedArgKind::Const { is_parameter: false },
parent: None, parent: None,
@ -341,12 +337,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
// FIXME: Ideally we should look into the generic constant // FIXME: Ideally we should look into the generic constant
// to figure out which inference var is actually unresolved so that // to figure out which inference var is actually unresolved so that
// this path is unreachable. // 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 { InferenceDiagnosticsData {
name: printer.into_buffer(), name: Highlighted { highlight, ns: Namespace::ValueNS, tcx, value: ct }
.to_string(),
span: None, span: None,
kind: UnderspecifiedArgKind::Const { is_parameter: false }, kind: UnderspecifiedArgKind::Const { is_parameter: false },
parent: None, parent: None,

View file

@ -21,9 +21,10 @@ use crate::traits::{ObligationCause, ObligationCauseCode};
// HACK(eddyb) maybe move this in a more central location. // HACK(eddyb) maybe move this in a more central location.
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Highlighted<'tcx, T> { pub struct Highlighted<'tcx, T> {
tcx: TyCtxt<'tcx>, pub tcx: TyCtxt<'tcx>,
highlight: RegionHighlightMode<'tcx>, pub highlight: RegionHighlightMode<'tcx>,
value: T, pub value: T,
pub ns: Namespace,
} }
impl<'tcx, T> IntoDiagArg for Highlighted<'tcx, T> impl<'tcx, T> IntoDiagArg for Highlighted<'tcx, T>
@ -37,7 +38,7 @@ where
impl<'tcx, T> Highlighted<'tcx, T> { impl<'tcx, T> Highlighted<'tcx, T> {
fn map<U>(self, f: impl FnOnce(T) -> U) -> Highlighted<'tcx, U> { 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 }
} }
} }
@ -46,7 +47,7 @@ where
T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>>, T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>>,
{ {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 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; printer.region_highlight_mode = self.highlight;
self.value.print(&mut printer)?; self.value.print(&mut printer)?;
@ -381,6 +382,7 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
tcx: self.tcx(), tcx: self.tcx(),
highlight: RegionHighlightMode::default(), highlight: RegionHighlightMode::default(),
value: trait_ref, value: trait_ref,
ns: Namespace::TypeNS,
}; };
let same_self_type = actual_trait_ref.self_ty() == expected_trait_ref.self_ty(); let same_self_type = actual_trait_ref.self_ty() == expected_trait_ref.self_ty();