Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e5d1156

Browse files
committedOct 17, 2023
Make ty::print::Printer take &mut self instead of self
This simplifies the code by removing all the `self` assignments and makes the flow of data clearer - always into the printer. Especially in v0 mangling, which already used `&mut self` in some places, it gets a lot more uniform.
1 parent 6fc6a6d commit e5d1156

File tree

17 files changed

+615
-592
lines changed

17 files changed

+615
-592
lines changed
 

‎compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
470470
}
471471
}
472472

473-
ty.print(printer).unwrap().into_buffer()
473+
ty.print(&mut printer).unwrap();
474+
printer.into_buffer()
474475
}
475476

476477
/// Returns the name of the provided `Ty` (that must be a reference)'s region with a
@@ -492,7 +493,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
492493
bug!("ty for annotation of borrow region is not a reference");
493494
};
494495

495-
region.print(printer).unwrap().into_buffer()
496+
region.print(&mut printer).unwrap();
497+
printer.into_buffer()
496498
}
497499
}
498500

‎compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ impl<Prov: Provenance> std::fmt::Display for ImmTy<'_, Prov> {
107107
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
108108
/// Helper function for printing a scalar to a FmtPrinter
109109
fn p<'a, 'tcx, Prov: Provenance>(
110-
cx: FmtPrinter<'a, 'tcx>,
110+
cx: &mut FmtPrinter<'a, 'tcx>,
111111
s: Scalar<Prov>,
112112
ty: Ty<'tcx>,
113-
) -> Result<FmtPrinter<'a, 'tcx>, std::fmt::Error> {
113+
) -> Result<(), std::fmt::Error> {
114114
match s {
115115
Scalar::Int(int) => cx.pretty_print_const_scalar_int(int, ty, true),
116116
Scalar::Ptr(ptr, _sz) => {
@@ -125,8 +125,9 @@ impl<Prov: Provenance> std::fmt::Display for ImmTy<'_, Prov> {
125125
match self.imm {
126126
Immediate::Scalar(s) => {
127127
if let Some(ty) = tcx.lift(self.layout.ty) {
128-
let cx = FmtPrinter::new(tcx, Namespace::ValueNS);
129-
f.write_str(&p(cx, s, ty)?.into_buffer())?;
128+
let s =
129+
FmtPrinter::print_string(tcx, Namespace::ValueNS, |cx| p(cx, s, ty))?;
130+
f.write_str(&s)?;
130131
return Ok(());
131132
}
132133
write!(f, "{:x}: {}", s, self.layout.ty)

‎compiler/rustc_const_eval/src/util/type_name.rs

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
1818
self.tcx
1919
}
2020

21-
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
22-
Ok(self)
21+
fn print_region(&mut self, _region: ty::Region<'_>) -> Result<(), PrintError> {
22+
Ok(())
2323
}
2424

25-
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
25+
fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
2626
match *ty.kind() {
2727
// Types without identity.
2828
ty::Bool
@@ -43,7 +43,7 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
4343
// Placeholders (all printed as `_` to uniformize them).
4444
ty::Param(_) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) | ty::Error(_) => {
4545
write!(self, "_")?;
46-
Ok(self)
46+
Ok(())
4747
}
4848

4949
// Types with identity (print the module path).
@@ -60,74 +60,74 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
6060
}
6161
}
6262

63-
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
63+
fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
6464
self.pretty_print_const(ct, false)
6565
}
6666

6767
fn print_dyn_existential(
68-
self,
68+
&mut self,
6969
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
70-
) -> Result<Self, PrintError> {
70+
) -> Result<(), PrintError> {
7171
self.pretty_print_dyn_existential(predicates)
7272
}
7373

74-
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, PrintError> {
74+
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
7575
self.path.push_str(self.tcx.crate_name(cnum).as_str());
76-
Ok(self)
76+
Ok(())
7777
}
7878

7979
fn path_qualified(
80-
self,
80+
&mut self,
8181
self_ty: Ty<'tcx>,
8282
trait_ref: Option<ty::TraitRef<'tcx>>,
83-
) -> Result<Self, PrintError> {
83+
) -> Result<(), PrintError> {
8484
self.pretty_path_qualified(self_ty, trait_ref)
8585
}
8686

8787
fn path_append_impl(
88-
self,
89-
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
88+
&mut self,
89+
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
9090
_disambiguated_data: &DisambiguatedDefPathData,
9191
self_ty: Ty<'tcx>,
9292
trait_ref: Option<ty::TraitRef<'tcx>>,
93-
) -> Result<Self, PrintError> {
93+
) -> Result<(), PrintError> {
9494
self.pretty_path_append_impl(
95-
|mut cx| {
96-
cx = print_prefix(cx)?;
95+
|cx| {
96+
print_prefix(cx)?;
9797

9898
cx.path.push_str("::");
9999

100-
Ok(cx)
100+
Ok(())
101101
},
102102
self_ty,
103103
trait_ref,
104104
)
105105
}
106106

107107
fn path_append(
108-
mut self,
109-
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
108+
&mut self,
109+
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
110110
disambiguated_data: &DisambiguatedDefPathData,
111-
) -> Result<Self, PrintError> {
112-
self = print_prefix(self)?;
111+
) -> Result<(), PrintError> {
112+
print_prefix(self)?;
113113

114114
write!(self.path, "::{}", disambiguated_data.data).unwrap();
115115

116-
Ok(self)
116+
Ok(())
117117
}
118118

119119
fn path_generic_args(
120-
mut self,
121-
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
120+
&mut self,
121+
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
122122
args: &[GenericArg<'tcx>],
123-
) -> Result<Self, PrintError> {
124-
self = print_prefix(self)?;
123+
) -> Result<(), PrintError> {
124+
print_prefix(self)?;
125125
let args =
126126
args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
127127
if args.clone().next().is_some() {
128128
self.generic_delimiters(|cx| cx.comma_sep(args))
129129
} else {
130-
Ok(self)
130+
Ok(())
131131
}
132132
}
133133
}
@@ -136,31 +136,31 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {
136136
fn should_print_region(&self, _region: ty::Region<'_>) -> bool {
137137
false
138138
}
139-
fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, PrintError>
139+
fn comma_sep<T>(&mut self, mut elems: impl Iterator<Item = T>) -> Result<(), PrintError>
140140
where
141141
T: Print<'tcx, Self>,
142142
{
143143
if let Some(first) = elems.next() {
144-
self = first.print(self)?;
144+
first.print(self)?;
145145
for elem in elems {
146146
self.path.push_str(", ");
147-
self = elem.print(self)?;
147+
elem.print(self)?;
148148
}
149149
}
150-
Ok(self)
150+
Ok(())
151151
}
152152

153153
fn generic_delimiters(
154-
mut self,
155-
f: impl FnOnce(Self) -> Result<Self, PrintError>,
156-
) -> Result<Self, PrintError> {
154+
&mut self,
155+
f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
156+
) -> Result<(), PrintError> {
157157
write!(self, "<")?;
158158

159-
self = f(self)?;
159+
f(self)?;
160160

161161
write!(self, ">")?;
162162

163-
Ok(self)
163+
Ok(())
164164
}
165165

166166
fn should_print_verbose(&self) -> bool {
@@ -177,5 +177,7 @@ impl Write for AbsolutePathPrinter<'_> {
177177
}
178178

179179
pub fn type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> String {
180-
AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path
180+
let mut printer = AbsolutePathPrinter { tcx, path: String::new() };
181+
printer.print_type(ty).unwrap();
182+
printer.path
181183
}

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -588,60 +588,60 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
588588
self.tcx
589589
}
590590

591-
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
591+
fn print_region(&mut self, _region: ty::Region<'_>) -> Result<(), PrintError> {
592592
Err(fmt::Error)
593593
}
594594

595-
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, PrintError> {
595+
fn print_type(&mut self, _ty: Ty<'tcx>) -> Result<(), PrintError> {
596596
Err(fmt::Error)
597597
}
598598

599599
fn print_dyn_existential(
600-
self,
600+
&mut self,
601601
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
602-
) -> Result<Self, PrintError> {
602+
) -> Result<(), PrintError> {
603603
Err(fmt::Error)
604604
}
605605

606-
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
606+
fn print_const(&mut self, _ct: ty::Const<'tcx>) -> Result<(), PrintError> {
607607
Err(fmt::Error)
608608
}
609609

610-
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, PrintError> {
610+
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
611611
self.segments = vec![self.tcx.crate_name(cnum).to_string()];
612-
Ok(self)
612+
Ok(())
613613
}
614614
fn path_qualified(
615-
self,
615+
&mut self,
616616
_self_ty: Ty<'tcx>,
617617
_trait_ref: Option<ty::TraitRef<'tcx>>,
618-
) -> Result<Self, PrintError> {
618+
) -> Result<(), PrintError> {
619619
Err(fmt::Error)
620620
}
621621

622622
fn path_append_impl(
623-
self,
624-
_print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
623+
&mut self,
624+
_print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
625625
_disambiguated_data: &DisambiguatedDefPathData,
626626
_self_ty: Ty<'tcx>,
627627
_trait_ref: Option<ty::TraitRef<'tcx>>,
628-
) -> Result<Self, PrintError> {
628+
) -> Result<(), PrintError> {
629629
Err(fmt::Error)
630630
}
631631
fn path_append(
632-
mut self,
633-
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
632+
&mut self,
633+
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
634634
disambiguated_data: &DisambiguatedDefPathData,
635-
) -> Result<Self, PrintError> {
636-
self = print_prefix(self)?;
635+
) -> Result<(), PrintError> {
636+
print_prefix(self)?;
637637
self.segments.push(disambiguated_data.to_string());
638-
Ok(self)
638+
Ok(())
639639
}
640640
fn path_generic_args(
641-
self,
642-
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
641+
&mut self,
642+
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
643643
_args: &[GenericArg<'tcx>],
644-
) -> Result<Self, PrintError> {
644+
) -> Result<(), PrintError> {
645645
print_prefix(self)
646646
}
647647
}
@@ -652,9 +652,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
652652
// let _ = [{struct Foo; Foo}, {struct Foo; Foo}];
653653
if did1.krate != did2.krate {
654654
let abs_path = |def_id| {
655-
AbsolutePathPrinter { tcx: self.tcx, segments: vec![] }
656-
.print_def_path(def_id, &[])
657-
.map(|p| p.segments)
655+
let mut printer = AbsolutePathPrinter { tcx: self.tcx, segments: vec![] };
656+
printer.print_def_path(def_id, &[]).map(|_| printer.segments)
658657
};
659658

660659
// We compare strings because DefPath can be different
@@ -1058,7 +1057,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
10581057

10591058
let get_lifetimes = |sig| {
10601059
use rustc_hir::def::Namespace;
1061-
let (_, sig, reg) = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS)
1060+
let (sig, reg) = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS)
10621061
.name_all_regions(sig)
10631062
.unwrap();
10641063
let lts: Vec<String> = reg.into_values().map(|kind| kind.to_string()).collect();

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,15 @@ fn ty_to_string<'tcx>(
200200
ty: Ty<'tcx>,
201201
called_method_def_id: Option<DefId>,
202202
) -> String {
203-
let printer = fmt_printer(infcx, Namespace::TypeNS);
203+
let mut printer = fmt_printer(infcx, Namespace::TypeNS);
204204
let ty = infcx.resolve_vars_if_possible(ty);
205205
match (ty.kind(), called_method_def_id) {
206206
// We don't want the regular output for `fn`s because it includes its path in
207207
// invalid pseudo-syntax, we want the `fn`-pointer output instead.
208-
(ty::FnDef(..), _) => ty.fn_sig(infcx.tcx).print(printer).unwrap().into_buffer(),
208+
(ty::FnDef(..), _) => {
209+
ty.fn_sig(infcx.tcx).print(&mut printer).unwrap();
210+
printer.into_buffer()
211+
}
209212
(_, Some(def_id))
210213
if ty.is_ty_or_numeric_infer()
211214
&& infcx.tcx.get_diagnostic_item(sym::iterator_collect_fn) == Some(def_id) =>
@@ -218,7 +221,10 @@ fn ty_to_string<'tcx>(
218221
//
219222
// We do have to hide the `extern "rust-call"` ABI in that case though,
220223
// which is too much of a bother for now.
221-
_ => ty.print(printer).unwrap().into_buffer(),
224+
_ => {
225+
ty.print(&mut printer).unwrap();
226+
printer.into_buffer()
227+
}
222228
}
223229
}
224230

@@ -285,8 +291,9 @@ impl<'tcx> InferCtxt<'tcx> {
285291
if let Some(highlight) = highlight {
286292
printer.region_highlight_mode = highlight;
287293
}
294+
ty.print(&mut printer).unwrap();
288295
InferenceDiagnosticsData {
289-
name: ty.print(printer).unwrap().into_buffer(),
296+
name: printer.into_buffer(),
290297
span: None,
291298
kind: UnderspecifiedArgKind::Type { prefix: ty.prefix_string(self.tcx) },
292299
parent: None,
@@ -312,8 +319,9 @@ impl<'tcx> InferCtxt<'tcx> {
312319
if let Some(highlight) = highlight {
313320
printer.region_highlight_mode = highlight;
314321
}
322+
ct.print(&mut printer).unwrap();
315323
InferenceDiagnosticsData {
316-
name: ct.print(printer).unwrap().into_buffer(),
324+
name: printer.into_buffer(),
317325
span: Some(origin.span),
318326
kind: UnderspecifiedArgKind::Const { is_parameter: false },
319327
parent: None,
@@ -329,8 +337,9 @@ impl<'tcx> InferCtxt<'tcx> {
329337
if let Some(highlight) = highlight {
330338
printer.region_highlight_mode = highlight;
331339
}
340+
ct.print(&mut printer).unwrap();
332341
InferenceDiagnosticsData {
333-
name: ct.print(printer).unwrap().into_buffer(),
342+
name: printer.into_buffer(),
334343
span: None,
335344
kind: UnderspecifiedArgKind::Const { is_parameter: false },
336345
parent: None,
@@ -487,7 +496,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
487496
{
488497
"Vec<_>".to_string()
489498
} else {
490-
fmt_printer(self, Namespace::TypeNS)
499+
let mut printer = fmt_printer(self, Namespace::TypeNS);
500+
printer
491501
.comma_sep(generic_args.iter().copied().map(|arg| {
492502
if arg.is_suggestable(self.tcx, true) {
493503
return arg;
@@ -512,8 +522,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
512522
.into(),
513523
}
514524
}))
515-
.unwrap()
516-
.into_buffer()
525+
.unwrap();
526+
printer.into_buffer()
517527
};
518528

519529
if !have_turbofish {
@@ -525,8 +535,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
525535
}
526536
}
527537
InferSourceKind::FullyQualifiedMethodCall { receiver, successor, args, def_id } => {
528-
let printer = fmt_printer(self, Namespace::ValueNS);
529-
let def_path = printer.print_def_path(def_id, args).unwrap().into_buffer();
538+
let mut printer = fmt_printer(self, Namespace::ValueNS);
539+
printer.print_def_path(def_id, args).unwrap();
540+
let def_path = printer.into_buffer();
530541

531542
// We only care about whether we have to add `&` or `&mut ` for now.
532543
// This is the case if the last adjustment is a borrow and the

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ where
4949
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS);
5050
printer.region_highlight_mode = self.highlight;
5151

52-
let s = self.value.print(printer)?.into_buffer();
53-
f.write_str(&s)
52+
self.value.print(&mut printer)?;
53+
f.write_str(&printer.into_buffer())
5454
}
5555
}
5656

‎compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,9 @@ fn foo(&self) -> Self::T { String::new() }
734734
}
735735

736736
pub fn format_generic_args(&self, args: &[ty::GenericArg<'tcx>]) -> String {
737-
FmtPrinter::new(self.tcx, hir::def::Namespace::TypeNS)
738-
.path_generic_args(Ok, args)
739-
.expect("could not write to `String`.")
740-
.into_buffer()
737+
FmtPrinter::print_string(self.tcx, hir::def::Namespace::TypeNS, |cx| {
738+
cx.path_generic_args(|_| Ok(()), args)
739+
})
740+
.expect("could not write to `String`.")
741741
}
742742
}

‎compiler/rustc_lint/src/context.rs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,35 +1210,35 @@ impl<'tcx> LateContext<'tcx> {
12101210
self.tcx
12111211
}
12121212

1213-
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
1214-
Ok(self)
1213+
fn print_region(&mut self, _region: ty::Region<'_>) -> Result<(), PrintError> {
1214+
Ok(())
12151215
}
12161216

1217-
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, PrintError> {
1218-
Ok(self)
1217+
fn print_type(&mut self, _ty: Ty<'tcx>) -> Result<(), PrintError> {
1218+
Ok(())
12191219
}
12201220

12211221
fn print_dyn_existential(
1222-
self,
1222+
&mut self,
12231223
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
1224-
) -> Result<Self, PrintError> {
1225-
Ok(self)
1224+
) -> Result<(), PrintError> {
1225+
Ok(())
12261226
}
12271227

1228-
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
1229-
Ok(self)
1228+
fn print_const(&mut self, _ct: ty::Const<'tcx>) -> Result<(), PrintError> {
1229+
Ok(())
12301230
}
12311231

1232-
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, PrintError> {
1232+
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
12331233
self.path = vec![self.tcx.crate_name(cnum)];
1234-
Ok(self)
1234+
Ok(())
12351235
}
12361236

12371237
fn path_qualified(
1238-
mut self,
1238+
&mut self,
12391239
self_ty: Ty<'tcx>,
12401240
trait_ref: Option<ty::TraitRef<'tcx>>,
1241-
) -> Result<Self, PrintError> {
1241+
) -> Result<(), PrintError> {
12421242
if trait_ref.is_none() {
12431243
if let ty::Adt(def, args) = self_ty.kind() {
12441244
return self.print_def_path(def.did(), args);
@@ -1251,21 +1251,21 @@ impl<'tcx> LateContext<'tcx> {
12511251
Some(trait_ref) => Symbol::intern(&format!("{trait_ref:?}")),
12521252
None => Symbol::intern(&format!("<{self_ty}>")),
12531253
}];
1254-
Ok(self)
1254+
Ok(())
12551255
})
12561256
}
12571257

12581258
fn path_append_impl(
1259-
self,
1260-
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
1259+
&mut self,
1260+
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
12611261
_disambiguated_data: &DisambiguatedDefPathData,
12621262
self_ty: Ty<'tcx>,
12631263
trait_ref: Option<ty::TraitRef<'tcx>>,
1264-
) -> Result<Self, PrintError> {
1265-
let mut path = print_prefix(self)?;
1264+
) -> Result<(), PrintError> {
1265+
print_prefix(self)?;
12661266

12671267
// This shouldn't ever be needed, but just in case:
1268-
path.path.push(match trait_ref {
1268+
self.path.push(match trait_ref {
12691269
Some(trait_ref) => {
12701270
with_no_trimmed_paths!(Symbol::intern(&format!(
12711271
"<impl {} for {}>",
@@ -1278,38 +1278,37 @@ impl<'tcx> LateContext<'tcx> {
12781278
}
12791279
});
12801280

1281-
Ok(path)
1281+
Ok(())
12821282
}
12831283

12841284
fn path_append(
1285-
self,
1286-
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
1285+
&mut self,
1286+
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
12871287
disambiguated_data: &DisambiguatedDefPathData,
1288-
) -> Result<Self, PrintError> {
1289-
let mut path = print_prefix(self)?;
1288+
) -> Result<(), PrintError> {
1289+
print_prefix(self)?;
12901290

12911291
// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
12921292
if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
1293-
return Ok(path);
1293+
return Ok(());
12941294
}
12951295

1296-
path.path.push(Symbol::intern(&disambiguated_data.data.to_string()));
1297-
Ok(path)
1296+
self.path.push(Symbol::intern(&disambiguated_data.data.to_string()));
1297+
Ok(())
12981298
}
12991299

13001300
fn path_generic_args(
1301-
self,
1302-
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
1301+
&mut self,
1302+
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
13031303
_args: &[GenericArg<'tcx>],
1304-
) -> Result<Self, PrintError> {
1304+
) -> Result<(), PrintError> {
13051305
print_prefix(self)
13061306
}
13071307
}
13081308

1309-
AbsolutePathPrinter { tcx: self.tcx, path: vec![] }
1310-
.print_def_path(def_id, &[])
1311-
.unwrap()
1312-
.path
1309+
let mut printer = AbsolutePathPrinter { tcx: self.tcx, path: vec![] };
1310+
printer.print_def_path(def_id, &[]).unwrap();
1311+
printer.path
13131312
}
13141313

13151314
/// Returns the associated type `name` for `self_ty` as an implementation of `trait_id`.

‎compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -983,9 +983,9 @@ impl<'tcx> Debug for Rvalue<'tcx> {
983983
ty::tls::with(|tcx| {
984984
let variant_def = &tcx.adt_def(adt_did).variant(variant);
985985
let args = tcx.lift(args).expect("could not lift for printing");
986-
let name = FmtPrinter::new(tcx, Namespace::ValueNS)
987-
.print_def_path(variant_def.def_id, args)?
988-
.into_buffer();
986+
let name = FmtPrinter::print_string(tcx, Namespace::ValueNS, |cx| {
987+
cx.print_def_path(variant_def.def_id, args)
988+
})?;
989989

990990
match variant_def.ctor_kind() {
991991
Some(CtorKind::Const) => fmt.write_str(&name),
@@ -1725,7 +1725,7 @@ fn pretty_print_const_value_tcx<'tcx>(
17251725
let args = tcx.lift(args).unwrap();
17261726
let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS);
17271727
cx.print_alloc_ids = true;
1728-
let cx = cx.print_value_path(variant_def.def_id, args)?;
1728+
cx.print_value_path(variant_def.def_id, args)?;
17291729
fmt.write_str(&cx.into_buffer())?;
17301730

17311731
match variant_def.ctor_kind() {
@@ -1760,14 +1760,14 @@ fn pretty_print_const_value_tcx<'tcx>(
17601760
let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS);
17611761
cx.print_alloc_ids = true;
17621762
let ty = tcx.lift(ty).unwrap();
1763-
cx = cx.pretty_print_const_scalar(scalar, ty)?;
1763+
cx.pretty_print_const_scalar(scalar, ty)?;
17641764
fmt.write_str(&cx.into_buffer())?;
17651765
return Ok(());
17661766
}
17671767
(ConstValue::ZeroSized, ty::FnDef(d, s)) => {
17681768
let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS);
17691769
cx.print_alloc_ids = true;
1770-
let cx = cx.print_value_path(*d, s)?;
1770+
cx.print_value_path(*d, s)?;
17711771
fmt.write_str(&cx.into_buffer())?;
17721772
return Ok(());
17731773
}

‎compiler/rustc_middle/src/ty/error.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -315,26 +315,25 @@ impl<'tcx> Ty<'tcx> {
315315
impl<'tcx> TyCtxt<'tcx> {
316316
pub fn ty_string_with_limit(self, ty: Ty<'tcx>, length_limit: usize) -> String {
317317
let mut type_limit = 50;
318-
let regular = FmtPrinter::new(self, hir::def::Namespace::TypeNS)
319-
.pretty_print_type(ty)
320-
.expect("could not write to `String`")
321-
.into_buffer();
318+
let regular = FmtPrinter::print_string(self, hir::def::Namespace::TypeNS, |cx| {
319+
cx.pretty_print_type(ty)
320+
})
321+
.expect("could not write to `String`");
322322
if regular.len() <= length_limit {
323323
return regular;
324324
}
325325
let mut short;
326326
loop {
327327
// Look for the longest properly trimmed path that still fits in length_limit.
328-
short = with_forced_trimmed_paths!(
329-
FmtPrinter::new_with_limit(
328+
short = with_forced_trimmed_paths!({
329+
let mut cx = FmtPrinter::new_with_limit(
330330
self,
331331
hir::def::Namespace::TypeNS,
332332
rustc_session::Limit(type_limit),
333-
)
334-
.pretty_print_type(ty)
335-
.expect("could not write to `String`")
336-
.into_buffer()
337-
);
333+
);
334+
cx.pretty_print_type(ty).expect("could not write to `String`");
335+
cx.into_buffer()
336+
});
338337
if short.len() <= length_limit || type_limit == 0 {
339338
break;
340339
}
@@ -344,10 +343,10 @@ impl<'tcx> TyCtxt<'tcx> {
344343
}
345344

346345
pub fn short_ty_string(self, ty: Ty<'tcx>) -> (String, Option<PathBuf>) {
347-
let regular = FmtPrinter::new(self, hir::def::Namespace::TypeNS)
348-
.pretty_print_type(ty)
349-
.expect("could not write to `String`")
350-
.into_buffer();
346+
let regular = FmtPrinter::print_string(self, hir::def::Namespace::TypeNS, |cx| {
347+
cx.pretty_print_type(ty)
348+
})
349+
.expect("could not write to `String`");
351350

352351
if !self.sess.opts.unstable_opts.write_long_types_to_disk {
353352
return (regular, None);

‎compiler/rustc_middle/src/ty/instance.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,9 @@ fn fmt_instance(
299299
ty::tls::with(|tcx| {
300300
let args = tcx.lift(instance.args).expect("could not lift for printing");
301301

302-
let s = FmtPrinter::new_with_limit(tcx, Namespace::ValueNS, type_length)
303-
.print_def_path(instance.def_id(), args)?
304-
.into_buffer();
302+
let mut cx = FmtPrinter::new_with_limit(tcx, Namespace::ValueNS, type_length);
303+
cx.print_def_path(instance.def_id(), args)?;
304+
let s = cx.into_buffer();
305305
f.write_str(&s)
306306
})?;
307307

‎compiler/rustc_middle/src/ty/print/mod.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub type PrintError = std::fmt::Error;
1515
// FIXME(eddyb) false positive, the lifetime parameters are used with `P: Printer<...>`.
1616
#[allow(unused_lifetimes)]
1717
pub trait Print<'tcx, P> {
18-
fn print(&self, cx: P) -> Result<P, PrintError>;
18+
fn print(&self, cx: &mut P) -> Result<(), PrintError>;
1919
}
2020

2121
/// Interface for outputting user-facing "type-system entities"
@@ -31,70 +31,70 @@ pub trait Printer<'tcx>: Sized {
3131
fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
3232

3333
fn print_def_path(
34-
self,
34+
&mut self,
3535
def_id: DefId,
3636
args: &'tcx [GenericArg<'tcx>],
37-
) -> Result<Self, PrintError> {
37+
) -> Result<(), PrintError> {
3838
self.default_print_def_path(def_id, args)
3939
}
4040

4141
fn print_impl_path(
42-
self,
42+
&mut self,
4343
impl_def_id: DefId,
4444
args: &'tcx [GenericArg<'tcx>],
4545
self_ty: Ty<'tcx>,
4646
trait_ref: Option<ty::TraitRef<'tcx>>,
47-
) -> Result<Self, PrintError> {
47+
) -> Result<(), PrintError> {
4848
self.default_print_impl_path(impl_def_id, args, self_ty, trait_ref)
4949
}
5050

51-
fn print_region(self, region: ty::Region<'tcx>) -> Result<Self, PrintError>;
51+
fn print_region(&mut self, region: ty::Region<'tcx>) -> Result<(), PrintError>;
5252

53-
fn print_type(self, ty: Ty<'tcx>) -> Result<Self, PrintError>;
53+
fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError>;
5454

5555
fn print_dyn_existential(
56-
self,
56+
&mut self,
5757
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
58-
) -> Result<Self, PrintError>;
58+
) -> Result<(), PrintError>;
5959

60-
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, PrintError>;
60+
fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError>;
6161

62-
fn path_crate(self, cnum: CrateNum) -> Result<Self, PrintError>;
62+
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError>;
6363

6464
fn path_qualified(
65-
self,
65+
&mut self,
6666
self_ty: Ty<'tcx>,
6767
trait_ref: Option<ty::TraitRef<'tcx>>,
68-
) -> Result<Self, PrintError>;
68+
) -> Result<(), PrintError>;
6969

7070
fn path_append_impl(
71-
self,
72-
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
71+
&mut self,
72+
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
7373
disambiguated_data: &DisambiguatedDefPathData,
7474
self_ty: Ty<'tcx>,
7575
trait_ref: Option<ty::TraitRef<'tcx>>,
76-
) -> Result<Self, PrintError>;
76+
) -> Result<(), PrintError>;
7777

7878
fn path_append(
79-
self,
80-
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
79+
&mut self,
80+
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
8181
disambiguated_data: &DisambiguatedDefPathData,
82-
) -> Result<Self, PrintError>;
82+
) -> Result<(), PrintError>;
8383

8484
fn path_generic_args(
85-
self,
86-
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
85+
&mut self,
86+
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
8787
args: &[GenericArg<'tcx>],
88-
) -> Result<Self, PrintError>;
88+
) -> Result<(), PrintError>;
8989

9090
// Defaults (should not be overridden):
9191

9292
#[instrument(skip(self), level = "debug")]
9393
fn default_print_def_path(
94-
self,
94+
&mut self,
9595
def_id: DefId,
9696
args: &'tcx [GenericArg<'tcx>],
97-
) -> Result<Self, PrintError> {
97+
) -> Result<(), PrintError> {
9898
let key = self.tcx().def_key(def_id);
9999
debug!(?key);
100100

@@ -161,7 +161,7 @@ pub trait Printer<'tcx>: Sized {
161161
}
162162

163163
self.path_append(
164-
|cx: Self| {
164+
|cx: &mut Self| {
165165
if trait_qualify_parent {
166166
let trait_ref = ty::TraitRef::new(
167167
cx.tcx(),
@@ -180,12 +180,12 @@ pub trait Printer<'tcx>: Sized {
180180
}
181181

182182
fn default_print_impl_path(
183-
self,
183+
&mut self,
184184
impl_def_id: DefId,
185185
_args: &'tcx [GenericArg<'tcx>],
186186
self_ty: Ty<'tcx>,
187187
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
188-
) -> Result<Self, PrintError> {
188+
) -> Result<(), PrintError> {
189189
debug!(
190190
"default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}",
191191
impl_def_id, self_ty, impl_trait_ref
@@ -286,25 +286,25 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
286286
}
287287

288288
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Region<'tcx> {
289-
fn print(&self, cx: P) -> Result<P, PrintError> {
289+
fn print(&self, cx: &mut P) -> Result<(), PrintError> {
290290
cx.print_region(*self)
291291
}
292292
}
293293

294294
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for Ty<'tcx> {
295-
fn print(&self, cx: P) -> Result<P, PrintError> {
295+
fn print(&self, cx: &mut P) -> Result<(), PrintError> {
296296
cx.print_type(*self)
297297
}
298298
}
299299

300300
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
301-
fn print(&self, cx: P) -> Result<P, PrintError> {
301+
fn print(&self, cx: &mut P) -> Result<(), PrintError> {
302302
cx.print_dyn_existential(self)
303303
}
304304
}
305305

306306
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Const<'tcx> {
307-
fn print(&self, cx: P) -> Result<P, PrintError> {
307+
fn print(&self, cx: &mut P) -> Result<(), PrintError> {
308308
cx.print_const(*self)
309309
}
310310
}

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

Lines changed: 265 additions & 248 deletions
Large diffs are not rendered by default.

‎compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ impl fmt::Debug for ty::TraitDef {
2222
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2323
ty::tls::with(|tcx| {
2424
with_no_trimmed_paths!({
25-
f.write_str(
26-
&FmtPrinter::new(tcx, Namespace::TypeNS)
27-
.print_def_path(self.def_id, &[])?
28-
.into_buffer(),
29-
)
25+
let s = FmtPrinter::print_string(tcx, Namespace::TypeNS, |cx| {
26+
cx.print_def_path(self.def_id, &[])
27+
})?;
28+
f.write_str(&s)
3029
})
3130
})
3231
}
@@ -36,11 +35,10 @@ impl<'tcx> fmt::Debug for ty::AdtDef<'tcx> {
3635
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3736
ty::tls::with(|tcx| {
3837
with_no_trimmed_paths!({
39-
f.write_str(
40-
&FmtPrinter::new(tcx, Namespace::TypeNS)
41-
.print_def_path(self.did(), &[])?
42-
.into_buffer(),
43-
)
38+
let s = FmtPrinter::print_string(tcx, Namespace::TypeNS, |cx| {
39+
cx.print_def_path(self.did(), &[])
40+
})?;
41+
f.write_str(&s)
4442
})
4543
})
4644
}
@@ -350,9 +348,8 @@ impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ty::Const<'tcx> {
350348
let ConstKind::Value(valtree) = lifted.kind() else {
351349
bug!("we checked that this is a valtree")
352350
};
353-
let cx = FmtPrinter::new(tcx, Namespace::ValueNS);
354-
let cx =
355-
cx.pretty_print_const_valtree(valtree, lifted.ty(), /*print_ty*/ true)?;
351+
let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS);
352+
cx.pretty_print_const_valtree(valtree, lifted.ty(), /*print_ty*/ true)?;
356353
f.write_str(&cx.into_buffer())
357354
});
358355
}

‎compiler/rustc_symbol_mangling/src/legacy.rs

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,16 @@ struct SymbolPrinter<'tcx> {
199199
// `PrettyPrinter` aka pretty printing of e.g. types in paths,
200200
// symbol names should have their own printing machinery.
201201

202-
impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
202+
impl<'tcx> Printer<'tcx> for SymbolPrinter<'tcx> {
203203
fn tcx(&self) -> TyCtxt<'tcx> {
204204
self.tcx
205205
}
206206

207-
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
208-
Ok(self)
207+
fn print_region(&mut self, _region: ty::Region<'_>) -> Result<(), PrintError> {
208+
Ok(())
209209
}
210210

211-
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
211+
fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
212212
match *ty.kind() {
213213
// Print all nominal types as paths (unlike `pretty_print_type`).
214214
ty::FnDef(def_id, args)
@@ -220,17 +220,17 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
220220
// -Zverbose flag, so we cannot reuse it here.
221221
ty::Array(ty, size) => {
222222
self.write_str("[")?;
223-
self = self.print_type(ty)?;
223+
self.print_type(ty)?;
224224
self.write_str("; ")?;
225225
if let Some(size) = size.try_to_target_usize(self.tcx()) {
226226
write!(self, "{size}")?
227227
} else if let ty::ConstKind::Param(param) = size.kind() {
228-
self = param.print(self)?
228+
param.print(self)?
229229
} else {
230230
self.write_str("_")?
231231
}
232232
self.write_str("]")?;
233-
Ok(self)
233+
Ok(())
234234
}
235235

236236
ty::Alias(ty::Inherent, _) => panic!("unexpected inherent projection"),
@@ -240,21 +240,21 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
240240
}
241241

242242
fn print_dyn_existential(
243-
mut self,
243+
&mut self,
244244
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
245-
) -> Result<Self, PrintError> {
245+
) -> Result<(), PrintError> {
246246
let mut first = true;
247247
for p in predicates {
248248
if !first {
249249
write!(self, "+")?;
250250
}
251251
first = false;
252-
self = p.print(self)?;
252+
p.print(self)?;
253253
}
254-
Ok(self)
254+
Ok(())
255255
}
256256

257-
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
257+
fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
258258
// only print integers
259259
match (ct.kind(), ct.ty().kind()) {
260260
(ty::ConstKind::Value(ty::ValTree::Leaf(scalar)), ty::Int(_) | ty::Uint(_)) => {
@@ -269,18 +269,18 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
269269
}
270270
_ => self.write_str("_")?,
271271
}
272-
Ok(self)
272+
Ok(())
273273
}
274274

275-
fn path_crate(self, cnum: CrateNum) -> Result<Self, PrintError> {
275+
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
276276
self.write_str(self.tcx.crate_name(cnum).as_str())?;
277-
Ok(self)
277+
Ok(())
278278
}
279279
fn path_qualified(
280-
self,
280+
&mut self,
281281
self_ty: Ty<'tcx>,
282282
trait_ref: Option<ty::TraitRef<'tcx>>,
283-
) -> Result<Self, PrintError> {
283+
) -> Result<(), PrintError> {
284284
// Similar to `pretty_path_qualified`, but for the other
285285
// types that are printed as paths (see `print_type` above).
286286
match self_ty.kind() {
@@ -295,15 +295,15 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
295295
}
296296

297297
fn path_append_impl(
298-
self,
299-
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
298+
&mut self,
299+
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
300300
_disambiguated_data: &DisambiguatedDefPathData,
301301
self_ty: Ty<'tcx>,
302302
trait_ref: Option<ty::TraitRef<'tcx>>,
303-
) -> Result<Self, PrintError> {
303+
) -> Result<(), PrintError> {
304304
self.pretty_path_append_impl(
305-
|mut cx| {
306-
cx = print_prefix(cx)?;
305+
|cx| {
306+
print_prefix(cx)?;
307307

308308
if cx.keep_within_component {
309309
// HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
@@ -312,22 +312,22 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
312312
cx.path.finalize_pending_component();
313313
}
314314

315-
Ok(cx)
315+
Ok(())
316316
},
317317
self_ty,
318318
trait_ref,
319319
)
320320
}
321321
fn path_append(
322-
mut self,
323-
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
322+
&mut self,
323+
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
324324
disambiguated_data: &DisambiguatedDefPathData,
325-
) -> Result<Self, PrintError> {
326-
self = print_prefix(self)?;
325+
) -> Result<(), PrintError> {
326+
print_prefix(self)?;
327327

328328
// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
329329
if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
330-
return Ok(self);
330+
return Ok(());
331331
}
332332

333333
if self.keep_within_component {
@@ -339,57 +339,57 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
339339

340340
write!(self, "{}", disambiguated_data.data)?;
341341

342-
Ok(self)
342+
Ok(())
343343
}
344344
fn path_generic_args(
345-
mut self,
346-
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
345+
&mut self,
346+
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
347347
args: &[GenericArg<'tcx>],
348-
) -> Result<Self, PrintError> {
349-
self = print_prefix(self)?;
348+
) -> Result<(), PrintError> {
349+
print_prefix(self)?;
350350

351351
let args =
352352
args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
353353

354354
if args.clone().next().is_some() {
355355
self.generic_delimiters(|cx| cx.comma_sep(args))
356356
} else {
357-
Ok(self)
357+
Ok(())
358358
}
359359
}
360360
}
361361

362-
impl<'tcx> PrettyPrinter<'tcx> for &mut SymbolPrinter<'tcx> {
362+
impl<'tcx> PrettyPrinter<'tcx> for SymbolPrinter<'tcx> {
363363
fn should_print_region(&self, _region: ty::Region<'_>) -> bool {
364364
false
365365
}
366-
fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, PrintError>
366+
fn comma_sep<T>(&mut self, mut elems: impl Iterator<Item = T>) -> Result<(), PrintError>
367367
where
368368
T: Print<'tcx, Self>,
369369
{
370370
if let Some(first) = elems.next() {
371-
self = first.print(self)?;
371+
first.print(self)?;
372372
for elem in elems {
373373
self.write_str(",")?;
374-
self = elem.print(self)?;
374+
elem.print(self)?;
375375
}
376376
}
377-
Ok(self)
377+
Ok(())
378378
}
379379

380380
fn generic_delimiters(
381-
mut self,
382-
f: impl FnOnce(Self) -> Result<Self, PrintError>,
383-
) -> Result<Self, PrintError> {
381+
&mut self,
382+
f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
383+
) -> Result<(), PrintError> {
384384
write!(self, "<")?;
385385

386386
let kept_within_component = mem::replace(&mut self.keep_within_component, true);
387-
self = f(self)?;
387+
f(self)?;
388388
self.keep_within_component = kept_within_component;
389389

390390
write!(self, ">")?;
391391

392-
Ok(self)
392+
Ok(())
393393
}
394394
}
395395

‎compiler/rustc_symbol_mangling/src/v0.rs

Lines changed: 91 additions & 90 deletions
Large diffs are not rendered by default.

‎compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,10 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
246246
if pred_str.len() > 50 {
247247
// We don't need to save the type to a file, we will be talking about this type already
248248
// in a separate note when we explain the obligation, so it will be available that way.
249-
pred_str = predicate
250-
.print(FmtPrinter::new_with_limit(
251-
self.tcx,
252-
Namespace::TypeNS,
253-
rustc_session::Limit(6),
254-
))
255-
.unwrap()
256-
.into_buffer();
249+
let mut cx: FmtPrinter<'_, '_> =
250+
FmtPrinter::new_with_limit(self.tcx, Namespace::TypeNS, rustc_session::Limit(6));
251+
predicate.print(&mut cx).unwrap();
252+
pred_str = cx.into_buffer();
257253
}
258254
let mut err = struct_span_err!(
259255
self.tcx.sess,
@@ -1408,17 +1404,15 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
14081404
self.maybe_detailed_projection_msg(predicate, normalized_term, expected_term)
14091405
})
14101406
.unwrap_or_else(|| {
1411-
with_forced_trimmed_paths!(format!(
1412-
"type mismatch resolving `{}`",
1413-
self.resolve_vars_if_possible(predicate)
1414-
.print(FmtPrinter::new_with_limit(
1415-
self.tcx,
1416-
Namespace::TypeNS,
1417-
rustc_session::Limit(10),
1418-
))
1419-
.unwrap()
1420-
.into_buffer()
1421-
))
1407+
let mut cx = FmtPrinter::new_with_limit(
1408+
self.tcx,
1409+
Namespace::TypeNS,
1410+
rustc_session::Limit(10),
1411+
);
1412+
with_forced_trimmed_paths!(format!("type mismatch resolving `{}`", {
1413+
self.resolve_vars_if_possible(predicate).print(&mut cx).unwrap();
1414+
cx.into_buffer()
1415+
}))
14221416
});
14231417
let mut diag = struct_span_err!(self.tcx.sess, obligation.cause.span, E0271, "{msg}");
14241418

@@ -1463,14 +1457,15 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
14631457
ty.span,
14641458
with_forced_trimmed_paths!(Cow::from(format!(
14651459
"type mismatch resolving `{}`",
1466-
self.resolve_vars_if_possible(predicate)
1467-
.print(FmtPrinter::new_with_limit(
1460+
{
1461+
let mut cx = FmtPrinter::new_with_limit(
14681462
self.tcx,
14691463
Namespace::TypeNS,
14701464
rustc_session::Limit(5),
1471-
))
1472-
.unwrap()
1473-
.into_buffer()
1465+
);
1466+
self.resolve_vars_if_possible(predicate).print(&mut cx).unwrap();
1467+
cx.into_buffer()
1468+
}
14741469
))),
14751470
)),
14761471
_ => None,

0 commit comments

Comments
 (0)
Please sign in to comment.