Skip to content

Commit

Permalink
rustc: remove PrintCx from ty::Print and rely on printers carrying Ty…
Browse files Browse the repository at this point in the history
…Ctxt.
  • Loading branch information
eddyb committed Mar 15, 2019
1 parent 2a65682 commit 52b4f2d
Show file tree
Hide file tree
Showing 11 changed files with 919 additions and 976 deletions.
54 changes: 27 additions & 27 deletions src/librustc/infer/error_reporting/mod.rs
Expand Up @@ -445,82 +445,82 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
sp: Span,
) {
use hir::def_id::CrateNum;
use ty::print::{PrintCx, Printer};
use ty::print::Printer;
use ty::subst::Kind;

struct AbsolutePathPrinter;
struct AbsolutePathPrinter<'a, 'gcx, 'tcx> {
tcx: TyCtxt<'a, 'gcx, 'tcx>,
}

struct NonTrivialPath;

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

type Path = Vec<String>;
type Region = !;
type Type = !;
type DynExistential = !;

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

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

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

fn print_dyn_existential<'tcx>(
self: PrintCx<'_, '_, 'tcx, Self>,
fn print_dyn_existential(
self,
_predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error> {
Err(NonTrivialPath)
}

fn path_crate(
self: PrintCx<'_, '_, '_, Self>,
self,
cnum: CrateNum,
) -> Result<Self::Path, Self::Error> {
Ok(vec![self.tcx.original_crate_name(cnum).to_string()])
}
fn path_qualified<'tcx>(
self: PrintCx<'_, '_, 'tcx, Self>,
fn path_qualified(
self,
_self_ty: Ty<'tcx>,
_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
Err(NonTrivialPath)
}

fn path_append_impl<'gcx, 'tcx>(
self: PrintCx<'_, 'gcx, 'tcx, Self>,
_print_prefix: impl FnOnce(
PrintCx<'_, 'gcx, 'tcx, Self>,
) -> Result<Self::Path, Self::Error>,
fn path_append_impl(
self,
_print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
_self_ty: Ty<'tcx>,
_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
Err(NonTrivialPath)
}
fn path_append<'gcx, 'tcx>(
self: PrintCx<'_, 'gcx, 'tcx, Self>,
print_prefix: impl FnOnce(
PrintCx<'_, 'gcx, 'tcx, Self>,
) -> Result<Self::Path, Self::Error>,
fn path_append(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
text: &str,
) -> Result<Self::Path, Self::Error> {
let mut path = print_prefix(self)?;
path.push(text.to_string());
Ok(path)
}
fn path_generic_args<'gcx, 'tcx>(
self: PrintCx<'_, 'gcx, 'tcx, Self>,
print_prefix: impl FnOnce(
PrintCx<'_, 'gcx, 'tcx, Self>,
) -> Result<Self::Path, Self::Error>,
fn path_generic_args(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
_args: &[Kind<'tcx>],
) -> Result<Self::Path, Self::Error> {
print_prefix(self)
Expand All @@ -532,7 +532,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
// module we could have false positives
if !(did1.is_local() || did2.is_local()) && did1.krate != did2.krate {
let abs_path = |def_id| {
PrintCx::new(self.tcx, AbsolutePathPrinter)
AbsolutePathPrinter { tcx: self.tcx }
.print_def_path(def_id, None)
};

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/infer/error_reporting/need_type_info.rs
Expand Up @@ -80,11 +80,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
}

let mut s = String::new();
let mut printer = ty::print::FmtPrinter::new(&mut s, Namespace::TypeNS);
let mut printer = ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::TypeNS);
if let Some(highlight) = highlight {
printer.region_highlight_mode = highlight;
}
let _ = ty.print(ty::print::PrintCx::new(self.tcx, printer));
let _ = ty.print(printer);
s
}

Expand Down
Expand Up @@ -337,17 +337,17 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
}
}

impl<'tcx, T> fmt::Display for Highlighted<'_, '_, 'tcx, T>
where T: for<'a, 'b> Print<'tcx,
FmtPrinter<&'a mut fmt::Formatter<'b>>,
impl<'a, 'gcx, 'tcx, T> fmt::Display for Highlighted<'a, 'gcx, 'tcx, T>
where T: for<'b, 'c> Print<'gcx, 'tcx,
FmtPrinter<'a, 'gcx, 'tcx, &'b mut fmt::Formatter<'c>>,
Error = fmt::Error,
>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut printer = ty::print::FmtPrinter::new(f, Namespace::TypeNS);
let mut printer = ty::print::FmtPrinter::new(self.tcx, f, Namespace::TypeNS);
printer.region_highlight_mode = self.highlight;

self.value.print(ty::print::PrintCx::new(self.tcx, printer))?;
self.value.print(printer)?;
Ok(())
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/librustc/mir/mod.rs
Expand Up @@ -34,7 +34,7 @@ use crate::ty::{
self, AdtDef, CanonicalUserTypeAnnotations, ClosureSubsts, GeneratorSubsts, Region, Ty, TyCtxt,
UserTypeAnnotationIndex,
};
use crate::ty::print::{FmtPrinter, Printer, PrintCx};
use crate::ty::print::{FmtPrinter, Printer};

pub use crate::mir::interpret::AssertMessage;

Expand Down Expand Up @@ -2407,9 +2407,10 @@ impl<'tcx> Debug for Rvalue<'tcx> {
let variant_def = &adt_def.variants[variant];

let f = &mut *fmt;
PrintCx::with_tls_tcx(FmtPrinter::new(f, Namespace::ValueNS), |cx| {
let substs = cx.tcx.lift(&substs).expect("could not lift for printing");
cx.print_def_path(variant_def.did, Some(substs))?;
ty::tls::with(|tcx| {
let substs = tcx.lift(&substs).expect("could not lift for printing");
FmtPrinter::new(tcx, f, Namespace::ValueNS)
.print_def_path(variant_def.did, Some(substs))?;
Ok(())
})?;

Expand Down
9 changes: 5 additions & 4 deletions src/librustc/ty/instance.rs
Expand Up @@ -2,7 +2,7 @@ use crate::hir::Unsafety;
use crate::hir::def::Namespace;
use crate::hir::def_id::DefId;
use crate::ty::{self, Ty, PolyFnSig, TypeFoldable, SubstsRef, TyCtxt};
use crate::ty::print::{FmtPrinter, Printer, PrintCx};
use crate::ty::print::{FmtPrinter, Printer};
use crate::traits;
use rustc_target::spec::abi::Abi;
use rustc_macros::HashStable;
Expand Down Expand Up @@ -176,9 +176,10 @@ impl<'tcx> InstanceDef<'tcx> {

impl<'tcx> fmt::Display for Instance<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
PrintCx::with_tls_tcx(FmtPrinter::new(&mut *f, Namespace::ValueNS), |cx| {
let substs = cx.tcx.lift(&self.substs).expect("could not lift for printing");
cx.print_def_path(self.def_id(), Some(substs))?;
ty::tls::with(|tcx| {
let substs = tcx.lift(&self.substs).expect("could not lift for printing");
FmtPrinter::new(tcx, &mut *f, Namespace::ValueNS)
.print_def_path(self.def_id(), Some(substs))?;
Ok(())
})?;

Expand Down

0 comments on commit 52b4f2d

Please sign in to comment.