Skip to content

Commit

Permalink
ast::ItemKind::Fn: use ast::FnSig
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Nov 8, 2019
1 parent 2cd48e8 commit b4c6abc
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/librustc/hir/lowering/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 4 additions & 9 deletions src/librustc/hir/map/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
)
}
Expand Down
13 changes: 7 additions & 6 deletions src/librustc_interface/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_save_analysis/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {

pub fn get_item_data(&self, item: &ast::Item) -> Option<Data> {
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);
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_save_analysis/sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ");
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2433,7 +2433,7 @@ pub enum ItemKind {
/// A function declaration (`fn`).
///
/// E.g., `fn foo(bar: usize) -> usize { .. }`.
Fn(P<FnDecl>, FnHeader, Generics, P<Block>),
Fn(FnSig, Generics, P<Block>),
/// A module declaration (`mod`).
///
/// E.g., `mod foo;` or `mod foo { .. }`.
Expand Down
11 changes: 5 additions & 6 deletions src/libsyntax/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ pub fn visit_bounds<T: MutVisitor>(bounds: &mut GenericBounds, vis: &mut T) {
}

// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
pub fn visit_method_sig<T: MutVisitor>(FnSig { header, decl }: &mut FnSig, vis: &mut T) {
pub fn visit_fn_sig<T: MutVisitor>(FnSig { header, decl }: &mut FnSig, vis: &mut T) {
vis.visit_fn_header(header);
vis.visit_fn_decl(decl);
}
Expand Down Expand Up @@ -878,9 +878,8 @@ pub fn noop_visit_item_kind<T: MutVisitor>(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);
}
Expand Down Expand Up @@ -938,7 +937,7 @@ pub fn noop_flat_map_trait_item<T: MutVisitor>(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) => {
Expand Down Expand Up @@ -970,7 +969,7 @@ pub fn noop_flat_map_impl_item<T: MutVisitor>(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),
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}

Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
15 changes: 5 additions & 10 deletions src/libsyntax_ext/global_allocator.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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),
Expand Down
14 changes: 7 additions & 7 deletions src/libsyntax_ext/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
}
Expand All @@ -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
};
Expand Down
7 changes: 3 additions & 4 deletions src/libsyntax_ext/test_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,9 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
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 {
Expand Down

0 comments on commit b4c6abc

Please sign in to comment.