Skip to content

Commit

Permalink
Remove Print::Error
Browse files Browse the repository at this point in the history
All printing goes through `fmt::Error` now.
  • Loading branch information
Nilstrieb committed Oct 17, 2023
1 parent 6038888 commit 6fc6a6d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 32 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/util/type_name.rs
Expand Up @@ -138,7 +138,7 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {
}
fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, PrintError>
where
T: Print<'tcx, Self, Error = PrintError>,
T: Print<'tcx, Self>,
{
if let Some(first) = elems.next() {
self = first.print(self)?;
Expand Down
Expand Up @@ -28,7 +28,7 @@ pub struct Highlighted<'tcx, T> {

impl<'tcx, T> IntoDiagnosticArg for Highlighted<'tcx, T>
where
T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>, Error = fmt::Error>,
T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>>,
{
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
rustc_errors::DiagnosticArgValue::Str(self.to_string().into())
Expand All @@ -43,7 +43,7 @@ impl<'tcx, T> Highlighted<'tcx, T> {

impl<'tcx, T> fmt::Display for Highlighted<'tcx, T>
where
T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>, Error = fmt::Error>,
T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS);
Expand Down
7 changes: 0 additions & 7 deletions compiler/rustc_middle/src/ty/print/mod.rs
Expand Up @@ -15,8 +15,6 @@ 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, PrintError>;
}

Expand Down Expand Up @@ -288,29 +286,24 @@ 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 = 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 = PrintError;

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 = 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 = PrintError;
fn print(&self, cx: P) -> Result<P, PrintError> {
cx.print_const(*self)
}
Expand Down
26 changes: 11 additions & 15 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Expand Up @@ -217,7 +217,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {

fn in_binder<T>(self, value: &ty::Binder<'tcx, T>) -> Result<Self, PrintError>
where
T: Print<'tcx, Self, Error = PrintError> + TypeFoldable<TyCtxt<'tcx>>,
T: Print<'tcx, Self> + TypeFoldable<TyCtxt<'tcx>>,
{
value.as_ref().skip_binder().print(self)
}
Expand All @@ -228,15 +228,15 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
f: F,
) -> Result<Self, PrintError>
where
T: Print<'tcx, Self, Error = PrintError> + TypeFoldable<TyCtxt<'tcx>>,
T: Print<'tcx, Self> + TypeFoldable<TyCtxt<'tcx>>,
{
f(value.as_ref().skip_binder(), self)
}

/// Prints comma-separated elements.
fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, PrintError>
where
T: Print<'tcx, Self, Error = PrintError>,
T: Print<'tcx, Self>,
{
if let Some(first) = elems.next() {
self = first.print(self)?;
Expand Down Expand Up @@ -2083,7 +2083,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {

fn in_binder<T>(self, value: &ty::Binder<'tcx, T>) -> Result<Self, PrintError>
where
T: Print<'tcx, Self, Error = PrintError> + TypeFoldable<TyCtxt<'tcx>>,
T: Print<'tcx, Self> + TypeFoldable<TyCtxt<'tcx>>,
{
self.pretty_in_binder(value)
}
Expand All @@ -2094,7 +2094,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
f: C,
) -> Result<Self, PrintError>
where
T: Print<'tcx, Self, Error = PrintError> + TypeFoldable<TyCtxt<'tcx>>,
T: Print<'tcx, Self> + TypeFoldable<TyCtxt<'tcx>>,
{
self.pretty_wrap_binder(value, f)
}
Expand Down Expand Up @@ -2343,7 +2343,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
value: &ty::Binder<'tcx, T>,
) -> Result<(Self, T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>), fmt::Error>
where
T: Print<'tcx, Self, Error = fmt::Error> + TypeFoldable<TyCtxt<'tcx>>,
T: Print<'tcx, Self> + TypeFoldable<TyCtxt<'tcx>>,
{
fn name_by_region_index(
index: usize,
Expand Down Expand Up @@ -2513,7 +2513,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {

pub fn pretty_in_binder<T>(self, value: &ty::Binder<'tcx, T>) -> Result<Self, fmt::Error>
where
T: Print<'tcx, Self, Error = fmt::Error> + TypeFoldable<TyCtxt<'tcx>>,
T: Print<'tcx, Self> + TypeFoldable<TyCtxt<'tcx>>,
{
let old_region_index = self.region_index;
let (new, new_value, _) = self.name_all_regions(value)?;
Expand All @@ -2529,7 +2529,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
f: C,
) -> Result<Self, fmt::Error>
where
T: Print<'tcx, Self, Error = fmt::Error> + TypeFoldable<TyCtxt<'tcx>>,
T: Print<'tcx, Self> + TypeFoldable<TyCtxt<'tcx>>,
{
let old_region_index = self.region_index;
let (new, new_value, _) = self.name_all_regions(value)?;
Expand Down Expand Up @@ -2594,21 +2594,18 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {

impl<'tcx, T, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::Binder<'tcx, T>
where
T: Print<'tcx, P, Error = PrintError> + TypeFoldable<TyCtxt<'tcx>>,
T: Print<'tcx, P> + TypeFoldable<TyCtxt<'tcx>>,
{
type Error = PrintError;

fn print(&self, cx: P) -> Result<P, PrintError> {
cx.in_binder(self)
}
}

impl<'tcx, T, U, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::OutlivesPredicate<T, U>
where
T: Print<'tcx, P, Error = PrintError>,
U: Print<'tcx, P, Error = PrintError>,
T: Print<'tcx, P>,
U: Print<'tcx, P>,
{
type Error = PrintError;
fn print(&self, mut cx: P) -> Result<P, PrintError> {
define_scoped_cx!(cx);
p!(print(self.0), ": ", print(self.1));
Expand Down Expand Up @@ -2636,7 +2633,6 @@ macro_rules! forward_display_to_print {
macro_rules! define_print_and_forward_display {
(($self:ident, $cx:ident): $($ty:ty $print:block)+) => {
$(impl<'tcx, P: PrettyPrinter<'tcx>> Print<'tcx, P> for $ty {
type Error = fmt::Error;
fn print(&$self, $cx: P) -> Result<P, PrintError> {
#[allow(unused_mut)]
let mut $cx = $cx;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_symbol_mangling/src/legacy.rs
Expand Up @@ -365,7 +365,7 @@ impl<'tcx> PrettyPrinter<'tcx> for &mut SymbolPrinter<'tcx> {
}
fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, PrintError>
where
T: Print<'tcx, Self, Error = PrintError>,
T: Print<'tcx, Self>,
{
if let Some(first) = elems.next() {
self = first.print(self)?;
Expand Down
Expand Up @@ -60,8 +60,7 @@ pub trait TypeErrCtxtExt<'tcx> {
suggest_increasing_limit: bool,
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>
where
T: fmt::Display + TypeFoldable<TyCtxt<'tcx>> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>,
<T as Print<'tcx, FmtPrinter<'tcx, 'tcx>>>::Error: std::fmt::Debug;
T: fmt::Display + TypeFoldable<TyCtxt<'tcx>> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>;

fn report_overflow_error<T>(
&self,
Expand All @@ -71,8 +70,7 @@ pub trait TypeErrCtxtExt<'tcx> {
mutate: impl FnOnce(&mut Diagnostic),
) -> !
where
T: fmt::Display + TypeFoldable<TyCtxt<'tcx>> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>,
<T as Print<'tcx, FmtPrinter<'tcx, 'tcx>>>::Error: std::fmt::Debug;
T: fmt::Display + TypeFoldable<TyCtxt<'tcx>> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>;

fn report_overflow_no_abort(&self, obligation: PredicateObligation<'tcx>) -> ErrorGuaranteed;

Expand Down Expand Up @@ -224,7 +222,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
) -> !
where
T: fmt::Display + TypeFoldable<TyCtxt<'tcx>> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>,
<T as Print<'tcx, FmtPrinter<'tcx, 'tcx>>>::Error: std::fmt::Debug,
{
let mut err = self.build_overflow_error(predicate, span, suggest_increasing_limit);
mutate(&mut err);
Expand All @@ -242,7 +239,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>
where
T: fmt::Display + TypeFoldable<TyCtxt<'tcx>> + Print<'tcx, FmtPrinter<'tcx, 'tcx>>,
<T as Print<'tcx, FmtPrinter<'tcx, 'tcx>>>::Error: std::fmt::Debug,
{
let predicate = self.resolve_vars_if_possible(predicate.clone());
let mut pred_str = predicate.to_string();
Expand Down

0 comments on commit 6fc6a6d

Please sign in to comment.