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

@ -470,7 +470,8 @@ pub(super) fn get_name_for_ty(&self, ty: Ty<'tcx>, counter: usize) -> String {
}
}
ty.print(printer).unwrap().into_buffer()
ty.print(&mut printer).unwrap();
printer.into_buffer()
}
/// Returns the name of the provided `Ty` (that must be a reference)'s region with a
@ -492,7 +493,8 @@ pub(super) fn get_region_name_for_ty(&self, ty: Ty<'tcx>, counter: usize) -> Str
bug!("ty for annotation of borrow region is not a reference");
};
region.print(printer).unwrap().into_buffer()
region.print(&mut printer).unwrap();
printer.into_buffer()
}
}

View File

@ -107,10 +107,10 @@ impl<Prov: Provenance> std::fmt::Display for ImmTy<'_, Prov> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// Helper function for printing a scalar to a FmtPrinter
fn p<'a, 'tcx, Prov: Provenance>(
cx: FmtPrinter<'a, 'tcx>,
cx: &mut FmtPrinter<'a, 'tcx>,
s: Scalar<Prov>,
ty: Ty<'tcx>,
) -> Result<FmtPrinter<'a, 'tcx>, std::fmt::Error> {
) -> Result<(), std::fmt::Error> {
match s {
Scalar::Int(int) => cx.pretty_print_const_scalar_int(int, ty, true),
Scalar::Ptr(ptr, _sz) => {
@ -125,8 +125,9 @@ fn p<'a, 'tcx, Prov: Provenance>(
match self.imm {
Immediate::Scalar(s) => {
if let Some(ty) = tcx.lift(self.layout.ty) {
let cx = FmtPrinter::new(tcx, Namespace::ValueNS);
f.write_str(&p(cx, s, ty)?.into_buffer())?;
let s =
FmtPrinter::print_string(tcx, Namespace::ValueNS, |cx| p(cx, s, ty))?;
f.write_str(&s)?;
return Ok(());
}
write!(f, "{:x}: {}", s, self.layout.ty)

View File

@ -18,11 +18,11 @@ fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
Ok(self)
fn print_region(&mut self, _region: ty::Region<'_>) -> Result<(), PrintError> {
Ok(())
}
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
match *ty.kind() {
// Types without identity.
ty::Bool
@ -43,7 +43,7 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
// Placeholders (all printed as `_` to uniformize them).
ty::Param(_) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) | ty::Error(_) => {
write!(self, "_")?;
Ok(self)
Ok(())
}
// Types with identity (print the module path).
@ -60,44 +60,44 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
}
}
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
self.pretty_print_const(ct, false)
}
fn print_dyn_existential(
self,
&mut self,
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
self.pretty_print_dyn_existential(predicates)
}
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, PrintError> {
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
self.path.push_str(self.tcx.crate_name(cnum).as_str());
Ok(self)
Ok(())
}
fn path_qualified(
self,
&mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
self.pretty_path_qualified(self_ty, trait_ref)
}
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> {
self.pretty_path_append_impl(
|mut cx| {
cx = print_prefix(cx)?;
|cx| {
print_prefix(cx)?;
cx.path.push_str("::");
Ok(cx)
Ok(())
},
self_ty,
trait_ref,
@ -105,29 +105,29 @@ fn path_append_impl(
}
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)?;
write!(self.path, "::{}", disambiguated_data.data).unwrap();
Ok(self)
Ok(())
}
fn path_generic_args(
mut 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> {
self = print_prefix(self)?;
) -> Result<(), PrintError> {
print_prefix(self)?;
let args =
args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
if args.clone().next().is_some() {
self.generic_delimiters(|cx| cx.comma_sep(args))
} else {
Ok(self)
Ok(())
}
}
}
@ -136,31 +136,31 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {
fn should_print_region(&self, _region: ty::Region<'_>) -> bool {
false
}
fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, PrintError>
fn comma_sep<T>(&mut self, mut elems: impl Iterator<Item = T>) -> Result<(), PrintError>
where
T: Print<'tcx, Self>,
{
if let Some(first) = elems.next() {
self = first.print(self)?;
first.print(self)?;
for elem in elems {
self.path.push_str(", ");
self = elem.print(self)?;
elem.print(self)?;
}
}
Ok(self)
Ok(())
}
fn generic_delimiters(
mut self,
f: impl FnOnce(Self) -> Result<Self, PrintError>,
) -> Result<Self, PrintError> {
&mut self,
f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
) -> Result<(), PrintError> {
write!(self, "<")?;
self = f(self)?;
f(self)?;
write!(self, ">")?;
Ok(self)
Ok(())
}
fn should_print_verbose(&self) -> bool {
@ -177,5 +177,7 @@ fn write_str(&mut self, s: &str) -> std::fmt::Result {
}
pub fn type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> String {
AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path
let mut printer = AbsolutePathPrinter { tcx, path: String::new() };
printer.print_type(ty).unwrap();
printer.path
}

View File

@ -588,60 +588,60 @@ fn tcx<'a>(&'a self) -> TyCtxt<'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 @@ fn path_generic_args(
// 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 @@ fn cmp_fn_sig(
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 @@ pub fn extract_inference_diagnostics_data(
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 @@ pub fn extract_inference_diagnostics_data(
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 @@ pub fn extract_inference_diagnostics_data(
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 @@ pub fn emit_inference_failure_err(
{
"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 @@ pub fn emit_inference_failure_err(
.into(),
}
}))
.unwrap()
.into_buffer()
.unwrap();
printer.into_buffer()
};
if !have_turbofish {
@ -525,8 +535,9 @@ pub fn emit_inference_failure_err(
}
}
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 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
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 constrain_associated_type_structured_suggestion(
}
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`.")
}
}

View File

@ -1210,35 +1210,35 @@ fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
Ok(self)
fn print_region(&mut self, _region: ty::Region<'_>) -> Result<(), PrintError> {
Ok(())
}
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, PrintError> {
Ok(self)
fn print_type(&mut self, _ty: Ty<'tcx>) -> Result<(), PrintError> {
Ok(())
}
fn print_dyn_existential(
self,
&mut self,
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self, PrintError> {
Ok(self)
) -> Result<(), PrintError> {
Ok(())
}
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
Ok(self)
fn print_const(&mut self, _ct: ty::Const<'tcx>) -> Result<(), PrintError> {
Ok(())
}
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, PrintError> {
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
self.path = vec![self.tcx.crate_name(cnum)];
Ok(self)
Ok(())
}
fn path_qualified(
mut self,
&mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
if trait_ref.is_none() {
if let ty::Adt(def, args) = self_ty.kind() {
return self.print_def_path(def.did(), args);
@ -1251,21 +1251,21 @@ fn path_qualified(
Some(trait_ref) => Symbol::intern(&format!("{trait_ref:?}")),
None => Symbol::intern(&format!("<{self_ty}>")),
}];
Ok(self)
Ok(())
})
}
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> {
let mut path = print_prefix(self)?;
) -> Result<(), PrintError> {
print_prefix(self)?;
// This shouldn't ever be needed, but just in case:
path.path.push(match trait_ref {
self.path.push(match trait_ref {
Some(trait_ref) => {
with_no_trimmed_paths!(Symbol::intern(&format!(
"<impl {} for {}>",
@ -1278,38 +1278,37 @@ fn path_append_impl(
}
});
Ok(path)
Ok(())
}
fn path_append(
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> {
let mut path = print_prefix(self)?;
) -> Result<(), PrintError> {
print_prefix(self)?;
// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
return Ok(path);
return Ok(());
}
path.path.push(Symbol::intern(&disambiguated_data.data.to_string()));
Ok(path)
self.path.push(Symbol::intern(&disambiguated_data.data.to_string()));
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)
}
}
AbsolutePathPrinter { tcx: self.tcx, path: vec![] }
.print_def_path(def_id, &[])
.unwrap()
.path
let mut printer = AbsolutePathPrinter { tcx: self.tcx, path: vec![] };
printer.print_def_path(def_id, &[]).unwrap();
printer.path
}
/// Returns the associated type `name` for `self_ty` as an implementation of `trait_id`.

View File

@ -998,9 +998,9 @@ fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
let variant_def = &tcx.adt_def(adt_did).variant(variant);
let args = tcx.lift(args).expect("could not lift for printing");
let name = FmtPrinter::new(tcx, Namespace::ValueNS)
.print_def_path(variant_def.def_id, args)?
.into_buffer();
let name = FmtPrinter::print_string(tcx, Namespace::ValueNS, |cx| {
cx.print_def_path(variant_def.def_id, args)
})?;
match variant_def.ctor_kind() {
Some(CtorKind::Const) => fmt.write_str(&name),
@ -1740,7 +1740,7 @@ fn pretty_print_const_value_tcx<'tcx>(
let args = tcx.lift(args).unwrap();
let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS);
cx.print_alloc_ids = true;
let cx = cx.print_value_path(variant_def.def_id, args)?;
cx.print_value_path(variant_def.def_id, args)?;
fmt.write_str(&cx.into_buffer())?;
match variant_def.ctor_kind() {
@ -1775,14 +1775,14 @@ fn pretty_print_const_value_tcx<'tcx>(
let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS);
cx.print_alloc_ids = true;
let ty = tcx.lift(ty).unwrap();
cx = cx.pretty_print_const_scalar(scalar, ty)?;
cx.pretty_print_const_scalar(scalar, ty)?;
fmt.write_str(&cx.into_buffer())?;
return Ok(());
}
(ConstValue::ZeroSized, ty::FnDef(d, s)) => {
let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS);
cx.print_alloc_ids = true;
let cx = cx.print_value_path(*d, s)?;
cx.print_value_path(*d, s)?;
fmt.write_str(&cx.into_buffer())?;
return Ok(());
}

View File

@ -315,26 +315,25 @@ pub fn prefix_string(self, tcx: TyCtxt<'_>) -> Cow<'static, str> {
impl<'tcx> TyCtxt<'tcx> {
pub fn ty_string_with_limit(self, ty: Ty<'tcx>, length_limit: usize) -> String {
let mut type_limit = 50;
let regular = FmtPrinter::new(self, hir::def::Namespace::TypeNS)
.pretty_print_type(ty)
.expect("could not write to `String`")
.into_buffer();
let regular = FmtPrinter::print_string(self, hir::def::Namespace::TypeNS, |cx| {
cx.pretty_print_type(ty)
})
.expect("could not write to `String`");
if regular.len() <= length_limit {
return regular;
}
let mut short;
loop {
// Look for the longest properly trimmed path that still fits in length_limit.
short = with_forced_trimmed_paths!(
FmtPrinter::new_with_limit(
short = with_forced_trimmed_paths!({
let mut cx = FmtPrinter::new_with_limit(
self,
hir::def::Namespace::TypeNS,
rustc_session::Limit(type_limit),
)
.pretty_print_type(ty)
.expect("could not write to `String`")
.into_buffer()
);
);
cx.pretty_print_type(ty).expect("could not write to `String`");
cx.into_buffer()
});
if short.len() <= length_limit || type_limit == 0 {
break;
}
@ -344,10 +343,10 @@ pub fn ty_string_with_limit(self, ty: Ty<'tcx>, length_limit: usize) -> String {
}
pub fn short_ty_string(self, ty: Ty<'tcx>) -> (String, Option<PathBuf>) {
let regular = FmtPrinter::new(self, hir::def::Namespace::TypeNS)
.pretty_print_type(ty)
.expect("could not write to `String`")
.into_buffer();
let regular = FmtPrinter::print_string(self, hir::def::Namespace::TypeNS, |cx| {
cx.pretty_print_type(ty)
})
.expect("could not write to `String`");
if !self.sess.opts.unstable_opts.write_long_types_to_disk {
return (regular, None);

View File

@ -298,9 +298,9 @@ fn fmt_instance(
ty::tls::with(|tcx| {
let args = tcx.lift(instance.args).expect("could not lift for printing");
let s = FmtPrinter::new_with_limit(tcx, Namespace::ValueNS, type_length)
.print_def_path(instance.def_id(), args)?
.into_buffer();
let mut cx = FmtPrinter::new_with_limit(tcx, Namespace::ValueNS, type_length);
cx.print_def_path(instance.def_id(), args)?;
let s = cx.into_buffer();
f.write_str(&s)
})?;

View File

@ -15,7 +15,7 @@
// FIXME(eddyb) false positive, the lifetime parameters are used with `P: Printer<...>`.
#[allow(unused_lifetimes)]
pub trait Print<'tcx, P> {
fn print(&self, cx: P) -> Result<P, PrintError>;
fn print(&self, cx: &mut P) -> Result<(), PrintError>;
}
/// Interface for outputting user-facing "type-system entities"
@ -31,70 +31,70 @@ pub trait Printer<'tcx>: Sized {
fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
fn print_def_path(
self,
&mut self,
def_id: DefId,
args: &'tcx [GenericArg<'tcx>],
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
self.default_print_def_path(def_id, args)
}
fn print_impl_path(
self,
&mut self,
impl_def_id: DefId,
args: &'tcx [GenericArg<'tcx>],
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
self.default_print_impl_path(impl_def_id, args, self_ty, trait_ref)
}
fn print_region(self, region: ty::Region<'tcx>) -> Result<Self, PrintError>;
fn print_region(&mut self, region: ty::Region<'tcx>) -> Result<(), PrintError>;
fn print_type(self, ty: Ty<'tcx>) -> Result<Self, PrintError>;
fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError>;
fn print_dyn_existential(
self,
&mut self,
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self, PrintError>;
) -> Result<(), PrintError>;
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, PrintError>;
fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError>;
fn path_crate(self, cnum: CrateNum) -> Result<Self, PrintError>;
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError>;
fn path_qualified(
self,
&mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, PrintError>;
) -> Result<(), PrintError>;
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>;
fn path_append(
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>;
) -> Result<(), PrintError>;
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>;
// Defaults (should not be overridden):
#[instrument(skip(self), level = "debug")]
fn default_print_def_path(
self,
&mut self,
def_id: DefId,
args: &'tcx [GenericArg<'tcx>],
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
let key = self.tcx().def_key(def_id);
debug!(?key);
@ -161,7 +161,7 @@ fn default_print_def_path(
}
self.path_append(
|cx: Self| {
|cx: &mut Self| {
if trait_qualify_parent {
let trait_ref = ty::TraitRef::new(
cx.tcx(),
@ -180,12 +180,12 @@ fn default_print_def_path(
}
fn default_print_impl_path(
self,
&mut self,
impl_def_id: DefId,
_args: &'tcx [GenericArg<'tcx>],
self_ty: Ty<'tcx>,
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
debug!(
"default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}",
impl_def_id, self_ty, impl_trait_ref
@ -286,25 +286,25 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
}
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Region<'tcx> {
fn print(&self, cx: P) -> Result<P, PrintError> {
fn print(&self, cx: &mut P) -> Result<(), PrintError> {
cx.print_region(*self)
}
}
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for Ty<'tcx> {
fn print(&self, cx: P) -> Result<P, PrintError> {
fn print(&self, cx: &mut P) -> Result<(), PrintError> {
cx.print_type(*self)
}
}
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
fn print(&self, cx: P) -> Result<P, PrintError> {
fn print(&self, cx: &mut P) -> Result<(), PrintError> {
cx.print_dyn_existential(self)
}
}
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Const<'tcx> {
fn print(&self, cx: P) -> Result<P, PrintError> {
fn print(&self, cx: &mut P) -> Result<(), PrintError> {
cx.print_const(*self)
}
}

File diff suppressed because it is too large Load Diff

View File

@ -22,11 +22,10 @@ impl fmt::Debug for ty::TraitDef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
with_no_trimmed_paths!({
f.write_str(
&FmtPrinter::new(tcx, Namespace::TypeNS)
.print_def_path(self.def_id, &[])?
.into_buffer(),
)
let s = FmtPrinter::print_string(tcx, Namespace::TypeNS, |cx| {
cx.print_def_path(self.def_id, &[])
})?;
f.write_str(&s)
})
})
}
@ -36,11 +35,10 @@ impl<'tcx> fmt::Debug for ty::AdtDef<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
with_no_trimmed_paths!({
f.write_str(
&FmtPrinter::new(tcx, Namespace::TypeNS)
.print_def_path(self.did(), &[])?
.into_buffer(),
)
let s = FmtPrinter::print_string(tcx, Namespace::TypeNS, |cx| {
cx.print_def_path(self.did(), &[])
})?;
f.write_str(&s)
})
})
}
@ -350,9 +348,8 @@ fn fmt<InfCtx: InferCtxtLike<TyCtxt<'tcx>>>(
let ConstKind::Value(valtree) = lifted.kind() else {
bug!("we checked that this is a valtree")
};
let cx = FmtPrinter::new(tcx, Namespace::ValueNS);
let cx =
cx.pretty_print_const_valtree(valtree, lifted.ty(), /*print_ty*/ true)?;
let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS);
cx.pretty_print_const_valtree(valtree, lifted.ty(), /*print_ty*/ true)?;
f.write_str(&cx.into_buffer())
});
}

View File

@ -199,16 +199,16 @@ struct SymbolPrinter<'tcx> {
// `PrettyPrinter` aka pretty printing of e.g. types in paths,
// symbol names should have their own printing machinery.
impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
impl<'tcx> Printer<'tcx> for SymbolPrinter<'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
Ok(self)
fn print_region(&mut self, _region: ty::Region<'_>) -> Result<(), PrintError> {
Ok(())
}
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
match *ty.kind() {
// Print all nominal types as paths (unlike `pretty_print_type`).
ty::FnDef(def_id, args)
@ -220,17 +220,17 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
// -Zverbose flag, so we cannot reuse it here.
ty::Array(ty, size) => {
self.write_str("[")?;
self = self.print_type(ty)?;
self.print_type(ty)?;
self.write_str("; ")?;
if let Some(size) = size.try_to_target_usize(self.tcx()) {
write!(self, "{size}")?
} else if let ty::ConstKind::Param(param) = size.kind() {
self = param.print(self)?
param.print(self)?
} else {
self.write_str("_")?
}
self.write_str("]")?;
Ok(self)
Ok(())
}
ty::Alias(ty::Inherent, _) => panic!("unexpected inherent projection"),
@ -240,21 +240,21 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
}
fn print_dyn_existential(
mut self,
&mut self,
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
let mut first = true;
for p in predicates {
if !first {
write!(self, "+")?;
}
first = false;
self = p.print(self)?;
p.print(self)?;
}
Ok(self)
Ok(())
}
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
// only print integers
match (ct.kind(), ct.ty().kind()) {
(ty::ConstKind::Value(ty::ValTree::Leaf(scalar)), ty::Int(_) | ty::Uint(_)) => {
@ -269,18 +269,18 @@ fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
}
_ => self.write_str("_")?,
}
Ok(self)
Ok(())
}
fn path_crate(self, cnum: CrateNum) -> Result<Self, PrintError> {
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
self.write_str(self.tcx.crate_name(cnum).as_str())?;
Ok(self)
Ok(())
}
fn path_qualified(
self,
&mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
// Similar to `pretty_path_qualified`, but for the other
// types that are printed as paths (see `print_type` above).
match self_ty.kind() {
@ -295,15 +295,15 @@ fn path_qualified(
}
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> {
self.pretty_path_append_impl(
|mut cx| {
cx = print_prefix(cx)?;
|cx| {
print_prefix(cx)?;
if cx.keep_within_component {
// HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
@ -312,22 +312,22 @@ fn path_append_impl(
cx.path.finalize_pending_component();
}
Ok(cx)
Ok(())
},
self_ty,
trait_ref,
)
}
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)?;
// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
return Ok(self);
return Ok(());
}
if self.keep_within_component {
@ -339,14 +339,14 @@ fn path_append(
write!(self, "{}", disambiguated_data.data)?;
Ok(self)
Ok(())
}
fn path_generic_args(
mut 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> {
self = print_prefix(self)?;
) -> Result<(), PrintError> {
print_prefix(self)?;
let args =
args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
@ -354,42 +354,42 @@ fn path_generic_args(
if args.clone().next().is_some() {
self.generic_delimiters(|cx| cx.comma_sep(args))
} else {
Ok(self)
Ok(())
}
}
}
impl<'tcx> PrettyPrinter<'tcx> for &mut SymbolPrinter<'tcx> {
impl<'tcx> PrettyPrinter<'tcx> for SymbolPrinter<'tcx> {
fn should_print_region(&self, _region: ty::Region<'_>) -> bool {
false
}
fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, PrintError>
fn comma_sep<T>(&mut self, mut elems: impl Iterator<Item = T>) -> Result<(), PrintError>
where
T: Print<'tcx, Self>,
{
if let Some(first) = elems.next() {
self = first.print(self)?;
first.print(self)?;
for elem in elems {
self.write_str(",")?;
self = elem.print(self)?;
elem.print(self)?;
}
}
Ok(self)
Ok(())
}
fn generic_delimiters(
mut self,
f: impl FnOnce(Self) -> Result<Self, PrintError>,
) -> Result<Self, PrintError> {
&mut self,
f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
) -> Result<(), PrintError> {
write!(self, "<")?;
let kept_within_component = mem::replace(&mut self.keep_within_component, true);
self = f(self)?;
f(self)?;
self.keep_within_component = kept_within_component;
write!(self, ">")?;
Ok(self)
Ok(())
}
}

View File

@ -30,7 +30,7 @@ pub(super) fn mangle<'tcx>(
let args = tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), instance.args);
let prefix = "_R";
let mut cx = &mut SymbolMangler {
let mut cx: SymbolMangler<'_> = SymbolMangler {
tcx,
start_offset: prefix.len(),
paths: FxHashMap::default(),
@ -49,13 +49,13 @@ pub(super) fn mangle<'tcx>(
_ => None,
};
cx = if let Some(shim_kind) = shim_kind {
if let Some(shim_kind) = shim_kind {
cx.path_append_ns(|cx| cx.print_def_path(def_id, args), 'S', 0, shim_kind).unwrap()
} else {
cx.print_def_path(def_id, args).unwrap()
};
if let Some(instantiating_crate) = instantiating_crate {
cx = cx.print_def_path(instantiating_crate.as_def_id(), &[]).unwrap();
cx.print_def_path(instantiating_crate.as_def_id(), &[]).unwrap();
}
std::mem::take(&mut cx.out)
}
@ -65,7 +65,7 @@ pub(super) fn mangle_typeid_for_trait_ref<'tcx>(
trait_ref: ty::PolyExistentialTraitRef<'tcx>,
) -> String {
// FIXME(flip1995): See comment in `mangle_typeid_for_fnabi`.
let mut cx = &mut SymbolMangler {
let mut cx = SymbolMangler {
tcx,
start_offset: 0,
paths: FxHashMap::default(),
@ -74,7 +74,7 @@ pub(super) fn mangle_typeid_for_trait_ref<'tcx>(
binders: vec![],
out: String::new(),
};
cx = cx.print_def_path(trait_ref.def_id(), &[]).unwrap();
cx.print_def_path(trait_ref.def_id(), &[]).unwrap();
std::mem::take(&mut cx.out)
}
@ -179,32 +179,32 @@ fn push_ident(&mut self, ident: &str) {
self.push(ident);
}
fn path_append_ns<'a>(
mut self: &'a mut Self,
print_prefix: impl FnOnce(&'a mut Self) -> Result<&'a mut Self, PrintError>,
fn path_append_ns(
&mut self,
print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
ns: char,
disambiguator: u64,
name: &str,
) -> Result<&'a mut Self, PrintError> {
) -> Result<(), PrintError> {
self.push("N");
self.out.push(ns);
self = print_prefix(self)?;
print_prefix(self)?;
self.push_disambiguator(disambiguator as u64);
self.push_ident(name);
Ok(self)
Ok(())
}
fn print_backref(&mut self, i: usize) -> Result<&mut Self, PrintError> {
fn print_backref(&mut self, i: usize) -> Result<(), PrintError> {
self.push("B");
self.push_integer_62((i - self.start_offset) as u64);
Ok(self)
Ok(())
}
fn in_binder<'a, T>(
mut self: &'a mut Self,
fn in_binder<T>(
&mut self,
value: &ty::Binder<'tcx, T>,
print_value: impl FnOnce(&'a mut Self, &T) -> Result<&'a mut Self, PrintError>,
) -> Result<&'a mut Self, PrintError>
print_value: impl FnOnce(&mut Self, &T) -> Result<(), PrintError>,
) -> Result<(), PrintError>
where
T: TypeVisitable<TyCtxt<'tcx>>,
{
@ -222,45 +222,45 @@ fn in_binder<'a, T>(
lifetime_depths.end += lifetimes;
self.binders.push(BinderLevel { lifetime_depths });
self = print_value(self, value.as_ref().skip_binder())?;
print_value(self, value.as_ref().skip_binder())?;
self.binders.pop();
Ok(self)
Ok(())
}
}
impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
fn print_def_path(
mut self,
&mut self,
def_id: DefId,
args: &'tcx [GenericArg<'tcx>],
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
if let Some(&i) = self.paths.get(&(def_id, args)) {
return self.print_backref(i);
}
let start = self.out.len();
self = self.default_print_def_path(def_id, args)?;
self.default_print_def_path(def_id, args)?;
// Only cache paths that do not refer to an enclosing
// binder (which would change depending on context).
if !args.iter().any(|k| k.has_escaping_bound_vars()) {
self.paths.insert((def_id, args), start);
}
Ok(self)
Ok(())
}
fn print_impl_path(
mut self,
&mut self,
impl_def_id: DefId,
args: &'tcx [GenericArg<'tcx>],
mut self_ty: Ty<'tcx>,
mut impl_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
let key = self.tcx.def_key(impl_def_id);
let parent_def_id = DefId { index: key.parent.unwrap(), ..impl_def_id };
@ -288,7 +288,7 @@ fn print_impl_path(
// Encode impl generic params if the substitutions contain parameters (implying
// polymorphization is enabled) and this isn't an inherent impl.
if impl_trait_ref.is_some() && args.iter().any(|a| a.has_non_region_param()) {
self = self.path_generic_args(
self.path_generic_args(
|this| {
this.path_append_ns(
|cx| cx.print_def_path(parent_def_id, &[]),
@ -301,19 +301,19 @@ fn print_impl_path(
)?;
} else {
self.push_disambiguator(key.disambiguated_data.disambiguator as u64);
self = self.print_def_path(parent_def_id, &[])?;
self.print_def_path(parent_def_id, &[])?;
}
self = self_ty.print(self)?;
self_ty.print(self)?;
if let Some(trait_ref) = impl_trait_ref {
self = self.print_def_path(trait_ref.def_id, trait_ref.args)?;
self.print_def_path(trait_ref.def_id, trait_ref.args)?;
}
Ok(self)
Ok(())
}
fn print_region(self, region: ty::Region<'_>) -> Result<Self, PrintError> {
fn print_region(&mut self, region: ty::Region<'_>) -> Result<(), PrintError> {
let i = match *region {
// Erased lifetimes use the index 0, for a
// shorter mangling of `L_`.
@ -332,10 +332,10 @@ fn print_region(self, region: ty::Region<'_>) -> Result<Self, PrintError> {
};
self.push("L");
self.push_integer_62(i as u64);
Ok(self)
Ok(())
}
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
// Basic types, never cached (single-character).
let basic_type = match ty.kind() {
ty::Bool => "b",
@ -365,7 +365,7 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
};
if !basic_type.is_empty() {
self.push(basic_type);
return Ok(self);
return Ok(());
}
if let Some(&i) = self.types.get(&ty) {
@ -391,9 +391,9 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
hir::Mutability::Mut => "Q",
});
if !r.is_erased() {
self = r.print(self)?;
r.print(self)?;
}
self = ty.print(self)?;
ty.print(self)?;
}
ty::RawPtr(mt) => {
@ -401,23 +401,23 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
hir::Mutability::Not => "P",
hir::Mutability::Mut => "O",
});
self = mt.ty.print(self)?;
mt.ty.print(self)?;
}
ty::Array(ty, len) => {
self.push("A");
self = ty.print(self)?;
self = self.print_const(len)?;
ty.print(self)?;
self.print_const(len)?;
}
ty::Slice(ty) => {
self.push("S");
self = ty.print(self)?;
ty.print(self)?;
}
ty::Tuple(tys) => {
self.push("T");
for ty in tys.iter() {
self = ty.print(self)?;
ty.print(self)?;
}
self.push("E");
}
@ -428,15 +428,15 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
| ty::Alias(ty::Projection | ty::Opaque, ty::AliasTy { def_id, args, .. })
| ty::Closure(def_id, args)
| ty::Coroutine(def_id, args, _) => {
self = self.print_def_path(def_id, args)?;
self.print_def_path(def_id, args)?;
}
ty::Foreign(def_id) => {
self = self.print_def_path(def_id, &[])?;
self.print_def_path(def_id, &[])?;
}
ty::FnPtr(sig) => {
self.push("F");
self = self.in_binder(&sig, |mut cx, sig| {
self.in_binder(&sig, |cx, sig| {
if sig.unsafety == hir::Unsafety::Unsafe {
cx.push("U");
}
@ -454,7 +454,7 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
}
}
for &ty in sig.inputs() {
cx = ty.print(cx)?;
ty.print(cx)?;
}
if sig.c_variadic {
cx.push("v");
@ -470,8 +470,8 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
// FIXME(dyn-star): need to update v0 mangling docs
ty::DynStar => "D*",
});
self = self.print_dyn_existential(predicates)?;
self = r.print(self)?;
self.print_dyn_existential(predicates)?;
r.print(self)?;
}
ty::Alias(ty::Inherent, _) => bug!("symbol_names: unexpected inherent projection"),
@ -484,13 +484,13 @@ fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
if !ty.has_escaping_bound_vars() {
self.types.insert(ty, start);
}
Ok(self)
Ok(())
}
fn print_dyn_existential(
mut self,
&mut self,
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
// Okay, so this is a bit tricky. Imagine we have a trait object like
// `dyn for<'a> Foo<'a, Bar = &'a ()>`. When we mangle this, the
// output looks really close to the syntax, where the `Bar = &'a ()` bit
@ -517,7 +517,7 @@ fn print_dyn_existential(
// [<Trait> [{<Projection>}]] [{<Auto>}]
// Since any predicates after the first one shouldn't change the binders,
// just put them all in the binders of the first.
self = self.in_binder(&predicates[0], |mut cx, _| {
self.in_binder(&predicates[0], |cx, _| {
for predicate in predicates.iter() {
// It would be nice to be able to validate bound vars here, but
// projections can actually include bound vars from super traits
@ -528,30 +528,30 @@ fn print_dyn_existential(
// Use a type that can't appear in defaults of type parameters.
let dummy_self = Ty::new_fresh(cx.tcx, 0);
let trait_ref = trait_ref.with_self_ty(cx.tcx, dummy_self);
cx = cx.print_def_path(trait_ref.def_id, trait_ref.args)?;
cx.print_def_path(trait_ref.def_id, trait_ref.args)?;
}
ty::ExistentialPredicate::Projection(projection) => {
let name = cx.tcx.associated_item(projection.def_id).name;
cx.push("p");
cx.push_ident(name.as_str());
cx = match projection.term.unpack() {
match projection.term.unpack() {
ty::TermKind::Ty(ty) => ty.print(cx),
ty::TermKind::Const(c) => c.print(cx),
}?;
}
ty::ExistentialPredicate::AutoTrait(def_id) => {
cx = cx.print_def_path(*def_id, &[])?;
cx.print_def_path(*def_id, &[])?;
}
}
}
Ok(cx)
Ok(())
})?;
self.push("E");
Ok(self)
Ok(())
}
fn print_const(mut self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
// We only mangle a typed value if the const can be evaluated.
let ct = ct.normalize(self.tcx, ty::ParamEnv::reveal_all());
match ct.kind() {
@ -570,12 +570,13 @@ fn print_const(mut self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
| ty::ConstKind::Error(_) => {
// Never cached (single-character).
self.push("p");
return Ok(self);
return Ok(());
}
}
if let Some(&i) = self.consts.get(&ct) {
return self.print_backref(i);
self.print_backref(i)?;
return Ok(());
}
let start = self.out.len();
@ -583,7 +584,7 @@ fn print_const(mut self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
match ty.kind() {
ty::Uint(_) | ty::Int(_) | ty::Bool | ty::Char => {
self = ty.print(self)?;
ty.print(self)?;
let mut bits = ct.eval_bits(self.tcx, ty::ParamEnv::reveal_all());
@ -645,7 +646,7 @@ fn print_const(mut self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
.ty;
// FIXME(const_generics): add an assert that we only do this for valtrees.
let dereferenced_const = self.tcx.mk_ct_from_kind(ct.kind(), pointee_ty);
self = dereferenced_const.print(self)?;
dereferenced_const.print(self)?;
}
}
}
@ -654,22 +655,22 @@ fn print_const(mut self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
let contents = self.tcx.destructure_const(ct);
let fields = contents.fields.iter().copied();
let print_field_list = |mut this: Self| {
let print_field_list = |this: &mut Self| {
for field in fields.clone() {
this = field.print(this)?;
field.print(this)?;
}
this.push("E");
Ok(this)
Ok(())
};
match *ct.ty().kind() {
ty::Array(..) | ty::Slice(_) => {
self.push("A");
self = print_field_list(self)?;
print_field_list(self)?;
}
ty::Tuple(..) => {
self.push("T");
self = print_field_list(self)?;
print_field_list(self)?;
}
ty::Adt(def, args) => {
let variant_idx =
@ -677,7 +678,7 @@ fn print_const(mut self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
let variant_def = &def.variant(variant_idx);
self.push("V");
self = self.print_def_path(variant_def.def_id, args)?;
self.print_def_path(variant_def.def_id, args)?;
match variant_def.ctor_kind() {
Some(CtorKind::Const) => {
@ -685,7 +686,7 @@ fn print_const(mut self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
}
Some(CtorKind::Fn) => {
self.push("T");
self = print_field_list(self)?;
print_field_list(self)?;
}
None => {
self.push("S");
@ -701,7 +702,7 @@ fn print_const(mut self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
);
self.push_ident(field_name.unwrap_or(kw::Empty).as_str());
self = field.print(self)?;
field.print(self)?;
}
self.push("E");
}
@ -720,47 +721,47 @@ fn print_const(mut self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
if !ct.has_escaping_bound_vars() {
self.consts.insert(ct, start);
}
Ok(self)
Ok(())
}
fn path_crate(self, cnum: CrateNum) -> Result<Self, PrintError> {
fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
self.push("C");
let stable_crate_id = self.tcx.def_path_hash(cnum.as_def_id()).stable_crate_id();
self.push_disambiguator(stable_crate_id.as_u64());
let name = self.tcx.crate_name(cnum);
self.push_ident(name.as_str());
Ok(self)
Ok(())
}
fn path_qualified(
mut self,
&mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
assert!(trait_ref.is_some());
let trait_ref = trait_ref.unwrap();
self.push("Y");
self = self_ty.print(self)?;
self_ty.print(self)?;
self.print_def_path(trait_ref.def_id, trait_ref.args)
}
fn path_append_impl(
self,
_: impl FnOnce(Self) -> Result<Self, PrintError>,
&mut self,
_: impl FnOnce(&mut Self) -> Result<(), PrintError>,
_: &DisambiguatedDefPathData,
_: Ty<'tcx>,
_: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, PrintError> {
) -> Result<(), PrintError> {
// Inlined into `print_impl_path`
unreachable!()
}
fn path_append(
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> {
) -> Result<(), PrintError> {
let ns = match disambiguated_data.data {
// Extern block segments can be skipped, names from extern blocks
// are effectively living in their parent modules.
@ -797,10 +798,10 @@ fn path_append(
}
fn path_generic_args(
mut 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> {
// Don't print any regions if they're all erased.
let print_regions = args.iter().any(|arg| match arg.unpack() {
GenericArgKind::Lifetime(r) => !r.is_erased(),
@ -816,23 +817,23 @@ fn path_generic_args(
}
self.push("I");
self = print_prefix(self)?;
print_prefix(self)?;
for arg in args {
match arg.unpack() {
GenericArgKind::Lifetime(lt) => {
self = lt.print(self)?;
lt.print(self)?;
}
GenericArgKind::Type(ty) => {
self = ty.print(self)?;
ty.print(self)?;
}
GenericArgKind::Const(c) => {
self.push("K");
self = c.print(self)?;
c.print(self)?;
}
}
}
self.push("E");
Ok(self)
Ok(())
}
}

View File

@ -246,14 +246,10 @@ fn build_overflow_error<T>(
if pred_str.len() > 50 {
// We don't need to save the type to a file, we will be talking about this type already
// in a separate note when we explain the obligation, so it will be available that way.
pred_str = predicate
.print(FmtPrinter::new_with_limit(
self.tcx,
Namespace::TypeNS,
rustc_session::Limit(6),
))
.unwrap()
.into_buffer();
let mut cx: FmtPrinter<'_, '_> =
FmtPrinter::new_with_limit(self.tcx, Namespace::TypeNS, rustc_session::Limit(6));
predicate.print(&mut cx).unwrap();
pred_str = cx.into_buffer();
}
let mut err = struct_span_err!(
self.tcx.sess,
@ -1408,17 +1404,15 @@ fn report_projection_error(
self.maybe_detailed_projection_msg(predicate, normalized_term, expected_term)
})
.unwrap_or_else(|| {
with_forced_trimmed_paths!(format!(
"type mismatch resolving `{}`",
self.resolve_vars_if_possible(predicate)
.print(FmtPrinter::new_with_limit(
self.tcx,
Namespace::TypeNS,
rustc_session::Limit(10),
))
.unwrap()
.into_buffer()
))
let mut cx = FmtPrinter::new_with_limit(
self.tcx,
Namespace::TypeNS,
rustc_session::Limit(10),
);
with_forced_trimmed_paths!(format!("type mismatch resolving `{}`", {
self.resolve_vars_if_possible(predicate).print(&mut cx).unwrap();
cx.into_buffer()
}))
});
let mut diag = struct_span_err!(self.tcx.sess, obligation.cause.span, E0271, "{msg}");
@ -1463,14 +1457,15 @@ fn report_projection_error(
ty.span,
with_forced_trimmed_paths!(Cow::from(format!(
"type mismatch resolving `{}`",
self.resolve_vars_if_possible(predicate)
.print(FmtPrinter::new_with_limit(
{
let mut cx = FmtPrinter::new_with_limit(
self.tcx,
Namespace::TypeNS,
rustc_session::Limit(5),
))
.unwrap()
.into_buffer()
);
self.resolve_vars_if_possible(predicate).print(&mut cx).unwrap();
cx.into_buffer()
}
))),
)),
_ => None,