Skip to content

Commit 39fd54a

Browse files
committed
rustc: move the FORCE_IMPL_FILENAME_LINE hack into print_def_path.
1 parent aec5a48 commit 39fd54a

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

src/librustc/ty/print.rs

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ pub trait Printer: Sized {
162162
impl_def_id: DefId,
163163
substs: Option<SubstsRef<'tcx>>,
164164
ns: Namespace,
165+
self_ty: Ty<'tcx>,
166+
trait_ref: Option<ty::TraitRef<'tcx>>,
165167
) -> Self::Path {
166-
self.default_print_impl_path(impl_def_id, substs, ns)
168+
self.default_print_impl_path(impl_def_id, substs, ns, self_ty, trait_ref)
167169
}
168170

169171
#[must_use]
@@ -273,7 +275,16 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
273275
}
274276

275277
DefPathData::Impl => {
276-
self.print_impl_path(def_id, substs, ns)
278+
let mut self_ty = self.tcx.type_of(def_id);
279+
if let Some(substs) = substs {
280+
self_ty = self_ty.subst(self.tcx, substs);
281+
}
282+
283+
let mut impl_trait_ref = self.tcx.impl_trait_ref(def_id);
284+
if let Some(substs) = substs {
285+
impl_trait_ref = impl_trait_ref.subst(self.tcx, substs);
286+
}
287+
self.print_impl_path(def_id, substs, ns, self_ty, impl_trait_ref)
277288
}
278289

279290
_ => {
@@ -323,30 +334,24 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
323334
fn default_print_impl_path(
324335
&mut self,
325336
impl_def_id: DefId,
326-
substs: Option<SubstsRef<'tcx>>,
337+
_substs: Option<SubstsRef<'tcx>>,
327338
ns: Namespace,
339+
self_ty: Ty<'tcx>,
340+
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
328341
) -> P::Path {
329-
debug!("default_print_impl_path: impl_def_id={:?}", impl_def_id);
330-
let parent_def_id = self.tcx.parent(impl_def_id).unwrap();
342+
debug!("default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}",
343+
impl_def_id, self_ty, impl_trait_ref);
331344

332345
// Decide whether to print the parent path for the impl.
333346
// Logically, since impls are global, it's never needed, but
334347
// users may find it useful. Currently, we omit the parent if
335348
// the impl is either in the same module as the self-type or
336349
// as the trait.
337-
let mut self_ty = self.tcx.type_of(impl_def_id);
338-
if let Some(substs) = substs {
339-
self_ty = self_ty.subst(self.tcx, substs);
340-
}
350+
let parent_def_id = self.tcx.parent(impl_def_id).unwrap();
341351
let in_self_mod = match characteristic_def_id_of_type(self_ty) {
342352
None => false,
343353
Some(ty_def_id) => self.tcx.parent(ty_def_id) == Some(parent_def_id),
344354
};
345-
346-
let mut impl_trait_ref = self.tcx.impl_trait_ref(impl_def_id);
347-
if let Some(substs) = substs {
348-
impl_trait_ref = impl_trait_ref.subst(self.tcx, substs);
349-
}
350355
let in_trait_mod = match impl_trait_ref {
351356
None => false,
352357
Some(trait_ref) => self.tcx.parent(trait_ref.def_id) == Some(parent_def_id),
@@ -702,7 +707,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
702707
ns: Namespace,
703708
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
704709
) -> Self::Path {
705-
// FIXME(eddyb) avoid querying `tcx.generics_of`
710+
// FIXME(eddyb) avoid querying `tcx.generics_of` and `tcx.def_key`
706711
// both here and in `default_print_def_path`.
707712
let generics = substs.map(|_| self.tcx.generics_of(def_id));
708713
if // HACK(eddyb) remove the `FORCE_ABSOLUTE` hack by bypassing `FmtPrinter`
@@ -720,35 +725,31 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
720725
}
721726
}
722727

723-
self.default_print_def_path(def_id, substs, ns, projections)
724-
}
725-
fn print_impl_path(
726-
self: &mut PrintCx<'_, '_, 'tcx, Self>,
727-
impl_def_id: DefId,
728-
substs: Option<SubstsRef<'tcx>>,
729-
ns: Namespace,
730-
) -> Self::Path {
731-
// Always use types for non-local impls, where types are always
732-
// available, and filename/line-number is mostly uninteresting.
733-
let use_types = // HACK(eddyb) remove the `FORCE_ABSOLUTE` hack by bypassing `FmtPrinter`
734-
FORCE_ABSOLUTE.with(|force| force.get()) ||
735-
!impl_def_id.is_local() || {
736-
// Otherwise, use filename/line-number if forced.
737-
let force_no_types = FORCE_IMPL_FILENAME_LINE.with(|f| f.get());
738-
!force_no_types
739-
};
728+
let key = self.tcx.def_key(def_id);
729+
if let DefPathData::Impl = key.disambiguated_data.data {
730+
// Always use types for non-local impls, where types are always
731+
// available, and filename/line-number is mostly uninteresting.
732+
let use_types =
733+
// HACK(eddyb) remove the `FORCE_ABSOLUTE` hack by bypassing `FmtPrinter`
734+
FORCE_ABSOLUTE.with(|force| force.get()) ||
735+
!def_id.is_local() || {
736+
// Otherwise, use filename/line-number if forced.
737+
let force_no_types = FORCE_IMPL_FILENAME_LINE.with(|f| f.get());
738+
!force_no_types
739+
};
740740

741-
if !use_types {
742-
// If no type info is available, fall back to
743-
// pretty printing some span information. This should
744-
// only occur very early in the compiler pipeline.
745-
let parent_def_id = self.tcx.parent(impl_def_id).unwrap();
746-
let path = self.print_def_path(parent_def_id, None, ns, iter::empty());
747-
let span = self.tcx.def_span(impl_def_id);
748-
return self.path_append(path, &format!("<impl at {:?}>", span));
741+
if !use_types {
742+
// If no type info is available, fall back to
743+
// pretty printing some span information. This should
744+
// only occur very early in the compiler pipeline.
745+
let parent_def_id = DefId { index: key.parent.unwrap(), ..def_id };
746+
let path = self.print_def_path(parent_def_id, None, ns, iter::empty());
747+
let span = self.tcx.def_span(def_id);
748+
return self.path_append(path, &format!("<impl at {:?}>", span));
749+
}
749750
}
750751

751-
self.default_print_impl_path(impl_def_id, substs, ns)
752+
self.default_print_def_path(def_id, substs, ns, projections)
752753
}
753754

754755
fn path_crate(self: &mut PrintCx<'_, '_, '_, Self>, cnum: CrateNum) -> Self::Path {

0 commit comments

Comments
 (0)