Skip to content

Commit c464658

Browse files
committed
Remove Printer::Error
It's always a `fmt::Error` except in some cases where it was `!`, but we're not really winning anything in that case.
1 parent f833309 commit c464658

File tree

6 files changed

+160
-170
lines changed

6 files changed

+160
-170
lines changed

compiler/rustc_const_eval/src/util/type_name.rs

+17-19
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,17 +14,15 @@ struct AbsolutePathPrinter<'tcx> {
1414
}
1515

1616
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
17-
type Error = std::fmt::Error;
18-
1917
fn tcx(&self) -> TyCtxt<'tcx> {
2018
self.tcx
2119
}
2220

23-
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
21+
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
2422
Ok(self)
2523
}
2624

27-
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, Self::Error> {
25+
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
2826
match *ty.kind() {
2927
// Types without identity.
3028
ty::Bool
@@ -62,18 +60,18 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
6260
}
6361
}
6462

65-
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
63+
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
6664
self.pretty_print_const(ct, false)
6765
}
6866

6967
fn print_dyn_existential(
7068
self,
7169
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
72-
) -> Result<Self, Self::Error> {
70+
) -> Result<Self, PrintError> {
7371
self.pretty_print_dyn_existential(predicates)
7472
}
7573

76-
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
74+
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, PrintError> {
7775
self.path.push_str(self.tcx.crate_name(cnum).as_str());
7876
Ok(self)
7977
}
@@ -82,17 +80,17 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
8280
self,
8381
self_ty: Ty<'tcx>,
8482
trait_ref: Option<ty::TraitRef<'tcx>>,
85-
) -> Result<Self, Self::Error> {
83+
) -> Result<Self, PrintError> {
8684
self.pretty_path_qualified(self_ty, trait_ref)
8785
}
8886

8987
fn path_append_impl(
9088
self,
91-
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
89+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
9290
_disambiguated_data: &DisambiguatedDefPathData,
9391
self_ty: Ty<'tcx>,
9492
trait_ref: Option<ty::TraitRef<'tcx>>,
95-
) -> Result<Self, Self::Error> {
93+
) -> Result<Self, PrintError> {
9694
self.pretty_path_append_impl(
9795
|mut cx| {
9896
cx = print_prefix(cx)?;
@@ -108,9 +106,9 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
108106

109107
fn path_append(
110108
mut self,
111-
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
109+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
112110
disambiguated_data: &DisambiguatedDefPathData,
113-
) -> Result<Self, Self::Error> {
111+
) -> Result<Self, PrintError> {
114112
self = print_prefix(self)?;
115113

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

121119
fn path_generic_args(
122120
mut self,
123-
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
121+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
124122
args: &[GenericArg<'tcx>],
125-
) -> Result<Self, Self::Error> {
123+
) -> Result<Self, PrintError> {
126124
self = print_prefix(self)?;
127125
let args =
128126
args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
@@ -138,9 +136,9 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {
138136
fn should_print_region(&self, _region: ty::Region<'_>) -> bool {
139137
false
140138
}
141-
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>
142140
where
143-
T: Print<'tcx, Self, Error = Self::Error>,
141+
T: Print<'tcx, Self, Error = PrintError>,
144142
{
145143
if let Some(first) = elems.next() {
146144
self = first.print(self)?;
@@ -154,8 +152,8 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {
154152

155153
fn generic_delimiters(
156154
mut self,
157-
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
158-
) -> Result<Self, Self::Error> {
155+
f: impl FnOnce(Self) -> Result<Self, PrintError>,
156+
) -> Result<Self, PrintError> {
159157
write!(self, "<")?;
160158

161159
self = f(self)?;

compiler/rustc_lint/src/context.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
3131
use rustc_middle::middle::privacy::EffectiveVisibilities;
3232
use rustc_middle::middle::stability;
3333
use rustc_middle::ty::layout::{LayoutError, LayoutOfHelpers, TyAndLayout};
34-
use rustc_middle::ty::print::with_no_trimmed_paths;
34+
use rustc_middle::ty::print::{with_no_trimmed_paths, PrintError};
3535
use rustc_middle::ty::{self, print::Printer, GenericArg, RegisteredTools, Ty, TyCtxt};
3636
use rustc_session::config::ExpectedValues;
3737
use rustc_session::lint::{BuiltinLintDiagnostics, LintExpectationId};
@@ -1206,32 +1206,30 @@ impl<'tcx> LateContext<'tcx> {
12061206
}
12071207

12081208
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
1209-
type Error = !;
1210-
12111209
fn tcx(&self) -> TyCtxt<'tcx> {
12121210
self.tcx
12131211
}
12141212

1215-
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
1213+
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
12161214
Ok(self)
12171215
}
12181216

1219-
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, Self::Error> {
1217+
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, PrintError> {
12201218
Ok(self)
12211219
}
12221220

12231221
fn print_dyn_existential(
12241222
self,
12251223
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
1226-
) -> Result<Self, Self::Error> {
1224+
) -> Result<Self, PrintError> {
12271225
Ok(self)
12281226
}
12291227

1230-
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
1228+
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
12311229
Ok(self)
12321230
}
12331231

1234-
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
1232+
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, PrintError> {
12351233
self.path = vec![self.tcx.crate_name(cnum)];
12361234
Ok(self)
12371235
}
@@ -1240,7 +1238,7 @@ impl<'tcx> LateContext<'tcx> {
12401238
mut self,
12411239
self_ty: Ty<'tcx>,
12421240
trait_ref: Option<ty::TraitRef<'tcx>>,
1243-
) -> Result<Self, Self::Error> {
1241+
) -> Result<Self, PrintError> {
12441242
if trait_ref.is_none() {
12451243
if let ty::Adt(def, args) = self_ty.kind() {
12461244
return self.print_def_path(def.did(), args);
@@ -1259,11 +1257,11 @@ impl<'tcx> LateContext<'tcx> {
12591257

12601258
fn path_append_impl(
12611259
self,
1262-
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
1260+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
12631261
_disambiguated_data: &DisambiguatedDefPathData,
12641262
self_ty: Ty<'tcx>,
12651263
trait_ref: Option<ty::TraitRef<'tcx>>,
1266-
) -> Result<Self, Self::Error> {
1264+
) -> Result<Self, PrintError> {
12671265
let mut path = print_prefix(self)?;
12681266

12691267
// This shouldn't ever be needed, but just in case:
@@ -1285,9 +1283,9 @@ impl<'tcx> LateContext<'tcx> {
12851283

12861284
fn path_append(
12871285
self,
1288-
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
1286+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
12891287
disambiguated_data: &DisambiguatedDefPathData,
1290-
) -> Result<Self, Self::Error> {
1288+
) -> Result<Self, PrintError> {
12911289
let mut path = print_prefix(self)?;
12921290

12931291
// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
@@ -1301,9 +1299,9 @@ impl<'tcx> LateContext<'tcx> {
13011299

13021300
fn path_generic_args(
13031301
self,
1304-
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
1302+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
13051303
_args: &[GenericArg<'tcx>],
1306-
) -> Result<Self, Self::Error> {
1304+
) -> Result<Self, PrintError> {
13071305
print_prefix(self)
13081306
}
13091307
}

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

+27-27
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
1010
mod pretty;
1111
pub use self::pretty::*;
1212

13+
pub type PrintError = std::fmt::Error;
14+
1315
// FIXME(eddyb) false positive, the lifetime parameters are used with `P: Printer<...>`.
1416
#[allow(unused_lifetimes)]
1517
pub trait Print<'tcx, P> {
1618
type Error;
1719

18-
fn print(&self, cx: P) -> Result<P, Self::Error>;
20+
fn print(&self, cx: P) -> Result<P, PrintError>;
1921
}
2022

2123
/// Interface for outputting user-facing "type-system entities"
@@ -28,15 +30,13 @@ pub trait Print<'tcx, P> {
2830
//
2931
// FIXME(eddyb) find a better name; this is more general than "printing".
3032
pub trait Printer<'tcx>: Sized {
31-
type Error;
32-
3333
fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
3434

3535
fn print_def_path(
3636
self,
3737
def_id: DefId,
3838
args: &'tcx [GenericArg<'tcx>],
39-
) -> Result<Self, Self::Error> {
39+
) -> Result<Self, PrintError> {
4040
self.default_print_def_path(def_id, args)
4141
}
4242

@@ -46,48 +46,48 @@ pub trait Printer<'tcx>: Sized {
4646
args: &'tcx [GenericArg<'tcx>],
4747
self_ty: Ty<'tcx>,
4848
trait_ref: Option<ty::TraitRef<'tcx>>,
49-
) -> Result<Self, Self::Error> {
49+
) -> Result<Self, PrintError> {
5050
self.default_print_impl_path(impl_def_id, args, self_ty, trait_ref)
5151
}
5252

53-
fn print_region(self, region: ty::Region<'tcx>) -> Result<Self, Self::Error>;
53+
fn print_region(self, region: ty::Region<'tcx>) -> Result<Self, PrintError>;
5454

55-
fn print_type(self, ty: Ty<'tcx>) -> Result<Self, Self::Error>;
55+
fn print_type(self, ty: Ty<'tcx>) -> Result<Self, PrintError>;
5656

5757
fn print_dyn_existential(
5858
self,
5959
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
60-
) -> Result<Self, Self::Error>;
60+
) -> Result<Self, PrintError>;
6161

62-
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, Self::Error>;
62+
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, PrintError>;
6363

64-
fn path_crate(self, cnum: CrateNum) -> Result<Self, Self::Error>;
64+
fn path_crate(self, cnum: CrateNum) -> Result<Self, PrintError>;
6565

6666
fn path_qualified(
6767
self,
6868
self_ty: Ty<'tcx>,
6969
trait_ref: Option<ty::TraitRef<'tcx>>,
70-
) -> Result<Self, Self::Error>;
70+
) -> Result<Self, PrintError>;
7171

7272
fn path_append_impl(
7373
self,
74-
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
74+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
7575
disambiguated_data: &DisambiguatedDefPathData,
7676
self_ty: Ty<'tcx>,
7777
trait_ref: Option<ty::TraitRef<'tcx>>,
78-
) -> Result<Self, Self::Error>;
78+
) -> Result<Self, PrintError>;
7979

8080
fn path_append(
8181
self,
82-
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
82+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
8383
disambiguated_data: &DisambiguatedDefPathData,
84-
) -> Result<Self, Self::Error>;
84+
) -> Result<Self, PrintError>;
8585

8686
fn path_generic_args(
8787
self,
88-
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
88+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
8989
args: &[GenericArg<'tcx>],
90-
) -> Result<Self, Self::Error>;
90+
) -> Result<Self, PrintError>;
9191

9292
// Defaults (should not be overridden):
9393

@@ -96,7 +96,7 @@ pub trait Printer<'tcx>: Sized {
9696
self,
9797
def_id: DefId,
9898
args: &'tcx [GenericArg<'tcx>],
99-
) -> Result<Self, Self::Error> {
99+
) -> Result<Self, PrintError> {
100100
let key = self.tcx().def_key(def_id);
101101
debug!(?key);
102102

@@ -187,7 +187,7 @@ pub trait Printer<'tcx>: Sized {
187187
_args: &'tcx [GenericArg<'tcx>],
188188
self_ty: Ty<'tcx>,
189189
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
190-
) -> Result<Self, Self::Error> {
190+
) -> Result<Self, PrintError> {
191191
debug!(
192192
"default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}",
193193
impl_def_id, self_ty, impl_trait_ref
@@ -288,30 +288,30 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
288288
}
289289

290290
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Region<'tcx> {
291-
type Error = P::Error;
292-
fn print(&self, cx: P) -> Result<P, Self::Error> {
291+
type Error = PrintError;
292+
fn print(&self, cx: P) -> Result<P, PrintError> {
293293
cx.print_region(*self)
294294
}
295295
}
296296

297297
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for Ty<'tcx> {
298-
type Error = P::Error;
298+
type Error = PrintError;
299299

300-
fn print(&self, cx: P) -> Result<P, Self::Error> {
300+
fn print(&self, cx: P) -> Result<P, PrintError> {
301301
cx.print_type(*self)
302302
}
303303
}
304304

305305
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
306-
type Error = P::Error;
307-
fn print(&self, cx: P) -> Result<P, Self::Error> {
306+
type Error = PrintError;
307+
fn print(&self, cx: P) -> Result<P, PrintError> {
308308
cx.print_dyn_existential(self)
309309
}
310310
}
311311

312312
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Const<'tcx> {
313-
type Error = P::Error;
314-
fn print(&self, cx: P) -> Result<P, Self::Error> {
313+
type Error = PrintError;
314+
fn print(&self, cx: P) -> Result<P, PrintError> {
315315
cx.print_const(*self)
316316
}
317317
}

0 commit comments

Comments
 (0)