From b4c6abcf9e6c1d2710e7ad6a9ce44b3ca6b10d52 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Thu, 7 Nov 2019 13:33:37 +0100 Subject: [PATCH] ast::ItemKind::Fn: use ast::FnSig --- src/librustc/hir/lowering/item.rs | 2 +- src/librustc/hir/map/def_collector.rs | 13 ++++--------- src/librustc_interface/util.rs | 13 +++++++------ src/librustc_passes/ast_validation.rs | 8 ++++---- src/librustc_resolve/late.rs | 2 +- src/librustc_save_analysis/dump_visitor.rs | 4 ++-- src/librustc_save_analysis/lib.rs | 4 ++-- src/librustc_save_analysis/sig.rs | 2 +- src/libsyntax/ast.rs | 2 +- src/libsyntax/mut_visit.rs | 11 +++++------ src/libsyntax/parse/parser/item.rs | 2 +- src/libsyntax/print/pprust.rs | 6 +++--- src/libsyntax/visit.rs | 9 ++++----- src/libsyntax_ext/global_allocator.rs | 15 +++++---------- src/libsyntax_ext/test.rs | 14 +++++++------- src/libsyntax_ext/test_harness.rs | 7 +++---- 16 files changed, 51 insertions(+), 63 deletions(-) diff --git a/src/librustc/hir/lowering/item.rs b/src/librustc/hir/lowering/item.rs index 861130f0c69b2..7aa1aa8bb514a 100644 --- a/src/librustc/hir/lowering/item.rs +++ b/src/librustc/hir/lowering/item.rs @@ -306,7 +306,7 @@ impl LoweringContext<'_> { self.lower_const_body(e) ) } - ItemKind::Fn(ref decl, header, ref generics, ref body) => { + ItemKind::Fn(FnSig { ref decl, header }, ref generics, ref body) => { let fn_def_id = self.resolver.definitions().local_def_id(id); self.with_new_scopes(|this| { this.current_item = Some(ident.span); diff --git a/src/librustc/hir/map/def_collector.rs b/src/librustc/hir/map/def_collector.rs index d705737cc93d4..d858e00a2e9cd 100644 --- a/src/librustc/hir/map/def_collector.rs +++ b/src/librustc/hir/map/def_collector.rs @@ -100,7 +100,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { // Pick the def data. This need not be unique, but the more // information we encapsulate into, the better - let def_data = match i.kind { + let def_data = match &i.kind { ItemKind::Impl(..) => DefPathData::Impl, ItemKind::Mod(..) if i.ident.name == kw::Invalid => { return visit::walk_item(self, i); @@ -109,19 +109,14 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) | ItemKind::OpaqueTy(..) | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) | ItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name), - ItemKind::Fn( - ref decl, - ref header, - ref generics, - ref body, - ) if header.asyncness.node.is_async() => { + ItemKind::Fn(sig, generics, body) if sig.header.asyncness.node.is_async() => { return self.visit_async_fn( i.id, i.ident.name, i.span, - header, + &sig.header, generics, - decl, + &sig.decl, body, ) } diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index cd7af3be9b5ee..a74ea4bca39eb 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -786,14 +786,17 @@ impl<'a> ReplaceBodyWithLoop<'a> { false } } + + fn is_sig_const(sig: &ast::FnSig) -> bool { + sig.header.constness.node == ast::Constness::Const || Self::should_ignore_fn(&sig.decl) + } } impl<'a> MutVisitor for ReplaceBodyWithLoop<'a> { fn visit_item_kind(&mut self, i: &mut ast::ItemKind) { let is_const = match i { ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => true, - ast::ItemKind::Fn(ref decl, ref header, _, _) => - header.constness.node == ast::Constness::Const || Self::should_ignore_fn(decl), + ast::ItemKind::Fn(ref sig, _, _) => Self::is_sig_const(sig), _ => false, }; self.run(is_const, |s| noop_visit_item_kind(i, s)) @@ -802,8 +805,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a> { fn flat_map_trait_item(&mut self, i: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> { let is_const = match i.kind { ast::TraitItemKind::Const(..) => true, - ast::TraitItemKind::Method(ast::FnSig { ref decl, ref header, .. }, _) => - header.constness.node == ast::Constness::Const || Self::should_ignore_fn(decl), + ast::TraitItemKind::Method(ref sig, _) => Self::is_sig_const(sig), _ => false, }; self.run(is_const, |s| noop_flat_map_trait_item(i, s)) @@ -812,8 +814,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a> { fn flat_map_impl_item(&mut self, i: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> { let is_const = match i.kind { ast::ImplItemKind::Const(..) => true, - ast::ImplItemKind::Method(ast::FnSig { ref decl, ref header, .. }, _) => - header.constness.node == ast::Constness::Const || Self::should_ignore_fn(decl), + ast::ImplItemKind::Method(ref sig, _) => Self::is_sig_const(sig), _ => false, }; self.run(is_const, |s| noop_flat_map_impl_item(i, s)) diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index d1a801b3006d7..6151fc58d894b 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -575,12 +575,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> { .note("only trait implementations may be annotated with default").emit(); } } - ItemKind::Fn(ref decl, ref header, ref generics, _) => { - self.visit_fn_header(header); - self.check_fn_decl(decl); + ItemKind::Fn(ref sig, ref generics, _) => { + self.visit_fn_header(&sig.header); + self.check_fn_decl(&sig.decl); // We currently do not permit const generics in `const fn`, as // this is tantamount to allowing compile-time dependent typing. - if header.constness.node == Constness::Const { + if sig.header.constness.node == Constness::Const { // Look for const generics and error if we find any. for param in &generics.params { match param.kind { diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index 58af4b817d267..a23684ea649cc 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -731,7 +731,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { match item.kind { ItemKind::TyAlias(_, ref generics) | ItemKind::OpaqueTy(_, ref generics) | - ItemKind::Fn(_, _, ref generics, _) => { + ItemKind::Fn(_, ref generics, _) => { self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| visit::walk_item(this, item)); } diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index 883d896456421..92c391fb4a338 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -1334,8 +1334,8 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> { ); } } - Fn(ref decl, ref header, ref ty_params, ref body) => { - self.process_fn(item, &decl, &header, ty_params, &body) + Fn(ref sig, ref ty_params, ref body) => { + self.process_fn(item, &sig.decl, &sig.header, ty_params, &body) } Static(ref typ, _, ref expr) => self.process_static_or_const_item(item, typ, expr), Const(ref typ, ref expr) => self.process_static_or_const_item(item, &typ, &expr), diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index a2f8837c58134..424d57c8fe7fa 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -180,7 +180,7 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> { pub fn get_item_data(&self, item: &ast::Item) -> Option { match item.kind { - ast::ItemKind::Fn(ref decl, .., ref generics, _) => { + ast::ItemKind::Fn(ref sig, .., ref generics, _) => { let qualname = format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id))); filter!(self.span_utils, item.ident.span); @@ -190,7 +190,7 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> { span: self.span_from_span(item.ident.span), name: item.ident.to_string(), qualname, - value: make_signature(decl, generics), + value: make_signature(&sig.decl, generics), parent: None, children: vec![], decl_id: None, diff --git a/src/librustc_save_analysis/sig.rs b/src/librustc_save_analysis/sig.rs index 887aeb03b8957..d1b9b8ff44ddb 100644 --- a/src/librustc_save_analysis/sig.rs +++ b/src/librustc_save_analysis/sig.rs @@ -376,7 +376,7 @@ impl Sig for ast::Item { Ok(extend_sig(ty, text, defs, vec![])) } - ast::ItemKind::Fn(ref decl, header, ref generics, _) => { + ast::ItemKind::Fn(ast::FnSig { ref decl, header }, ref generics, _) => { let mut text = String::new(); if header.constness.node == ast::Constness::Const { text.push_str("const "); diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 86a353cdfd2f6..b57d223899184 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -2433,7 +2433,7 @@ pub enum ItemKind { /// A function declaration (`fn`). /// /// E.g., `fn foo(bar: usize) -> usize { .. }`. - Fn(P, FnHeader, Generics, P), + Fn(FnSig, Generics, P), /// A module declaration (`mod`). /// /// E.g., `mod foo;` or `mod foo { .. }`. diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index f2ad7dea73b2b..7696ea48f9338 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -357,7 +357,7 @@ pub fn visit_bounds(bounds: &mut GenericBounds, vis: &mut T) { } // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`. -pub fn visit_method_sig(FnSig { header, decl }: &mut FnSig, vis: &mut T) { +pub fn visit_fn_sig(FnSig { header, decl }: &mut FnSig, vis: &mut T) { vis.visit_fn_header(header); vis.visit_fn_decl(decl); } @@ -878,9 +878,8 @@ pub fn noop_visit_item_kind(kind: &mut ItemKind, vis: &mut T) { vis.visit_ty(ty); vis.visit_expr(expr); } - ItemKind::Fn(decl, header, generics, body) => { - vis.visit_fn_decl(decl); - vis.visit_fn_header(header); + ItemKind::Fn(sig, generics, body) => { + visit_fn_sig(sig, vis); vis.visit_generics(generics); vis.visit_block(body); } @@ -938,7 +937,7 @@ pub fn noop_flat_map_trait_item(mut item: TraitItem, vis: &mut T) visit_opt(default, |default| vis.visit_expr(default)); } TraitItemKind::Method(sig, body) => { - visit_method_sig(sig, vis); + visit_fn_sig(sig, vis); visit_opt(body, |body| vis.visit_block(body)); } TraitItemKind::Type(bounds, default) => { @@ -970,7 +969,7 @@ pub fn noop_flat_map_impl_item(mut item: ImplItem, visitor: &mut visitor.visit_expr(expr); } ImplItemKind::Method(sig, body) => { - visit_method_sig(sig, visitor); + visit_fn_sig(sig, visitor); visitor.visit_block(body); } ImplItemKind::TyAlias(ty) => visitor.visit_ty(ty), diff --git a/src/libsyntax/parse/parser/item.rs b/src/libsyntax/parse/parser/item.rs index 500e70ae8e5c8..531ad532a54dc 100644 --- a/src/libsyntax/parse/parser/item.rs +++ b/src/libsyntax/parse/parser/item.rs @@ -1800,7 +1800,7 @@ impl<'a> Parser<'a> { is_name_required: |_| true, })?; let (inner_attrs, body) = self.parse_inner_attrs_and_block()?; - let kind = ItemKind::Fn(decl, header, generics, body); + let kind = ItemKind::Fn(FnSig { decl, header }, generics, body); self.mk_item_with_info(attrs, lo, vis, (ident, kind, Some(inner_attrs))) } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index e7335a00cb088..2203e8d9d0637 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1199,11 +1199,11 @@ impl<'a> State<'a> { self.s.word(";"); self.end(); // end the outer cbox } - ast::ItemKind::Fn(ref decl, header, ref param_names, ref body) => { + ast::ItemKind::Fn(ref sig, ref param_names, ref body) => { self.head(""); self.print_fn( - decl, - header, + &sig.decl, + sig.header, Some(item.ident), param_names, &item.vis diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index e2983db4318e4..ea2dc357e6ebf 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -244,12 +244,11 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { visitor.visit_ty(typ); visitor.visit_expr(expr); } - ItemKind::Fn(ref declaration, ref header, ref generics, ref body) => { + ItemKind::Fn(ref sig, ref generics, ref body) => { visitor.visit_generics(generics); - visitor.visit_fn_header(header); - visitor.visit_fn(FnKind::ItemFn(item.ident, header, - &item.vis, body), - declaration, + visitor.visit_fn_header(&sig.header); + visitor.visit_fn(FnKind::ItemFn(item.ident, &sig.header, &item.vis, body), + &sig.decl, item.span, item.id) } diff --git a/src/libsyntax_ext/global_allocator.rs b/src/libsyntax_ext/global_allocator.rs index 90d2ea38bc336..dc29e057455d1 100644 --- a/src/libsyntax_ext/global_allocator.rs +++ b/src/libsyntax_ext/global_allocator.rs @@ -1,7 +1,7 @@ use crate::util::check_builtin_macro_attribute; use syntax::ast::{ItemKind, Mutability, Stmt, Ty, TyKind, Unsafety}; -use syntax::ast::{self, Param, Attribute, Expr, FnHeader, Generics, Ident}; +use syntax::ast::{self, Param, Attribute, Expr, FnSig, FnHeader, Generics, Ident}; use syntax::expand::allocator::{AllocatorKind, AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS}; use syntax::ptr::P; use syntax::symbol::{kw, sym, Symbol}; @@ -73,15 +73,10 @@ impl AllocFnFactory<'_, '_> { .collect(); let result = self.call_allocator(method.name, args); let (output_ty, output_expr) = self.ret_ty(&method.output, result); - let kind = ItemKind::Fn( - self.cx.fn_decl(abi_args, ast::FunctionRetTy::Ty(output_ty)), - FnHeader { - unsafety: Unsafety::Unsafe, - ..FnHeader::default() - }, - Generics::default(), - self.cx.block_expr(output_expr), - ); + let decl = self.cx.fn_decl(abi_args, ast::FunctionRetTy::Ty(output_ty)); + let header = FnHeader { unsafety: Unsafety::Unsafe, ..FnHeader::default() }; + let sig = FnSig { decl, header }; + let kind = ItemKind::Fn(sig, Generics::default(), self.cx.block_expr(output_expr)); let item = self.cx.item( self.span, self.cx.ident_of(&self.kind.fn_name(method.name), self.span), diff --git a/src/libsyntax_ext/test.rs b/src/libsyntax_ext/test.rs index b0da413d63a04..8656100c92127 100644 --- a/src/libsyntax_ext/test.rs +++ b/src/libsyntax_ext/test.rs @@ -310,15 +310,15 @@ fn test_type(cx: &ExtCtxt<'_>) -> TestType { fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool { let has_should_panic_attr = attr::contains_name(&i.attrs, sym::should_panic); let ref sd = cx.parse_sess.span_diagnostic; - if let ast::ItemKind::Fn(ref decl, ref header, ref generics, _) = i.kind { - if header.unsafety == ast::Unsafety::Unsafe { + if let ast::ItemKind::Fn(ref sig, ref generics, _) = i.kind { + if sig.header.unsafety == ast::Unsafety::Unsafe { sd.span_err( i.span, "unsafe functions cannot be used for tests" ); return false } - if header.asyncness.node.is_async() { + if sig.header.asyncness.node.is_async() { sd.span_err( i.span, "async functions cannot be used for tests" @@ -329,13 +329,13 @@ fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool { // If the termination trait is active, the compiler will check that the output // type implements the `Termination` trait as `libtest` enforces that. - let has_output = match decl.output { + let has_output = match sig.decl.output { ast::FunctionRetTy::Default(..) => false, ast::FunctionRetTy::Ty(ref t) if t.kind.is_unit() => false, _ => true }; - if !decl.inputs.is_empty() { + if !sig.decl.inputs.is_empty() { sd.span_err(i.span, "functions used as tests can not have any arguments"); return false; } @@ -361,10 +361,10 @@ fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool { } fn has_bench_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool { - let has_sig = if let ast::ItemKind::Fn(ref decl, _, _, _) = i.kind { + let has_sig = if let ast::ItemKind::Fn(ref sig, _, _) = i.kind { // N.B., inadequate check, but we're running // well before resolve, can't get too deep. - decl.inputs.len() == 1 + sig.decl.inputs.len() == 1 } else { false }; diff --git a/src/libsyntax_ext/test_harness.rs b/src/libsyntax_ext/test_harness.rs index 33d41a7f53e5e..1492f6f575ff7 100644 --- a/src/libsyntax_ext/test_harness.rs +++ b/src/libsyntax_ext/test_harness.rs @@ -306,10 +306,9 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P { ecx.block(sp, vec![call_test_main]) }; - let main = ast::ItemKind::Fn(ecx.fn_decl(vec![], ast::FunctionRetTy::Ty(main_ret_ty)), - ast::FnHeader::default(), - ast::Generics::default(), - main_body); + let decl = ecx.fn_decl(vec![], ast::FunctionRetTy::Ty(main_ret_ty)); + let sig = ast::FnSig { decl, header: ast::FnHeader::default() }; + let main = ast::ItemKind::Fn(sig, ast::Generics::default(), main_body); // Honor the reexport_test_harness_main attribute let main_id = match cx.reexport_test_harness_main {