Skip to content

Commit 2839307

Browse files
authored
Rollup merge of #136164 - celinval:chores-fnkind, r=oli-obk
Refactor FnKind variant to hold &Fn Pulling the change suggested in #128045 to reduce the impact of changing `Fn` item. r? `@oli-obk`
2 parents 7e123e4 + c22a271 commit 2839307

File tree

12 files changed

+93
-91
lines changed

12 files changed

+93
-91
lines changed

compiler/rustc_ast/src/mut_visit.rs

+14-22
Original file line numberDiff line numberDiff line change
@@ -954,8 +954,14 @@ 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 { defaultness, generics, body, sig: FnSig { header, decl, span } },
962+
) => {
958963
// Identifier and visibility are visited as a part of the item.
964+
visit_defaultness(vis, defaultness);
959965
vis.visit_fn_header(header);
960966
vis.visit_generics(generics);
961967
vis.visit_fn_decl(decl);
@@ -1205,13 +1211,8 @@ impl WalkItemKind for ItemKind {
12051211
ItemKind::Const(item) => {
12061212
visit_const_item(item, vis);
12071213
}
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-
);
1214+
ItemKind::Fn(func) => {
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,9 @@ 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) => {
13341334
visitor.visit_fn(
1335-
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, visibility, generics, body),
1335+
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, visibility, &mut *func),
13361336
span,
13371337
id,
13381338
);
@@ -1476,10 +1476,9 @@ impl WalkItemKind for ForeignItemKind {
14761476
visitor.visit_ty(ty);
14771477
visit_opt(expr, |expr| visitor.visit_expr(expr));
14781478
}
1479-
ForeignItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
1480-
visit_defaultness(visitor, defaultness);
1479+
ForeignItemKind::Fn(func) => {
14811480
visitor.visit_fn(
1482-
FnKind::Fn(FnCtxt::Foreign, ident, sig, visibility, generics, body),
1481+
FnKind::Fn(FnCtxt::Foreign, ident, visibility, &mut *func),
14831482
span,
14841483
id,
14851484
);
@@ -1965,14 +1964,7 @@ impl<N: DummyAstNode, T: DummyAstNode> DummyAstNode for crate::ast_traits::AstNo
19651964
#[derive(Debug)]
19661965
pub enum FnKind<'a> {
19671966
/// 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-
),
1967+
Fn(FnCtxt, &'a mut Ident, &'a mut Visibility, &'a mut Fn),
19761968

19771969
/// E.g., `|x, y| body`.
19781970
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 { defaultness: _, 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

+11-11
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ 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 }) => {
920+
ItemKind::Fn(func @ box Fn { defaultness, generics: _, sig, body }) => {
921921
self.check_defaultness(item.span, *defaultness);
922922

923923
let is_intrinsic =
@@ -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

+9-19
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,19 +645,17 @@ 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
) {
653+
let ast::Fn { defaultness, generics, sig, body } = func;
664654
if body.is_some() {
665655
self.head("");
666656
}
667657
self.print_visibility(vis);
668-
self.print_defaultness(defaultness);
658+
self.print_defaultness(*defaultness);
669659
self.print_fn(&sig.decl, sig.header, Some(name), generics);
670660
if let Some(body) = body {
671661
self.nbsp();

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_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;

compiler/rustc_resolve/src/late/diagnostics.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
224224
let suggestion = if self.current_trait_ref.is_none()
225225
&& let Some((fn_kind, _)) = self.diag_metadata.current_function
226226
&& let Some(FnCtxt::Assoc(_)) = fn_kind.ctxt()
227-
&& let FnKind::Fn(_, _, sig, ..) = fn_kind
227+
&& let FnKind::Fn(_, _, _, ast::Fn { sig, .. }) = fn_kind
228228
&& let Some(items) = self.diag_metadata.current_impl_items
229229
&& let Some(item) = items.iter().find(|i| {
230230
i.ident.name == item_str.name
@@ -560,7 +560,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
560560
Applicability::MaybeIncorrect,
561561
);
562562
if !self.self_value_is_available(path[0].ident.span) {
563-
if let Some((FnKind::Fn(_, _, sig, ..), fn_span)) =
563+
if let Some((FnKind::Fn(_, _, _, ast::Fn { sig, .. }), fn_span)) =
564564
&self.diag_metadata.current_function
565565
{
566566
let (span, sugg) = if let Some(param) = sig.decl.inputs.get(0) {
@@ -3249,7 +3249,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32493249
{
32503250
let pre = if lt.kind == MissingLifetimeKind::Ampersand
32513251
&& let Some((kind, _span)) = self.diag_metadata.current_function
3252-
&& let FnKind::Fn(_, _, sig, _, _, _) = kind
3252+
&& let FnKind::Fn(_, _, _, ast::Fn { sig, .. }) = kind
32533253
&& !sig.decl.inputs.is_empty()
32543254
&& let sugg = sig
32553255
.decl
@@ -3290,7 +3290,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32903290
} else if (lt.kind == MissingLifetimeKind::Ampersand
32913291
|| lt.kind == MissingLifetimeKind::Underscore)
32923292
&& let Some((kind, _span)) = self.diag_metadata.current_function
3293-
&& let FnKind::Fn(_, _, sig, _, _, _) = kind
3293+
&& let FnKind::Fn(_, _, _, ast::Fn { sig, .. }) = kind
32943294
&& let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output
32953295
&& !sig.decl.inputs.is_empty()
32963296
&& let arg_refs = sig
@@ -3350,7 +3350,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
33503350
let mut owned_sugg = lt.kind == MissingLifetimeKind::Ampersand;
33513351
let mut sugg = vec![(lt.span, String::new())];
33523352
if let Some((kind, _span)) = self.diag_metadata.current_function
3353-
&& let FnKind::Fn(_, _, sig, _, _, _) = kind
3353+
&& let FnKind::Fn(_, _, _, ast::Fn { sig, .. }) = kind
33543354
&& let ast::FnRetTy::Ty(ty) = &sig.decl.output
33553355
{
33563356
let mut lt_finder =

0 commit comments

Comments
 (0)