Skip to content

rustdoc: Fix rendering associated constants #24989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,9 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
let types = t.items.iter().filter(|m| {
match m.inner { clean::AssociatedTypeItem(..) => true, _ => false }
}).collect::<Vec<_>>();
let consts = t.items.iter().filter(|m| {
match m.inner { clean::AssociatedConstItem(..) => true, _ => false }
}).collect::<Vec<_>>();
let required = t.items.iter().filter(|m| {
match m.inner { clean::TyMethodItem(_) => true, _ => false }
}).collect::<Vec<_>>();
Expand All @@ -1803,7 +1806,15 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
try!(render_assoc_item(w, t, AssocItemLink::Anchor));
try!(write!(w, ";\n"));
}
if !types.is_empty() && !required.is_empty() {
if !types.is_empty() && !consts.is_empty() {
try!(w.write_str("\n"));
}
for t in &consts {
try!(write!(w, " "));
try!(render_assoc_item(w, t, AssocItemLink::Anchor));
try!(write!(w, ";\n"));
}
if !consts.is_empty() && !required.is_empty() {
try!(w.write_str("\n"));
}
for m in &required {
Expand Down Expand Up @@ -1905,11 +1916,11 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
}

fn assoc_const(w: &mut fmt::Formatter, it: &clean::Item,
ty: &clean::Type, default: &Option<String>)
ty: &clean::Type, default: Option<&String>)
-> fmt::Result {
try!(write!(w, "const {}", it.name.as_ref().unwrap()));
try!(write!(w, ": {}", ty));
if let Some(ref default) = *default {
if let Some(default) = default {
try!(write!(w, " = {}", default));
}
Ok(())
Expand Down Expand Up @@ -1971,7 +1982,7 @@ fn render_assoc_item(w: &mut fmt::Formatter, meth: &clean::Item,
link)
}
clean::AssociatedConstItem(ref ty, ref default) => {
assoc_const(w, meth, ty, default)
assoc_const(w, meth, ty, default.as_ref())
}
clean::AssociatedTypeItem(ref bounds, ref default) => {
assoc_type(w, meth, bounds, default)
Expand Down Expand Up @@ -2335,9 +2346,15 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl, link: AssocItemLink,
clean::AssociatedConstItem(ref ty, ref default) => {
let name = item.name.as_ref().unwrap();
try!(write!(w, "<h4 id='assoc_const.{}' class='{}'><code>",
*name,
shortty(item)));
try!(assoc_const(w, item, ty, default));
*name, shortty(item)));
try!(assoc_const(w, item, ty, default.as_ref()));
try!(write!(w, "</code></h4>\n"));
}
clean::ConstantItem(ref c) => {
let name = item.name.as_ref().unwrap();
try!(write!(w, "<h4 id='assoc_const.{}' class='{}'><code>",
*name, shortty(item)));
try!(assoc_const(w, item, &c.type_, Some(&c.expr)));
try!(write!(w, "</code></h4>\n"));
}
clean::AssociatedTypeItem(ref bounds, ref default) => {
Expand Down
25 changes: 25 additions & 0 deletions src/test/rustdoc/assoc-consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(associated_consts)]

pub trait Foo {
// @has assoc_consts/trait.Foo.html '//*[@class="rust trait"]' \
// 'const FOO: usize;'
const FOO: usize;
}

pub struct Bar;

impl Bar {
// @has assoc_consts/struct.Bar.html '//*[@id="assoc_const.BAR"]' \
// 'const BAR: usize = 3'
pub const BAR: usize = 3;
}