Skip to content

Commit 6038888

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 0b5a4c1 commit 6038888

File tree

7 files changed

+180
-194
lines changed

7 files changed

+180
-194
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_infer/src/infer/error_reporting/mod.rs

+20-24
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,
@@ -583,69 +583,65 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
583583
segments: Vec<String>,
584584
}
585585

586-
struct NonTrivialPath;
587-
588586
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
589-
type Error = NonTrivialPath;
590-
591587
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
592588
self.tcx
593589
}
594590

595-
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
596-
Err(NonTrivialPath)
591+
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
592+
Err(fmt::Error)
597593
}
598594

599-
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, Self::Error> {
600-
Err(NonTrivialPath)
595+
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, PrintError> {
596+
Err(fmt::Error)
601597
}
602598

603599
fn print_dyn_existential(
604600
self,
605601
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
606-
) -> Result<Self, Self::Error> {
607-
Err(NonTrivialPath)
602+
) -> Result<Self, PrintError> {
603+
Err(fmt::Error)
608604
}
609605

610-
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
611-
Err(NonTrivialPath)
606+
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
607+
Err(fmt::Error)
612608
}
613609

614-
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
610+
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, PrintError> {
615611
self.segments = vec![self.tcx.crate_name(cnum).to_string()];
616612
Ok(self)
617613
}
618614
fn path_qualified(
619615
self,
620616
_self_ty: Ty<'tcx>,
621617
_trait_ref: Option<ty::TraitRef<'tcx>>,
622-
) -> Result<Self, Self::Error> {
623-
Err(NonTrivialPath)
618+
) -> Result<Self, PrintError> {
619+
Err(fmt::Error)
624620
}
625621

626622
fn path_append_impl(
627623
self,
628-
_print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
624+
_print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
629625
_disambiguated_data: &DisambiguatedDefPathData,
630626
_self_ty: Ty<'tcx>,
631627
_trait_ref: Option<ty::TraitRef<'tcx>>,
632-
) -> Result<Self, Self::Error> {
633-
Err(NonTrivialPath)
628+
) -> Result<Self, PrintError> {
629+
Err(fmt::Error)
634630
}
635631
fn path_append(
636632
mut self,
637-
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
633+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
638634
disambiguated_data: &DisambiguatedDefPathData,
639-
) -> Result<Self, Self::Error> {
635+
) -> Result<Self, PrintError> {
640636
self = print_prefix(self)?;
641637
self.segments.push(disambiguated_data.to_string());
642638
Ok(self)
643639
}
644640
fn path_generic_args(
645641
self,
646-
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
642+
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
647643
_args: &[GenericArg<'tcx>],
648-
) -> Result<Self, Self::Error> {
644+
) -> Result<Self, PrintError> {
649645
print_prefix(self)
650646
}
651647
}
@@ -663,7 +659,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
663659

664660
// We compare strings because DefPath can be different
665661
// for imported and non-imported crates
666-
let same_path = || -> Result<_, NonTrivialPath> {
662+
let same_path = || -> Result<_, PrintError> {
667663
Ok(self.tcx.def_path_str(did1) == self.tcx.def_path_str(did2)
668664
|| abs_path(did1)? == abs_path(did2)?)
669665
};

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
}

0 commit comments

Comments
 (0)