Skip to content

Commit c4f836a

Browse files
committed
Auto merge of rust-lang#77820 - jyn514:from-inner, r=petrochenkov
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 rust-lang#76998, I really didn't want to add `const_visibility` in 20 different places. r? `@GuillaumeGomez`
2 parents 87776d7 + 0e1a302 commit c4f836a

File tree

10 files changed

+274
-446
lines changed

10 files changed

+274
-446
lines changed

compiler/rustc_middle/src/hir/map/mod.rs

+26-17
Original file line numberDiff line numberDiff line change
@@ -806,25 +806,34 @@ impl<'hir> Map<'hir> {
806806
/// Given a node ID, gets a list of attributes associated with the AST
807807
/// corresponding to the node-ID.
808808
pub fn attrs(&self, id: HirId) -> &'hir [ast::Attribute] {
809-
let attrs = match self.find_entry(id).map(|entry| entry.node) {
810-
Some(Node::Param(a)) => Some(&a.attrs[..]),
811-
Some(Node::Local(l)) => Some(&l.attrs[..]),
812-
Some(Node::Item(i)) => Some(&i.attrs[..]),
813-
Some(Node::ForeignItem(fi)) => Some(&fi.attrs[..]),
814-
Some(Node::TraitItem(ref ti)) => Some(&ti.attrs[..]),
815-
Some(Node::ImplItem(ref ii)) => Some(&ii.attrs[..]),
816-
Some(Node::Variant(ref v)) => Some(&v.attrs[..]),
817-
Some(Node::Field(ref f)) => Some(&f.attrs[..]),
818-
Some(Node::Expr(ref e)) => Some(&*e.attrs),
819-
Some(Node::Stmt(ref s)) => Some(s.kind.attrs(|id| self.item(id.id))),
820-
Some(Node::Arm(ref a)) => Some(&*a.attrs),
821-
Some(Node::GenericParam(param)) => Some(&param.attrs[..]),
809+
let attrs = self.find_entry(id).map(|entry| match entry.node {
810+
Node::Param(a) => &a.attrs[..],
811+
Node::Local(l) => &l.attrs[..],
812+
Node::Item(i) => &i.attrs[..],
813+
Node::ForeignItem(fi) => &fi.attrs[..],
814+
Node::TraitItem(ref ti) => &ti.attrs[..],
815+
Node::ImplItem(ref ii) => &ii.attrs[..],
816+
Node::Variant(ref v) => &v.attrs[..],
817+
Node::Field(ref f) => &f.attrs[..],
818+
Node::Expr(ref e) => &*e.attrs,
819+
Node::Stmt(ref s) => s.kind.attrs(|id| self.item(id.id)),
820+
Node::Arm(ref a) => &*a.attrs,
821+
Node::GenericParam(param) => &param.attrs[..],
822822
// Unit/tuple structs/variants take the attributes straight from
823823
// the struct/variant definition.
824-
Some(Node::Ctor(..)) => return self.attrs(self.get_parent_item(id)),
825-
Some(Node::Crate(item)) => Some(&item.attrs[..]),
826-
_ => None,
827-
};
824+
Node::Ctor(..) => self.attrs(self.get_parent_item(id)),
825+
Node::Crate(item) => &item.attrs[..],
826+
Node::MacroDef(def) => def.attrs,
827+
Node::AnonConst(..)
828+
| Node::PathSegment(..)
829+
| Node::Ty(..)
830+
| Node::Pat(..)
831+
| Node::Binding(..)
832+
| Node::TraitRef(..)
833+
| Node::Block(..)
834+
| Node::Lifetime(..)
835+
| Node::Visibility(..) => &[],
836+
});
828837
attrs.unwrap_or(&[])
829838
}
830839

src/librustdoc/clean/inline.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,8 @@ crate fn try_inline(
124124
let attrs = merge_attrs(cx, Some(parent_module), target_attrs, attrs_clone);
125125

126126
cx.renderinfo.borrow_mut().inlined.insert(did);
127-
ret.push(clean::Item {
128-
source: cx.tcx.def_span(did).clean(cx),
129-
name: Some(name.clean(cx)),
130-
attrs,
131-
kind,
132-
visibility: clean::Public,
133-
stability: cx.tcx.lookup_stability(did).cloned(),
134-
deprecation: cx.tcx.lookup_deprecation(did).clean(cx),
135-
def_id: did,
136-
});
127+
let what_rustc_thinks = clean::Item::from_def_id_and_parts(did, Some(name), kind, cx);
128+
ret.push(clean::Item { attrs, ..what_rustc_thinks });
137129
Some(ret)
138130
}
139131

@@ -443,8 +435,10 @@ crate fn build_impl(
443435

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

446-
ret.push(clean::Item {
447-
kind: clean::ImplItem(clean::Impl {
438+
ret.push(clean::Item::from_def_id_and_parts(
439+
did,
440+
None,
441+
clean::ImplItem(clean::Impl {
448442
unsafety: hir::Unsafety::Normal,
449443
generics,
450444
provided_trait_methods: provided,
@@ -455,14 +449,8 @@ crate fn build_impl(
455449
synthetic: false,
456450
blanket_impl: None,
457451
}),
458-
source: tcx.def_span(did).clean(cx),
459-
name: None,
460-
attrs,
461-
visibility: clean::Inherited,
462-
stability: tcx.lookup_stability(did).cloned(),
463-
deprecation: tcx.lookup_deprecation(did).clean(cx),
464-
def_id: did,
465-
});
452+
cx,
453+
));
466454
}
467455

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

0 commit comments

Comments
 (0)