Skip to content

Commit a16c1dd

Browse files
committed
Remove "subprinter" types from Printer
These are `Self` in almost all printers except one, which can just store the state as a field instead. This simplifies the printer and allows for further simplifications, for example using `&mut self` instead of passing around the printer.
1 parent 86f397e commit a16c1dd

File tree

7 files changed

+131
-177
lines changed

7 files changed

+131
-177
lines changed

compiler/rustc_const_eval/src/util/type_name.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,15 @@ struct AbsolutePathPrinter<'tcx> {
1616
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
1717
type Error = std::fmt::Error;
1818

19-
type Path = Self;
20-
type Region = Self;
21-
type Type = Self;
22-
type DynExistential = Self;
23-
type Const = Self;
24-
2519
fn tcx(&self) -> TyCtxt<'tcx> {
2620
self.tcx
2721
}
2822

29-
fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
23+
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
3024
Ok(self)
3125
}
3226

33-
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
27+
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, Self::Error> {
3428
match *ty.kind() {
3529
// Types without identity.
3630
ty::Bool
@@ -68,18 +62,18 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
6862
}
6963
}
7064

71-
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
65+
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
7266
self.pretty_print_const(ct, false)
7367
}
7468

7569
fn print_dyn_existential(
7670
self,
7771
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
78-
) -> Result<Self::DynExistential, Self::Error> {
72+
) -> Result<Self, Self::Error> {
7973
self.pretty_print_dyn_existential(predicates)
8074
}
8175

82-
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
76+
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
8377
self.path.push_str(self.tcx.crate_name(cnum).as_str());
8478
Ok(self)
8579
}
@@ -88,17 +82,17 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
8882
self,
8983
self_ty: Ty<'tcx>,
9084
trait_ref: Option<ty::TraitRef<'tcx>>,
91-
) -> Result<Self::Path, Self::Error> {
85+
) -> Result<Self, Self::Error> {
9286
self.pretty_path_qualified(self_ty, trait_ref)
9387
}
9488

9589
fn path_append_impl(
9690
self,
97-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
91+
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
9892
_disambiguated_data: &DisambiguatedDefPathData,
9993
self_ty: Ty<'tcx>,
10094
trait_ref: Option<ty::TraitRef<'tcx>>,
101-
) -> Result<Self::Path, Self::Error> {
95+
) -> Result<Self, Self::Error> {
10296
self.pretty_path_append_impl(
10397
|mut cx| {
10498
cx = print_prefix(cx)?;
@@ -114,9 +108,9 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
114108

115109
fn path_append(
116110
mut self,
117-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
111+
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
118112
disambiguated_data: &DisambiguatedDefPathData,
119-
) -> Result<Self::Path, Self::Error> {
113+
) -> Result<Self, Self::Error> {
120114
self = print_prefix(self)?;
121115

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

127121
fn path_generic_args(
128122
mut self,
129-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
123+
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
130124
args: &[GenericArg<'tcx>],
131-
) -> Result<Self::Path, Self::Error> {
125+
) -> Result<Self, Self::Error> {
132126
self = print_prefix(self)?;
133127
let args =
134128
args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_)));

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ 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, with_no_trimmed_paths, with_no_visible_paths};
70+
use rustc_middle::ty::print::{
71+
with_forced_trimmed_paths, with_no_trimmed_paths, with_no_visible_paths,
72+
};
7173
use rustc_middle::ty::relate::{self, RelateResult, TypeRelation};
7274
use rustc_middle::ty::{
7375
self, error::TypeError, IsSuggestable, List, Region, Ty, TyCtxt, TypeFoldable,
@@ -580,7 +582,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
580582
if did1.krate != did2.krate {
581583
// We compare strings because DefPath can be different
582584
// for imported and non-imported crates
583-
if with_no_trimmed_paths!(with_no_visible_paths!(self.tcx.def_path_str(did1) == self.tcx.def_path_str(did2))) {
585+
if with_no_trimmed_paths!(with_no_visible_paths!(
586+
self.tcx.def_path_str(did1) == self.tcx.def_path_str(did2)
587+
)) {
584588
let crate_name = self.tcx.crate_name(did1.krate);
585589
let msg = if did1.is_local() || did2.is_local() {
586590
format!(

compiler/rustc_lint/src/context.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,51 +1200,47 @@ impl<'tcx> LateContext<'tcx> {
12001200
/// }
12011201
/// ```
12021202
pub fn get_def_path(&self, def_id: DefId) -> Vec<Symbol> {
1203-
pub struct AbsolutePathPrinter<'tcx> {
1204-
pub tcx: TyCtxt<'tcx>,
1203+
struct AbsolutePathPrinter<'tcx> {
1204+
tcx: TyCtxt<'tcx>,
1205+
path: Vec<Symbol>,
12051206
}
12061207

12071208
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
12081209
type Error = !;
12091210

1210-
type Path = Vec<Symbol>;
1211-
type Region = ();
1212-
type Type = ();
1213-
type DynExistential = ();
1214-
type Const = ();
1215-
12161211
fn tcx(&self) -> TyCtxt<'tcx> {
12171212
self.tcx
12181213
}
12191214

1220-
fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
1221-
Ok(())
1215+
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
1216+
Ok(self)
12221217
}
12231218

1224-
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
1225-
Ok(())
1219+
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, Self::Error> {
1220+
Ok(self)
12261221
}
12271222

12281223
fn print_dyn_existential(
12291224
self,
12301225
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
1231-
) -> Result<Self::DynExistential, Self::Error> {
1232-
Ok(())
1226+
) -> Result<Self, Self::Error> {
1227+
Ok(self)
12331228
}
12341229

1235-
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
1236-
Ok(())
1230+
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
1231+
Ok(self)
12371232
}
12381233

1239-
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
1240-
Ok(vec![self.tcx.crate_name(cnum)])
1234+
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
1235+
self.path = vec![self.tcx.crate_name(cnum)];
1236+
Ok(self)
12411237
}
12421238

12431239
fn path_qualified(
1244-
self,
1240+
mut self,
12451241
self_ty: Ty<'tcx>,
12461242
trait_ref: Option<ty::TraitRef<'tcx>>,
1247-
) -> Result<Self::Path, Self::Error> {
1243+
) -> Result<Self, Self::Error> {
12481244
if trait_ref.is_none() {
12491245
if let ty::Adt(def, args) = self_ty.kind() {
12501246
return self.print_def_path(def.did(), args);
@@ -1253,24 +1249,25 @@ impl<'tcx> LateContext<'tcx> {
12531249

12541250
// This shouldn't ever be needed, but just in case:
12551251
with_no_trimmed_paths!({
1256-
Ok(vec![match trait_ref {
1252+
self.path = vec![match trait_ref {
12571253
Some(trait_ref) => Symbol::intern(&format!("{trait_ref:?}")),
12581254
None => Symbol::intern(&format!("<{self_ty}>")),
1259-
}])
1255+
}];
1256+
Ok(self)
12601257
})
12611258
}
12621259

12631260
fn path_append_impl(
12641261
self,
1265-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
1262+
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
12661263
_disambiguated_data: &DisambiguatedDefPathData,
12671264
self_ty: Ty<'tcx>,
12681265
trait_ref: Option<ty::TraitRef<'tcx>>,
1269-
) -> Result<Self::Path, Self::Error> {
1266+
) -> Result<Self, Self::Error> {
12701267
let mut path = print_prefix(self)?;
12711268

12721269
// This shouldn't ever be needed, but just in case:
1273-
path.push(match trait_ref {
1270+
path.path.push(match trait_ref {
12741271
Some(trait_ref) => {
12751272
with_no_trimmed_paths!(Symbol::intern(&format!(
12761273
"<impl {} for {}>",
@@ -1288,30 +1285,33 @@ impl<'tcx> LateContext<'tcx> {
12881285

12891286
fn path_append(
12901287
self,
1291-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
1288+
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
12921289
disambiguated_data: &DisambiguatedDefPathData,
1293-
) -> Result<Self::Path, Self::Error> {
1290+
) -> Result<Self, Self::Error> {
12941291
let mut path = print_prefix(self)?;
12951292

12961293
// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
12971294
if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
12981295
return Ok(path);
12991296
}
13001297

1301-
path.push(Symbol::intern(&disambiguated_data.data.to_string()));
1298+
path.path.push(Symbol::intern(&disambiguated_data.data.to_string()));
13021299
Ok(path)
13031300
}
13041301

13051302
fn path_generic_args(
13061303
self,
1307-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
1304+
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
13081305
_args: &[GenericArg<'tcx>],
1309-
) -> Result<Self::Path, Self::Error> {
1306+
) -> Result<Self, Self::Error> {
13101307
print_prefix(self)
13111308
}
13121309
}
13131310

1314-
AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]).unwrap()
1311+
AbsolutePathPrinter { tcx: self.tcx, path: vec![] }
1312+
.print_def_path(def_id, &[])
1313+
.unwrap()
1314+
.path
13151315
}
13161316

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

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

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,13 @@ pub trait Print<'tcx, P> {
3131
pub trait Printer<'tcx>: Sized {
3232
type Error;
3333

34-
type Path;
35-
type Region;
36-
type Type;
37-
type DynExistential;
38-
type Const;
39-
4034
fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
4135

4236
fn print_def_path(
4337
self,
4438
def_id: DefId,
4539
args: &'tcx [GenericArg<'tcx>],
46-
) -> Result<Self::Path, Self::Error> {
40+
) -> Result<Self, Self::Error> {
4741
self.default_print_def_path(def_id, args)
4842
}
4943

@@ -53,48 +47,48 @@ pub trait Printer<'tcx>: Sized {
5347
args: &'tcx [GenericArg<'tcx>],
5448
self_ty: Ty<'tcx>,
5549
trait_ref: Option<ty::TraitRef<'tcx>>,
56-
) -> Result<Self::Path, Self::Error> {
50+
) -> Result<Self, Self::Error> {
5751
self.default_print_impl_path(impl_def_id, args, self_ty, trait_ref)
5852
}
5953

60-
fn print_region(self, region: ty::Region<'tcx>) -> Result<Self::Region, Self::Error>;
54+
fn print_region(self, region: ty::Region<'tcx>) -> Result<Self, Self::Error>;
6155

62-
fn print_type(self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error>;
56+
fn print_type(self, ty: Ty<'tcx>) -> Result<Self, Self::Error>;
6357

6458
fn print_dyn_existential(
6559
self,
6660
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
67-
) -> Result<Self::DynExistential, Self::Error>;
61+
) -> Result<Self, Self::Error>;
6862

69-
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error>;
63+
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, Self::Error>;
7064

71-
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error>;
65+
fn path_crate(self, cnum: CrateNum) -> Result<Self, Self::Error>;
7266

7367
fn path_qualified(
7468
self,
7569
self_ty: Ty<'tcx>,
7670
trait_ref: Option<ty::TraitRef<'tcx>>,
77-
) -> Result<Self::Path, Self::Error>;
71+
) -> Result<Self, Self::Error>;
7872

7973
fn path_append_impl(
8074
self,
81-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
75+
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
8276
disambiguated_data: &DisambiguatedDefPathData,
8377
self_ty: Ty<'tcx>,
8478
trait_ref: Option<ty::TraitRef<'tcx>>,
85-
) -> Result<Self::Path, Self::Error>;
79+
) -> Result<Self, Self::Error>;
8680

8781
fn path_append(
8882
self,
89-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
83+
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
9084
disambiguated_data: &DisambiguatedDefPathData,
91-
) -> Result<Self::Path, Self::Error>;
85+
) -> Result<Self, Self::Error>;
9286

9387
fn path_generic_args(
9488
self,
95-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
89+
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
9690
args: &[GenericArg<'tcx>],
97-
) -> Result<Self::Path, Self::Error>;
91+
) -> Result<Self, Self::Error>;
9892

9993
// Defaults (should not be overridden):
10094

@@ -103,7 +97,7 @@ pub trait Printer<'tcx>: Sized {
10397
self,
10498
def_id: DefId,
10599
args: &'tcx [GenericArg<'tcx>],
106-
) -> Result<Self::Path, Self::Error> {
100+
) -> Result<Self, Self::Error> {
107101
let key = self.tcx().def_key(def_id);
108102
debug!(?key);
109103

@@ -194,7 +188,7 @@ pub trait Printer<'tcx>: Sized {
194188
_args: &'tcx [GenericArg<'tcx>],
195189
self_ty: Ty<'tcx>,
196190
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
197-
) -> Result<Self::Path, Self::Error> {
191+
) -> Result<Self, Self::Error> {
198192
debug!(
199193
"default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}",
200194
impl_def_id, self_ty, impl_trait_ref
@@ -295,15 +289,15 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
295289
}
296290

297291
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Region<'tcx> {
298-
type Output = P::Region;
292+
type Output = P;
299293
type Error = P::Error;
300294
fn print(&self, cx: P) -> Result<Self::Output, Self::Error> {
301295
cx.print_region(*self)
302296
}
303297
}
304298

305299
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for Ty<'tcx> {
306-
type Output = P::Type;
300+
type Output = P;
307301
type Error = P::Error;
308302

309303
fn print(&self, cx: P) -> Result<Self::Output, Self::Error> {
@@ -312,15 +306,15 @@ impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for Ty<'tcx> {
312306
}
313307

314308
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
315-
type Output = P::DynExistential;
309+
type Output = P;
316310
type Error = P::Error;
317311
fn print(&self, cx: P) -> Result<Self::Output, Self::Error> {
318312
cx.print_dyn_existential(self)
319313
}
320314
}
321315

322316
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Const<'tcx> {
323-
type Output = P::Const;
317+
type Output = P;
324318
type Error = P::Error;
325319
fn print(&self, cx: P) -> Result<Self::Output, Self::Error> {
326320
cx.print_const(*self)

0 commit comments

Comments
 (0)