]> git.lizzy.rs Git - rust.git/commitdiff
Expose attached attributes to `FnKind` abstraction so that I can look at them in...
authorFelix S. Klock II <pnkfelix@pnkfx.org>
Mon, 25 Jan 2016 13:11:51 +0000 (14:11 +0100)
committerFelix S. Klock II <pnkfelix@pnkfx.org>
Mon, 21 Mar 2016 17:36:22 +0000 (18:36 +0100)
14 files changed:
src/librustc/front/map/blocks.rs
src/librustc/middle/check_match.rs
src/librustc/middle/const_eval.rs
src/librustc/middle/effect.rs
src/librustc/middle/intrinsicck.rs
src/librustc/middle/resolve_lifetime.rs
src/librustc_borrowck/borrowck/mod.rs
src/librustc_front/intravisit.rs
src/librustc_front/util.rs
src/librustc_lint/bad_style.rs
src/librustc_lint/builtin.rs
src/librustc_mir/mir_map.rs
src/librustc_passes/consts.rs
src/librustc_resolve/lib.rs

index 752b625f529c084ce3aa077a32bc92b5e1f693a8..976a8c6dda0461f8e344acb22a5c87a2b7a3b850 100644 (file)
@@ -26,7 +26,8 @@
 use front::map::{self, Node};
 use syntax::abi;
 use rustc_front::hir::{Block, FnDecl};
-use syntax::ast::{Name, NodeId};
+use syntax::ast::{Attribute, Name, NodeId};
+use syntax::attr::ThinAttributesExt;
 use rustc_front::hir as ast;
 use syntax::codemap::Span;
 use rustc_front::intravisit::FnKind;
@@ -116,7 +117,8 @@ struct ItemFnParts<'a> {
     generics: &'a ast::Generics,
     body:     &'a Block,
     id:       NodeId,
-    span:     Span
+    span:     Span,
+    attrs:    &'a [Attribute],
 }
 
 /// These are all the components one can extract from a closure expr
@@ -125,12 +127,13 @@ struct ClosureParts<'a> {
     decl: &'a FnDecl,
     body: &'a Block,
     id: NodeId,
-    span: Span
+    span: Span,
+    attrs: &'a [Attribute],
 }
 
 impl<'a> ClosureParts<'a> {
-    fn new(d: &'a FnDecl, b: &'a Block, id: NodeId, s: Span) -> ClosureParts<'a> {
-        ClosureParts { decl: d, body: b, id: id, span: s }
+    fn new(d: &'a FnDecl, b: &'a Block, id: NodeId, s: Span, attrs: &'a [Attribute]) -> Self {
+        ClosureParts { decl: d, body: b, id: id, span: s, attrs: attrs }
     }
 }
 
@@ -165,37 +168,37 @@ pub fn to_fn_parts(self) -> FnParts<'a> {
 
     pub fn body(self) -> &'a Block {
         self.handle(|i: ItemFnParts<'a>|  &*i.body,
-                    |_, _, _: &'a ast::MethodSig, _, body: &'a ast::Block, _|  body,
+                    |_, _, _: &'a ast::MethodSig, _, body: &'a ast::Block, _, _|  body,
                     |c: ClosureParts<'a>| c.body)
     }
 
     pub fn decl(self) -> &'a FnDecl {
         self.handle(|i: ItemFnParts<'a>|  &*i.decl,
-                    |_, _, sig: &'a ast::MethodSig, _, _, _|  &sig.decl,
+                    |_, _, sig: &'a ast::MethodSig, _, _, _, _|  &sig.decl,
                     |c: ClosureParts<'a>| c.decl)
     }
 
     pub fn span(self) -> Span {
         self.handle(|i: ItemFnParts|     i.span,
-                    |_, _, _: &'a ast::MethodSig, _, _, span| span,
+                    |_, _, _: &'a ast::MethodSig, _, _, span, _| span,
                     |c: ClosureParts|    c.span)
     }
 
     pub fn id(self) -> NodeId {
         self.handle(|i: ItemFnParts|     i.id,
-                    |id, _, _: &'a ast::MethodSig, _, _, _| id,
+                    |id, _, _: &'a ast::MethodSig, _, _, _, _| id,
                     |c: ClosureParts|    c.id)
     }
 
     pub fn kind(self) -> FnKind<'a> {
         let item = |p: ItemFnParts<'a>| -> FnKind<'a> {
-            FnKind::ItemFn(p.name, p.generics, p.unsafety, p.constness, p.abi, p.vis)
+            FnKind::ItemFn(p.name, p.generics, p.unsafety, p.constness, p.abi, p.vis, p.attrs)
         };
-        let closure = |_: ClosureParts| {
-            FnKind::Closure
+        let closure = |c: ClosureParts<'a>| {
+            FnKind::Closure(c.attrs)
         };
-        let method = |_, name: Name, sig: &'a ast::MethodSig, vis, _, _| {
-            FnKind::Method(name, sig, vis)
+        let method = |_, name: Name, sig: &'a ast::MethodSig, vis, _, _, attrs| {
+            FnKind::Method(name, sig, vis, attrs)
         };
         self.handle(item, method, closure)
     }
@@ -207,7 +210,8 @@ fn handle<A, I, M, C>(self, item_fn: I, method: M, closure: C) -> A where
                   &'a ast::MethodSig,
                   Option<ast::Visibility>,
                   &'a ast::Block,
-                  Span)
+                  Span,
+                  &'a [Attribute])
                   -> A,
         C: FnOnce(ClosureParts<'a>) -> A,
     {
@@ -224,20 +228,21 @@ fn handle<A, I, M, C>(self, item_fn: I, method: M, closure: C) -> A where
                         abi: abi,
                         vis: i.vis,
                         constness: constness,
-                        span: i.span
+                        span: i.span,
+                        attrs: &i.attrs,
                     }),
                 _ => panic!("item FnLikeNode that is not fn-like"),
             },
             map::NodeTraitItem(ti) => match ti.node {
                 ast::MethodTraitItem(ref sig, Some(ref body)) => {
-                    method(ti.id, ti.name, sig, None, body, ti.span)
+                    method(ti.id, ti.name, sig, None, body, ti.span, &ti.attrs)
                 }
                 _ => panic!("trait method FnLikeNode that is not fn-like"),
             },
             map::NodeImplItem(ii) => {
                 match ii.node {
                     ast::ImplItemKind::Method(ref sig, ref body) => {
-                        method(ii.id, ii.name, sig, Some(ii.vis), body, ii.span)
+                        method(ii.id, ii.name, sig, Some(ii.vis), body, ii.span, &ii.attrs)
                     }
                     _ => {
                         panic!("impl method FnLikeNode that is not fn-like")
@@ -246,7 +251,11 @@ fn handle<A, I, M, C>(self, item_fn: I, method: M, closure: C) -> A where
             }
             map::NodeExpr(e) => match e.node {
                 ast::ExprClosure(_, ref decl, ref block) =>
-                    closure(ClosureParts::new(&decl, &block, e.id, e.span)),
+                    closure(ClosureParts::new(&decl,
+                                              &block,
+                                              e.id,
+                                              e.span,
+                                              e.attrs.as_attr_slice())),
                 _ => panic!("expr FnLikeNode that is not fn-like"),
             },
             _ => panic!("other FnLikeNode that is not fn-like"),
index 16e0a334440ff0f3570e2bde57c64eb69aebbe4b..b456291b17a0ac6c418aaffdd964d6999cb61ba0 100644 (file)
@@ -1017,7 +1017,7 @@ fn check_fn(cx: &mut MatchCheckCtxt,
             sp: Span,
             fn_id: NodeId) {
     match kind {
-        FnKind::Closure => {}
+        FnKind::Closure(_) => {}
         _ => cx.param_env = ParameterEnvironment::for_item(cx.tcx, fn_id),
     }
 
index 47b6c49fddb6d69d5b0ad87876aea789d0c8f767..228a7d21007e50f28c236c7c063fc5151cef0cb1 100644 (file)
@@ -226,10 +226,10 @@ pub fn lookup_const_fn_by_id<'tcx>(tcx: &TyCtxt<'tcx>, def_id: DefId)
     };
 
     match fn_like.kind() {
-        FnKind::ItemFn(_, _, _, hir::Constness::Const, _, _) => {
+        FnKind::ItemFn(_, _, _, hir::Constness::Const, _, _, _) => {
             Some(fn_like)
         }
-        FnKind::Method(_, m, _) => {
+        FnKind::Method(_, m, _, _) => {
             if m.constness == hir::Constness::Const {
                 Some(fn_like)
             } else {
index e6821cf639eca2d8058d0a81b82e71f45a503eb9..d4ff4b797c839861d1cba34a989e4474d6bf96dc 100644 (file)
@@ -82,9 +82,9 @@ fn visit_fn(&mut self, fn_kind: FnKind<'v>, fn_decl: &'v hir::FnDecl,
                 block: &'v hir::Block, span: Span, _: ast::NodeId) {
 
         let (is_item_fn, is_unsafe_fn) = match fn_kind {
-            FnKind::ItemFn(_, _, unsafety, _, _, _) =>
+            FnKind::ItemFn(_, _, unsafety, _, _, _, _) =>
                 (true, unsafety == hir::Unsafety::Unsafe),
-            FnKind::Method(_, sig, _) =>
+            FnKind::Method(_, sig, _, _) =>
                 (true, sig.unsafety == hir::Unsafety::Unsafe),
             _ => (false, false),
         };
index fd857513e5b560c0f9e1f69fa4c16e4feae38880..233e55cdbb5dde2afc897a2d4787655b8c62cea9 100644 (file)
@@ -226,7 +226,7 @@ fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v hir::FnDecl,
                 intravisit::walk_fn(self, fk, fd, b, s);
                 self.param_envs.pop();
             }
-            FnKind::Closure => {
+            FnKind::Closure(..) => {
                 intravisit::walk_fn(self, fk, fd, b, s);
             }
         }
index f29e14f67d73fa7b3407ecd70d2e08b53be9dd80..b5ea365f880a2e3efc32ef4b1271642291a76ede 100644 (file)
@@ -182,17 +182,17 @@ fn visit_foreign_item(&mut self, item: &hir::ForeignItem) {
     fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v hir::FnDecl,
                 b: &'v hir::Block, s: Span, fn_id: ast::NodeId) {
         match fk {
-            FnKind::ItemFn(_, generics, _, _, _, _) => {
+            FnKind::ItemFn(_, generics, _, _, _, _, _) => {
                 self.visit_early_late(subst::FnSpace, generics, |this| {
                     this.add_scope_and_walk_fn(fk, fd, b, s, fn_id)
                 })
             }
-            FnKind::Method(_, sig, _) => {
+            FnKind::Method(_, sig, _, _) => {
                 self.visit_early_late(subst::FnSpace, &sig.generics, |this| {
                     this.add_scope_and_walk_fn(fk, fd, b, s, fn_id)
                 })
             }
-            FnKind::Closure => {
+            FnKind::Closure(_) => {
                 self.add_scope_and_walk_fn(fk, fd, b, s, fn_id)
             }
         }
@@ -471,16 +471,16 @@ fn add_scope_and_walk_fn<'b>(&mut self,
                                  fn_id: ast::NodeId) {
 
         match fk {
-            FnKind::ItemFn(_, generics, _, _, _, _) => {
+            FnKind::ItemFn(_, generics, _, _, _, _, _) => {
                 intravisit::walk_fn_decl(self, fd);
                 self.visit_generics(generics);
             }
-            FnKind::Method(_, sig, _) => {
+            FnKind::Method(_, sig, _, _) => {
                 intravisit::walk_fn_decl(self, fd);
                 self.visit_generics(&sig.generics);
                 self.visit_explicit_self(&sig.explicit_self);
             }
-            FnKind::Closure => {
+            FnKind::Closure(_) => {
                 intravisit::walk_fn_decl(self, fd);
             }
         }
index cab8125a61b33e06da6f3533e5ad90da400390fe..139c881dc1d58352409c4fa62a08d6cf5a495a68 100644 (file)
@@ -73,7 +73,7 @@ fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl,
                 self.free_region_map = old_free_region_map;
             }
 
-            FnKind::Closure => {
+            FnKind::Closure(..) => {
                 borrowck_fn(self, fk, fd, b, s, id);
             }
         }
index e031dfc5b161dd9e4c564350fea5329f20b8542f..be1cc528d889e59134bf8f67449153f3548b99c5 100644 (file)
 
 use syntax::abi::Abi;
 use syntax::ast::{NodeId, CRATE_NODE_ID, Name, Attribute};
+use syntax::attr::ThinAttributesExt;
 use syntax::codemap::Span;
 use hir::*;
 
 #[derive(Copy, Clone, PartialEq, Eq)]
 pub enum FnKind<'a> {
     /// fn foo() or extern "Abi" fn foo()
-    ItemFn(Name, &'a Generics, Unsafety, Constness, Abi, Visibility),
+    ItemFn(Name, &'a Generics, Unsafety, Constness, Abi, Visibility, &'a [Attribute]),
 
     /// fn foo(&self)
-    Method(Name, &'a MethodSig, Option<Visibility>),
+    Method(Name, &'a MethodSig, Option<Visibility>, &'a [Attribute]),
 
     /// |x, y| {}
-    Closure,
+    Closure(&'a [Attribute]),
+}
+
+impl<'a> FnKind<'a> {
+    pub fn attrs(&self) -> &'a [Attribute] {
+        match *self {
+            FnKind::ItemFn(_, _, _, _, _, _, attrs) => attrs,
+            FnKind::Method(_, _, _, attrs) => attrs,
+            FnKind::Closure(attrs) => attrs,
+        }
+    }
 }
 
 /// Each method of the Visitor trait is a hook to be potentially
@@ -310,7 +321,8 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
                                             unsafety,
                                             constness,
                                             abi,
-                                            item.vis),
+                                            item.vis,
+                                            &item.attrs),
                              declaration,
                              body,
                              item.span,
@@ -595,14 +607,14 @@ pub fn walk_fn_decl_nopat<'v, V: Visitor<'v>>(visitor: &mut V, function_declarat
 
 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
     match function_kind {
-        FnKind::ItemFn(_, generics, _, _, _, _) => {
+        FnKind::ItemFn(_, generics, _, _, _, _, _) => {
             visitor.visit_generics(generics);
         }
-        FnKind::Method(_, sig, _) => {
+        FnKind::Method(_, sig, _, _) => {
             visitor.visit_generics(&sig.generics);
             visitor.visit_explicit_self(&sig.explicit_self);
         }
-        FnKind::Closure => {}
+        FnKind::Closure(_) => {}
     }
 }
 
@@ -630,7 +642,10 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
             walk_fn_decl(visitor, &sig.decl);
         }
         MethodTraitItem(ref sig, Some(ref body)) => {
-            visitor.visit_fn(FnKind::Method(trait_item.name, sig, None),
+            visitor.visit_fn(FnKind::Method(trait_item.name,
+                                            sig,
+                                            None,
+                                            &trait_item.attrs),
                              &sig.decl,
                              body,
                              trait_item.span,
@@ -652,7 +667,10 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
             visitor.visit_expr(expr);
         }
         ImplItemKind::Method(ref sig, ref body) => {
-            visitor.visit_fn(FnKind::Method(impl_item.name, sig, Some(impl_item.vis)),
+            visitor.visit_fn(FnKind::Method(impl_item.name,
+                                            sig,
+                                            Some(impl_item.vis),
+                                            &impl_item.attrs),
                              &sig.decl,
                              body,
                              impl_item.span,
@@ -758,7 +776,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
             walk_list!(visitor, visit_arm, arms);
         }
         ExprClosure(_, ref function_declaration, ref body) => {
-            visitor.visit_fn(FnKind::Closure,
+            visitor.visit_fn(FnKind::Closure(expression.attrs.as_attr_slice()),
                              function_declaration,
                              body,
                              expression.span,
index 2c86c713b1b7ac9001195264c8dfcc92d49b1f66..f4f9cb75eaf36efc361e3c4be218ca58884cb33d 100644 (file)
@@ -254,13 +254,13 @@ fn visit_fn(&mut self,
         self.operation.visit_id(node_id);
 
         match function_kind {
-            FnKind::ItemFn(_, generics, _, _, _, _) => {
+            FnKind::ItemFn(_, generics, _, _, _, _, _) => {
                 self.visit_generics_helper(generics)
             }
-            FnKind::Method(_, sig, _) => {
+            FnKind::Method(_, sig, _, _) => {
                 self.visit_generics_helper(&sig.generics)
             }
-            FnKind::Closure => {}
+            FnKind::Closure(_) => {}
         }
 
         for argument in &function_declaration.inputs {
index 925aec9894eaa337904db4a92ed937a2017cc62d..e399270197e2b6626683b21270b002ed93594302 100644 (file)
@@ -237,7 +237,7 @@ fn check_fn(&mut self, cx: &LateContext,
                 fk: FnKind, _: &hir::FnDecl,
                 _: &hir::Block, span: Span, id: ast::NodeId) {
         match fk {
-            FnKind::Method(name, _, _) => match method_context(cx, id, span) {
+            FnKind::Method(name, _, _, _) => match method_context(cx, id, span) {
                 MethodLateContext::PlainImpl => {
                     self.check_snake_case(cx, "method", &name.as_str(), Some(span))
                 },
@@ -246,10 +246,10 @@ fn check_fn(&mut self, cx: &LateContext,
                 },
                 _ => (),
             },
-            FnKind::ItemFn(name, _, _, _, _, _) => {
+            FnKind::ItemFn(name, _, _, _, _, _, _) => {
                 self.check_snake_case(cx, "function", &name.as_str(), Some(span))
             },
-            _ => (),
+            FnKind::Closure(_) => (),
         }
     }
 
index 88027931022e755824cee9766c6c91b3e63509f1..2ccb905b6ecdf1cd90158863e8c526a3fce6a3d5 100644 (file)
@@ -223,10 +223,10 @@ fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
     fn check_fn(&mut self, cx: &LateContext, fk: FnKind, _: &hir::FnDecl,
                 _: &hir::Block, span: Span, _: ast::NodeId) {
         match fk {
-            FnKind::ItemFn(_, _, hir::Unsafety::Unsafe, _, _, _) =>
+            FnKind::ItemFn(_, _, hir::Unsafety::Unsafe, _, _, _, _) =>
                 cx.span_lint(UNSAFE_CODE, span, "declaration of an `unsafe` function"),
 
-            FnKind::Method(_, sig, _) => {
+            FnKind::Method(_, sig, _, _) => {
                 if sig.unsafety == hir::Unsafety::Unsafe {
                     cx.span_lint(UNSAFE_CODE, span, "implementation of an `unsafe` method")
                 }
@@ -670,7 +670,7 @@ fn check_fn(&mut self, cx: &LateContext, fn_kind: FnKind, _: &hir::FnDecl,
                 cx.tcx.impl_or_trait_item(cx.tcx.map.local_def_id(id)).as_opt_method()
             }
             // closures can't recur, so they don't matter.
-            FnKind::Closure => return
+            FnKind::Closure(_) => return
         };
 
         // Walk through this function (say `f`) looking to see if
index 4717c54ca649161e6b692c2031761110c778459c..05dbd63ef1a4221b827ba4486878e614ae5c5f36 100644 (file)
@@ -132,7 +132,7 @@ fn visit_fn(&mut self,
                 body: &'tcx hir::Block,
                 span: Span,
                 id: ast::NodeId) {
-        let implicit_arg_tys = if let intravisit::FnKind::Closure = fk {
+        let implicit_arg_tys = if let intravisit::FnKind::Closure(..) = fk {
             vec![closure_self_ty(&self.tcx, id, body.id)]
         } else {
             vec![]
index c964179d4076a2a87d707e2faa4b518b79b47f4b..c2b764594180cfeb4eacdfa2d0aa94b74e4a2455 100644 (file)
@@ -132,10 +132,10 @@ fn fn_like(&mut self,
         }
 
         let mode = match fk {
-            FnKind::ItemFn(_, _, _, hir::Constness::Const, _, _) => {
+            FnKind::ItemFn(_, _, _, hir::Constness::Const, _, _, _) => {
                 Mode::ConstFn
             }
-            FnKind::Method(_, m, _) => {
+            FnKind::Method(_, m, _, _) => {
                 if m.constness == hir::Constness::Const {
                     Mode::ConstFn
                 } else {
index 9d09e8d1eb1a17ce77610c55acfbdbf5053b66dd..af8c9d81687421457d089e80ed7cd1e90c040006 100644 (file)
@@ -634,16 +634,16 @@ fn visit_fn(&mut self,
                 _: Span,
                 node_id: NodeId) {
         let rib_kind = match function_kind {
-            FnKind::ItemFn(_, generics, _, _, _, _) => {
+            FnKind::ItemFn(_, generics, _, _, _, _, _) => {
                 self.visit_generics(generics);
                 ItemRibKind
             }
-            FnKind::Method(_, sig, _) => {
+            FnKind::Method(_, sig, _, _) => {
                 self.visit_generics(&sig.generics);
                 self.visit_explicit_self(&sig.explicit_self);
                 MethodRibKind
             }
-            FnKind::Closure => ClosureRibKind(node_id),
+            FnKind::Closure(_) => ClosureRibKind(node_id),
         };
         self.resolve_function(rib_kind, declaration, block);
     }