Skip to content

Commit e5d1156

Browse files
committed
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

+4-2
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

+5-4
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

+39-37
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

+23-24
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();

0 commit comments

Comments
 (0)