Skip to content
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

84304 - rustdoc: shrink Item::Attributes #84494

Merged
merged 2 commits into from
Apr 27, 2021
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
1 change: 1 addition & 0 deletions src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
synthetic: true,
blanket_impl: None,
}),
cfg: None,
})
}

Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/clean/blanket_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
synthetic: false,
blanket_impl: Some(trait_ref.self_ty().clean(self.cx)),
}),
cfg: None,
});
}
}
Expand Down
47 changes: 28 additions & 19 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Support for inlining external documentation into the current AST.

use std::iter::once;
use std::sync::Arc;

use rustc_ast as ast;
use rustc_data_structures::fx::FxHashSet;
Expand All @@ -15,7 +16,7 @@ use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;

use crate::clean::{self, Attributes, GetDefId, ToSource};
use crate::clean::{self, Attributes, AttributesExt, GetDefId, ToSource};
use crate::core::DocContext;
use crate::formats::item_type::ItemType;

Expand Down Expand Up @@ -120,12 +121,16 @@ crate fn try_inline(
_ => return None,
};

let target_attrs = load_attrs(cx, did);
let attrs = box merge_attrs(cx, Some(parent_module), target_attrs, attrs_clone);

let (attrs, cfg) = merge_attrs(cx, Some(parent_module), load_attrs(cx, did), attrs_clone);
cx.inlined.insert(did);
let what_rustc_thinks = clean::Item::from_def_id_and_parts(did, Some(name), kind, cx);
ret.push(clean::Item { attrs, ..what_rustc_thinks });
ret.push(clean::Item::from_def_id_and_attrs_and_parts(
did,
Some(name),
kind,
box attrs,
cx,
cfg,
));
Some(ret)
}

Expand Down Expand Up @@ -289,22 +294,24 @@ fn merge_attrs(
parent_module: Option<DefId>,
old_attrs: Attrs<'_>,
new_attrs: Option<Attrs<'_>>,
) -> clean::Attributes {
) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
// NOTE: If we have additional attributes (from a re-export),
// always insert them first. This ensure that re-export
// doc comments show up before the original doc comments
// when we render them.
if let Some(inner) = new_attrs {
if let Some(new_id) = parent_module {
let diag = cx.sess().diagnostic();
Attributes::from_ast(diag, old_attrs, Some((inner, new_id)))
} else {
let mut both = inner.to_vec();
both.extend_from_slice(old_attrs);
both.clean(cx)
}
let mut both = inner.to_vec();
both.extend_from_slice(old_attrs);
(
if let Some(new_id) = parent_module {
Attributes::from_ast(old_attrs, Some((inner, new_id)))
} else {
Attributes::from_ast(&both, None)
},
both.cfg(cx.sess().diagnostic()),
)
} else {
old_attrs.clean(cx)
(old_attrs.clean(cx), old_attrs.cfg(cx.sess().diagnostic()))
}
}

Expand Down Expand Up @@ -415,8 +422,8 @@ crate fn build_impl(

debug!("build_impl: impl {:?} for {:?}", trait_.def_id(), for_.def_id());

let attrs = box merge_attrs(cx, parent_module.into(), load_attrs(cx, did), attrs);
debug!("merged_attrs={:?}", attrs);
let (merged_attrs, cfg) = merge_attrs(cx, parent_module.into(), load_attrs(cx, did), attrs);
debug!("merged_attrs={:?}", merged_attrs);

ret.push(clean::Item::from_def_id_and_attrs_and_parts(
did,
Expand All @@ -433,8 +440,9 @@ crate fn build_impl(
synthetic: false,
blanket_impl: None,
}),
attrs,
box merged_attrs,
cx,
cfg,
));
}

Expand Down Expand Up @@ -480,6 +488,7 @@ fn build_module(
},
true,
)),
cfg: None,
});
} else if let Some(i) = try_inline(cx, did, item.res, item.ident.name, None, visited) {
items.extend(i)
Expand Down
13 changes: 6 additions & 7 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
}

impl Clean<ExternalCrate> for CrateNum {
fn clean(&self, cx: &mut DocContext<'_>) -> ExternalCrate {
let root = DefId { krate: *self, index: CRATE_DEF_INDEX };
ExternalCrate { crate_num: *self, attrs: cx.tcx.get_attrs(root).clean(cx) }
fn clean(&self, _cx: &mut DocContext<'_>) -> ExternalCrate {
ExternalCrate { crate_num: *self }
}
}

Expand Down Expand Up @@ -123,8 +122,8 @@ impl Clean<Item> for doctree::Module<'_> {
}

impl Clean<Attributes> for [ast::Attribute] {
fn clean(&self, cx: &mut DocContext<'_>) -> Attributes {
Attributes::from_ast(cx.sess().diagnostic(), self, None)
fn clean(&self, _cx: &mut DocContext<'_>) -> Attributes {
Attributes::from_ast(self, None)
}
}

Expand Down Expand Up @@ -850,7 +849,6 @@ where
inputs: (self.0.inputs, self.1).clean(cx),
output: self.0.output.clean(cx),
c_variadic: self.0.c_variadic,
attrs: Attributes::default(),
}
}
}
Expand All @@ -862,7 +860,6 @@ impl<'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {

FnDecl {
output: Return(sig.skip_binder().output().clean(cx)),
attrs: Attributes::default(),
c_variadic: sig.skip_binder().c_variadic,
inputs: Arguments {
values: sig
Expand Down Expand Up @@ -2001,13 +1998,15 @@ fn clean_extern_crate(
return items;
}
}

// FIXME: using `from_def_id_and_kind` breaks `rustdoc/masked` for some reason
vec![Item {
name: Some(name),
attrs: box attrs.clean(cx),
def_id: crate_def_id,
visibility: krate.vis.clean(cx),
kind: box ExternCrateItem { src: orig_name },
cfg: attrs.cfg(cx.sess().diagnostic()),
}]
}

Expand Down
Loading