Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 4c6385f

Browse files
committed
Make Impl.trait_ a Path, not a Type
It should only ever be a `ResolvedPath`, so this (a) enforces that, and (b) reduces the size of `Impl`. I had to update a test because the order of the rendered auto trait impl bounds changed. I think the order changed because rustdoc sorts auto trait bounds using their `Debug` output.
1 parent d91946b commit 4c6385f

File tree

10 files changed

+73
-25
lines changed

10 files changed

+73
-25
lines changed

src/librustdoc/clean/auto_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
118118
span: Span::dummy(),
119119
unsafety: hir::Unsafety::Normal,
120120
generics: new_generics,
121-
trait_: Some(trait_ref.clean(self.cx).get_trait_type().unwrap()),
121+
trait_: Some(trait_ref.clean(self.cx).get_trait_type().unwrap().expect_path()),
122122
for_: ty.clean(self.cx),
123123
items: Vec::new(),
124124
negative_polarity,

src/librustdoc/clean/blanket_impl.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
114114
.clean(self.cx),
115115
// FIXME(eddyb) compute both `trait_` and `for_` from
116116
// the post-inference `trait_ref`, as it's more accurate.
117-
trait_: Some(trait_ref.clean(self.cx).get_trait_type().unwrap()),
117+
trait_: Some(
118+
trait_ref.clean(self.cx).get_trait_type().unwrap().expect_path(),
119+
),
118120
for_: ty.clean(self.cx),
119121
items: self
120122
.cx

src/librustdoc/clean/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ crate fn build_impl(
483483
span: clean::types::rustc_span(did, cx.tcx),
484484
unsafety: hir::Unsafety::Normal,
485485
generics,
486-
trait_,
486+
trait_: trait_.map(|t| t.expect_path()),
487487
for_,
488488
items: trait_items,
489489
negative_polarity: polarity.clean(cx),

src/librustdoc/clean/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,12 @@ impl Clean<Type> for hir::TraitRef<'_> {
903903
}
904904
}
905905

906+
impl Clean<Path> for hir::TraitRef<'_> {
907+
fn clean(&self, cx: &mut DocContext<'_>) -> Path {
908+
self.path.clean(cx)
909+
}
910+
}
911+
906912
impl Clean<PolyTrait> for hir::PolyTraitRef<'_> {
907913
fn clean(&self, cx: &mut DocContext<'_>) -> PolyTrait {
908914
PolyTrait {
@@ -1902,7 +1908,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>
19021908
DefKind::TyAlias => Some(tcx.type_of(did).clean(cx)),
19031909
_ => None,
19041910
});
1905-
let mut make_item = |trait_: Option<Type>, for_: Type, items: Vec<Item>| {
1911+
let mut make_item = |trait_: Option<Path>, for_: Type, items: Vec<Item>| {
19061912
let kind = ImplItem(Impl {
19071913
span: types::rustc_span(tcx.hir().local_def_id(hir_id).to_def_id(), tcx),
19081914
unsafety: impl_.unsafety,

src/librustdoc/clean/types.rs

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,15 @@ impl Type {
14731473
}
14741474
}
14751475

1476+
// FIXME: temporary
1477+
#[track_caller]
1478+
crate fn expect_path(self) -> Path {
1479+
match self {
1480+
ResolvedPath { path, .. } => path,
1481+
_ => panic!("not a ResolvedPath: {:?}", self),
1482+
}
1483+
}
1484+
14761485
crate fn is_self_type(&self) -> bool {
14771486
match *self {
14781487
Generic(name) => name == kw::SelfUpper,
@@ -1481,21 +1490,8 @@ impl Type {
14811490
}
14821491

14831492
crate fn generics(&self) -> Option<Vec<&Type>> {
1484-
match *self {
1485-
ResolvedPath { ref path, .. } => path.segments.last().and_then(|seg| {
1486-
if let GenericArgs::AngleBracketed { ref args, .. } = seg.args {
1487-
Some(
1488-
args.iter()
1489-
.filter_map(|arg| match arg {
1490-
GenericArg::Type(ty) => Some(ty),
1491-
_ => None,
1492-
})
1493-
.collect(),
1494-
)
1495-
} else {
1496-
None
1497-
}
1498-
}),
1493+
match self {
1494+
ResolvedPath { path, .. } => path.generics(),
14991495
_ => None,
15001496
}
15011497
}
@@ -1993,6 +1989,34 @@ impl Path {
19931989
_ => false,
19941990
}
19951991
}
1992+
1993+
crate fn generics(&self) -> Option<Vec<&Type>> {
1994+
self.segments.last().and_then(|seg| {
1995+
if let GenericArgs::AngleBracketed { ref args, .. } = seg.args {
1996+
Some(
1997+
args.iter()
1998+
.filter_map(|arg| match arg {
1999+
GenericArg::Type(ty) => Some(ty),
2000+
_ => None,
2001+
})
2002+
.collect(),
2003+
)
2004+
} else {
2005+
None
2006+
}
2007+
})
2008+
}
2009+
}
2010+
2011+
// FIXME: this is temporary
2012+
impl GetDefId for Path {
2013+
fn def_id(&self) -> Option<DefId> {
2014+
Some(self.res.def_id())
2015+
}
2016+
2017+
fn def_id_full(&self, _: &Cache) -> Option<DefId> {
2018+
self.def_id()
2019+
}
19962020
}
19972021

19982022
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
@@ -2136,7 +2160,7 @@ crate struct Impl {
21362160
crate span: Span,
21372161
crate unsafety: hir::Unsafety,
21382162
crate generics: Generics,
2139-
crate trait_: Option<Type>,
2163+
crate trait_: Option<Path>,
21402164
crate for_: Type,
21412165
crate items: Vec<Item>,
21422166
crate negative_polarity: bool,

src/librustdoc/formats/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::formats::cache::Cache;
1414
/// impl.
1515
crate enum AssocItemRender<'a> {
1616
All,
17-
DerefFor { trait_: &'a clean::Type, type_: &'a clean::Type, deref_mut_: bool },
17+
DerefFor { trait_: &'a clean::Path, type_: &'a clean::Type, deref_mut_: bool },
1818
}
1919

2020
/// For different handling of associated items from the Deref target of a type rather than the type

src/librustdoc/html/format.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,15 @@ impl clean::Type {
979979
}
980980
}
981981

982+
impl clean::Path {
983+
crate fn print<'b, 'a: 'b, 'tcx: 'a>(
984+
&'a self,
985+
cx: &'a Context<'tcx>,
986+
) -> impl fmt::Display + 'b + Captures<'tcx> {
987+
display_fn(move |f| resolved_path(f, self.res.def_id(), self, false, false, cx))
988+
}
989+
}
990+
982991
impl clean::Impl {
983992
crate fn print<'a, 'tcx: 'a>(
984993
&'a self,

src/librustdoc/html/render/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,10 +2056,10 @@ fn sidebar_struct(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, s: &clea
20562056

20572057
fn get_id_for_impl_on_foreign_type(
20582058
for_: &clean::Type,
2059-
trait_: &clean::Type,
2059+
trait_: &clean::Path,
20602060
cx: &Context<'_>,
20612061
) -> String {
2062-
small_url_encode(format!("impl-{:#}-for-{:#}", trait_.print(cx), for_.print(cx),))
2062+
small_url_encode(format!("impl-{:#}-for-{:#}", trait_.print(cx), for_.print(cx)))
20632063
}
20642064

20652065
fn extract_for_impl_name(item: &clean::Item, cx: &Context<'_>) -> Option<(String, String)> {

src/librustdoc/json/conversions.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,14 +507,19 @@ impl FromWithTcx<clean::Impl> for Impl {
507507
blanket_impl,
508508
span: _span,
509509
} = impl_;
510+
// FIXME: should `trait_` be a Path in JSON?
511+
let trait_ = trait_.map(|path| {
512+
let did = path.res.def_id();
513+
clean::ResolvedPath { path, did }.into_tcx(tcx)
514+
});
510515
Impl {
511516
is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
512517
generics: generics.into_tcx(tcx),
513518
provided_trait_methods: provided_trait_methods
514519
.into_iter()
515520
.map(|x| x.to_string())
516521
.collect(),
517-
trait_: trait_.map(|x| x.into_tcx(tcx)),
522+
trait_,
518523
for_: for_.into_tcx(tcx),
519524
items: ids(items),
520525
negative: negative_polarity,

src/librustdoc/passes/collect_trait_impls.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
7878
new_items.retain(|it| {
7979
if let ImplItem(Impl { ref for_, ref trait_, ref blanket_impl, .. }) = *it.kind {
8080
cleaner.keep_impl(for_)
81-
|| trait_.as_ref().map_or(false, |t| cleaner.keep_impl(t))
81+
|| trait_
82+
.as_ref()
83+
.map_or(false, |t| cleaner.keep_impl_with_def_id(t.res.def_id().into()))
8284
|| blanket_impl.is_some()
8385
} else {
8486
true

0 commit comments

Comments
 (0)