Skip to content

Commit

Permalink
Fix broken handling of MacroDef in Map::attrs
Browse files Browse the repository at this point in the history
This also uses an exhaustive match to avoid future similar bugs.
  • Loading branch information
jyn514 committed Nov 15, 2020
1 parent fff9203 commit b9f7690
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 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
6 changes: 2 additions & 4 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2283,7 +2283,7 @@ impl Clean<Item> for doctree::ForeignItem<'_> {

impl Clean<Item> for doctree::Macro<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Item {
let what_rustc_thinks = Item::from_def_id_and_parts(
Item::from_def_id_and_parts(
self.def_id,
Some(self.name),
MacroItem(Macro {
Expand All @@ -2298,9 +2298,7 @@ impl Clean<Item> for doctree::Macro<'_> {
imported_from: self.imported_from.clean(cx),
}),
cx,
);
// rustc_metadata strips attributes on macros; use the attributes from the HIR instead
Item { attrs: self.attrs.clean(cx), ..what_rustc_thinks }
)
}
}

Expand Down

0 comments on commit b9f7690

Please sign in to comment.