Skip to content

Commit c634ec2

Browse files
committed
Auto merge of rust-lang#24989 - alexcrichton:rustdoc-associated-constant, r=brson
Associated constants were now showing up for traits and would panic if they were found on an inherent impl. This commit unblocks the nighly builders.
2 parents 8a60e56 + 543b910 commit c634ec2

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

src/librustdoc/html/render.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,6 +1787,9 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
17871787
let types = t.items.iter().filter(|m| {
17881788
match m.inner { clean::AssociatedTypeItem(..) => true, _ => false }
17891789
}).collect::<Vec<_>>();
1790+
let consts = t.items.iter().filter(|m| {
1791+
match m.inner { clean::AssociatedConstItem(..) => true, _ => false }
1792+
}).collect::<Vec<_>>();
17901793
let required = t.items.iter().filter(|m| {
17911794
match m.inner { clean::TyMethodItem(_) => true, _ => false }
17921795
}).collect::<Vec<_>>();
@@ -1803,7 +1806,15 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
18031806
try!(render_assoc_item(w, t, AssocItemLink::Anchor));
18041807
try!(write!(w, ";\n"));
18051808
}
1806-
if !types.is_empty() && !required.is_empty() {
1809+
if !types.is_empty() && !consts.is_empty() {
1810+
try!(w.write_str("\n"));
1811+
}
1812+
for t in &consts {
1813+
try!(write!(w, " "));
1814+
try!(render_assoc_item(w, t, AssocItemLink::Anchor));
1815+
try!(write!(w, ";\n"));
1816+
}
1817+
if !consts.is_empty() && !required.is_empty() {
18071818
try!(w.write_str("\n"));
18081819
}
18091820
for m in &required {
@@ -1905,11 +1916,11 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
19051916
}
19061917

19071918
fn assoc_const(w: &mut fmt::Formatter, it: &clean::Item,
1908-
ty: &clean::Type, default: &Option<String>)
1919+
ty: &clean::Type, default: Option<&String>)
19091920
-> fmt::Result {
19101921
try!(write!(w, "const {}", it.name.as_ref().unwrap()));
19111922
try!(write!(w, ": {}", ty));
1912-
if let Some(ref default) = *default {
1923+
if let Some(default) = default {
19131924
try!(write!(w, " = {}", default));
19141925
}
19151926
Ok(())
@@ -1971,7 +1982,7 @@ fn render_assoc_item(w: &mut fmt::Formatter, meth: &clean::Item,
19711982
link)
19721983
}
19731984
clean::AssociatedConstItem(ref ty, ref default) => {
1974-
assoc_const(w, meth, ty, default)
1985+
assoc_const(w, meth, ty, default.as_ref())
19751986
}
19761987
clean::AssociatedTypeItem(ref bounds, ref default) => {
19771988
assoc_type(w, meth, bounds, default)
@@ -2335,9 +2346,15 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl, link: AssocItemLink,
23352346
clean::AssociatedConstItem(ref ty, ref default) => {
23362347
let name = item.name.as_ref().unwrap();
23372348
try!(write!(w, "<h4 id='assoc_const.{}' class='{}'><code>",
2338-
*name,
2339-
shortty(item)));
2340-
try!(assoc_const(w, item, ty, default));
2349+
*name, shortty(item)));
2350+
try!(assoc_const(w, item, ty, default.as_ref()));
2351+
try!(write!(w, "</code></h4>\n"));
2352+
}
2353+
clean::ConstantItem(ref c) => {
2354+
let name = item.name.as_ref().unwrap();
2355+
try!(write!(w, "<h4 id='assoc_const.{}' class='{}'><code>",
2356+
*name, shortty(item)));
2357+
try!(assoc_const(w, item, &c.type_, Some(&c.expr)));
23412358
try!(write!(w, "</code></h4>\n"));
23422359
}
23432360
clean::AssociatedTypeItem(ref bounds, ref default) => {

src/test/rustdoc/assoc-consts.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(associated_consts)]
12+
13+
pub trait Foo {
14+
// @has assoc_consts/trait.Foo.html '//*[@class="rust trait"]' \
15+
// 'const FOO: usize;'
16+
const FOO: usize;
17+
}
18+
19+
pub struct Bar;
20+
21+
impl Bar {
22+
// @has assoc_consts/struct.Bar.html '//*[@id="assoc_const.BAR"]' \
23+
// 'const BAR: usize = 3'
24+
pub const BAR: usize = 3;
25+
}

0 commit comments

Comments
 (0)