Skip to content

Commit 3438131

Browse files
committed
Unify FnKind
1 parent 40980e3 commit 3438131

File tree

2 files changed

+53
-54
lines changed

2 files changed

+53
-54
lines changed

compiler/rustc_ast/src/visitors.rs

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,28 @@ macro_rules! make_ast_visitor {
151151
};
152152
}
153153

154+
macro_rules! fn_kind_derives {
155+
($i: item) => {
156+
macro_if!{$($mut)? {
157+
#[derive(Debug)]
158+
$i
159+
} else {
160+
#[derive(Debug, Copy, Clone)]
161+
$i
162+
}}
163+
}
164+
}
165+
166+
fn_kind_derives!{
167+
pub enum FnKind<'a> {
168+
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
169+
Fn(FnCtxt, &'a $($mut)? Ident, &'a $($mut)? FnSig, &'a $($mut)? Visibility, &'a $($mut)? Generics, &'a $($mut)? Option<P<Block>>),
170+
171+
/// E.g., `|x, y| body`.
172+
Closure(&'a $($mut)? ClosureBinder, &'a $($mut)? Option<CoroutineKind>, &'a $($mut)? P!(FnDecl), &'a $($mut)? P!(Expr)),
173+
}
174+
}
175+
154176
/// Each method of the traits `Visitor` and `MutVisitor` trait is a hook
155177
/// to be potentially overridden. Each method's default implementation
156178
/// recursively visits the substructure of the input via the corresponding
@@ -1291,15 +1313,6 @@ pub mod visit {
12911313
}
12921314
}
12931315

1294-
#[derive(Copy, Clone, Debug)]
1295-
pub enum FnKind<'a> {
1296-
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
1297-
Fn(FnCtxt, &'a Ident, &'a FnSig, &'a Visibility, &'a Generics, Option<&'a Block>),
1298-
1299-
/// E.g., `|x, y| body`.
1300-
Closure(&'a ClosureBinder, &'a Option<CoroutineKind>, &'a FnDecl, &'a Expr),
1301-
}
1302-
13031316
impl<'a> FnKind<'a> {
13041317
pub fn header(&self) -> Option<&'a FnHeader> {
13051318
match *self {
@@ -1364,7 +1377,7 @@ pub mod visit {
13641377
visit_opt!(visitor, visit_expr, expr);
13651378
}
13661379
ItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
1367-
let kind = FnKind::Fn(FnCtxt::Free, ident, sig, vis, generics, body.as_deref());
1380+
let kind = FnKind::Fn(FnCtxt::Free, ident, sig, vis, generics, body);
13681381
try_visit!(visitor.visit_fn(kind, span, id));
13691382
}
13701383
ItemKind::Mod(safety, mod_kind) => {
@@ -1475,8 +1488,7 @@ pub mod visit {
14751488
visit_opt!(visitor, visit_expr, expr);
14761489
}
14771490
ForeignItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
1478-
let kind =
1479-
FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, body.as_deref());
1491+
let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, body);
14801492
try_visit!(visitor.visit_fn(kind, span, id));
14811493
}
14821494
ForeignItemKind::TyAlias(box TyAlias {
@@ -1533,8 +1545,7 @@ pub mod visit {
15331545
visit_opt!(visitor, visit_expr, expr);
15341546
}
15351547
AssocItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
1536-
let kind =
1537-
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body.as_deref());
1548+
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body);
15381549
try_visit!(visitor.visit_fn(kind, *span, *id));
15391550
}
15401551
AssocItemKind::Type(box TyAlias {
@@ -1772,7 +1783,7 @@ pub mod mut_visit {
17721783
//! that are created by the expansion of a macro.
17731784
17741785
use super::*;
1775-
use crate::visit::{AssocCtxt, BoundKind, LifetimeCtxt};
1786+
use crate::visit::{AssocCtxt, BoundKind, FnCtxt, LifetimeCtxt};
17761787

17771788
pub trait ExpectOne<A: Array> {
17781789
fn expect_one(self, err: &'static str) -> A::Item;
@@ -2095,7 +2106,7 @@ pub mod mut_visit {
20952106

20962107
fn walk_fn<T: MutVisitor>(vis: &mut T, kind: FnKind<'_>) {
20972108
match kind {
2098-
FnKind::Fn(FnSig { header, decl, span }, generics, body) => {
2109+
FnKind::Fn(_, _, FnSig { header, decl, span }, _, generics, body) => {
20992110
// Identifier and visibility are visited as a part of the item.
21002111
vis.visit_fn_header(header);
21012112
vis.visit_generics(generics);
@@ -2105,7 +2116,8 @@ pub mod mut_visit {
21052116
}
21062117
vis.visit_span(span);
21072118
}
2108-
FnKind::Closure(binder, decl, body) => {
2119+
FnKind::Closure(binder, coroutine_kind, decl, body) => {
2120+
coroutine_kind.as_mut().map(|ck| vis.visit_coroutine_kind(ck));
21092121
vis.visit_closure_binder(binder);
21102122
vis.visit_fn_decl(decl);
21112123
vis.visit_expr(body);
@@ -2146,8 +2158,8 @@ pub mod mut_visit {
21462158
&mut self,
21472159
id: NodeId,
21482160
span: Span,
2149-
_vis: &mut Visibility,
2150-
_ident: &mut Ident,
2161+
vis: &mut Visibility,
2162+
ident: &mut Ident,
21512163
visitor: &mut V,
21522164
) {
21532165
match self {
@@ -2162,7 +2174,11 @@ pub mod mut_visit {
21622174
}
21632175
ItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
21642176
visit_defaultness(visitor, defaultness);
2165-
visitor.visit_fn(FnKind::Fn(sig, generics, body), span, id);
2177+
visitor.visit_fn(
2178+
FnKind::Fn(FnCtxt::Free, ident, sig, vis, generics, body),
2179+
span,
2180+
id,
2181+
);
21662182
}
21672183
ItemKind::Mod(safety, mod_kind) => {
21682184
visitor.visit_safety(safety);
@@ -2313,7 +2329,7 @@ pub mod mut_visit {
23132329
pub fn walk_flat_map_assoc_item(
23142330
visitor: &mut impl MutVisitor,
23152331
mut item: P<AssocItem>,
2316-
_ctxt: AssocCtxt,
2332+
ctxt: AssocCtxt,
23172333
) -> SmallVec<[P<AssocItem>; 1]> {
23182334
let Item { ident, attrs, id, kind, vis, span, tokens } = item.deref_mut();
23192335
visitor.visit_id(id);
@@ -2326,7 +2342,11 @@ pub mod mut_visit {
23262342
}
23272343
AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
23282344
visit_defaultness(visitor, defaultness);
2329-
visitor.visit_fn(FnKind::Fn(sig, generics, body), *span, *id);
2345+
visitor.visit_fn(
2346+
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body),
2347+
*span,
2348+
*id,
2349+
);
23302350
}
23312351
AssocItemKind::Type(box TyAlias {
23322352
defaultness,
@@ -2386,8 +2406,8 @@ pub mod mut_visit {
23862406
&mut self,
23872407
id: NodeId,
23882408
span: Span,
2389-
_vis: &mut Visibility,
2390-
_ident: &mut Ident,
2409+
vis: &mut Visibility,
2410+
ident: &mut Ident,
23912411
visitor: &mut V,
23922412
) {
23932413
match self {
@@ -2397,7 +2417,11 @@ pub mod mut_visit {
23972417
}
23982418
ForeignItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
23992419
visit_defaultness(visitor, defaultness);
2400-
visitor.visit_fn(FnKind::Fn(sig, generics, body), span, id);
2420+
visitor.visit_fn(
2421+
FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, body),
2422+
span,
2423+
id,
2424+
);
24012425
}
24022426
ForeignItemKind::TyAlias(box TyAlias {
24032427
defaultness,
@@ -2506,11 +2530,8 @@ pub mod mut_visit {
25062530
fn_arg_span,
25072531
}) => {
25082532
visit_constness(vis, constness);
2509-
coroutine_kind
2510-
.as_mut()
2511-
.map(|coroutine_kind| vis.visit_coroutine_kind(coroutine_kind));
25122533
vis.visit_capture_by(capture_clause);
2513-
vis.visit_fn(FnKind::Closure(binder, fn_decl, body), *span, *id);
2534+
vis.visit_fn(FnKind::Closure(binder, coroutine_kind, fn_decl, body), *span, *id);
25142535
vis.visit_span(fn_decl_span);
25152536
vis.visit_span(fn_arg_span);
25162537
}
@@ -2754,13 +2775,4 @@ pub mod mut_visit {
27542775
crate::ast_traits::AstNodeWrapper::new(N::dummy(), T::dummy())
27552776
}
27562777
}
2757-
2758-
#[derive(Debug)]
2759-
pub enum FnKind<'a> {
2760-
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
2761-
Fn(&'a mut FnSig, &'a mut Generics, &'a mut Option<P<Block>>),
2762-
2763-
/// E.g., `|x, y| body`.
2764-
Closure(&'a mut ClosureBinder, &'a mut P<FnDecl>, &'a mut P<Expr>),
2765-
}
27662778
}

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -989,14 +989,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
989989

990990
self.visit_vis(&item.vis);
991991
self.visit_ident(&item.ident);
992-
let kind = FnKind::Fn(
993-
FnCtxt::Free,
994-
&item.ident,
995-
sig,
996-
&item.vis,
997-
generics,
998-
body.as_deref(),
999-
);
992+
let kind = FnKind::Fn(FnCtxt::Free, &item.ident, sig, &item.vis, generics, body);
1000993
self.visit_fn(kind, item.span, item.id);
1001994
walk_list!(self, visit_attribute, &item.attrs);
1002995
return; // Avoid visiting again.
@@ -1525,14 +1518,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15251518
{
15261519
self.visit_vis(&item.vis);
15271520
self.visit_ident(&item.ident);
1528-
let kind = FnKind::Fn(
1529-
FnCtxt::Assoc(ctxt),
1530-
&item.ident,
1531-
sig,
1532-
&item.vis,
1533-
generics,
1534-
body.as_deref(),
1535-
);
1521+
let kind =
1522+
FnKind::Fn(FnCtxt::Assoc(ctxt), &item.ident, sig, &item.vis, generics, body);
15361523
walk_list!(self, visit_attribute, &item.attrs);
15371524
self.visit_fn(kind, item.span, item.id);
15381525
}

0 commit comments

Comments
 (0)