Skip to content

Commit a9da326

Browse files
committed
Refactor FnKind variant to hold &Fn
1 parent 2f348cb commit a9da326

File tree

13 files changed

+104
-98
lines changed

13 files changed

+104
-98
lines changed

compiler/rustc_ast/src/mut_visit.rs

+16-22
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,12 @@ fn walk_coroutine_kind<T: MutVisitor>(vis: &mut T, coroutine_kind: &mut Coroutin
954954

955955
fn walk_fn<T: MutVisitor>(vis: &mut T, kind: FnKind<'_>) {
956956
match kind {
957-
FnKind::Fn(_ctxt, _ident, FnSig { header, decl, span }, _visibility, generics, body) => {
957+
FnKind::Fn(
958+
_ctxt,
959+
_ident,
960+
_vis,
961+
Fn { generics, body, sig: FnSig { header, decl, span }, .. },
962+
) => {
958963
// Identifier and visibility are visited as a part of the item.
959964
vis.visit_fn_header(header);
960965
vis.visit_generics(generics);
@@ -1205,13 +1210,9 @@ impl WalkItemKind for ItemKind {
12051210
ItemKind::Const(item) => {
12061211
visit_const_item(item, vis);
12071212
}
1208-
ItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
1209-
visit_defaultness(vis, defaultness);
1210-
vis.visit_fn(
1211-
FnKind::Fn(FnCtxt::Free, ident, sig, visibility, generics, body),
1212-
span,
1213-
id,
1214-
);
1213+
ItemKind::Fn(func) => {
1214+
visit_defaultness(vis, &mut func.defaultness);
1215+
vis.visit_fn(FnKind::Fn(FnCtxt::Free, ident, visibility, &mut *func), span, id);
12151216
}
12161217
ItemKind::Mod(safety, mod_kind) => {
12171218
visit_safety(vis, safety);
@@ -1329,10 +1330,10 @@ impl WalkItemKind for AssocItemKind {
13291330
AssocItemKind::Const(item) => {
13301331
visit_const_item(item, visitor);
13311332
}
1332-
AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
1333-
visit_defaultness(visitor, defaultness);
1333+
AssocItemKind::Fn(func) => {
1334+
visit_defaultness(visitor, &mut func.defaultness);
13341335
visitor.visit_fn(
1335-
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, visibility, generics, body),
1336+
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, visibility, &mut *func),
13361337
span,
13371338
id,
13381339
);
@@ -1476,10 +1477,10 @@ impl WalkItemKind for ForeignItemKind {
14761477
visitor.visit_ty(ty);
14771478
visit_opt(expr, |expr| visitor.visit_expr(expr));
14781479
}
1479-
ForeignItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
1480-
visit_defaultness(visitor, defaultness);
1480+
ForeignItemKind::Fn(func) => {
1481+
visit_defaultness(visitor, &mut func.defaultness);
14811482
visitor.visit_fn(
1482-
FnKind::Fn(FnCtxt::Foreign, ident, sig, visibility, generics, body),
1483+
FnKind::Fn(FnCtxt::Foreign, ident, visibility, &mut *func),
14831484
span,
14841485
id,
14851486
);
@@ -1965,14 +1966,7 @@ impl<N: DummyAstNode, T: DummyAstNode> DummyAstNode for crate::ast_traits::AstNo
19651966
#[derive(Debug)]
19661967
pub enum FnKind<'a> {
19671968
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
1968-
Fn(
1969-
FnCtxt,
1970-
&'a mut Ident,
1971-
&'a mut FnSig,
1972-
&'a mut Visibility,
1973-
&'a mut Generics,
1974-
&'a mut Option<P<Block>>,
1975-
),
1969+
Fn(FnCtxt, &'a mut Ident, &'a mut Visibility, &'a mut Fn),
19761970

19771971
/// E.g., `|x, y| body`.
19781972
Closure(

compiler/rustc_ast/src/visit.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl BoundKind {
6565
#[derive(Copy, Clone, Debug)]
6666
pub enum FnKind<'a> {
6767
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
68-
Fn(FnCtxt, &'a Ident, &'a FnSig, &'a Visibility, &'a Generics, &'a Option<P<Block>>),
68+
Fn(FnCtxt, &'a Ident, &'a Visibility, &'a Fn),
6969

7070
/// E.g., `|x, y| body`.
7171
Closure(&'a ClosureBinder, &'a Option<CoroutineKind>, &'a FnDecl, &'a Expr),
@@ -74,7 +74,7 @@ pub enum FnKind<'a> {
7474
impl<'a> FnKind<'a> {
7575
pub fn header(&self) -> Option<&'a FnHeader> {
7676
match *self {
77-
FnKind::Fn(_, _, sig, _, _, _) => Some(&sig.header),
77+
FnKind::Fn(_, _, _, Fn { sig, .. }) => Some(&sig.header),
7878
FnKind::Closure(..) => None,
7979
}
8080
}
@@ -88,7 +88,7 @@ impl<'a> FnKind<'a> {
8888

8989
pub fn decl(&self) -> &'a FnDecl {
9090
match self {
91-
FnKind::Fn(_, _, sig, _, _, _) => &sig.decl,
91+
FnKind::Fn(_, _, _, Fn { sig, .. }) => &sig.decl,
9292
FnKind::Closure(_, _, decl, _) => decl,
9393
}
9494
}
@@ -374,8 +374,8 @@ impl WalkItemKind for ItemKind {
374374
try_visit!(visitor.visit_ty(ty));
375375
visit_opt!(visitor, visit_expr, expr);
376376
}
377-
ItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
378-
let kind = FnKind::Fn(FnCtxt::Free, ident, sig, vis, generics, body);
377+
ItemKind::Fn(func) => {
378+
let kind = FnKind::Fn(FnCtxt::Free, ident, vis, &*func);
379379
try_visit!(visitor.visit_fn(kind, span, id));
380380
}
381381
ItemKind::Mod(_unsafety, mod_kind) => match mod_kind {
@@ -715,8 +715,8 @@ impl WalkItemKind for ForeignItemKind {
715715
try_visit!(visitor.visit_ty(ty));
716716
visit_opt!(visitor, visit_expr, expr);
717717
}
718-
ForeignItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
719-
let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, body);
718+
ForeignItemKind::Fn(func) => {
719+
let kind = FnKind::Fn(FnCtxt::Foreign, ident, vis, &*func);
720720
try_visit!(visitor.visit_fn(kind, span, id));
721721
}
722722
ForeignItemKind::TyAlias(box TyAlias {
@@ -858,7 +858,12 @@ pub fn walk_fn_decl<'a, V: Visitor<'a>>(
858858

859859
pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>) -> V::Result {
860860
match kind {
861-
FnKind::Fn(_ctxt, _ident, FnSig { header, decl, span: _ }, _vis, generics, body) => {
861+
FnKind::Fn(
862+
_ctxt,
863+
_ident,
864+
_vis,
865+
Fn { sig: FnSig { header, decl, span: _ }, generics, body, .. },
866+
) => {
862867
// Identifier and visibility are visited as a part of the item.
863868
try_visit!(visitor.visit_fn_header(header));
864869
try_visit!(visitor.visit_generics(generics));
@@ -892,8 +897,8 @@ impl WalkItemKind for AssocItemKind {
892897
try_visit!(visitor.visit_ty(ty));
893898
visit_opt!(visitor, visit_expr, expr);
894899
}
895-
AssocItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
896-
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body);
900+
AssocItemKind::Fn(func) => {
901+
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, vis, &*func);
897902
try_visit!(visitor.visit_fn(kind, span, id));
898903
}
899904
AssocItemKind::Type(box TyAlias {

compiler/rustc_ast_lowering/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2125,7 +2125,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
21252125
self.arena.alloc(self.expr_call_mut(span, e, args))
21262126
}
21272127

2128-
fn expr_call_lang_item_fn_mut(
2128+
pub(super) fn expr_call_lang_item_fn_mut(
21292129
&mut self,
21302130
span: Span,
21312131
lang_item: hir::LangItem,
@@ -2135,7 +2135,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
21352135
self.expr_call_mut(span, path, args)
21362136
}
21372137

2138-
fn expr_call_lang_item_fn(
2138+
pub(super) fn expr_call_lang_item_fn(
21392139
&mut self,
21402140
span: Span,
21412141
lang_item: hir::LangItem,

compiler/rustc_ast_passes/src/ast_validation.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -917,16 +917,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
917917
walk_list!(self, visit_attribute, &item.attrs);
918918
return; // Avoid visiting again.
919919
}
920-
ItemKind::Fn(box Fn { defaultness, sig, generics, body }) => {
921-
self.check_defaultness(item.span, *defaultness);
920+
ItemKind::Fn(func) => {
921+
self.check_defaultness(item.span, func.defaultness);
922922

923923
let is_intrinsic =
924924
item.attrs.iter().any(|a| a.name_or_empty() == sym::rustc_intrinsic);
925-
if body.is_none() && !is_intrinsic {
925+
if func.body.is_none() && !is_intrinsic {
926926
self.dcx().emit_err(errors::FnWithoutBody {
927927
span: item.span,
928928
replace_span: self.ending_semi_or_hi(item.span),
929-
extern_block_suggestion: match sig.header.ext {
929+
extern_block_suggestion: match func.sig.header.ext {
930930
Extern::None => None,
931931
Extern::Implicit(start_span) => {
932932
Some(errors::ExternBlockSuggestion::Implicit {
@@ -947,7 +947,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
947947

948948
self.visit_vis(&item.vis);
949949
self.visit_ident(&item.ident);
950-
let kind = FnKind::Fn(FnCtxt::Free, &item.ident, sig, &item.vis, generics, body);
950+
let kind = FnKind::Fn(FnCtxt::Free, &item.ident, &item.vis, &*func);
951951
self.visit_fn(kind, item.span, item.id);
952952
walk_list!(self, visit_attribute, &item.attrs);
953953
return; // Avoid visiting again.
@@ -1348,19 +1348,20 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13481348
}
13491349

13501350
if let FnKind::Fn(
1351-
_,
1352-
_,
1353-
FnSig { header: FnHeader { ext: Extern::Implicit(extern_span), .. }, .. },
13541351
_,
13551352
_,
13561353
_,
1354+
Fn {
1355+
sig: FnSig { header: FnHeader { ext: Extern::Implicit(extern_span), .. }, .. },
1356+
..
1357+
},
13571358
) = fk
13581359
{
13591360
self.maybe_lint_missing_abi(*extern_span, id);
13601361
}
13611362

13621363
// Functions without bodies cannot have patterns.
1363-
if let FnKind::Fn(ctxt, _, sig, _, _, None) = fk {
1364+
if let FnKind::Fn(ctxt, _, _, Fn { body: None, sig, .. }) = fk {
13641365
Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| {
13651366
if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) {
13661367
if let Some(ident) = ident {
@@ -1394,7 +1395,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13941395
.is_some();
13951396

13961397
let disallowed = (!tilde_const_allowed).then(|| match fk {
1397-
FnKind::Fn(_, ident, _, _, _, _) => TildeConstReason::Function { ident: ident.span },
1398+
FnKind::Fn(_, ident, _, _) => TildeConstReason::Function { ident: ident.span },
13981399
FnKind::Closure(..) => TildeConstReason::Closure,
13991400
});
14001401
self.with_tilde_const(disallowed, |this| visit::walk_fn(this, fk));
@@ -1470,15 +1471,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14701471
self.outer_trait_or_trait_impl.as_ref().and_then(TraitOrTraitImpl::constness).is_some();
14711472

14721473
match &item.kind {
1473-
AssocItemKind::Fn(box Fn { sig, generics, body, .. })
1474+
AssocItemKind::Fn(func)
14741475
if parent_is_const
14751476
|| ctxt == AssocCtxt::Trait
1476-
|| matches!(sig.header.constness, Const::Yes(_)) =>
1477+
|| matches!(func.sig.header.constness, Const::Yes(_)) =>
14771478
{
14781479
self.visit_vis(&item.vis);
14791480
self.visit_ident(&item.ident);
1480-
let kind =
1481-
FnKind::Fn(FnCtxt::Assoc(ctxt), &item.ident, sig, &item.vis, generics, body);
1481+
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), &item.ident, &item.vis, &*func);
14821482
walk_list!(self, visit_attribute, &item.attrs);
14831483
self.visit_fn(kind, item.span, item.id);
14841484
}

compiler/rustc_ast_pretty/src/pprust/state/item.rs

+11-22
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ impl<'a> State<'a> {
3434
self.maybe_print_comment(span.lo());
3535
self.print_outer_attributes(attrs);
3636
match kind {
37-
ast::ForeignItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
38-
self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs);
37+
ast::ForeignItemKind::Fn(func) => {
38+
self.print_fn_full(ident, vis, attrs, &*func);
3939
}
4040
ast::ForeignItemKind::Static(box ast::StaticItem { ty, mutability, expr, safety }) => {
4141
self.print_item_const(
@@ -199,16 +199,8 @@ impl<'a> State<'a> {
199199
*defaultness,
200200
);
201201
}
202-
ast::ItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
203-
self.print_fn_full(
204-
sig,
205-
item.ident,
206-
generics,
207-
&item.vis,
208-
*defaultness,
209-
body.as_deref(),
210-
&item.attrs,
211-
);
202+
ast::ItemKind::Fn(func) => {
203+
self.print_fn_full(item.ident, &item.vis, &item.attrs, &*func);
212204
}
213205
ast::ItemKind::Mod(safety, mod_kind) => {
214206
self.head(Self::to_string(|s| {
@@ -542,8 +534,8 @@ impl<'a> State<'a> {
542534
self.maybe_print_comment(span.lo());
543535
self.print_outer_attributes(attrs);
544536
match kind {
545-
ast::AssocItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
546-
self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs);
537+
ast::AssocItemKind::Fn(func) => {
538+
self.print_fn_full(ident, vis, attrs, &*func);
547539
}
548540
ast::AssocItemKind::Const(box ast::ConstItem { defaultness, generics, ty, expr }) => {
549541
self.print_item_const(
@@ -653,21 +645,18 @@ impl<'a> State<'a> {
653645

654646
fn print_fn_full(
655647
&mut self,
656-
sig: &ast::FnSig,
657648
name: Ident,
658-
generics: &ast::Generics,
659649
vis: &ast::Visibility,
660-
defaultness: ast::Defaultness,
661-
body: Option<&ast::Block>,
662650
attrs: &[ast::Attribute],
651+
func: &ast::Fn,
663652
) {
664-
if body.is_some() {
653+
if func.body.is_some() {
665654
self.head("");
666655
}
667656
self.print_visibility(vis);
668-
self.print_defaultness(defaultness);
669-
self.print_fn(&sig.decl, sig.header, Some(name), generics);
670-
if let Some(body) = body {
657+
self.print_defaultness(func.defaultness);
658+
self.print_fn(&func.sig.decl, func.sig.header, Some(name), &func.generics);
659+
if let Some(body) = &func.body {
671660
self.nbsp();
672661
self.print_block_with_attrs(body, attrs);
673662
} else {

compiler/rustc_lint/src/builtin.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,12 @@ impl EarlyLintPass for UnsafeCode {
330330
if let FnKind::Fn(
331331
ctxt,
332332
_,
333-
ast::FnSig { header: ast::FnHeader { safety: ast::Safety::Unsafe(_), .. }, .. },
334333
_,
335-
_,
336-
body,
334+
ast::Fn {
335+
sig: ast::FnSig { header: ast::FnHeader { safety: ast::Safety::Unsafe(_), .. }, .. },
336+
body,
337+
..
338+
},
337339
) = fk
338340
{
339341
let decorator = match ctxt {

compiler/rustc_parse/src/parser/item.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ impl<'a> Parser<'a> {
215215
// FUNCTION ITEM
216216
let (ident, sig, generics, body) =
217217
self.parse_fn(attrs, fn_parse_mode, lo, vis, case)?;
218-
(ident, ItemKind::Fn(Box::new(Fn { defaultness: def_(), sig, generics, body })))
218+
(
219+
ident,
220+
ItemKind::Fn(Box::new(Fn { defaultness: def_(), sig, generics, body })),
221+
)
219222
} else if self.eat_keyword(exp!(Extern)) {
220223
if self.eat_keyword(exp!(Crate)) {
221224
// EXTERN CRATE

compiler/rustc_resolve/src/def_collector.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,12 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
170170

171171
fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
172172
match fn_kind {
173-
FnKind::Fn(_ctxt, _ident, FnSig { header, decl, span: _ }, _vis, generics, body)
174-
if let Some(coroutine_kind) = header.coroutine_kind =>
175-
{
173+
FnKind::Fn(
174+
_ctxt,
175+
_ident,
176+
_vis,
177+
Fn { sig: FnSig { header, decl, span: _ }, generics, body, .. },
178+
) if let Some(coroutine_kind) = header.coroutine_kind => {
176179
self.visit_fn_header(header);
177180
self.visit_generics(generics);
178181

compiler/rustc_resolve/src/late.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,8 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r
986986
match fn_kind {
987987
// Bail if the function is foreign, and thus cannot validly have
988988
// a body, or if there's no body for some other reason.
989-
FnKind::Fn(FnCtxt::Foreign, _, sig, _, generics, _)
990-
| FnKind::Fn(_, _, sig, _, generics, None) => {
989+
FnKind::Fn(FnCtxt::Foreign, _, _, Fn { sig, generics, .. })
990+
| FnKind::Fn(_, _, _, Fn { sig, generics, body: None, .. }) => {
991991
self.visit_fn_header(&sig.header);
992992
self.visit_generics(generics);
993993
self.with_lifetime_rib(
@@ -1019,7 +1019,7 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r
10191019
// Create a label rib for the function.
10201020
this.with_label_rib(RibKind::FnOrCoroutine, |this| {
10211021
match fn_kind {
1022-
FnKind::Fn(_, _, sig, _, generics, body) => {
1022+
FnKind::Fn(_, _, _, Fn { sig, generics, body, .. }) => {
10231023
this.visit_generics(generics);
10241024

10251025
let declaration = &sig.decl;

0 commit comments

Comments
 (0)