Skip to content

Commit 8e0cac1

Browse files
committed
rustdoc: refactor notable_traits_decl to just act on the type directly
1 parent 391ba78 commit 8e0cac1

File tree

2 files changed

+72
-68
lines changed

2 files changed

+72
-68
lines changed

src/librustdoc/html/render/mod.rs

+66-67
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,11 @@ fn assoc_method(
861861
name = name,
862862
generics = g.print(cx),
863863
decl = d.full_print(header_len, indent, cx),
864-
notable_traits = notable_traits_decl(d, cx),
864+
notable_traits = d
865+
.output
866+
.as_return()
867+
.and_then(|output| notable_traits_decl(output, cx))
868+
.unwrap_or_default(),
865869
where_clause = print_where_clause(g, cx, indent, end_newline),
866870
)
867871
}
@@ -1273,88 +1277,83 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) ->
12731277
}
12741278
}
12751279

1276-
fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String {
1280+
fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> Option<String> {
12771281
let mut out = Buffer::html();
12781282

1279-
if let Some((did, ty)) = decl.output.as_return().and_then(|t| Some((t.def_id(cx.cache())?, t)))
1283+
let did = ty.def_id(cx.cache())?;
1284+
1285+
// Box has pass-through impls for Read, Write, Iterator, and Future when the
1286+
// boxed type implements one of those. We don't want to treat every Box return
1287+
// as being notably an Iterator (etc), though, so we exempt it. Pin has the same
1288+
// issue, with a pass-through impl for Future.
1289+
if Some(did) == cx.tcx().lang_items().owned_box()
1290+
|| Some(did) == cx.tcx().lang_items().pin_type()
12801291
{
1281-
// Box has pass-through impls for Read, Write, Iterator, and Future when the
1282-
// boxed type implements one of those. We don't want to treat every Box return
1283-
// as being notably an Iterator (etc), though, so we exempt it. Pin has the same
1284-
// issue, with a pass-through impl for Future.
1285-
if Some(did) == cx.tcx().lang_items().owned_box()
1286-
|| Some(did) == cx.tcx().lang_items().pin_type()
1287-
{
1288-
return "".to_string();
1289-
}
1290-
if let Some(impls) = cx.cache().impls.get(&did) {
1291-
for i in impls {
1292-
let impl_ = i.inner_impl();
1293-
if !impl_.for_.without_borrowed_ref().is_same(ty.without_borrowed_ref(), cx.cache())
1294-
{
1295-
// Two different types might have the same did,
1296-
// without actually being the same.
1297-
continue;
1298-
}
1299-
if let Some(trait_) = &impl_.trait_ {
1300-
let trait_did = trait_.def_id();
1301-
1302-
if cx
1303-
.cache()
1304-
.traits
1305-
.get(&trait_did)
1306-
.map_or(false, |t| t.is_notable_trait(cx.tcx()))
1307-
{
1308-
if out.is_empty() {
1309-
write!(
1310-
&mut out,
1311-
"<span class=\"notable\">Notable traits for {}</span>\
1312-
<code class=\"content\">",
1313-
impl_.for_.print(cx)
1314-
);
1315-
}
1292+
return None;
1293+
}
1294+
if let Some(impls) = cx.cache().impls.get(&did) {
1295+
for i in impls {
1296+
let impl_ = i.inner_impl();
1297+
if !impl_.for_.without_borrowed_ref().is_same(ty.without_borrowed_ref(), cx.cache()) {
1298+
// Two different types might have the same did,
1299+
// without actually being the same.
1300+
continue;
1301+
}
1302+
if let Some(trait_) = &impl_.trait_ {
1303+
let trait_did = trait_.def_id();
13161304

1317-
//use the "where" class here to make it small
1305+
if cx.cache().traits.get(&trait_did).map_or(false, |t| t.is_notable_trait(cx.tcx()))
1306+
{
1307+
if out.is_empty() {
13181308
write!(
13191309
&mut out,
1320-
"<span class=\"where fmt-newline\">{}</span>",
1321-
impl_.print(false, cx)
1310+
"<span class=\"notable\">Notable traits for {}</span>\
1311+
<code class=\"content\">",
1312+
impl_.for_.print(cx)
13221313
);
1323-
for it in &impl_.items {
1324-
if let clean::AssocTypeItem(ref tydef, ref _bounds) = *it.kind {
1325-
out.push_str("<span class=\"where fmt-newline\"> ");
1326-
let empty_set = FxHashSet::default();
1327-
let src_link =
1328-
AssocItemLink::GotoSource(trait_did.into(), &empty_set);
1329-
assoc_type(
1330-
&mut out,
1331-
it,
1332-
&tydef.generics,
1333-
&[], // intentionally leaving out bounds
1334-
Some(&tydef.type_),
1335-
src_link,
1336-
0,
1337-
cx,
1338-
);
1339-
out.push_str(";</span>");
1340-
}
1314+
}
1315+
1316+
//use the "where" class here to make it small
1317+
write!(
1318+
&mut out,
1319+
"<span class=\"where fmt-newline\">{}</span>",
1320+
impl_.print(false, cx)
1321+
);
1322+
for it in &impl_.items {
1323+
if let clean::AssocTypeItem(ref tydef, ref _bounds) = *it.kind {
1324+
out.push_str("<span class=\"where fmt-newline\"> ");
1325+
let empty_set = FxHashSet::default();
1326+
let src_link = AssocItemLink::GotoSource(trait_did.into(), &empty_set);
1327+
assoc_type(
1328+
&mut out,
1329+
it,
1330+
&tydef.generics,
1331+
&[], // intentionally leaving out bounds
1332+
Some(&tydef.type_),
1333+
src_link,
1334+
0,
1335+
cx,
1336+
);
1337+
out.push_str(";</span>");
13411338
}
13421339
}
13431340
}
13441341
}
13451342
}
13461343
}
13471344

1348-
if !out.is_empty() {
1349-
out.insert_str(
1350-
0,
1351-
"<span class=\"notable-traits\"><span class=\"notable-traits-tooltip\">ⓘ\
1352-
<span class=\"notable-traits-tooltiptext\"><span class=\"docblock\">",
1353-
);
1354-
out.push_str("</code></span></span></span></span>");
1345+
if out.is_empty() {
1346+
return None;
13551347
}
13561348

1357-
out.into_inner()
1349+
out.insert_str(
1350+
0,
1351+
"<span class=\"notable-traits\"><span class=\"notable-traits-tooltip\">ⓘ\
1352+
<span class=\"notable-traits-tooltiptext\"><span class=\"docblock\">",
1353+
);
1354+
out.push_str("</code></span></span></span></span>");
1355+
1356+
Some(out.into_inner())
13581357
}
13591358

13601359
#[derive(Clone, Copy, Debug)]

src/librustdoc/html/render/print_item.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,12 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
533533
generics = f.generics.print(cx),
534534
where_clause = print_where_clause(&f.generics, cx, 0, Ending::Newline),
535535
decl = f.decl.full_print(header_len, 0, cx),
536-
notable_traits = notable_traits_decl(&f.decl, cx),
536+
notable_traits = f
537+
.decl
538+
.output
539+
.as_return()
540+
.and_then(|output| notable_traits_decl(output, cx))
541+
.unwrap_or_default(),
537542
);
538543
});
539544
});

0 commit comments

Comments
 (0)