Skip to content

Commit 3895f0e

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 98c1e3d commit 3895f0e

File tree

7 files changed

+149
-200
lines changed

7 files changed

+149
-200
lines changed

compiler/rustc_const_eval/src/util/type_name.rs

+12-18
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

+24-25
Original file line numberDiff line numberDiff line change
@@ -580,76 +580,72 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
580580

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

585586
struct NonTrivialPath;
586587

587588
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
588589
type Error = NonTrivialPath;
589590

590-
type Path = Vec<String>;
591-
type Region = !;
592-
type Type = !;
593-
type DynExistential = !;
594-
type Const = !;
595-
596591
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
597592
self.tcx
598593
}
599594

600-
fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
595+
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
601596
Err(NonTrivialPath)
602597
}
603598

604-
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
599+
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, Self::Error> {
605600
Err(NonTrivialPath)
606601
}
607602

608603
fn print_dyn_existential(
609604
self,
610605
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
611-
) -> Result<Self::DynExistential, Self::Error> {
606+
) -> Result<Self, Self::Error> {
612607
Err(NonTrivialPath)
613608
}
614609

615-
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
610+
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
616611
Err(NonTrivialPath)
617612
}
618613

619-
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
620-
Ok(vec![self.tcx.crate_name(cnum).to_string()])
614+
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
615+
self.segments = vec![self.tcx.crate_name(cnum).to_string()];
616+
Ok(self)
621617
}
622618
fn path_qualified(
623619
self,
624620
_self_ty: Ty<'tcx>,
625621
_trait_ref: Option<ty::TraitRef<'tcx>>,
626-
) -> Result<Self::Path, Self::Error> {
622+
) -> Result<Self, Self::Error> {
627623
Err(NonTrivialPath)
628624
}
629625

630626
fn path_append_impl(
631627
self,
632-
_print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
628+
_print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
633629
_disambiguated_data: &DisambiguatedDefPathData,
634630
_self_ty: Ty<'tcx>,
635631
_trait_ref: Option<ty::TraitRef<'tcx>>,
636-
) -> Result<Self::Path, Self::Error> {
632+
) -> Result<Self, Self::Error> {
637633
Err(NonTrivialPath)
638634
}
639635
fn path_append(
640-
self,
641-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
636+
mut self,
637+
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
642638
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)
639+
) -> Result<Self, Self::Error> {
640+
self = print_prefix(self)?;
641+
self.segments.push(disambiguated_data.to_string());
642+
Ok(self)
647643
}
648644
fn path_generic_args(
649645
self,
650-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
646+
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
651647
_args: &[GenericArg<'tcx>],
652-
) -> Result<Self::Path, Self::Error> {
648+
) -> Result<Self, Self::Error> {
653649
print_prefix(self)
654650
}
655651
}
@@ -659,8 +655,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
659655
// are from a local module we could have false positives, e.g.
660656
// let _ = [{struct Foo; Foo}, {struct Foo; Foo}];
661657
if did1.krate != did2.krate {
662-
let abs_path =
663-
|def_id| AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]);
658+
let abs_path = |def_id| {
659+
AbsolutePathPrinter { tcx: self.tcx, segments: vec![] }
660+
.print_def_path(def_id, &[])
661+
.map(|p| p.segments)
662+
};
664663

665664
// We compare strings because DefPath can be different
666665
// for imported and non-imported crates

compiler/rustc_lint/src/context.rs

+31-31
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`.

0 commit comments

Comments
 (0)