Fix display for "const" deref methods in rustdoc

This commit is contained in:
Guillaume Gomez 2021-11-27 14:37:30 +01:00
parent 9981e56d3b
commit 588a99bbdd
2 changed files with 70 additions and 14 deletions

View File

@ -861,6 +861,7 @@ fn render_assoc_item(
link: AssocItemLink<'_>, link: AssocItemLink<'_>,
parent: ItemType, parent: ItemType,
cx: &Context<'_>, cx: &Context<'_>,
render_mode: RenderMode,
) { ) {
fn method( fn method(
w: &mut Buffer, w: &mut Buffer,
@ -871,6 +872,7 @@ fn render_assoc_item(
link: AssocItemLink<'_>, link: AssocItemLink<'_>,
parent: ItemType, parent: ItemType,
cx: &Context<'_>, cx: &Context<'_>,
render_mode: RenderMode,
) { ) {
let name = meth.name.as_ref().unwrap(); let name = meth.name.as_ref().unwrap();
let href = match link { let href = match link {
@ -893,8 +895,14 @@ fn render_assoc_item(
} }
}; };
let vis = meth.visibility.print_with_space(meth.def_id, cx).to_string(); let vis = meth.visibility.print_with_space(meth.def_id, cx).to_string();
let constness = // FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove
print_constness_with_space(&header.constness, meth.const_stability(cx.tcx())); // this condition.
let constness = match render_mode {
RenderMode::Normal => {
print_constness_with_space(&header.constness, meth.const_stability(cx.tcx()))
}
RenderMode::ForDeref { .. } => "",
};
let asyncness = header.asyncness.print_with_space(); let asyncness = header.asyncness.print_with_space();
let unsafety = header.unsafety.print_with_space(); let unsafety = header.unsafety.print_with_space();
let defaultness = print_default_space(meth.is_default()); let defaultness = print_default_space(meth.is_default());
@ -945,10 +953,10 @@ fn render_assoc_item(
match *item.kind { match *item.kind {
clean::StrippedItem(..) => {} clean::StrippedItem(..) => {}
clean::TyMethodItem(ref m) => { clean::TyMethodItem(ref m) => {
method(w, item, m.header, &m.generics, &m.decl, link, parent, cx) method(w, item, m.header, &m.generics, &m.decl, link, parent, cx, render_mode)
} }
clean::MethodItem(ref m, _) => { clean::MethodItem(ref m, _) => {
method(w, item, m.header, &m.generics, &m.decl, link, parent, cx) method(w, item, m.header, &m.generics, &m.decl, link, parent, cx, render_mode)
} }
clean::AssocConstItem(ref ty, ref default) => assoc_const( clean::AssocConstItem(ref ty, ref default) => assoc_const(
w, w,
@ -1415,7 +1423,7 @@ fn render_impl(
"<div id=\"{}\" class=\"{}{} has-srclink\">", "<div id=\"{}\" class=\"{}{} has-srclink\">",
id, item_type, in_trait_class, id, item_type, in_trait_class,
); );
render_rightside(w, cx, item, containing_item); render_rightside(w, cx, item, containing_item, render_mode);
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id); write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
w.write_str("<h4 class=\"code-header\">"); w.write_str("<h4 class=\"code-header\">");
render_assoc_item( render_assoc_item(
@ -1424,6 +1432,7 @@ fn render_impl(
link.anchor(source_id.as_ref().unwrap_or(&id)), link.anchor(source_id.as_ref().unwrap_or(&id)),
ItemType::Impl, ItemType::Impl,
cx, cx,
render_mode,
); );
w.write_str("</h4>"); w.write_str("</h4>");
w.write_str("</div>"); w.write_str("</div>");
@ -1459,7 +1468,7 @@ fn render_impl(
"<div id=\"{}\" class=\"{}{} has-srclink\">", "<div id=\"{}\" class=\"{}{} has-srclink\">",
id, item_type, in_trait_class id, item_type, in_trait_class
); );
render_rightside(w, cx, item, containing_item); render_rightside(w, cx, item, containing_item, render_mode);
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id); write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
w.write_str("<h4 class=\"code-header\">"); w.write_str("<h4 class=\"code-header\">");
assoc_const( assoc_const(
@ -1638,16 +1647,28 @@ fn render_rightside(
cx: &Context<'_>, cx: &Context<'_>,
item: &clean::Item, item: &clean::Item,
containing_item: &clean::Item, containing_item: &clean::Item,
render_mode: RenderMode,
) { ) {
let tcx = cx.tcx(); let tcx = cx.tcx();
let const_stable_since;
// FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove
// this condition.
let (const_stability, const_stable_since) = match render_mode {
RenderMode::Normal => {
const_stable_since = containing_item.const_stable_since(tcx);
(item.const_stability(tcx), const_stable_since.as_deref())
}
RenderMode::ForDeref { .. } => (None, None),
};
write!(w, "<div class=\"rightside\">"); write!(w, "<div class=\"rightside\">");
render_stability_since_raw( render_stability_since_raw(
w, w,
item.stable_since(tcx).as_deref(), item.stable_since(tcx).as_deref(),
item.const_stability(tcx), const_stability,
containing_item.stable_since(tcx).as_deref(), containing_item.stable_since(tcx).as_deref(),
containing_item.const_stable_since(tcx).as_deref(), const_stable_since,
); );
write_srclink(cx, item, w); write_srclink(cx, item, w);
@ -1683,7 +1704,7 @@ pub(crate) fn render_impl_summary(
format!(" data-aliases=\"{}\"", aliases.join(",")) format!(" data-aliases=\"{}\"", aliases.join(","))
}; };
write!(w, "<div id=\"{}\" class=\"impl has-srclink\"{}>", id, aliases); write!(w, "<div id=\"{}\" class=\"impl has-srclink\"{}>", id, aliases);
render_rightside(w, cx, &i.impl_item, containing_item); render_rightside(w, cx, &i.impl_item, containing_item, RenderMode::Normal);
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id); write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
write!(w, "<h3 class=\"code-header in-band\">"); write!(w, "<h3 class=\"code-header in-band\">");

View File

@ -556,7 +556,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
); );
} }
for t in &types { for t in &types {
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx); render_assoc_item(
w,
t,
AssocItemLink::Anchor(None),
ItemType::Trait,
cx,
RenderMode::Normal,
);
w.write_str(";\n"); w.write_str(";\n");
} }
// If there are too many associated constants, hide everything after them // 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
w.write_str("\n"); w.write_str("\n");
} }
for t in &consts { for t in &consts {
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx); render_assoc_item(
w,
t,
AssocItemLink::Anchor(None),
ItemType::Trait,
cx,
RenderMode::Normal,
);
w.write_str(";\n"); w.write_str(";\n");
} }
if !toggle && should_hide_fields(count_methods) { 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
w.write_str("\n"); w.write_str("\n");
} }
for (pos, m) in required.iter().enumerate() { for (pos, m) in required.iter().enumerate() {
render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, cx); render_assoc_item(
w,
m,
AssocItemLink::Anchor(None),
ItemType::Trait,
cx,
RenderMode::Normal,
);
w.write_str(";\n"); w.write_str(";\n");
if pos < required.len() - 1 { if pos < required.len() - 1 {
@ -602,7 +623,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
w.write_str("\n"); w.write_str("\n");
} }
for (pos, m) in provided.iter().enumerate() { for (pos, m) in provided.iter().enumerate() {
render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, cx); render_assoc_item(
w,
m,
AssocItemLink::Anchor(None),
ItemType::Trait,
cx,
RenderMode::Normal,
);
match *m.kind { match *m.kind {
clean::MethodItem(ref inner, _) clean::MethodItem(ref inner, _)
if !inner.generics.where_predicates.is_empty() => 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
write_srclink(cx, m, w); write_srclink(cx, m, w);
write!(w, "</div>"); write!(w, "</div>");
write!(w, "<h4 class=\"code-header\">"); write!(w, "<h4 class=\"code-header\">");
render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl, cx); render_assoc_item(
w,
m,
AssocItemLink::Anchor(Some(&id)),
ItemType::Impl,
cx,
RenderMode::Normal,
);
w.write_str("</h4>"); w.write_str("</h4>");
w.write_str("</div>"); w.write_str("</div>");
if toggled { if toggled {