Skip to content

Commit 147d173

Browse files
committed
rustdoc: Show attributes on all item types
Currently attributes are only shown for structs, unions and enums but they should be shown for all items. For example it is useful to know if a function is `#[no_mangle]`.
1 parent fc02736 commit 147d173

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/librustdoc/html/render.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,9 @@ impl<'a> fmt::Display for Initializer<'a> {
19361936

19371937
fn item_constant(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
19381938
c: &clean::Constant) -> fmt::Result {
1939-
write!(w, "<pre class='rust const'>{vis}const \
1939+
write!(w, "<pre class='rust const'>")?;
1940+
render_attributes(w, it)?;
1941+
write!(w, "{vis}const \
19401942
{name}: {typ}{init}</pre>",
19411943
vis = VisSpace(&it.visibility),
19421944
name = it.name.as_ref().unwrap(),
@@ -1947,7 +1949,9 @@ fn item_constant(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
19471949

19481950
fn item_static(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
19491951
s: &clean::Static) -> fmt::Result {
1950-
write!(w, "<pre class='rust static'>{vis}static {mutability}\
1952+
write!(w, "<pre class='rust static'>")?;
1953+
render_attributes(w, it)?;
1954+
write!(w, "{vis}static {mutability}\
19511955
{name}: {typ}{init}</pre>",
19521956
vis = VisSpace(&it.visibility),
19531957
mutability = MutableSpace(s.mutability),
@@ -1971,7 +1975,9 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
19711975
AbiSpace(f.abi),
19721976
it.name.as_ref().unwrap(),
19731977
f.generics).len();
1974-
write!(w, "<pre class='rust fn'>{vis}{constness}{unsafety}{abi}fn \
1978+
write!(w, "<pre class='rust fn'>")?;
1979+
render_attributes(w, it)?;
1980+
write!(w, "{vis}{constness}{unsafety}{abi}fn \
19751981
{name}{generics}{decl}{where_clause}</pre>",
19761982
vis = VisSpace(&it.visibility),
19771983
constness = ConstnessSpace(vis_constness),
@@ -2006,7 +2012,9 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
20062012
}
20072013

20082014
// Output the trait definition
2009-
write!(w, "<pre class='rust trait'>{}{}trait {}{}{}{} ",
2015+
write!(w, "<pre class='rust trait'>")?;
2016+
render_attributes(w, it)?;
2017+
write!(w, "{}{}trait {}{}{}{} ",
20102018
VisSpace(&it.visibility),
20112019
UnsafetySpace(t.unsafety),
20122020
it.name.as_ref().unwrap(),
@@ -2987,7 +2995,9 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
29872995
fn item_typedef(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
29882996
t: &clean::Typedef) -> fmt::Result {
29892997
let indent = format!("type {}{:#} ", it.name.as_ref().unwrap(), t.generics).len();
2990-
write!(w, "<pre class='rust typedef'>type {}{}{where_clause} = {type_};</pre>",
2998+
write!(w, "<pre class='rust typedef'>")?;
2999+
render_attributes(w, it)?;
3000+
write!(w, "type {}{}{where_clause} = {type_};</pre>",
29913001
it.name.as_ref().unwrap(),
29923002
t.generics,
29933003
where_clause = WhereClause(&t.generics, indent),

src/test/rustdoc/attributes.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 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+
#![crate_name = "foo"]
12+
13+
// @has foo/fn.f.html '//*[@class="docblock attributes"]' '#[no_mangle]'
14+
#[no_mangle]
15+
pub extern "C" fn f() {}
16+
17+
// @has foo/fn.g.html '//*[@class="docblock attributes"]' '#[export_name = "bar"]'
18+
#[export_name = "bar"]
19+
pub extern "C" fn g() {}
20+
21+
// @has foo/enum.Foo.html '//*[@class="docblock attributes"]' '#[repr(i64)]'
22+
// @has foo/enum.Foo.html '//*[@class="docblock attributes"]' '#[must_use]'
23+
#[repr(i64)]
24+
#[must_use]
25+
pub enum Foo {
26+
Bar,
27+
}

0 commit comments

Comments
 (0)