Skip to content

Commit 86f397e

Browse files
committed
Get rid of the absolute path printer in type mismatch diagnostics
The logic was added in #42826. The test for it (`run-make/type-mismatch-same-crate-name`) passes when just using the macros to control pretty printing. Note that the test does fail unless both of the macros are used, indicating that we're doing something right.
1 parent 98c1e3d commit 86f397e

File tree

1 file changed

+2
-90
lines changed
  • compiler/rustc_infer/src/infer/error_reporting

1 file changed

+2
-90
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+2-90
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
6767
use rustc_hir::intravisit::Visitor;
6868
use rustc_hir::lang_items::LangItem;
6969
use rustc_middle::dep_graph::DepContext;
70-
use rustc_middle::ty::print::with_forced_trimmed_paths;
70+
use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths, with_no_visible_paths};
7171
use rustc_middle::ty::relate::{self, RelateResult, TypeRelation};
7272
use rustc_middle::ty::{
7373
self, error::TypeError, IsSuggestable, List, Region, Ty, TyCtxt, TypeFoldable,
@@ -573,102 +573,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
573573

574574
/// Adds a note if the types come from similarly named crates
575575
fn check_and_note_conflicting_crates(&self, err: &mut Diagnostic, terr: TypeError<'tcx>) {
576-
use hir::def_id::CrateNum;
577-
use rustc_hir::definitions::DisambiguatedDefPathData;
578-
use ty::print::Printer;
579-
use ty::GenericArg;
580-
581-
struct AbsolutePathPrinter<'tcx> {
582-
tcx: TyCtxt<'tcx>,
583-
}
584-
585-
struct NonTrivialPath;
586-
587-
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
588-
type Error = NonTrivialPath;
589-
590-
type Path = Vec<String>;
591-
type Region = !;
592-
type Type = !;
593-
type DynExistential = !;
594-
type Const = !;
595-
596-
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
597-
self.tcx
598-
}
599-
600-
fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
601-
Err(NonTrivialPath)
602-
}
603-
604-
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
605-
Err(NonTrivialPath)
606-
}
607-
608-
fn print_dyn_existential(
609-
self,
610-
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
611-
) -> Result<Self::DynExistential, Self::Error> {
612-
Err(NonTrivialPath)
613-
}
614-
615-
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
616-
Err(NonTrivialPath)
617-
}
618-
619-
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
620-
Ok(vec![self.tcx.crate_name(cnum).to_string()])
621-
}
622-
fn path_qualified(
623-
self,
624-
_self_ty: Ty<'tcx>,
625-
_trait_ref: Option<ty::TraitRef<'tcx>>,
626-
) -> Result<Self::Path, Self::Error> {
627-
Err(NonTrivialPath)
628-
}
629-
630-
fn path_append_impl(
631-
self,
632-
_print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
633-
_disambiguated_data: &DisambiguatedDefPathData,
634-
_self_ty: Ty<'tcx>,
635-
_trait_ref: Option<ty::TraitRef<'tcx>>,
636-
) -> Result<Self::Path, Self::Error> {
637-
Err(NonTrivialPath)
638-
}
639-
fn path_append(
640-
self,
641-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
642-
disambiguated_data: &DisambiguatedDefPathData,
643-
) -> Result<Self::Path, Self::Error> {
644-
let mut path = print_prefix(self)?;
645-
path.push(disambiguated_data.to_string());
646-
Ok(path)
647-
}
648-
fn path_generic_args(
649-
self,
650-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
651-
_args: &[GenericArg<'tcx>],
652-
) -> Result<Self::Path, Self::Error> {
653-
print_prefix(self)
654-
}
655-
}
656-
657576
let report_path_match = |err: &mut Diagnostic, did1: DefId, did2: DefId| {
658577
// Only report definitions from different crates. If both definitions
659578
// are from a local module we could have false positives, e.g.
660579
// let _ = [{struct Foo; Foo}, {struct Foo; Foo}];
661580
if did1.krate != did2.krate {
662-
let abs_path =
663-
|def_id| AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]);
664-
665581
// We compare strings because DefPath can be different
666582
// for imported and non-imported crates
667-
let same_path = || -> Result<_, NonTrivialPath> {
668-
Ok(self.tcx.def_path_str(did1) == self.tcx.def_path_str(did2)
669-
|| abs_path(did1)? == abs_path(did2)?)
670-
};
671-
if same_path().unwrap_or(false) {
583+
if with_no_trimmed_paths!(with_no_visible_paths!(self.tcx.def_path_str(did1) == self.tcx.def_path_str(did2))) {
672584
let crate_name = self.tcx.crate_name(did1.krate);
673585
let msg = if did1.is_local() || did2.is_local() {
674586
format!(

0 commit comments

Comments
 (0)