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 16, 2023
1 parent f833309 commit c464658
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 170 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
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
54 changes: 27 additions & 27 deletions compiler/rustc_middle/src/ty/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
mod pretty;
pub use self::pretty::*;

pub type PrintError = std::fmt::Error;

// FIXME(eddyb) false positive, the lifetime parameters are used with `P: Printer<...>`.
#[allow(unused_lifetimes)]
pub trait Print<'tcx, P> {
type Error;

fn print(&self, cx: P) -> Result<P, Self::Error>;
fn print(&self, cx: P) -> Result<P, PrintError>;
}

/// Interface for outputting user-facing "type-system entities"
Expand All @@ -28,15 +30,13 @@ pub trait Print<'tcx, P> {
//
// FIXME(eddyb) find a better name; this is more general than "printing".
pub trait Printer<'tcx>: Sized {
type Error;

fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;

fn print_def_path(
self,
def_id: DefId,
args: &'tcx [GenericArg<'tcx>],
) -> Result<Self, Self::Error> {
) -> Result<Self, PrintError> {
self.default_print_def_path(def_id, args)
}

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

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

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

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

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

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

fn path_qualified(
self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, Self::Error>;
) -> Result<Self, PrintError>;

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>;

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>;

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>;

// Defaults (should not be overridden):

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

Expand Down Expand Up @@ -187,7 +187,7 @@ pub trait Printer<'tcx>: Sized {
_args: &'tcx [GenericArg<'tcx>],
self_ty: Ty<'tcx>,
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self, Self::Error> {
) -> Result<Self, PrintError> {
debug!(
"default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}",
impl_def_id, self_ty, impl_trait_ref
Expand Down Expand Up @@ -288,30 +288,30 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
}

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

impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for Ty<'tcx> {
type Error = P::Error;
type Error = PrintError;

fn print(&self, cx: P) -> Result<P, Self::Error> {
fn print(&self, cx: P) -> Result<P, PrintError> {
cx.print_type(*self)
}
}

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

impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Const<'tcx> {
type Error = P::Error;
fn print(&self, cx: P) -> Result<P, Self::Error> {
type Error = PrintError;
fn print(&self, cx: P) -> Result<P, PrintError> {
cx.print_const(*self)
}
}
Expand Down
Loading

0 comments on commit c464658

Please sign in to comment.