Skip to content

Commit efb99d7

Browse files
Always format to internal String in FmtPrinter
This avoids monomorphizing for different parameters, decreasing generic code instantiated downstream from rustc_middle.
1 parent 45e2c28 commit efb99d7

File tree

10 files changed

+82
-80
lines changed

10 files changed

+82
-80
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
488488
/// Return the name of the provided `Ty` (that must be a reference) with a synthesized lifetime
489489
/// name where required.
490490
pub(super) fn get_name_for_ty(&self, ty: Ty<'tcx>, counter: usize) -> String {
491-
let mut s = String::new();
492-
let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, &mut s, Namespace::TypeNS);
491+
let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, Namespace::TypeNS);
493492

494493
// We need to add synthesized lifetimes where appropriate. We do
495494
// this by hooking into the pretty printer and telling it to label the
@@ -504,15 +503,13 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
504503
}
505504
}
506505

507-
let _ = ty.print(printer);
508-
s
506+
ty.print(printer).unwrap().into_buffer()
509507
}
510508

511509
/// Returns the name of the provided `Ty` (that must be a reference)'s region with a
512510
/// synthesized lifetime name where required.
513511
pub(super) fn get_region_name_for_ty(&self, ty: Ty<'tcx>, counter: usize) -> String {
514-
let mut s = String::new();
515-
let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, &mut s, Namespace::TypeNS);
512+
let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, Namespace::TypeNS);
516513

517514
let region = if let ty::Ref(region, ..) = ty.kind() {
518515
match **region {
@@ -527,8 +524,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
527524
bug!("ty for annotation of borrow region is not a reference");
528525
};
529526

530-
let _ = region.print(printer);
531-
s
527+
region.print(printer).unwrap().into_buffer()
532528
}
533529
}
534530

compiler/rustc_const_eval/src/interpret/operand.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ rustc_data_structures::static_assert_size!(ImmTy<'_>, 72);
109109
impl<Tag: Provenance> std::fmt::Display for ImmTy<'_, Tag> {
110110
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111111
/// Helper function for printing a scalar to a FmtPrinter
112-
fn p<'a, 'tcx, F: std::fmt::Write, Tag: Provenance>(
113-
cx: FmtPrinter<'a, 'tcx, F>,
112+
fn p<'a, 'tcx, Tag: Provenance>(
113+
cx: FmtPrinter<'a, 'tcx>,
114114
s: ScalarMaybeUninit<Tag>,
115115
ty: Ty<'tcx>,
116-
) -> Result<FmtPrinter<'a, 'tcx, F>, std::fmt::Error> {
116+
) -> Result<FmtPrinter<'a, 'tcx>, std::fmt::Error> {
117117
match s {
118118
ScalarMaybeUninit::Scalar(Scalar::Int(int)) => {
119119
cx.pretty_print_const_scalar_int(int, ty, true)
@@ -138,8 +138,8 @@ impl<Tag: Provenance> std::fmt::Display for ImmTy<'_, Tag> {
138138
match self.imm {
139139
Immediate::Scalar(s) => {
140140
if let Some(ty) = tcx.lift(self.layout.ty) {
141-
let cx = FmtPrinter::new(tcx, f, Namespace::ValueNS);
142-
p(cx, s, ty)?;
141+
let cx = FmtPrinter::new(tcx, Namespace::ValueNS);
142+
f.write_str(&p(cx, s, ty)?.into_buffer())?;
143143
return Ok(());
144144
}
145145
write!(f, "{}: {}", s, self.layout.ty)

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
988988
) -> (DiagnosticStyledString, DiagnosticStyledString) {
989989
let get_lifetimes = |sig| {
990990
use rustc_hir::def::Namespace;
991-
let mut s = String::new();
992-
let (_, sig, reg) = ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::TypeNS)
991+
let (_, sig, reg) = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS)
993992
.name_all_regions(sig)
994993
.unwrap();
995994
let lts: Vec<String> = reg.into_iter().map(|(_, kind)| kind.to_string()).collect();

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

+11-16
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
395395
}
396396
}
397397

398-
let mut s = String::new();
399-
let mut printer = ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::TypeNS);
398+
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS);
400399
if let Some(highlight) = highlight {
401400
printer.region_highlight_mode = highlight;
402401
}
403-
let _ = ty.print(printer);
402+
let name = ty.print(printer).unwrap().into_buffer();
404403
InferenceDiagnosticsData {
405-
name: s,
404+
name,
406405
span: None,
407406
kind: UnderspecifiedArgKind::Type { prefix: ty.prefix_string(self.tcx) },
408407
parent: None,
@@ -431,15 +430,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
431430
}
432431

433432
debug_assert!(!origin.span.is_dummy());
434-
let mut s = String::new();
435-
let mut printer =
436-
ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::ValueNS);
433+
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::ValueNS);
437434
if let Some(highlight) = highlight {
438435
printer.region_highlight_mode = highlight;
439436
}
440-
let _ = ct.print(printer);
437+
let name = ct.print(printer).unwrap().into_buffer();
441438
InferenceDiagnosticsData {
442-
name: s,
439+
name,
443440
span: Some(origin.span),
444441
kind: UnderspecifiedArgKind::Const { is_parameter: false },
445442
parent: None,
@@ -495,8 +492,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
495492

496493
let mut local_visitor = FindHirNodeVisitor::new(&self, arg, span);
497494
let ty_to_string = |ty: Ty<'tcx>| -> String {
498-
let mut s = String::new();
499-
let mut printer = ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::TypeNS);
495+
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS);
500496
let ty_getter = move |ty_vid| {
501497
if let TypeVariableOriginKind::TypeParameterDefinition(name, _) =
502498
self.inner.borrow_mut().type_variables().var_origin(ty_vid).kind
@@ -523,14 +519,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
523519
};
524520
printer.const_infer_name_resolver = Some(Box::new(const_getter));
525521

526-
let _ = if let ty::FnDef(..) = ty.kind() {
522+
if let ty::FnDef(..) = ty.kind() {
527523
// We don't want the regular output for `fn`s because it includes its path in
528524
// invalid pseudo-syntax, we want the `fn`-pointer output instead.
529-
ty.fn_sig(self.tcx).print(printer)
525+
ty.fn_sig(self.tcx).print(printer).unwrap().into_buffer()
530526
} else {
531-
ty.print(printer)
532-
};
533-
s
527+
ty.print(printer).unwrap().into_buffer()
528+
}
534529
};
535530

536531
if let Some(body_id) = body_id {

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,19 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
335335

336336
impl<'tcx, T> fmt::Display for Highlighted<'tcx, T>
337337
where
338-
T: for<'a, 'b, 'c> Print<
338+
T: for<'a> Print<
339339
'tcx,
340-
FmtPrinter<'a, 'tcx, &'b mut fmt::Formatter<'c>>,
340+
FmtPrinter<'a, 'tcx>,
341341
Error = fmt::Error,
342+
Output = FmtPrinter<'a, 'tcx>,
342343
>,
343344
{
344345
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
345-
let mut printer = ty::print::FmtPrinter::new(self.tcx, f, Namespace::TypeNS);
346+
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS);
346347
printer.region_highlight_mode = self.highlight;
347348

348-
self.value.print(printer)?;
349-
Ok(())
349+
let s = self.value.print(printer)?.into_buffer();
350+
f.write_str(&s)
350351
}
351352
}
352353

compiler/rustc_middle/src/mir/mod.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -2421,11 +2421,11 @@ impl<'tcx> Debug for Rvalue<'tcx> {
24212421

24222422
AggregateKind::Adt(adt_did, variant, substs, _user_ty, _) => {
24232423
ty::tls::with(|tcx| {
2424-
let mut name = String::new();
24252424
let variant_def = &tcx.adt_def(adt_did).variants[variant];
24262425
let substs = tcx.lift(substs).expect("could not lift for printing");
2427-
FmtPrinter::new(tcx, &mut name, Namespace::ValueNS)
2428-
.print_def_path(variant_def.def_id, substs)?;
2426+
let name = FmtPrinter::new(tcx, Namespace::ValueNS)
2427+
.print_def_path(variant_def.def_id, substs)?
2428+
.into_buffer();
24292429

24302430
match variant_def.ctor_kind {
24312431
CtorKind::Const => fmt.write_str(&name),
@@ -2847,9 +2847,10 @@ fn pretty_print_const<'tcx>(
28472847
use crate::ty::print::PrettyPrinter;
28482848
ty::tls::with(|tcx| {
28492849
let literal = tcx.lift(c).unwrap();
2850-
let mut cx = FmtPrinter::new(tcx, fmt, Namespace::ValueNS);
2850+
let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS);
28512851
cx.print_alloc_ids = true;
2852-
cx.pretty_print_const(literal, print_types)?;
2852+
let cx = cx.pretty_print_const(literal, print_types)?;
2853+
fmt.write_str(&cx.into_buffer())?;
28532854
Ok(())
28542855
})
28552856
}
@@ -2864,9 +2865,10 @@ fn pretty_print_const_value<'tcx>(
28642865
ty::tls::with(|tcx| {
28652866
let val = tcx.lift(val).unwrap();
28662867
let ty = tcx.lift(ty).unwrap();
2867-
let mut cx = FmtPrinter::new(tcx, fmt, Namespace::ValueNS);
2868+
let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS);
28682869
cx.print_alloc_ids = true;
2869-
cx.pretty_print_const_value(val, ty, print_types)?;
2870+
let cx = cx.pretty_print_const_value(val, ty, print_types)?;
2871+
fmt.write_str(&cx.into_buffer())?;
28702872
Ok(())
28712873
})
28722874
}

compiler/rustc_middle/src/ty/error.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -983,10 +983,9 @@ fn foo(&self) -> Self::T { String::new() }
983983
}
984984

985985
fn format_generic_args(self, args: &[ty::GenericArg<'tcx>]) -> String {
986-
let mut item_args = String::new();
987-
FmtPrinter::new(self, &mut item_args, hir::def::Namespace::TypeNS)
986+
FmtPrinter::new(self, hir::def::Namespace::TypeNS)
988987
.path_generic_args(Ok, args)
989-
.expect("could not write to `String`.");
990-
item_args
988+
.expect("could not write to `String`.")
989+
.into_buffer()
991990
}
992991
}

compiler/rustc_middle/src/ty/instance.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,10 @@ impl<'tcx> fmt::Display for Instance<'tcx> {
279279
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
280280
ty::tls::with(|tcx| {
281281
let substs = tcx.lift(self.substs).expect("could not lift for printing");
282-
FmtPrinter::new(tcx, &mut *f, Namespace::ValueNS)
283-
.print_def_path(self.def_id(), substs)?;
284-
Ok(())
282+
let s = FmtPrinter::new(tcx, Namespace::ValueNS)
283+
.print_def_path(self.def_id(), substs)?
284+
.into_buffer();
285+
f.write_str(&s)
285286
})?;
286287

287288
match self.def {

compiler/rustc_middle/src/ty/print/pretty.rs

+25-22
Original file line numberDiff line numberDiff line change
@@ -1545,11 +1545,11 @@ pub trait PrettyPrinter<'tcx>:
15451545
}
15461546

15471547
// HACK(eddyb) boxed to avoid moving around a large struct by-value.
1548-
pub struct FmtPrinter<'a, 'tcx, F>(Box<FmtPrinterData<'a, 'tcx, F>>);
1548+
pub struct FmtPrinter<'a, 'tcx>(Box<FmtPrinterData<'a, 'tcx>>);
15491549

1550-
pub struct FmtPrinterData<'a, 'tcx, F> {
1550+
pub struct FmtPrinterData<'a, 'tcx> {
15511551
tcx: TyCtxt<'tcx>,
1552-
fmt: F,
1552+
fmt: String,
15531553

15541554
empty_path: bool,
15551555
in_value: bool,
@@ -1566,24 +1566,24 @@ pub struct FmtPrinterData<'a, 'tcx, F> {
15661566
pub const_infer_name_resolver: Option<Box<dyn Fn(ty::ConstVid<'tcx>) -> Option<String> + 'a>>,
15671567
}
15681568

1569-
impl<'a, 'tcx, F> Deref for FmtPrinter<'a, 'tcx, F> {
1570-
type Target = FmtPrinterData<'a, 'tcx, F>;
1569+
impl<'a, 'tcx> Deref for FmtPrinter<'a, 'tcx> {
1570+
type Target = FmtPrinterData<'a, 'tcx>;
15711571
fn deref(&self) -> &Self::Target {
15721572
&self.0
15731573
}
15741574
}
15751575

1576-
impl<F> DerefMut for FmtPrinter<'_, '_, F> {
1576+
impl DerefMut for FmtPrinter<'_, '_> {
15771577
fn deref_mut(&mut self) -> &mut Self::Target {
15781578
&mut self.0
15791579
}
15801580
}
15811581

1582-
impl<'a, 'tcx, F> FmtPrinter<'a, 'tcx, F> {
1583-
pub fn new(tcx: TyCtxt<'tcx>, fmt: F, ns: Namespace) -> Self {
1582+
impl<'a, 'tcx> FmtPrinter<'a, 'tcx> {
1583+
pub fn new(tcx: TyCtxt<'tcx>, ns: Namespace) -> Self {
15841584
FmtPrinter(Box::new(FmtPrinterData {
15851585
tcx,
1586-
fmt,
1586+
fmt: String::new(),
15871587
empty_path: false,
15881588
in_value: ns == Namespace::ValueNS,
15891589
print_alloc_ids: false,
@@ -1596,6 +1596,10 @@ impl<'a, 'tcx, F> FmtPrinter<'a, 'tcx, F> {
15961596
const_infer_name_resolver: None,
15971597
}))
15981598
}
1599+
1600+
pub fn into_buffer(self) -> String {
1601+
self.0.fmt
1602+
}
15991603
}
16001604

16011605
// HACK(eddyb) get rid of `def_path_str` and/or pass `Namespace` explicitly always
@@ -1627,19 +1631,18 @@ impl<'t> TyCtxt<'t> {
16271631
pub fn def_path_str_with_substs(self, def_id: DefId, substs: &'t [GenericArg<'t>]) -> String {
16281632
let ns = guess_def_namespace(self, def_id);
16291633
debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns);
1630-
let mut s = String::new();
1631-
let _ = FmtPrinter::new(self, &mut s, ns).print_def_path(def_id, substs);
1632-
s
1634+
FmtPrinter::new(self, ns).print_def_path(def_id, substs).unwrap().into_buffer()
16331635
}
16341636
}
16351637

1636-
impl<F: fmt::Write> fmt::Write for FmtPrinter<'_, '_, F> {
1638+
impl fmt::Write for FmtPrinter<'_, '_> {
16371639
fn write_str(&mut self, s: &str) -> fmt::Result {
1638-
self.fmt.write_str(s)
1640+
self.fmt.push_str(s);
1641+
Ok(())
16391642
}
16401643
}
16411644

1642-
impl<'tcx, F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
1645+
impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
16431646
type Error = fmt::Error;
16441647

16451648
type Path = Self;
@@ -1847,7 +1850,7 @@ impl<'tcx, F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
18471850
}
18481851
}
18491852

1850-
impl<'tcx, F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
1853+
impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
18511854
fn ty_infer_name(&self, id: ty::TyVid) -> Option<String> {
18521855
self.0.ty_infer_name_resolver.as_ref().and_then(|func| func(id))
18531856
}
@@ -1983,7 +1986,7 @@ impl<'tcx, F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
19831986
}
19841987

19851988
// HACK(eddyb) limited to `FmtPrinter` because of `region_highlight_mode`.
1986-
impl<F: fmt::Write> FmtPrinter<'_, '_, F> {
1989+
impl FmtPrinter<'_, '_> {
19871990
pub fn pretty_print_region(mut self, region: ty::Region<'_>) -> Result<Self, fmt::Error> {
19881991
define_scoped_cx!(self);
19891992

@@ -2117,7 +2120,7 @@ impl<'a, 'tcx> ty::TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
21172120

21182121
// HACK(eddyb) limited to `FmtPrinter` because of `binder_depth`,
21192122
// `region_index` and `used_region_names`.
2120-
impl<'tcx, F: fmt::Write> FmtPrinter<'_, 'tcx, F> {
2123+
impl<'tcx> FmtPrinter<'_, 'tcx> {
21212124
pub fn name_all_regions<T>(
21222125
mut self,
21232126
value: &ty::Binder<'tcx, T>,
@@ -2369,9 +2372,10 @@ macro_rules! forward_display_to_print {
23692372
$(#[allow(unused_lifetimes)] impl<'tcx> fmt::Display for $ty {
23702373
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23712374
ty::tls::with(|tcx| {
2372-
tcx.lift(*self)
2375+
let cx = tcx.lift(*self)
23732376
.expect("could not lift for printing")
2374-
.print(FmtPrinter::new(tcx, f, Namespace::TypeNS))?;
2377+
.print(FmtPrinter::new(tcx, Namespace::TypeNS))?;
2378+
f.write_str(&cx.into_buffer())?;
23752379
Ok(())
23762380
})
23772381
}
@@ -2402,8 +2406,7 @@ macro_rules! define_print_and_forward_display {
24022406
impl<'tcx> fmt::Display for ty::Region<'tcx> {
24032407
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24042408
ty::tls::with(|tcx| {
2405-
self.print(FmtPrinter::new(tcx, f, Namespace::TypeNS))?;
2406-
Ok(())
2409+
f.write_str(&self.print(FmtPrinter::new(tcx, Namespace::TypeNS))?.into_buffer())
24072410
})
24082411
}
24092412
}

0 commit comments

Comments
 (0)