diff options
| author | bors <bors@rust-lang.org> | 2015-03-10 14:50:40 +0000 | 
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-03-10 14:50:40 +0000 | 
| commit | 698c1008d6009fc6cf5a4e797cef62b52f7d1e75 (patch) | |
| tree | 46519566e57b792398f02b9a2f130e296471e704 | |
| parent | 5f47c0613ed4eb46fca3633c1297364c09e5e451 (diff) | |
| parent | caf6f17c0f89b3384a25b6d74bb2332c7ec2d2ac (diff) | |
| download | rust-698c1008d6009fc6cf5a4e797cef62b52f7d1e75.tar.gz rust-698c1008d6009fc6cf5a4e797cef62b52f7d1e75.zip | |
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.
| -rw-r--r-- | src/librustc/middle/ty.rs | 3 | ||||
| -rw-r--r-- | src/libsyntax/ast_map/mod.rs | 27 | 
2 files changed, 13 insertions, 17 deletions
| diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 9f841b5acf8..4cb4d343de7 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -5606,8 +5606,7 @@ pub fn predicates<'tcx>( pub fn get_attrs<'tcx>(tcx: &'tcx ctxt, did: DefId) -> Cow<'tcx, [ast::Attribute]> { if is_local(did) { - let item = tcx.map.expect_item(did.node); - Cow::Borrowed(&item.attrs) + Cow::Borrowed(tcx.map.attrs(did.node)) } else { Cow::Owned(csearch::get_item_attrs(&tcx.sess.cstore, did)) } diff --git a/src/libsyntax/ast_map/mod.rs b/src/libsyntax/ast_map/mod.rs index faa681c0255..b96d735d92d 100644 --- a/src/libsyntax/ast_map/mod.rs +++ b/src/libsyntax/ast_map/mod.rs @@ -457,35 +457,32 @@ impl<'ast> Map<'ast> { } } - /// Given a node ID and a closure, apply the closure to the array - /// of attributes associated with the AST corresponding to the Node ID - pub fn with_attrs<T, F>(&self, id: NodeId, f: F) -> T where - F: FnOnce(Option<&[Attribute]>) -> T, - { - let attrs = match self.get(id) { - NodeItem(i) => Some(&i.attrs[..]), - NodeForeignItem(fi) => Some(&fi.attrs[..]), - NodeTraitItem(ref tm) => match **tm { + /// Given a node ID, get a list of of attributes associated with the AST + /// corresponding to the Node ID + pub fn attrs(&self, id: NodeId) -> &[Attribute] { + let attrs = match self.find(id) { + Some(NodeItem(i)) => Some(&i.attrs[..]), + Some(NodeForeignItem(fi)) => Some(&fi.attrs[..]), + Some(NodeTraitItem(ref tm)) => match **tm { RequiredMethod(ref type_m) => Some(&type_m.attrs[..]), ProvidedMethod(ref m) => Some(&m.attrs[..]), TypeTraitItem(ref typ) => Some(&typ.attrs[..]), }, - NodeImplItem(ref ii) => { + Some(NodeImplItem(ref ii)) => { match **ii { MethodImplItem(ref m) => Some(&m.attrs[..]), TypeImplItem(ref t) => Some(&t.attrs[..]), } } - NodeVariant(ref v) => Some(&v.node.attrs[..]), + Some(NodeVariant(ref v)) => Some(&v.node.attrs[..]), // unit/tuple structs take the attributes straight from // the struct definition. - // FIXME(eddyb) make this work again (requires access to the map). - NodeStructCtor(_) => { - return self.with_attrs(self.get_parent(id), f); + Some(NodeStructCtor(_)) => { + return self.attrs(self.get_parent(id)); } _ => None }; - f(attrs) + attrs.unwrap_or(&[]) } /// Returns an iterator that yields the node id's with paths that | 
