Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8572eac

Browse files
committed
Get rid of the absolute path printer in type mismatch diagnostics
The logic was added in #42826. The tests for it (`run-make/type-mismatch-same-crate-name` and `ui/type/type/type-mismatch-same-crate-name.rs) pass when being replaced by this, which makes sense, as `no_trimmed!(no_visible!())` should be equivalent to the custom absolute printer.
1 parent 98c1e3d commit 8572eac

File tree

1 file changed

+9
-92
lines changed
  • compiler/rustc_infer/src/infer/error_reporting

1 file changed

+9
-92
lines changed

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

Lines changed: 9 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ 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::{
71+
with_forced_trimmed_paths, with_no_trimmed_paths, with_no_visible_paths,
72+
};
7173
use rustc_middle::ty::relate::{self, RelateResult, TypeRelation};
7274
use rustc_middle::ty::{
7375
self, error::TypeError, IsSuggestable, List, Region, Ty, TyCtxt, TypeFoldable,
@@ -573,102 +575,17 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
573575

574576
/// Adds a note if the types come from similarly named crates
575577
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-
657578
let report_path_match = |err: &mut Diagnostic, did1: DefId, did2: DefId| {
658579
// Only report definitions from different crates. If both definitions
659580
// are from a local module we could have false positives, e.g.
660581
// let _ = [{struct Foo; Foo}, {struct Foo; Foo}];
661582
if did1.krate != did2.krate {
662-
let abs_path =
663-
|def_id| AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]);
664-
665-
// We compare strings because DefPath can be different
666-
// 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+
// We check for both equality of their definition paths (when the crates have the same name) anbd equality
584+
// of their import paths (then the crates have been imported using the same name).
585+
// Either of these should cause the note to be emitted.
586+
let equals = || self.tcx.def_path_str(did1) == self.tcx.def_path_str(did2);
587+
588+
if equals() || with_no_trimmed_paths!(with_no_visible_paths!(equals())) {
672589
let crate_name = self.tcx.crate_name(did1.krate);
673590
let msg = if did1.is_local() || did2.is_local() {
674591
format!(

0 commit comments

Comments
 (0)