Skip to content

Commit 1c7d54e

Browse files
committed
Deduplicate some pretty printing logic
1 parent 4032b9d commit 1c7d54e

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_span::symbol::{Ident, sym};
2121
use rustc_span::{BytePos, DUMMY_SP, FileName, Span};
2222
use tracing::{debug, instrument, warn};
2323

24+
use super::nice_region_error::placeholder_error::Highlighted;
2425
use crate::error_reporting::TypeErrCtxt;
2526
use crate::errors::{
2627
AmbiguousImpl, AmbiguousReturn, AnnotationRequired, InferenceBadError,
@@ -281,6 +282,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
281282
arg: GenericArg<'tcx>,
282283
highlight: ty::print::RegionHighlightMode<'tcx>,
283284
) -> InferenceDiagnosticsData {
285+
let tcx = self.tcx;
284286
match arg.unpack() {
285287
GenericArgKind::Type(ty) => {
286288
if let ty::Infer(ty::TyVar(ty_vid)) = *ty.kind() {
@@ -300,12 +302,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
300302
}
301303
}
302304

303-
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS);
304-
printer.region_highlight_mode = highlight;
305-
306-
ty.print(&mut printer).unwrap();
307305
InferenceDiagnosticsData {
308-
name: printer.into_buffer(),
306+
name: Highlighted { highlight, ns: Namespace::TypeNS, tcx, value: ty }
307+
.to_string(),
309308
span: None,
310309
kind: UnderspecifiedArgKind::Type { prefix: ty.prefix_string(self.tcx) },
311310
parent: None,
@@ -324,12 +323,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
324323
}
325324

326325
debug_assert!(!origin.span.is_dummy());
327-
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::ValueNS);
328-
printer.region_highlight_mode = highlight;
329-
330-
ct.print(&mut printer).unwrap();
331326
InferenceDiagnosticsData {
332-
name: printer.into_buffer(),
327+
name: Highlighted { highlight, ns: Namespace::ValueNS, tcx, value: ct }
328+
.to_string(),
333329
span: Some(origin.span),
334330
kind: UnderspecifiedArgKind::Const { is_parameter: false },
335331
parent: None,
@@ -341,12 +337,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
341337
// FIXME: Ideally we should look into the generic constant
342338
// to figure out which inference var is actually unresolved so that
343339
// this path is unreachable.
344-
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::ValueNS);
345-
printer.region_highlight_mode = highlight;
346-
347-
ct.print(&mut printer).unwrap();
348340
InferenceDiagnosticsData {
349-
name: printer.into_buffer(),
341+
name: Highlighted { highlight, ns: Namespace::ValueNS, tcx, value: ct }
342+
.to_string(),
350343
span: None,
351344
kind: UnderspecifiedArgKind::Const { is_parameter: false },
352345
parent: None,

compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_error.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ use crate::traits::{ObligationCause, ObligationCauseCode};
2121
// HACK(eddyb) maybe move this in a more central location.
2222
#[derive(Copy, Clone)]
2323
pub struct Highlighted<'tcx, T> {
24-
tcx: TyCtxt<'tcx>,
25-
highlight: RegionHighlightMode<'tcx>,
26-
value: T,
24+
pub tcx: TyCtxt<'tcx>,
25+
pub highlight: RegionHighlightMode<'tcx>,
26+
pub value: T,
27+
pub ns: Namespace,
2728
}
2829

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

3839
impl<'tcx, T> Highlighted<'tcx, T> {
3940
fn map<U>(self, f: impl FnOnce(T) -> U) -> Highlighted<'tcx, U> {
40-
Highlighted { tcx: self.tcx, highlight: self.highlight, value: f(self.value) }
41+
Highlighted { tcx: self.tcx, highlight: self.highlight, value: f(self.value), ns: self.ns }
4142
}
4243
}
4344

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

5253
self.value.print(&mut printer)?;
@@ -381,6 +382,7 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
381382
tcx: self.tcx(),
382383
highlight: RegionHighlightMode::default(),
383384
value: trait_ref,
385+
ns: Namespace::TypeNS,
384386
};
385387

386388
let same_self_type = actual_trait_ref.self_ty() == expected_trait_ref.self_ty();

0 commit comments

Comments
 (0)