Skip to content

Commit 66cc029

Browse files
committed
rustc: assert ty::print::FORCE_ABSOLUTE isn't needed anymore.
1 parent df6650f commit 66cc029

File tree

2 files changed

+63
-18
lines changed

2 files changed

+63
-18
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use crate::traits::{ObligationCause, ObligationCauseCode};
5858
use crate::ty::error::TypeError;
5959
use crate::ty::{self, subst::{Subst, SubstsRef}, Region, Ty, TyCtxt, TyKind, TypeFoldable};
6060
use errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString};
61-
use std::{cmp, fmt};
61+
use std::{cmp, fmt, iter};
6262
use syntax_pos::{Pos, Span};
6363

6464
mod note;
@@ -444,20 +444,69 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
444444
terr: &TypeError<'tcx>,
445445
sp: Span,
446446
) {
447+
use hir::def::Namespace;
448+
use hir::def_id::CrateNum;
449+
use ty::print::{PrintCx, Printer};
450+
use ty::subst::Substs;
451+
452+
struct AbsolutePathPrinter;
453+
454+
struct NonTrivialPath;
455+
456+
impl Printer for AbsolutePathPrinter {
457+
type Path = Result<Vec<String>, NonTrivialPath>;
458+
459+
fn path_crate(self: &mut PrintCx<'_, '_, '_, Self>, cnum: CrateNum) -> Self::Path {
460+
Ok(vec![self.tcx.original_crate_name(cnum).to_string()])
461+
}
462+
fn path_qualified<'tcx>(
463+
self: &mut PrintCx<'_, '_, 'tcx, Self>,
464+
_impl_prefix: Option<Self::Path>,
465+
_self_ty: Ty<'tcx>,
466+
_trait_ref: Option<ty::TraitRef<'tcx>>,
467+
_ns: Namespace,
468+
) -> Self::Path {
469+
Err(NonTrivialPath)
470+
}
471+
fn path_append(
472+
self: &mut PrintCx<'_, '_, '_, Self>,
473+
path: Self::Path,
474+
text: &str,
475+
) -> Self::Path {
476+
let mut path = path?;
477+
path.push(text.to_string());
478+
Ok(path)
479+
}
480+
fn path_generic_args<'tcx>(
481+
self: &mut PrintCx<'_, '_, 'tcx, Self>,
482+
path: Self::Path,
483+
_params: &[ty::GenericParamDef],
484+
_substs: &'tcx Substs<'tcx>,
485+
_ns: Namespace,
486+
_projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
487+
) -> Self::Path {
488+
path
489+
}
490+
}
491+
447492
let report_path_match = |err: &mut DiagnosticBuilder<'_>, did1: DefId, did2: DefId| {
448493
// Only external crates, if either is from a local
449494
// module we could have false positives
450495
if !(did1.is_local() || did2.is_local()) && did1.krate != did2.krate {
451-
let exp_path = self.tcx.def_path_str(did1);
452-
let found_path = self.tcx.def_path_str(did2);
453-
// HACK(eddyb) switch form `with_forced_absolute_paths`
454-
// to a custom implementation of `ty::print::Printer`.
455-
let (exp_abs_path, found_abs_path) = ty::print::with_forced_absolute_paths(|| {
456-
(self.tcx.def_path_str(did1), self.tcx.def_path_str(did2))
457-
});
496+
let abs_path = |def_id| {
497+
PrintCx::new(self.tcx, AbsolutePathPrinter)
498+
.print_def_path(def_id, None, Namespace::TypeNS, iter::empty())
499+
};
500+
458501
// We compare strings because DefPath can be different
459502
// for imported and non-imported crates
460-
if exp_path == found_path || exp_abs_path == found_abs_path {
503+
let same_path = || -> Result<_, NonTrivialPath> {
504+
Ok(
505+
self.tcx.def_path_str(did1) == self.tcx.def_path_str(did2) ||
506+
abs_path(did1)? == abs_path(did2)?
507+
)
508+
};
509+
if same_path().unwrap_or(false) {
461510
let crate_name = self.tcx.crate_name(did1.krate);
462511
err.span_note(
463512
sp,

src/librustc/ty/print.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -722,9 +722,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
722722
// FIXME(eddyb) avoid querying `tcx.generics_of` and `tcx.def_key`
723723
// both here and in `default_print_def_path`.
724724
let generics = substs.map(|_| self.tcx.generics_of(def_id));
725-
if // HACK(eddyb) remove the `FORCE_ABSOLUTE` hack by bypassing `FmtPrinter`
726-
!FORCE_ABSOLUTE.with(|force| force.get()) &&
727-
generics.as_ref().and_then(|g| g.parent).is_none() {
725+
// HACK(eddyb) remove the `FORCE_ABSOLUTE` hack by bypassing `FmtPrinter`
726+
assert!(!FORCE_ABSOLUTE.with(|force| force.get()));
727+
if generics.as_ref().and_then(|g| g.parent).is_none() {
728728
if let Some(path) = self.try_print_visible_def_path(def_id) {
729729
let path = if let (Some(generics), Some(substs)) = (generics, substs) {
730730
let has_own_self = generics.has_self && generics.parent_count == 0;
@@ -742,8 +742,6 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
742742
// Always use types for non-local impls, where types are always
743743
// available, and filename/line-number is mostly uninteresting.
744744
let use_types =
745-
// HACK(eddyb) remove the `FORCE_ABSOLUTE` hack by bypassing `FmtPrinter`
746-
FORCE_ABSOLUTE.with(|force| force.get()) ||
747745
!def_id.is_local() || {
748746
// Otherwise, use filename/line-number if forced.
749747
let force_no_types = FORCE_IMPL_FILENAME_LINE.with(|f| f.get());
@@ -766,10 +764,8 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
766764

767765
fn path_crate(self: &mut PrintCx<'_, '_, '_, Self>, cnum: CrateNum) -> Self::Path {
768766
// HACK(eddyb) remove the `FORCE_ABSOLUTE` hack by bypassing `FmtPrinter`
769-
if FORCE_ABSOLUTE.with(|force| force.get()) {
770-
write!(self.printer, "{}", self.tcx.original_crate_name(cnum))?;
771-
return Ok(PrettyPath { empty: false });
772-
}
767+
assert!(!FORCE_ABSOLUTE.with(|force| force.get()));
768+
773769
if cnum == LOCAL_CRATE {
774770
if self.tcx.sess.rust_2018() {
775771
// We add the `crate::` keyword on Rust 2018, only when desired.

0 commit comments

Comments
 (0)