Skip to content

Commit e5de0b1

Browse files
authored
Rollup merge of #69686 - varkor:rustdoc-attributes, r=GuillaumeGomez
Use `pprust` to print attributes in rustdoc Fixes #69559. I'm not sure what the original motivation was for the `render_attribute`, so I may be missing something, but replacing it with `pprust::attribute_to_string` seems to give the intended output (modulo some spacing idiosyncrasies). r? @GuillaumeGomez
2 parents 45ebd58 + c599ec4 commit e5de0b1

File tree

2 files changed

+7
-23
lines changed

2 files changed

+7
-23
lines changed

src/librustdoc/html/render.rs

+3-23
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ use std::sync::Arc;
4444

4545
use rustc::middle::privacy::AccessLevels;
4646
use rustc::middle::stability;
47-
use rustc_ast::ast;
4847
use rustc_ast_pretty::pprust;
4948
use rustc_data_structures::flock;
5049
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -3126,25 +3125,6 @@ fn item_enum(w: &mut Buffer, cx: &Context, it: &clean::Item, e: &clean::Enum) {
31263125
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
31273126
}
31283127

3129-
fn render_attribute(attr: &ast::MetaItem) -> Option<String> {
3130-
let path = pprust::path_to_string(&attr.path);
3131-
3132-
if attr.is_word() {
3133-
Some(path)
3134-
} else if let Some(v) = attr.value_str() {
3135-
Some(format!("{} = {:?}", path, v))
3136-
} else if let Some(values) = attr.meta_item_list() {
3137-
let display: Vec<_> = values
3138-
.iter()
3139-
.filter_map(|attr| attr.meta_item().and_then(|mi| render_attribute(mi)))
3140-
.collect();
3141-
3142-
if !display.is_empty() { Some(format!("{}({})", path, display.join(", "))) } else { None }
3143-
} else {
3144-
None
3145-
}
3146-
}
3147-
31483128
const ATTRIBUTE_WHITELIST: &[Symbol] = &[
31493129
sym::export_name,
31503130
sym::lang,
@@ -3170,9 +3150,9 @@ fn render_attributes(w: &mut Buffer, it: &clean::Item, top: bool) {
31703150
if !ATTRIBUTE_WHITELIST.contains(&attr.name_or_empty()) {
31713151
continue;
31723152
}
3173-
if let Some(s) = render_attribute(&attr.meta().unwrap()) {
3174-
attrs.push_str(&format!("#[{}]\n", s));
3175-
}
3153+
3154+
// FIXME: this currently renders too many spaces as in: `#[repr(C, align (8))]`.
3155+
attrs.push_str(&pprust::attribute_to_string(&attr));
31763156
}
31773157
if !attrs.is_empty() {
31783158
write!(

src/test/rustdoc/attributes.rs

+4
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ pub extern "C" fn g() {}
1515
pub enum Foo {
1616
Bar,
1717
}
18+
19+
// @has foo/struct.Repr.html '//*[@class="docblock attributes top-attr"]' '#[repr(C, align (8))]'
20+
#[repr(C, align(8))]
21+
pub struct Repr;

0 commit comments

Comments
 (0)