1
Fork 0

Rollup merge of #116859 - Nilstrieb:more-more-funny-pretty-printers, r=oli-obk

Make `ty::print::Printer` take `&mut self` instead of `self`

based on #116815

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.
This commit is contained in:
Matthias Krüger 2023-10-23 22:26:29 +02:00 committed by GitHub
commit 8af4a3f9cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 615 additions and 592 deletions

View file

@ -588,60 +588,60 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
self.tcx
}
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
fn print_region(&mut self, _region: ty::Region<'_>) -> Result<(), PrintError> {
Err(fmt::Error)
}
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, PrintError> {
fn print_type(&mut self, _ty: Ty<'tcx>) -> Result<(), PrintError> {
Err(fmt::Error)
}
fn print_dyn_existential(
self,
&mut self,
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
Err(fmt::Error)
}
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
fn print_const(&mut self, _ct: ty::Const<'tcx>) -> Result<(), PrintError> {
Err(fmt::Error)
}
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, PrintError> {
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
self.segments = vec![self.tcx.crate_name(cnum).to_string()];
Ok(self)
Ok(())
}
fn path_qualified(
self,
&mut self,
_self_ty: Ty<'tcx>,
_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
Err(fmt::Error)
}
fn path_append_impl(
self,
_print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
&mut self,
_print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
_disambiguated_data: &DisambiguatedDefPathData,
_self_ty: Ty<'tcx>,
_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
Err(fmt::Error)
}
fn path_append(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self, PrintError> {
self = print_prefix(self)?;
) -> Result<(), PrintError> {
print_prefix(self)?;
self.segments.push(disambiguated_data.to_string());
Ok(self)
Ok(())
}
fn path_generic_args(
self,
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
_args: &[GenericArg<'tcx>],
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
print_prefix(self)
}
}
@ -652,9 +652,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
// let _ = [{struct Foo; Foo}, {struct Foo; Foo}];
if did1.krate != did2.krate {
let abs_path = |def_id| {
AbsolutePathPrinter { tcx: self.tcx, segments: vec![] }
.print_def_path(def_id, &[])
.map(|p| p.segments)
let mut printer = AbsolutePathPrinter { tcx: self.tcx, segments: vec![] };
printer.print_def_path(def_id, &[]).map(|_| printer.segments)
};
// We compare strings because DefPath can be different
@ -1071,7 +1070,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let get_lifetimes = |sig| {
use rustc_hir::def::Namespace;
let (_, sig, reg) = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS)
let (sig, reg) = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS)
.name_all_regions(sig)
.unwrap();
let lts: Vec<String> = reg.into_values().map(|kind| kind.to_string()).collect();

View file

@ -200,12 +200,15 @@ fn ty_to_string<'tcx>(
ty: Ty<'tcx>,
called_method_def_id: Option<DefId>,
) -> String {
let printer = fmt_printer(infcx, Namespace::TypeNS);
let mut printer = fmt_printer(infcx, Namespace::TypeNS);
let ty = infcx.resolve_vars_if_possible(ty);
match (ty.kind(), called_method_def_id) {
// We don't want the regular output for `fn`s because it includes its path in
// invalid pseudo-syntax, we want the `fn`-pointer output instead.
(ty::FnDef(..), _) => ty.fn_sig(infcx.tcx).print(printer).unwrap().into_buffer(),
(ty::FnDef(..), _) => {
ty.fn_sig(infcx.tcx).print(&mut printer).unwrap();
printer.into_buffer()
}
(_, Some(def_id))
if ty.is_ty_or_numeric_infer()
&& infcx.tcx.get_diagnostic_item(sym::iterator_collect_fn) == Some(def_id) =>
@ -218,7 +221,10 @@ fn ty_to_string<'tcx>(
//
// We do have to hide the `extern "rust-call"` ABI in that case though,
// which is too much of a bother for now.
_ => ty.print(printer).unwrap().into_buffer(),
_ => {
ty.print(&mut printer).unwrap();
printer.into_buffer()
}
}
}
@ -285,8 +291,9 @@ impl<'tcx> InferCtxt<'tcx> {
if let Some(highlight) = highlight {
printer.region_highlight_mode = highlight;
}
ty.print(&mut printer).unwrap();
InferenceDiagnosticsData {
name: ty.print(printer).unwrap().into_buffer(),
name: printer.into_buffer(),
span: None,
kind: UnderspecifiedArgKind::Type { prefix: ty.prefix_string(self.tcx) },
parent: None,
@ -312,8 +319,9 @@ impl<'tcx> InferCtxt<'tcx> {
if let Some(highlight) = highlight {
printer.region_highlight_mode = highlight;
}
ct.print(&mut printer).unwrap();
InferenceDiagnosticsData {
name: ct.print(printer).unwrap().into_buffer(),
name: printer.into_buffer(),
span: Some(origin.span),
kind: UnderspecifiedArgKind::Const { is_parameter: false },
parent: None,
@ -329,8 +337,9 @@ impl<'tcx> InferCtxt<'tcx> {
if let Some(highlight) = highlight {
printer.region_highlight_mode = highlight;
}
ct.print(&mut printer).unwrap();
InferenceDiagnosticsData {
name: ct.print(printer).unwrap().into_buffer(),
name: printer.into_buffer(),
span: None,
kind: UnderspecifiedArgKind::Const { is_parameter: false },
parent: None,
@ -487,7 +496,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
{
"Vec<_>".to_string()
} else {
fmt_printer(self, Namespace::TypeNS)
let mut printer = fmt_printer(self, Namespace::TypeNS);
printer
.comma_sep(generic_args.iter().copied().map(|arg| {
if arg.is_suggestable(self.tcx, true) {
return arg;
@ -512,8 +522,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
.into(),
}
}))
.unwrap()
.into_buffer()
.unwrap();
printer.into_buffer()
};
if !have_turbofish {
@ -525,8 +535,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
}
InferSourceKind::FullyQualifiedMethodCall { receiver, successor, args, def_id } => {
let printer = fmt_printer(self, Namespace::ValueNS);
let def_path = printer.print_def_path(def_id, args).unwrap().into_buffer();
let mut printer = fmt_printer(self, Namespace::ValueNS);
printer.print_def_path(def_id, args).unwrap();
let def_path = printer.into_buffer();
// We only care about whether we have to add `&` or `&mut ` for now.
// This is the case if the last adjustment is a borrow and the

View file

@ -49,8 +49,8 @@ where
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS);
printer.region_highlight_mode = self.highlight;
let s = self.value.print(printer)?.into_buffer();
f.write_str(&s)
self.value.print(&mut printer)?;
f.write_str(&printer.into_buffer())
}
}

View file

@ -763,9 +763,9 @@ fn foo(&self) -> Self::T { String::new() }
}
pub fn format_generic_args(&self, args: &[ty::GenericArg<'tcx>]) -> String {
FmtPrinter::new(self.tcx, hir::def::Namespace::TypeNS)
.path_generic_args(Ok, args)
.expect("could not write to `String`.")
.into_buffer()
FmtPrinter::print_string(self.tcx, hir::def::Namespace::TypeNS, |cx| {
cx.path_generic_args(|_| Ok(()), args)
})
.expect("could not write to `String`.")
}
}