Skip to content

Commit 698c100

Browse files
committed
Auto merge of #23028 - Munksgaard:get_attrs_opt, r=eddyb
This is more flexible and less error-prone. `get_attrs` and `get_attrs_opt` can be used on many more items than the old `get_attrs` could. This is all courtesy of @huonw, and directly taken from here: https://github.com/rust-lang/rust/pull/22348/files#diff-0f85fcb07fb739876892e633fa0e2be6R5575 Also thanks to @Manishearth for pointing it out to me.
2 parents 5f47c06 + caf6f17 commit 698c100

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

src/librustc/middle/ty.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5606,8 +5606,7 @@ pub fn predicates<'tcx>(
56065606
pub fn get_attrs<'tcx>(tcx: &'tcx ctxt, did: DefId)
56075607
-> Cow<'tcx, [ast::Attribute]> {
56085608
if is_local(did) {
5609-
let item = tcx.map.expect_item(did.node);
5610-
Cow::Borrowed(&item.attrs)
5609+
Cow::Borrowed(tcx.map.attrs(did.node))
56115610
} else {
56125611
Cow::Owned(csearch::get_item_attrs(&tcx.sess.cstore, did))
56135612
}

src/libsyntax/ast_map/mod.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -457,35 +457,32 @@ impl<'ast> Map<'ast> {
457457
}
458458
}
459459

460-
/// Given a node ID and a closure, apply the closure to the array
461-
/// of attributes associated with the AST corresponding to the Node ID
462-
pub fn with_attrs<T, F>(&self, id: NodeId, f: F) -> T where
463-
F: FnOnce(Option<&[Attribute]>) -> T,
464-
{
465-
let attrs = match self.get(id) {
466-
NodeItem(i) => Some(&i.attrs[..]),
467-
NodeForeignItem(fi) => Some(&fi.attrs[..]),
468-
NodeTraitItem(ref tm) => match **tm {
460+
/// Given a node ID, get a list of of attributes associated with the AST
461+
/// corresponding to the Node ID
462+
pub fn attrs(&self, id: NodeId) -> &[Attribute] {
463+
let attrs = match self.find(id) {
464+
Some(NodeItem(i)) => Some(&i.attrs[..]),
465+
Some(NodeForeignItem(fi)) => Some(&fi.attrs[..]),
466+
Some(NodeTraitItem(ref tm)) => match **tm {
469467
RequiredMethod(ref type_m) => Some(&type_m.attrs[..]),
470468
ProvidedMethod(ref m) => Some(&m.attrs[..]),
471469
TypeTraitItem(ref typ) => Some(&typ.attrs[..]),
472470
},
473-
NodeImplItem(ref ii) => {
471+
Some(NodeImplItem(ref ii)) => {
474472
match **ii {
475473
MethodImplItem(ref m) => Some(&m.attrs[..]),
476474
TypeImplItem(ref t) => Some(&t.attrs[..]),
477475
}
478476
}
479-
NodeVariant(ref v) => Some(&v.node.attrs[..]),
477+
Some(NodeVariant(ref v)) => Some(&v.node.attrs[..]),
480478
// unit/tuple structs take the attributes straight from
481479
// the struct definition.
482-
// FIXME(eddyb) make this work again (requires access to the map).
483-
NodeStructCtor(_) => {
484-
return self.with_attrs(self.get_parent(id), f);
480+
Some(NodeStructCtor(_)) => {
481+
return self.attrs(self.get_parent(id));
485482
}
486483
_ => None
487484
};
488-
f(attrs)
485+
attrs.unwrap_or(&[])
489486
}
490487

491488
/// Returns an iterator that yields the node id's with paths that

0 commit comments

Comments
 (0)