Skip to content

Commit

Permalink
Remove Printer::Error
Browse files Browse the repository at this point in the history
It's always a `fmt::Error` except in some cases where it was `!`, but
we're not really winning anything in that case.
  • Loading branch information
Noratrieb committed Oct 17, 2023
1 parent 0b5a4c1 commit 6038888
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 194 deletions.
36 changes: 17 additions & 19 deletions compiler/rustc_const_eval/src/util/type_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_hir::def_id::CrateNum;
use rustc_hir::definitions::DisambiguatedDefPathData;
use rustc_middle::ty::{
self,
print::{PrettyPrinter, Print, Printer},
print::{PrettyPrinter, Print, PrintError, Printer},
GenericArg, GenericArgKind, Ty, TyCtxt,
};
use std::fmt::Write;
Expand All @@ -14,17 +14,15 @@ struct AbsolutePathPrinter<'tcx> {
}

impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
type Error = std::fmt::Error;

fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}

fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
Ok(self)
}

fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, Self::Error> {
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, PrintError> {
match *ty.kind() {
// Types without identity.
ty::Bool
Expand Down Expand Up @@ -62,18 +60,18 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
}
}

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

fn print_dyn_existential(
self,
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self, Self::Error> {
) -> Result<Self, PrintError> {
self.pretty_print_dyn_existential(predicates)
}

fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, PrintError> {
self.path.push_str(self.tcx.crate_name(cnum).as_str());
Ok(self)
}
Expand All @@ -82,17 +80,17 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, Self::Error> {
) -> Result<Self, PrintError> {
self.pretty_path_qualified(self_ty, trait_ref)
}

fn path_append_impl(
self,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
_disambiguated_data: &DisambiguatedDefPathData,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, Self::Error> {
) -> Result<Self, PrintError> {
self.pretty_path_append_impl(
|mut cx| {
cx = print_prefix(cx)?;
Expand All @@ -108,9 +106,9 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {

fn path_append(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self, Self::Error> {
) -> Result<Self, PrintError> {
self = print_prefix(self)?;

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

fn path_generic_args(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
args: &[GenericArg<'tcx>],
) -> Result<Self, Self::Error> {
) -> Result<Self, PrintError> {
self = print_prefix(self)?;
let args =
args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
Expand All @@ -138,9 +136,9 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {
fn should_print_region(&self, _region: ty::Region<'_>) -> bool {
false
}
fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, Self::Error>
fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, PrintError>
where
T: Print<'tcx, Self, Error = Self::Error>,
T: Print<'tcx, Self, Error = PrintError>,
{
if let Some(first) = elems.next() {
self = first.print(self)?;
Expand All @@ -154,8 +152,8 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {

fn generic_delimiters(
mut self,
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
) -> Result<Self, Self::Error> {
f: impl FnOnce(Self) -> Result<Self, PrintError>,
) -> Result<Self, PrintError> {
write!(self, "<")?;

self = f(self)?;
Expand Down
44 changes: 20 additions & 24 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::Visitor;
use rustc_hir::lang_items::LangItem;
use rustc_middle::dep_graph::DepContext;
use rustc_middle::ty::print::with_forced_trimmed_paths;
use rustc_middle::ty::print::{with_forced_trimmed_paths, PrintError};
use rustc_middle::ty::relate::{self, RelateResult, TypeRelation};
use rustc_middle::ty::{
self, error::TypeError, IsSuggestable, List, Region, Ty, TyCtxt, TypeFoldable,
Expand Down Expand Up @@ -583,69 +583,65 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
segments: Vec<String>,
}

struct NonTrivialPath;

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

fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
self.tcx
}

fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
Err(fmt::Error)
}

fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, PrintError> {
Err(fmt::Error)
}

fn print_dyn_existential(
self,
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
) -> Result<Self, PrintError> {
Err(fmt::Error)
}

fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
Err(fmt::Error)
}

fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, PrintError> {
self.segments = vec![self.tcx.crate_name(cnum).to_string()];
Ok(self)
}
fn path_qualified(
self,
_self_ty: Ty<'tcx>,
_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
) -> Result<Self, PrintError> {
Err(fmt::Error)
}

fn path_append_impl(
self,
_print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
_print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
_disambiguated_data: &DisambiguatedDefPathData,
_self_ty: Ty<'tcx>,
_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
) -> Result<Self, PrintError> {
Err(fmt::Error)
}
fn path_append(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self, Self::Error> {
) -> Result<Self, PrintError> {
self = print_prefix(self)?;
self.segments.push(disambiguated_data.to_string());
Ok(self)
}
fn path_generic_args(
self,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
_args: &[GenericArg<'tcx>],
) -> Result<Self, Self::Error> {
) -> Result<Self, PrintError> {
print_prefix(self)
}
}
Expand All @@ -663,7 +659,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {

// We compare strings because DefPath can be different
// for imported and non-imported crates
let same_path = || -> Result<_, NonTrivialPath> {
let same_path = || -> Result<_, PrintError> {
Ok(self.tcx.def_path_str(did1) == self.tcx.def_path_str(did2)
|| abs_path(did1)? == abs_path(did2)?)
};
Expand Down
28 changes: 13 additions & 15 deletions compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
use rustc_middle::middle::privacy::EffectiveVisibilities;
use rustc_middle::middle::stability;
use rustc_middle::ty::layout::{LayoutError, LayoutOfHelpers, TyAndLayout};
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::print::{with_no_trimmed_paths, PrintError};
use rustc_middle::ty::{self, print::Printer, GenericArg, RegisteredTools, Ty, TyCtxt};
use rustc_session::config::ExpectedValues;
use rustc_session::lint::{BuiltinLintDiagnostics, LintExpectationId};
Expand Down Expand Up @@ -1206,32 +1206,30 @@ impl<'tcx> LateContext<'tcx> {
}

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

fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}

fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, PrintError> {
Ok(self)
}

fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, Self::Error> {
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, PrintError> {
Ok(self)
}

fn print_dyn_existential(
self,
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self, Self::Error> {
) -> Result<Self, PrintError> {
Ok(self)
}

fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, PrintError> {
Ok(self)
}

fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, PrintError> {
self.path = vec![self.tcx.crate_name(cnum)];
Ok(self)
}
Expand All @@ -1240,7 +1238,7 @@ impl<'tcx> LateContext<'tcx> {
mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, Self::Error> {
) -> Result<Self, PrintError> {
if trait_ref.is_none() {
if let ty::Adt(def, args) = self_ty.kind() {
return self.print_def_path(def.did(), args);
Expand All @@ -1259,11 +1257,11 @@ impl<'tcx> LateContext<'tcx> {

fn path_append_impl(
self,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
_disambiguated_data: &DisambiguatedDefPathData,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, Self::Error> {
) -> Result<Self, PrintError> {
let mut path = print_prefix(self)?;

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

fn path_append(
self,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self, Self::Error> {
) -> Result<Self, PrintError> {
let mut path = print_prefix(self)?;

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

fn path_generic_args(
self,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
_args: &[GenericArg<'tcx>],
) -> Result<Self, Self::Error> {
) -> Result<Self, PrintError> {
print_prefix(self)
}
}
Expand Down
Loading

0 comments on commit 6038888

Please sign in to comment.