Skip to content

Commit 9b7242c

Browse files
committed
rustc: assert ty::print::FORCE_ABSOLUTE isn't needed anymore.
1 parent e8fb697 commit 9b7242c

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

src/librustc/ty/print.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -716,9 +716,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
716716
// FIXME(eddyb) avoid querying `tcx.generics_of` and `tcx.def_key`
717717
// both here and in `default_print_def_path`.
718718
let generics = substs.map(|_| self.tcx.generics_of(def_id));
719-
if // HACK(eddyb) remove the `FORCE_ABSOLUTE` hack by bypassing `FmtPrinter`
720-
!FORCE_ABSOLUTE.with(|force| force.get()) &&
721-
generics.as_ref().and_then(|g| g.parent).is_none() {
719+
// HACK(eddyb) remove the `FORCE_ABSOLUTE` hack by bypassing `FmtPrinter`
720+
assert!(!FORCE_ABSOLUTE.with(|force| force.get()));
721+
if generics.as_ref().and_then(|g| g.parent).is_none() {
722722
if let Some(path) = self.try_print_visible_def_path(def_id) {
723723
let path = if let (Some(generics), Some(substs)) = (generics, substs) {
724724
let has_own_self = generics.has_self && generics.parent_count == 0;
@@ -736,8 +736,6 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
736736
// Always use types for non-local impls, where types are always
737737
// available, and filename/line-number is mostly uninteresting.
738738
let use_types =
739-
// HACK(eddyb) remove the `FORCE_ABSOLUTE` hack by bypassing `FmtPrinter`
740-
FORCE_ABSOLUTE.with(|force| force.get()) ||
741739
!def_id.is_local() || {
742740
// Otherwise, use filename/line-number if forced.
743741
let force_no_types = FORCE_IMPL_FILENAME_LINE.with(|f| f.get());
@@ -760,10 +758,8 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
760758

761759
fn path_crate(self: &mut PrintCx<'_, '_, '_, Self>, cnum: CrateNum) -> Self::Path {
762760
// HACK(eddyb) remove the `FORCE_ABSOLUTE` hack by bypassing `FmtPrinter`
763-
if FORCE_ABSOLUTE.with(|force| force.get()) {
764-
write!(self.printer, "{}", self.tcx.original_crate_name(cnum))?;
765-
return Ok(PrettyPath { empty: false });
766-
}
761+
assert!(!FORCE_ABSOLUTE.with(|force| force.get()));
762+
767763
if cnum == LOCAL_CRATE {
768764
if self.tcx.sess.rust_2018() {
769765
// We add the `crate::` keyword on Rust 2018, only when desired.

0 commit comments

Comments
 (0)