Skip to content

Commit

Permalink
Auto merge of #77820 - jyn514:from-inner, r=petrochenkov
Browse files Browse the repository at this point in the history
Add `Item::from_def_id_and_kind` to reduce duplication in rustdoc

This makes it harder to make typos, and also makes it much more clear what's intentionally different rather than a typo (look for `what_rustc_thinks`).

Found this while working on #76998, I really didn't want to add `const_visibility` in 20 different places.

r? `@GuillaumeGomez`
  • Loading branch information
bors committed Nov 18, 2020
2 parents 87776d7 + 0e1a302 commit c4f836a
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 446 deletions.
43 changes: 26 additions & 17 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,25 +806,34 @@ impl<'hir> Map<'hir> {
/// Given a node ID, gets a list of attributes associated with the AST
/// corresponding to the node-ID.
pub fn attrs(&self, id: HirId) -> &'hir [ast::Attribute] {
let attrs = match self.find_entry(id).map(|entry| entry.node) {
Some(Node::Param(a)) => Some(&a.attrs[..]),
Some(Node::Local(l)) => Some(&l.attrs[..]),
Some(Node::Item(i)) => Some(&i.attrs[..]),
Some(Node::ForeignItem(fi)) => Some(&fi.attrs[..]),
Some(Node::TraitItem(ref ti)) => Some(&ti.attrs[..]),
Some(Node::ImplItem(ref ii)) => Some(&ii.attrs[..]),
Some(Node::Variant(ref v)) => Some(&v.attrs[..]),
Some(Node::Field(ref f)) => Some(&f.attrs[..]),
Some(Node::Expr(ref e)) => Some(&*e.attrs),
Some(Node::Stmt(ref s)) => Some(s.kind.attrs(|id| self.item(id.id))),
Some(Node::Arm(ref a)) => Some(&*a.attrs),
Some(Node::GenericParam(param)) => Some(&param.attrs[..]),
let attrs = self.find_entry(id).map(|entry| match entry.node {
Node::Param(a) => &a.attrs[..],
Node::Local(l) => &l.attrs[..],
Node::Item(i) => &i.attrs[..],
Node::ForeignItem(fi) => &fi.attrs[..],
Node::TraitItem(ref ti) => &ti.attrs[..],
Node::ImplItem(ref ii) => &ii.attrs[..],
Node::Variant(ref v) => &v.attrs[..],
Node::Field(ref f) => &f.attrs[..],
Node::Expr(ref e) => &*e.attrs,
Node::Stmt(ref s) => s.kind.attrs(|id| self.item(id.id)),
Node::Arm(ref a) => &*a.attrs,
Node::GenericParam(param) => &param.attrs[..],
// Unit/tuple structs/variants take the attributes straight from
// the struct/variant definition.
Some(Node::Ctor(..)) => return self.attrs(self.get_parent_item(id)),
Some(Node::Crate(item)) => Some(&item.attrs[..]),
_ => None,
};
Node::Ctor(..) => self.attrs(self.get_parent_item(id)),
Node::Crate(item) => &item.attrs[..],
Node::MacroDef(def) => def.attrs,
Node::AnonConst(..)
| Node::PathSegment(..)
| Node::Ty(..)
| Node::Pat(..)
| Node::Binding(..)
| Node::TraitRef(..)
| Node::Block(..)
| Node::Lifetime(..)
| Node::Visibility(..) => &[],
});
attrs.unwrap_or(&[])
}

Expand Down
28 changes: 8 additions & 20 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,8 @@ crate fn try_inline(
let attrs = merge_attrs(cx, Some(parent_module), target_attrs, attrs_clone);

cx.renderinfo.borrow_mut().inlined.insert(did);
ret.push(clean::Item {
source: cx.tcx.def_span(did).clean(cx),
name: Some(name.clean(cx)),
attrs,
kind,
visibility: clean::Public,
stability: cx.tcx.lookup_stability(did).cloned(),
deprecation: cx.tcx.lookup_deprecation(did).clean(cx),
def_id: 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 });
Some(ret)
}

Expand Down Expand Up @@ -443,8 +435,10 @@ crate fn build_impl(

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

ret.push(clean::Item {
kind: clean::ImplItem(clean::Impl {
ret.push(clean::Item::from_def_id_and_parts(
did,
None,
clean::ImplItem(clean::Impl {
unsafety: hir::Unsafety::Normal,
generics,
provided_trait_methods: provided,
Expand All @@ -455,14 +449,8 @@ crate fn build_impl(
synthetic: false,
blanket_impl: None,
}),
source: tcx.def_span(did).clean(cx),
name: None,
attrs,
visibility: clean::Inherited,
stability: tcx.lookup_stability(did).cloned(),
deprecation: tcx.lookup_deprecation(did).clean(cx),
def_id: did,
});
cx,
));
}

fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>) -> clean::Module {
Expand Down
Loading

0 comments on commit c4f836a

Please sign in to comment.