Skip to content

Commit e47f66d

Browse files
committed
Visit generics inside visit_fn.
1 parent aa2b5ef commit e47f66d

File tree

8 files changed

+65
-40
lines changed

8 files changed

+65
-40
lines changed

compiler/rustc_ast/src/visit.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub enum FnCtxt {
3535
#[derive(Copy, Clone, Debug)]
3636
pub enum FnKind<'a> {
3737
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
38-
Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, Option<&'a Block>),
38+
Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, &'a Generics, Option<&'a Block>),
3939

4040
/// E.g., `|x, y| body`.
4141
Closure(&'a FnDecl, &'a Expr),
@@ -44,7 +44,7 @@ pub enum FnKind<'a> {
4444
impl<'a> FnKind<'a> {
4545
pub fn header(&self) -> Option<&'a FnHeader> {
4646
match *self {
47-
FnKind::Fn(_, _, sig, _, _) => Some(&sig.header),
47+
FnKind::Fn(_, _, sig, _, _, _) => Some(&sig.header),
4848
FnKind::Closure(_, _) => None,
4949
}
5050
}
@@ -58,7 +58,7 @@ impl<'a> FnKind<'a> {
5858

5959
pub fn decl(&self) -> &'a FnDecl {
6060
match self {
61-
FnKind::Fn(_, _, sig, _, _) => &sig.decl,
61+
FnKind::Fn(_, _, sig, _, _, _) => &sig.decl,
6262
FnKind::Closure(decl, _) => decl,
6363
}
6464
}
@@ -295,8 +295,8 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
295295
walk_list!(visitor, visit_expr, expr);
296296
}
297297
ItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
298-
visitor.visit_generics(generics);
299-
let kind = FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, body.as_deref());
298+
let kind =
299+
FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, generics, body.as_deref());
300300
visitor.visit_fn(kind, item.span, item.id)
301301
}
302302
ItemKind::Mod(_unsafety, ref mod_kind) => match mod_kind {
@@ -561,8 +561,7 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI
561561
walk_list!(visitor, visit_expr, expr);
562562
}
563563
ForeignItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
564-
visitor.visit_generics(generics);
565-
let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, body.as_deref());
564+
let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, body.as_deref());
566565
visitor.visit_fn(kind, span, id);
567566
}
568567
ForeignItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
@@ -644,7 +643,8 @@ pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &
644643

645644
pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>, _span: Span) {
646645
match kind {
647-
FnKind::Fn(_, _, sig, _, body) => {
646+
FnKind::Fn(_, _, sig, _, generics, body) => {
647+
visitor.visit_generics(generics);
648648
visitor.visit_fn_header(&sig.header);
649649
walk_fn_decl(visitor, &sig.decl);
650650
walk_list!(visitor, visit_block, body);
@@ -667,8 +667,7 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
667667
walk_list!(visitor, visit_expr, expr);
668668
}
669669
AssocItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
670-
visitor.visit_generics(generics);
671-
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, body.as_deref());
670+
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body.as_deref());
672671
visitor.visit_fn(kind, span, id);
673672
}
674673
AssocItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,18 @@ impl<'a> AstValidator<'a> {
9191
self.is_impl_trait_banned = old;
9292
}
9393

94-
fn with_tilde_const_allowed(&mut self, f: impl FnOnce(&mut Self)) {
95-
let old = mem::replace(&mut self.is_tilde_const_allowed, true);
94+
fn with_tilde_const(&mut self, allowed: bool, f: impl FnOnce(&mut Self)) {
95+
let old = mem::replace(&mut self.is_tilde_const_allowed, allowed);
9696
f(self);
9797
self.is_tilde_const_allowed = old;
9898
}
9999

100+
fn with_tilde_const_allowed(&mut self, f: impl FnOnce(&mut Self)) {
101+
self.with_tilde_const(true, f)
102+
}
103+
100104
fn with_banned_tilde_const(&mut self, f: impl FnOnce(&mut Self)) {
101-
let old = mem::replace(&mut self.is_tilde_const_allowed, false);
102-
f(self);
103-
self.is_tilde_const_allowed = old;
105+
self.with_tilde_const(false, f)
104106
}
105107

106108
fn with_let_management(
@@ -1202,12 +1204,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12021204
}
12031205
self.visit_vis(&item.vis);
12041206
self.visit_ident(item.ident);
1205-
if let Const::Yes(_) = sig.header.constness {
1206-
self.with_tilde_const_allowed(|this| this.visit_generics(generics));
1207-
} else {
1208-
self.visit_generics(generics);
1209-
}
1210-
let kind = FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, body.as_deref());
1207+
let kind =
1208+
FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, generics, body.as_deref());
12111209
self.visit_fn(kind, item.span, item.id);
12121210
walk_list!(self, visit_attribute, &item.attrs);
12131211
return; // Avoid visiting again.
@@ -1555,13 +1553,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15551553
FnSig { span: sig_span, header: FnHeader { ext: Extern::Implicit, .. }, .. },
15561554
_,
15571555
_,
1556+
_,
15581557
) = fk
15591558
{
15601559
self.maybe_lint_missing_abi(*sig_span, id);
15611560
}
15621561

15631562
// Functions without bodies cannot have patterns.
1564-
if let FnKind::Fn(ctxt, _, sig, _, None) = fk {
1563+
if let FnKind::Fn(ctxt, _, sig, _, _, None) = fk {
15651564
Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| {
15661565
let (code, msg, label) = match ctxt {
15671566
FnCtxt::Foreign => (
@@ -1596,7 +1595,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15961595
});
15971596
}
15981597

1599-
visit::walk_fn(self, fk, span);
1598+
let tilde_const_allowed =
1599+
matches!(fk.header(), Some(FnHeader { constness: Const::Yes(_), .. }))
1600+
|| matches!(fk.ctxt(), Some(FnCtxt::Assoc(_)));
1601+
1602+
self.with_tilde_const(tilde_const_allowed, |this| visit::walk_fn(this, fk, span));
16001603
}
16011604

16021605
fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
@@ -1670,9 +1673,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
16701673
{
16711674
self.visit_vis(&item.vis);
16721675
self.visit_ident(item.ident);
1673-
self.with_tilde_const_allowed(|this| this.visit_generics(generics));
1674-
let kind =
1675-
FnKind::Fn(FnCtxt::Assoc(ctxt), item.ident, sig, &item.vis, body.as_deref());
1676+
let kind = FnKind::Fn(
1677+
FnCtxt::Assoc(ctxt),
1678+
item.ident,
1679+
sig,
1680+
&item.vis,
1681+
generics,
1682+
body.as_deref(),
1683+
);
16761684
self.visit_fn(kind, item.span, item.id);
16771685
}
16781686
_ => self

compiler/rustc_lint/src/builtin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ impl EarlyLintPass for UnsafeCode {
441441
_,
442442
ast::FnSig { header: ast::FnHeader { unsafety: ast::Unsafe::Yes(_), .. }, .. },
443443
_,
444+
_,
444445
body,
445446
) = fk
446447
{

compiler/rustc_lint/src/early.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
158158

159159
// Explicitly check for lints associated with 'closure_id', since
160160
// it does not have a corresponding AST node
161-
if let ast_visit::FnKind::Fn(_, _, sig, _, _) = fk {
161+
if let ast_visit::FnKind::Fn(_, _, sig, _, _, _) = fk {
162162
if let ast::Async::Yes { closure_id, .. } = sig.header.asyncness {
163163
self.check_id(closure_id);
164164
}

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,10 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
133133
}
134134

135135
fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
136-
if let FnKind::Fn(_, _, sig, _, body) = fn_kind {
136+
if let FnKind::Fn(_, _, sig, _, generics, body) = fn_kind {
137137
if let Async::Yes { closure_id, return_impl_trait_id, .. } = sig.header.asyncness {
138+
self.visit_generics(generics);
139+
138140
let return_impl_trait_id =
139141
self.create_def(return_impl_trait_id, DefPathData::ImplTrait, span);
140142

compiler/rustc_resolve/src/late.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,12 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
537537
let rib_kind = match fn_kind {
538538
// Bail if the function is foreign, and thus cannot validly have
539539
// a body, or if there's no body for some other reason.
540-
FnKind::Fn(FnCtxt::Foreign, _, sig, ..) | FnKind::Fn(_, _, sig, .., None) => {
540+
FnKind::Fn(FnCtxt::Foreign, _, sig, _, generics, _)
541+
| FnKind::Fn(_, _, sig, _, generics, None) => {
541542
// We don't need to deal with patterns in parameters, because
542543
// they are not possible for foreign or bodiless functions.
543544
self.visit_fn_header(&sig.header);
545+
self.visit_generics(generics);
544546
visit::walk_fn_decl(self, &sig.decl);
545547
return;
546548
}
@@ -559,6 +561,10 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
559561
self.with_rib(ValueNS, rib_kind, |this| {
560562
// Create a label rib for the function.
561563
this.with_label_rib(rib_kind, |this| {
564+
if let FnKind::Fn(_, _, _, _, generics, _) = fn_kind {
565+
this.visit_generics(generics);
566+
}
567+
562568
// Add each argument to the rib.
563569
this.resolve_params(&declaration.inputs);
564570

src/tools/rustfmt/src/items.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,11 @@ impl<'a> FnSig<'a> {
204204

205205
pub(crate) fn from_fn_kind(
206206
fn_kind: &'a visit::FnKind<'_>,
207-
generics: &'a ast::Generics,
208207
decl: &'a ast::FnDecl,
209208
defaultness: ast::Defaultness,
210209
) -> FnSig<'a> {
211210
match *fn_kind {
212-
visit::FnKind::Fn(fn_ctxt, _, fn_sig, vis, _) => match fn_ctxt {
211+
visit::FnKind::Fn(fn_ctxt, _, fn_sig, vis, generics, _) => match fn_ctxt {
213212
visit::FnCtxt::Assoc(..) => {
214213
let mut fn_sig = FnSig::from_method_sig(fn_sig, generics, vis);
215214
fn_sig.defaultness = defaultness;
@@ -3180,8 +3179,14 @@ impl Rewrite for ast::ForeignItem {
31803179
let inner_attrs = inner_attributes(&self.attrs);
31813180
let fn_ctxt = visit::FnCtxt::Foreign;
31823181
visitor.visit_fn(
3183-
visit::FnKind::Fn(fn_ctxt, self.ident, sig, &self.vis, Some(body)),
3184-
generics,
3182+
visit::FnKind::Fn(
3183+
fn_ctxt,
3184+
self.ident,
3185+
sig,
3186+
&self.vis,
3187+
generics,
3188+
Some(body),
3189+
),
31853190
&sig.decl,
31863191
self.span,
31873192
defaultness,

src/tools/rustfmt/src/visitor.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,6 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
382382
pub(crate) fn visit_fn(
383383
&mut self,
384384
fk: visit::FnKind<'_>,
385-
generics: &ast::Generics,
386385
fd: &ast::FnDecl,
387386
s: Span,
388387
defaultness: ast::Defaultness,
@@ -391,12 +390,12 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
391390
let indent = self.block_indent;
392391
let block;
393392
let rewrite = match fk {
394-
visit::FnKind::Fn(_, ident, _, _, Some(ref b)) => {
393+
visit::FnKind::Fn(_, ident, _, _, _, Some(ref b)) => {
395394
block = b;
396395
self.rewrite_fn_before_block(
397396
indent,
398397
ident,
399-
&FnSig::from_fn_kind(&fk, generics, fd, defaultness),
398+
&FnSig::from_fn_kind(&fk, fd, defaultness),
400399
mk_sp(s.lo(), b.span.lo()),
401400
)
402401
}
@@ -552,8 +551,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
552551
_ => visit::FnCtxt::Foreign,
553552
};
554553
self.visit_fn(
555-
visit::FnKind::Fn(fn_ctxt, item.ident, sig, &item.vis, Some(body)),
556-
generics,
554+
visit::FnKind::Fn(
555+
fn_ctxt,
556+
item.ident,
557+
sig,
558+
&item.vis,
559+
generics,
560+
Some(body),
561+
),
557562
&sig.decl,
558563
item.span,
559564
defaultness,
@@ -642,8 +647,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
642647
let inner_attrs = inner_attributes(&ai.attrs);
643648
let fn_ctxt = visit::FnCtxt::Assoc(assoc_ctxt);
644649
self.visit_fn(
645-
visit::FnKind::Fn(fn_ctxt, ai.ident, sig, &ai.vis, Some(body)),
646-
generics,
650+
visit::FnKind::Fn(fn_ctxt, ai.ident, sig, &ai.vis, generics, Some(body)),
647651
&sig.decl,
648652
ai.span,
649653
defaultness,

0 commit comments

Comments
 (0)