Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 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,23 +14,15 @@ struct AbsolutePathPrinter<'tcx> {
}

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

type Path = Self;
type Region = Self;
type Type = Self;
type DynExistential = Self;
type Const = Self;

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

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

fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, 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 @@ -68,18 +60,18 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
}
}

fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, 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::DynExistential, Self::Error> {
) -> Result<Self, PrintError> {
self.pretty_print_dyn_existential(predicates)
}

fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, 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 @@ -88,17 +80,17 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, 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::Path, 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::Path, Self::Error> {
) -> Result<Self, PrintError> {
self.pretty_path_append_impl(
|mut cx| {
cx = print_prefix(cx)?;
Expand All @@ -114,9 +106,9 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {

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

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

fn path_generic_args(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
args: &[GenericArg<'tcx>],
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, PrintError> {
self = print_prefix(self)?;
let args =
args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
Expand All @@ -144,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, Output = Self, Error = Self::Error>,
T: Print<'tcx, Self>,
{
if let Some(first) = elems.next() {
self = first.print(self)?;
Expand All @@ -160,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
69 changes: 32 additions & 37 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 @@ -580,76 +580,68 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {

struct AbsolutePathPrinter<'tcx> {
tcx: TyCtxt<'tcx>,
segments: Vec<String>,
}

struct NonTrivialPath;

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

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

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

fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, 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::Type, 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::DynExistential, Self::Error> {
Err(NonTrivialPath)
) -> Result<Self, PrintError> {
Err(fmt::Error)
}

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

fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
Ok(vec![self.tcx.crate_name(cnum).to_string()])
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::Path, Self::Error> {
Err(NonTrivialPath)
) -> Result<Self, PrintError> {
Err(fmt::Error)
}

fn path_append_impl(
self,
_print_prefix: impl FnOnce(Self) -> Result<Self::Path, 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::Path, Self::Error> {
Err(NonTrivialPath)
) -> Result<Self, PrintError> {
Err(fmt::Error)
}
fn path_append(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error> {
let mut path = print_prefix(self)?;
path.push(disambiguated_data.to_string());
Ok(path)
) -> 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::Path, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, PrintError>,
_args: &[GenericArg<'tcx>],
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, PrintError> {
print_prefix(self)
}
}
Expand All @@ -659,12 +651,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
// are from a local module we could have false positives, e.g.
// let _ = [{struct Foo; Foo}, {struct Foo; Foo}];
if did1.krate != did2.krate {
let abs_path =
|def_id| AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]);
let abs_path = |def_id| {
AbsolutePathPrinter { tcx: self.tcx, segments: vec![] }
.print_def_path(def_id, &[])
.map(|p| p.segments)
};

// 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
Original file line number Diff line number Diff line change
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, Output = FmtPrinter<'a, 'tcx>>,
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, Output = FmtPrinter<'a, 'tcx>>,
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
Loading