Skip to content

Commit e8b8c78

Browse files
committed
Auto merge of rust-lang#116815 - Nilstrieb:more-funny-pretty-printers, r=compiler-errors
Remove lots of generics from `ty::print` All of these generics mostly resolve to the same thing, which means we can remove them, greatly simplifying the types involved in pretty printing and unlocking another simplification (that is not performed in this PR): Using `&mut self` instead of passing `self` through the return type. cc `@eddyb` you probably know why it's like this, just checking in and making sure I didn't do anything bad r? oli-obk
2 parents 6d7160c + 6fc6a6d commit e8b8c78

File tree

9 files changed

+214
-310
lines changed

9 files changed

+214
-310
lines changed

compiler/rustc_const_eval/src/util/type_name.rs

+17-25
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir::def_id::CrateNum;
33
use rustc_hir::definitions::DisambiguatedDefPathData;
44
use rustc_middle::ty::{
55
self,
6-
print::{PrettyPrinter, Print, Printer},
6+
print::{PrettyPrinter, Print, PrintError, Printer},
77
GenericArg, GenericArgKind, Ty, TyCtxt,
88
};
99
use std::fmt::Write;
@@ -14,23 +14,15 @@ struct AbsolutePathPrinter<'tcx> {
1414
}
1515

1616
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
17-
type Error = std::fmt::Error;
18-
19-
type Path = Self;
20-
type Region = Self;
21-
type Type = Self;
22-
type DynExistential = Self;
23-
type Const = Self;
24-
2517
fn tcx(&self) -> TyCtxt<'tcx> {
2618
self.tcx
2719
}
2820

29-
fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
21+
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
3022
Ok(self)
3123
}
3224

33-
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
25+
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
3426
match *ty.kind() {
3527
// Types without identity.
3628
ty::Bool
@@ -68,18 +60,18 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
6860
}
6961
}
7062

71-
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
63+
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
7264
self.pretty_print_const(ct, false)
7365
}
7466

7567
fn print_dyn_existential(
7668
self,
7769
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
78-
) -> Result<Self::DynExistential, Self::Error> {
70+
) -> Result<Self, PrintError> {
7971
self.pretty_print_dyn_existential(predicates)
8072
}
8173

82-
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
74+
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, PrintError> {
8375
self.path.push_str(self.tcx.crate_name(cnum).as_str());
8476
Ok(self)
8577
}
@@ -88,17 +80,17 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
8880
self,
8981
self_ty: Ty<'tcx>,
9082
trait_ref: Option<ty::TraitRef<'tcx>>,
91-
) -> Result<Self::Path, Self::Error> {
83+
) -> Result<Self, PrintError> {
9284
self.pretty_path_qualified(self_ty, trait_ref)
9385
}
9486

9587
fn path_append_impl(
9688
self,
97-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
89+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
9890
_disambiguated_data: &DisambiguatedDefPathData,
9991
self_ty: Ty<'tcx>,
10092
trait_ref: Option<ty::TraitRef<'tcx>>,
101-
) -> Result<Self::Path, Self::Error> {
93+
) -> Result<Self, PrintError> {
10294
self.pretty_path_append_impl(
10395
|mut cx| {
10496
cx = print_prefix(cx)?;
@@ -114,9 +106,9 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
114106

115107
fn path_append(
116108
mut self,
117-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
109+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
118110
disambiguated_data: &DisambiguatedDefPathData,
119-
) -> Result<Self::Path, Self::Error> {
111+
) -> Result<Self, PrintError> {
120112
self = print_prefix(self)?;
121113

122114
write!(self.path, "::{}", disambiguated_data.data).unwrap();
@@ -126,9 +118,9 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
126118

127119
fn path_generic_args(
128120
mut self,
129-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
121+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
130122
args: &[GenericArg<'tcx>],
131-
) -> Result<Self::Path, Self::Error> {
123+
) -> Result<Self, PrintError> {
132124
self = print_prefix(self)?;
133125
let args =
134126
args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
@@ -144,9 +136,9 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {
144136
fn should_print_region(&self, _region: ty::Region<'_>) -> bool {
145137
false
146138
}
147-
fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, Self::Error>
139+
fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, PrintError>
148140
where
149-
T: Print<'tcx, Self, Output = Self, Error = Self::Error>,
141+
T: Print<'tcx, Self>,
150142
{
151143
if let Some(first) = elems.next() {
152144
self = first.print(self)?;
@@ -160,8 +152,8 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {
160152

161153
fn generic_delimiters(
162154
mut self,
163-
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
164-
) -> Result<Self, Self::Error> {
155+
f: impl FnOnce(Self) -> Result<Self, PrintError>,
156+
) -> Result<Self, PrintError> {
165157
write!(self, "<")?;
166158

167159
self = f(self)?;

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

+32-37
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
6767
use rustc_hir::intravisit::Visitor;
6868
use rustc_hir::lang_items::LangItem;
6969
use rustc_middle::dep_graph::DepContext;
70-
use rustc_middle::ty::print::with_forced_trimmed_paths;
70+
use rustc_middle::ty::print::{with_forced_trimmed_paths, PrintError};
7171
use rustc_middle::ty::relate::{self, RelateResult, TypeRelation};
7272
use rustc_middle::ty::{
7373
self, error::TypeError, IsSuggestable, List, Region, Ty, TyCtxt, TypeFoldable,
@@ -580,76 +580,68 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
580580

581581
struct AbsolutePathPrinter<'tcx> {
582582
tcx: TyCtxt<'tcx>,
583+
segments: Vec<String>,
583584
}
584585

585-
struct NonTrivialPath;
586-
587586
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
588-
type Error = NonTrivialPath;
589-
590-
type Path = Vec<String>;
591-
type Region = !;
592-
type Type = !;
593-
type DynExistential = !;
594-
type Const = !;
595-
596587
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
597588
self.tcx
598589
}
599590

600-
fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
601-
Err(NonTrivialPath)
591+
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
592+
Err(fmt::Error)
602593
}
603594

604-
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
605-
Err(NonTrivialPath)
595+
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, PrintError> {
596+
Err(fmt::Error)
606597
}
607598

608599
fn print_dyn_existential(
609600
self,
610601
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
611-
) -> Result<Self::DynExistential, Self::Error> {
612-
Err(NonTrivialPath)
602+
) -> Result<Self, PrintError> {
603+
Err(fmt::Error)
613604
}
614605

615-
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
616-
Err(NonTrivialPath)
606+
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
607+
Err(fmt::Error)
617608
}
618609

619-
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
620-
Ok(vec![self.tcx.crate_name(cnum).to_string()])
610+
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, PrintError> {
611+
self.segments = vec![self.tcx.crate_name(cnum).to_string()];
612+
Ok(self)
621613
}
622614
fn path_qualified(
623615
self,
624616
_self_ty: Ty<'tcx>,
625617
_trait_ref: Option<ty::TraitRef<'tcx>>,
626-
) -> Result<Self::Path, Self::Error> {
627-
Err(NonTrivialPath)
618+
) -> Result<Self, PrintError> {
619+
Err(fmt::Error)
628620
}
629621

630622
fn path_append_impl(
631623
self,
632-
_print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
624+
_print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
633625
_disambiguated_data: &DisambiguatedDefPathData,
634626
_self_ty: Ty<'tcx>,
635627
_trait_ref: Option<ty::TraitRef<'tcx>>,
636-
) -> Result<Self::Path, Self::Error> {
637-
Err(NonTrivialPath)
628+
) -> Result<Self, PrintError> {
629+
Err(fmt::Error)
638630
}
639631
fn path_append(
640-
self,
641-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
632+
mut self,
633+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
642634
disambiguated_data: &DisambiguatedDefPathData,
643-
) -> Result<Self::Path, Self::Error> {
644-
let mut path = print_prefix(self)?;
645-
path.push(disambiguated_data.to_string());
646-
Ok(path)
635+
) -> Result<Self, PrintError> {
636+
self = print_prefix(self)?;
637+
self.segments.push(disambiguated_data.to_string());
638+
Ok(self)
647639
}
648640
fn path_generic_args(
649641
self,
650-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
642+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
651643
_args: &[GenericArg<'tcx>],
652-
) -> Result<Self::Path, Self::Error> {
644+
) -> Result<Self, PrintError> {
653645
print_prefix(self)
654646
}
655647
}
@@ -659,12 +651,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
659651
// are from a local module we could have false positives, e.g.
660652
// let _ = [{struct Foo; Foo}, {struct Foo; Foo}];
661653
if did1.krate != did2.krate {
662-
let abs_path =
663-
|def_id| AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]);
654+
let abs_path = |def_id| {
655+
AbsolutePathPrinter { tcx: self.tcx, segments: vec![] }
656+
.print_def_path(def_id, &[])
657+
.map(|p| p.segments)
658+
};
664659

665660
// We compare strings because DefPath can be different
666661
// for imported and non-imported crates
667-
let same_path = || -> Result<_, NonTrivialPath> {
662+
let same_path = || -> Result<_, PrintError> {
668663
Ok(self.tcx.def_path_str(did1) == self.tcx.def_path_str(did2)
669664
|| abs_path(did1)? == abs_path(did2)?)
670665
};

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct Highlighted<'tcx, T> {
2828

2929
impl<'tcx, T> IntoDiagnosticArg for Highlighted<'tcx, T>
3030
where
31-
T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>, Error = fmt::Error, Output = FmtPrinter<'a, 'tcx>>,
31+
T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>>,
3232
{
3333
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
3434
rustc_errors::DiagnosticArgValue::Str(self.to_string().into())
@@ -43,7 +43,7 @@ impl<'tcx, T> Highlighted<'tcx, T> {
4343

4444
impl<'tcx, T> fmt::Display for Highlighted<'tcx, T>
4545
where
46-
T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>, Error = fmt::Error, Output = FmtPrinter<'a, 'tcx>>,
46+
T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>>,
4747
{
4848
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4949
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS);

0 commit comments

Comments
 (0)