From 01678acf5066e2c4d29f3617ac5892acedf3ddbe Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Tue, 14 Apr 2015 09:54:58 +1200 Subject: [PATCH] Expose visibility for fns in syntax::visit --- src/librustc/middle/dead.rs | 2 +- src/librustc/middle/effect.rs | 4 ++-- src/librustc/middle/resolve_lifetime.rs | 4 ++-- src/librustc_lint/builtin.rs | 12 +++++----- src/librustc_resolve/lib.rs | 4 ++-- src/libsyntax/ast_map/blocks.rs | 29 +++++++++++++++---------- src/libsyntax/ast_util.rs | 4 ++-- src/libsyntax/feature_gate.rs | 6 ++--- src/libsyntax/visit.rs | 14 ++++++------ 9 files changed, 43 insertions(+), 36 deletions(-) diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs index 426540ce156..b3188f260b9 100644 --- a/src/librustc/middle/dead.rs +++ b/src/librustc/middle/dead.rs @@ -562,7 +562,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> { span: codemap::Span, id: ast::NodeId) { // Have to warn method here because methods are not ast::Item match fk { - visit::FkMethod(name, _) => { + visit::FkMethod(name, _, _) => { if !self.symbol_is_live(id, None) { self.warn_dead_code(id, span, name.name, "method"); } diff --git a/src/librustc/middle/effect.rs b/src/librustc/middle/effect.rs index 814492cbef1..b6a070c9332 100644 --- a/src/librustc/middle/effect.rs +++ b/src/librustc/middle/effect.rs @@ -87,9 +87,9 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> { block: &'v ast::Block, span: Span, _: ast::NodeId) { let (is_item_fn, is_unsafe_fn) = match fn_kind { - visit::FkItemFn(_, _, fn_style, _) => + visit::FkItemFn(_, _, fn_style, _, _) => (true, fn_style == ast::Unsafety::Unsafe), - visit::FkMethod(_, sig) => + visit::FkMethod(_, sig, _) => (true, sig.unsafety == ast::Unsafety::Unsafe), _ => (false, false), }; diff --git a/src/librustc/middle/resolve_lifetime.rs b/src/librustc/middle/resolve_lifetime.rs index 1036c97a5ad..53ed3b91948 100644 --- a/src/librustc/middle/resolve_lifetime.rs +++ b/src/librustc/middle/resolve_lifetime.rs @@ -142,12 +142,12 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> { fn visit_fn(&mut self, fk: visit::FnKind<'v>, fd: &'v ast::FnDecl, b: &'v ast::Block, s: Span, _: ast::NodeId) { match fk { - visit::FkItemFn(_, generics, _, _) => { + visit::FkItemFn(_, generics, _, _, _) => { self.visit_early_late(subst::FnSpace, generics, |this| { visit::walk_fn(this, fk, fd, b, s) }) } - visit::FkMethod(_, sig) => { + visit::FkMethod(_, sig, _) => { self.visit_early_late(subst::FnSpace, &sig.generics, |this| { visit::walk_fn(this, fk, fd, b, s) }) diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 3bb737ddc12..f6e0d085719 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -957,7 +957,7 @@ impl LintPass for NonSnakeCase { fk: visit::FnKind, _: &ast::FnDecl, _: &ast::Block, span: Span, id: ast::NodeId) { match fk { - visit::FkMethod(ident, _) => match method_context(cx, id, span) { + visit::FkMethod(ident, _, _) => match method_context(cx, id, span) { MethodContext::PlainImpl => { self.check_snake_case(cx, "method", ident, span) }, @@ -966,7 +966,7 @@ impl LintPass for NonSnakeCase { }, _ => (), }, - visit::FkItemFn(ident, _, _, _) => { + visit::FkItemFn(ident, _, _, _, _) => { self.check_snake_case(cx, "function", ident, span) }, _ => (), @@ -1290,10 +1290,10 @@ impl LintPass for UnsafeCode { fn check_fn(&mut self, cx: &Context, fk: visit::FnKind, _: &ast::FnDecl, _: &ast::Block, span: Span, _: ast::NodeId) { match fk { - visit::FkItemFn(_, _, ast::Unsafety::Unsafe, _) => + visit::FkItemFn(_, _, ast::Unsafety::Unsafe, _, _) => cx.span_lint(UNSAFE_CODE, span, "declaration of an `unsafe` function"), - visit::FkMethod(_, sig) => { + visit::FkMethod(_, sig, _) => { if sig.unsafety == ast::Unsafety::Unsafe { cx.span_lint(UNSAFE_CODE, span, "implementation of an `unsafe` method") } @@ -1818,8 +1818,8 @@ impl LintPass for UnconditionalRecursion { ast::NodeId, ast::NodeId, ast::Ident, ast::NodeId) -> bool; let (name, checker) = match fn_kind { - visit::FkItemFn(name, _, _, _) => (name, id_refers_to_this_fn as F), - visit::FkMethod(name, _) => (name, id_refers_to_this_method as F), + visit::FkItemFn(name, _, _, _, _) => (name, id_refers_to_this_fn as F), + visit::FkMethod(name, _, _) => (name, id_refers_to_this_method as F), // closures can't recur, so they don't matter. visit::FkFnBlock => return }; diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index f90c7640f8c..6fd59a20532 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -242,11 +242,11 @@ impl<'a, 'v, 'tcx> Visitor<'v> for Resolver<'a, 'tcx> { _: Span, node_id: NodeId) { let rib_kind = match function_kind { - visit::FkItemFn(_, generics, _, _) => { + visit::FkItemFn(_, generics, _, _, _) => { self.visit_generics(generics); ItemRibKind } - visit::FkMethod(_, sig) => { + visit::FkMethod(_, sig, _) => { self.visit_generics(&sig.generics); self.visit_explicit_self(&sig.explicit_self); MethodRibKind diff --git a/src/libsyntax/ast_map/blocks.rs b/src/libsyntax/ast_map/blocks.rs index 475970ac30a..1505d1e91b8 100644 --- a/src/libsyntax/ast_map/blocks.rs +++ b/src/libsyntax/ast_map/blocks.rs @@ -121,6 +121,7 @@ struct ItemFnParts<'a> { decl: &'a ast::FnDecl, unsafety: ast::Unsafety, abi: abi::Abi, + vis: ast::Visibility, generics: &'a ast::Generics, body: &'a Block, id: ast::NodeId, @@ -155,44 +156,50 @@ impl<'a> FnLikeNode<'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) -> visit::FnKind<'a> { let item = |p: ItemFnParts<'a>| -> visit::FnKind<'a> { - visit::FkItemFn(p.ident, p.generics, p.unsafety, p.abi) + visit::FkItemFn(p.ident, p.generics, p.unsafety, p.abi, p.vis) }; let closure = |_: ClosureParts| { visit::FkFnBlock }; - let method = |_, ident, sig: &'a ast::MethodSig, _, _| { - visit::FkMethod(ident, sig) + let method = |_, ident, sig: &'a ast::MethodSig, vis, _, _| { + visit::FkMethod(ident, sig, vis) }; self.handle(item, method, closure) } fn handle(self, item_fn: I, method: M, closure: C) -> A where I: FnOnce(ItemFnParts<'a>) -> A, - M: FnOnce(NodeId, ast::Ident, &'a ast::MethodSig, &'a ast::Block, Span) -> A, + M: FnOnce(NodeId, + ast::Ident, + &'a ast::MethodSig, + Option, + &'a ast::Block, + Span) + -> A, C: FnOnce(ClosureParts<'a>) -> A, { match self.node { @@ -200,20 +207,20 @@ impl<'a> FnLikeNode<'a> { ast::ItemFn(ref decl, unsafety, abi, ref generics, ref block) => item_fn(ItemFnParts{ ident: i.ident, decl: &**decl, unsafety: unsafety, body: &**block, - generics: generics, abi: abi, id: i.id, span: i.span + generics: generics, abi: abi, vis: i.vis, id: i.id, span: i.span }), _ => panic!("item FnLikeNode that is not fn-like"), }, ast_map::NodeTraitItem(ti) => match ti.node { ast::MethodTraitItem(ref sig, Some(ref body)) => { - method(ti.id, ti.ident, sig, body, ti.span) + method(ti.id, ti.ident, sig, None, body, ti.span) } _ => panic!("trait method FnLikeNode that is not fn-like"), }, ast_map::NodeImplItem(ii) => { match ii.node { ast::MethodImplItem(ref sig, ref body) => { - method(ii.id, ii.ident, sig, body, ii.span) + method(ii.id, ii.ident, sig, Some(ii.vis), body, ii.span) } ast::TypeImplItem(_) | ast::MacImplItem(_) => { diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index c4c2249d029..0ad75c5ec8c 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -440,10 +440,10 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> { self.operation.visit_id(node_id); match function_kind { - visit::FkItemFn(_, generics, _, _) => { + visit::FkItemFn(_, generics, _, _, _) => { self.visit_generics_helper(generics) } - visit::FkMethod(_, sig) => { + visit::FkMethod(_, sig, _) => { self.visit_generics_helper(&sig.generics) } visit::FkFnBlock => {} diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 689b4595d39..a6f8a718b33 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -644,13 +644,13 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> { span: Span, _node_id: NodeId) { match fn_kind { - visit::FkItemFn(_, _, _, abi) if abi == Abi::RustIntrinsic => { + visit::FkItemFn(_, _, _, abi, _) if abi == Abi::RustIntrinsic => { self.gate_feature("intrinsics", span, "intrinsics are subject to change") } - visit::FkItemFn(_, _, _, abi) | - visit::FkMethod(_, &ast::MethodSig { abi, .. }) if abi == Abi::RustCall => { + visit::FkItemFn(_, _, _, abi, _) | + visit::FkMethod(_, &ast::MethodSig { abi, .. }, _) if abi == Abi::RustCall => { self.gate_feature("unboxed_closures", span, "rust-call ABI is subject to change") diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 5c345c75642..4c70fc9f81f 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -35,10 +35,10 @@ use owned_slice::OwnedSlice; #[derive(Copy, Clone)] pub enum FnKind<'a> { /// fn foo() or extern "Abi" fn foo() - FkItemFn(Ident, &'a Generics, Unsafety, Abi), + FkItemFn(Ident, &'a Generics, Unsafety, Abi, Visibility), /// fn foo(&self) - FkMethod(Ident, &'a MethodSig), + FkMethod(Ident, &'a MethodSig, Option), /// |x, y| ... /// proc(x, y) ... @@ -247,7 +247,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) { visitor.visit_expr(&**expr); } ItemFn(ref declaration, fn_style, abi, ref generics, ref body) => { - visitor.visit_fn(FkItemFn(item.ident, generics, fn_style, abi), + visitor.visit_fn(FkItemFn(item.ident, generics, fn_style, abi, item.vis), &**declaration, &**body, item.span, @@ -600,10 +600,10 @@ pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V, walk_fn_decl(visitor, function_declaration); match function_kind { - FkItemFn(_, generics, _, _) => { + FkItemFn(_, generics, _, _, _) => { visitor.visit_generics(generics); } - FkMethod(_, sig) => { + FkMethod(_, sig, _) => { visitor.visit_generics(&sig.generics); visitor.visit_explicit_self(&sig.explicit_self); } @@ -625,7 +625,7 @@ 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(FkMethod(trait_item.ident, sig), &sig.decl, + visitor.visit_fn(FkMethod(trait_item.ident, sig, None), &sig.decl, body, trait_item.span, trait_item.id); } TypeTraitItem(ref bounds, ref default) => { @@ -642,7 +642,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt } match impl_item.node { MethodImplItem(ref sig, ref body) => { - visitor.visit_fn(FkMethod(impl_item.ident, sig), &sig.decl, + visitor.visit_fn(FkMethod(impl_item.ident, sig, Some(impl_item.vis)), &sig.decl, body, impl_item.span, impl_item.id); } TypeImplItem(ref ty) => {