Skip to content

Commit bbedb51

Browse files
committed
rustc: move the FORCE_IMPL_FILENAME_LINE hack into print_def_path.
1 parent 039f513 commit bbedb51

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<&'tcx Substs<'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]
@@ -272,7 +274,16 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
272274
}
273275

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

278289
_ => {
@@ -322,30 +333,24 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
322333
fn default_print_impl_path(
323334
&mut self,
324335
impl_def_id: DefId,
325-
substs: Option<&'tcx Substs<'tcx>>,
336+
_substs: Option<&'tcx Substs<'tcx>>,
326337
ns: Namespace,
338+
self_ty: Ty<'tcx>,
339+
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
327340
) -> P::Path {
328-
debug!("default_print_impl_path: impl_def_id={:?}", impl_def_id);
329-
let parent_def_id = self.tcx.parent(impl_def_id).unwrap();
341+
debug!("default_print_impl_path: impl_def_id={:?}, self_ty={}, impl_trait_ref={:?}",
342+
impl_def_id, self_ty, impl_trait_ref);
330343

331344
// Decide whether to print the parent path for the impl.
332345
// Logically, since impls are global, it's never needed, but
333346
// users may find it useful. Currently, we omit the parent if
334347
// the impl is either in the same module as the self-type or
335348
// as the trait.
336-
let mut self_ty = self.tcx.type_of(impl_def_id);
337-
if let Some(substs) = substs {
338-
self_ty = self_ty.subst(self.tcx, substs);
339-
}
349+
let parent_def_id = self.tcx.parent(impl_def_id).unwrap();
340350
let in_self_mod = match characteristic_def_id_of_type(self_ty) {
341351
None => false,
342352
Some(ty_def_id) => self.tcx.parent(ty_def_id) == Some(parent_def_id),
343353
};
344-
345-
let mut impl_trait_ref = self.tcx.impl_trait_ref(impl_def_id);
346-
if let Some(substs) = substs {
347-
impl_trait_ref = impl_trait_ref.subst(self.tcx, substs);
348-
}
349354
let in_trait_mod = match impl_trait_ref {
350355
None => false,
351356
Some(trait_ref) => self.tcx.parent(trait_ref.def_id) == Some(parent_def_id),
@@ -696,7 +701,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
696701
ns: Namespace,
697702
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
698703
) -> Self::Path {
699-
// FIXME(eddyb) avoid querying `tcx.generics_of`
704+
// FIXME(eddyb) avoid querying `tcx.generics_of` and `tcx.def_key`
700705
// both here and in `default_print_def_path`.
701706
let generics = substs.map(|_| self.tcx.generics_of(def_id));
702707
if // HACK(eddyb) remove the `FORCE_ABSOLUTE` hack by bypassing `FmtPrinter`
@@ -714,35 +719,31 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
714719
}
715720
}
716721

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

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

745-
self.default_print_impl_path(impl_def_id, substs, ns)
746+
self.default_print_def_path(def_id, substs, ns, projections)
746747
}
747748

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

0 commit comments

Comments
 (0)