Skip to content

Commit 588a99b

Browse files
Fix display for "const" deref methods in rustdoc
1 parent 9981e56 commit 588a99b

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ fn render_assoc_item(
861861
link: AssocItemLink<'_>,
862862
parent: ItemType,
863863
cx: &Context<'_>,
864+
render_mode: RenderMode,
864865
) {
865866
fn method(
866867
w: &mut Buffer,
@@ -871,6 +872,7 @@ fn render_assoc_item(
871872
link: AssocItemLink<'_>,
872873
parent: ItemType,
873874
cx: &Context<'_>,
875+
render_mode: RenderMode,
874876
) {
875877
let name = meth.name.as_ref().unwrap();
876878
let href = match link {
@@ -893,8 +895,14 @@ fn render_assoc_item(
893895
}
894896
};
895897
let vis = meth.visibility.print_with_space(meth.def_id, cx).to_string();
896-
let constness =
897-
print_constness_with_space(&header.constness, meth.const_stability(cx.tcx()));
898+
// FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove
899+
// this condition.
900+
let constness = match render_mode {
901+
RenderMode::Normal => {
902+
print_constness_with_space(&header.constness, meth.const_stability(cx.tcx()))
903+
}
904+
RenderMode::ForDeref { .. } => "",
905+
};
898906
let asyncness = header.asyncness.print_with_space();
899907
let unsafety = header.unsafety.print_with_space();
900908
let defaultness = print_default_space(meth.is_default());
@@ -945,10 +953,10 @@ fn render_assoc_item(
945953
match *item.kind {
946954
clean::StrippedItem(..) => {}
947955
clean::TyMethodItem(ref m) => {
948-
method(w, item, m.header, &m.generics, &m.decl, link, parent, cx)
956+
method(w, item, m.header, &m.generics, &m.decl, link, parent, cx, render_mode)
949957
}
950958
clean::MethodItem(ref m, _) => {
951-
method(w, item, m.header, &m.generics, &m.decl, link, parent, cx)
959+
method(w, item, m.header, &m.generics, &m.decl, link, parent, cx, render_mode)
952960
}
953961
clean::AssocConstItem(ref ty, ref default) => assoc_const(
954962
w,
@@ -1415,7 +1423,7 @@ fn render_impl(
14151423
"<div id=\"{}\" class=\"{}{} has-srclink\">",
14161424
id, item_type, in_trait_class,
14171425
);
1418-
render_rightside(w, cx, item, containing_item);
1426+
render_rightside(w, cx, item, containing_item, render_mode);
14191427
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
14201428
w.write_str("<h4 class=\"code-header\">");
14211429
render_assoc_item(
@@ -1424,6 +1432,7 @@ fn render_impl(
14241432
link.anchor(source_id.as_ref().unwrap_or(&id)),
14251433
ItemType::Impl,
14261434
cx,
1435+
render_mode,
14271436
);
14281437
w.write_str("</h4>");
14291438
w.write_str("</div>");
@@ -1459,7 +1468,7 @@ fn render_impl(
14591468
"<div id=\"{}\" class=\"{}{} has-srclink\">",
14601469
id, item_type, in_trait_class
14611470
);
1462-
render_rightside(w, cx, item, containing_item);
1471+
render_rightside(w, cx, item, containing_item, render_mode);
14631472
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
14641473
w.write_str("<h4 class=\"code-header\">");
14651474
assoc_const(
@@ -1638,16 +1647,28 @@ fn render_rightside(
16381647
cx: &Context<'_>,
16391648
item: &clean::Item,
16401649
containing_item: &clean::Item,
1650+
render_mode: RenderMode,
16411651
) {
16421652
let tcx = cx.tcx();
16431653

1654+
let const_stable_since;
1655+
// FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove
1656+
// this condition.
1657+
let (const_stability, const_stable_since) = match render_mode {
1658+
RenderMode::Normal => {
1659+
const_stable_since = containing_item.const_stable_since(tcx);
1660+
(item.const_stability(tcx), const_stable_since.as_deref())
1661+
}
1662+
RenderMode::ForDeref { .. } => (None, None),
1663+
};
1664+
16441665
write!(w, "<div class=\"rightside\">");
16451666
render_stability_since_raw(
16461667
w,
16471668
item.stable_since(tcx).as_deref(),
1648-
item.const_stability(tcx),
1669+
const_stability,
16491670
containing_item.stable_since(tcx).as_deref(),
1650-
containing_item.const_stable_since(tcx).as_deref(),
1671+
const_stable_since,
16511672
);
16521673

16531674
write_srclink(cx, item, w);
@@ -1683,7 +1704,7 @@ pub(crate) fn render_impl_summary(
16831704
format!(" data-aliases=\"{}\"", aliases.join(","))
16841705
};
16851706
write!(w, "<div id=\"{}\" class=\"impl has-srclink\"{}>", id, aliases);
1686-
render_rightside(w, cx, &i.impl_item, containing_item);
1707+
render_rightside(w, cx, &i.impl_item, containing_item, RenderMode::Normal);
16871708
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
16881709
write!(w, "<h3 class=\"code-header in-band\">");
16891710

src/librustdoc/html/render/print_item.rs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
556556
);
557557
}
558558
for t in &types {
559-
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx);
559+
render_assoc_item(
560+
w,
561+
t,
562+
AssocItemLink::Anchor(None),
563+
ItemType::Trait,
564+
cx,
565+
RenderMode::Normal,
566+
);
560567
w.write_str(";\n");
561568
}
562569
// If there are too many associated constants, hide everything after them
@@ -580,7 +587,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
580587
w.write_str("\n");
581588
}
582589
for t in &consts {
583-
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx);
590+
render_assoc_item(
591+
w,
592+
t,
593+
AssocItemLink::Anchor(None),
594+
ItemType::Trait,
595+
cx,
596+
RenderMode::Normal,
597+
);
584598
w.write_str(";\n");
585599
}
586600
if !toggle && should_hide_fields(count_methods) {
@@ -591,7 +605,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
591605
w.write_str("\n");
592606
}
593607
for (pos, m) in required.iter().enumerate() {
594-
render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, cx);
608+
render_assoc_item(
609+
w,
610+
m,
611+
AssocItemLink::Anchor(None),
612+
ItemType::Trait,
613+
cx,
614+
RenderMode::Normal,
615+
);
595616
w.write_str(";\n");
596617

597618
if pos < required.len() - 1 {
@@ -602,7 +623,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
602623
w.write_str("\n");
603624
}
604625
for (pos, m) in provided.iter().enumerate() {
605-
render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, cx);
626+
render_assoc_item(
627+
w,
628+
m,
629+
AssocItemLink::Anchor(None),
630+
ItemType::Trait,
631+
cx,
632+
RenderMode::Normal,
633+
);
606634
match *m.kind {
607635
clean::MethodItem(ref inner, _)
608636
if !inner.generics.where_predicates.is_empty() =>
@@ -655,7 +683,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
655683
write_srclink(cx, m, w);
656684
write!(w, "</div>");
657685
write!(w, "<h4 class=\"code-header\">");
658-
render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl, cx);
686+
render_assoc_item(
687+
w,
688+
m,
689+
AssocItemLink::Anchor(Some(&id)),
690+
ItemType::Impl,
691+
cx,
692+
RenderMode::Normal,
693+
);
659694
w.write_str("</h4>");
660695
w.write_str("</div>");
661696
if toggled {

0 commit comments

Comments
 (0)