Skip to content

Commit 75d7906

Browse files
committed
Visit generics inside visit_fn.
1 parent 65022af commit 75d7906

File tree

9 files changed

+57
-40
lines changed

9 files changed

+57
-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
}
@@ -286,8 +286,8 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
286286
walk_list!(visitor, visit_expr, expr);
287287
}
288288
ItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
289-
visitor.visit_generics(generics);
290-
let kind = FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, body.as_deref());
289+
let kind =
290+
FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, generics, body.as_deref());
291291
visitor.visit_fn(kind, item.span, item.id)
292292
}
293293
ItemKind::Mod(_unsafety, ref mod_kind) => match mod_kind {
@@ -554,8 +554,7 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI
554554
walk_list!(visitor, visit_expr, expr);
555555
}
556556
ForeignItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
557-
visitor.visit_generics(generics);
558-
let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, body.as_deref());
557+
let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, body.as_deref());
559558
visitor.visit_fn(kind, span, id);
560559
}
561560
ForeignItemKind::TyAlias(box TyAlias { defaultness: _, generics, bounds, ty }) => {
@@ -637,7 +636,8 @@ pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &
637636

638637
pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>, _span: Span) {
639638
match kind {
640-
FnKind::Fn(_, _, sig, _, body) => {
639+
FnKind::Fn(_, _, sig, _, generics, body) => {
640+
visitor.visit_generics(generics);
641641
visitor.visit_fn_header(&sig.header);
642642
walk_fn_decl(visitor, &sig.decl);
643643
walk_list!(visitor, visit_block, body);
@@ -660,8 +660,7 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
660660
walk_list!(visitor, visit_expr, expr);
661661
}
662662
AssocItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
663-
visitor.visit_generics(generics);
664-
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, body.as_deref());
663+
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body.as_deref());
665664
visitor.visit_fn(kind, span, id);
666665
}
667666
AssocItemKind::TyAlias(box TyAlias { defaultness: _, generics, bounds, ty }) => {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
6464

6565
fn visit_fn(&mut self, fk: FnKind<'a>, sp: Span, _: NodeId) {
6666
match fk {
67-
FnKind::Fn(FnCtxt::Foreign, _, sig, _, _) => {
67+
FnKind::Fn(FnCtxt::Foreign, _, sig, _, generics, _) => {
68+
self.visit_generics(generics);
6869
self.visit_fn_header(&sig.header);
6970
visit::walk_fn_decl(self, &sig.decl);
7071
// Don't visit the foreign function body even if it has one, since lowering the

compiler/rustc_ast_passes/src/ast_validation.rs

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

92-
fn with_tilde_const_allowed(&mut self, f: impl FnOnce(&mut Self)) {
93-
let old = mem::replace(&mut self.is_tilde_const_allowed, true);
92+
fn with_tilde_const(&mut self, allowed: bool, f: impl FnOnce(&mut Self)) {
93+
let old = mem::replace(&mut self.is_tilde_const_allowed, allowed);
9494
f(self);
9595
self.is_tilde_const_allowed = old;
9696
}
9797

98+
fn with_tilde_const_allowed(&mut self, f: impl FnOnce(&mut Self)) {
99+
self.with_tilde_const(true, f)
100+
}
101+
98102
fn with_banned_tilde_const(&mut self, f: impl FnOnce(&mut Self)) {
99-
let old = mem::replace(&mut self.is_tilde_const_allowed, false);
100-
f(self);
101-
self.is_tilde_const_allowed = old;
103+
self.with_tilde_const(false, f)
102104
}
103105

104106
fn with_let_allowed(&mut self, allowed: bool, f: impl FnOnce(&mut Self, bool)) {
@@ -1161,12 +1163,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11611163
}
11621164
self.visit_vis(&item.vis);
11631165
self.visit_ident(item.ident);
1164-
if let Const::Yes(_) = sig.header.constness {
1165-
self.with_tilde_const_allowed(|this| this.visit_generics(generics));
1166-
} else {
1167-
self.visit_generics(generics);
1168-
}
1169-
let kind = FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, body.as_deref());
1166+
let kind =
1167+
FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, generics, body.as_deref());
11701168
self.visit_fn(kind, item.span, item.id);
11711169
walk_list!(self, visit_attribute, &item.attrs);
11721170
return; // Avoid visiting again.
@@ -1523,13 +1521,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15231521
FnSig { span: sig_span, header: FnHeader { ext: Extern::Implicit, .. }, .. },
15241522
_,
15251523
_,
1524+
_,
15261525
) = fk
15271526
{
15281527
self.maybe_lint_missing_abi(*sig_span, id);
15291528
}
15301529

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

1567-
visit::walk_fn(self, fk, span);
1566+
let tilde_const_allowed =
1567+
matches!(fk.header(), Some(FnHeader { constness: Const::Yes(_), .. }))
1568+
|| matches!(fk.ctxt(), Some(FnCtxt::Assoc(_)));
1569+
1570+
self.with_tilde_const(tilde_const_allowed, |this| visit::walk_fn(this, fk, span));
15681571
}
15691572

15701573
fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
@@ -1624,9 +1627,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
16241627
{
16251628
self.visit_vis(&item.vis);
16261629
self.visit_ident(item.ident);
1627-
self.with_tilde_const_allowed(|this| this.visit_generics(generics));
1628-
let kind =
1629-
FnKind::Fn(FnCtxt::Assoc(ctxt), item.ident, sig, &item.vis, body.as_deref());
1630+
let kind = FnKind::Fn(
1631+
FnCtxt::Assoc(ctxt),
1632+
item.ident,
1633+
sig,
1634+
&item.vis,
1635+
generics,
1636+
body.as_deref(),
1637+
);
16301638
self.visit_fn(kind, item.span, item.id);
16311639
}
16321640
_ => 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
@@ -153,7 +153,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
153153

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

compiler/rustc_resolve/src/def_collector.rs

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

139139
fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
140-
if let FnKind::Fn(_, _, sig, _, body) = fn_kind {
140+
if let FnKind::Fn(_, _, sig, _, generics, body) = fn_kind {
141141
if let Async::Yes { closure_id, return_impl_trait_id, .. } = sig.header.asyncness {
142+
self.visit_generics(generics);
143+
142144
let return_impl_trait_id =
143145
self.create_def(return_impl_trait_id, DefPathData::ImplTrait, span);
144146

compiler/rustc_resolve/src/late.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,10 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
537537
self.with_rib(ValueNS, rib_kind, |this| {
538538
// Create a label rib for the function.
539539
this.with_label_rib(rib_kind, |this| {
540+
if let FnKind::Fn(_, _, _, _, generics, _) = fn_kind {
541+
this.visit_generics(generics);
542+
}
543+
540544
// Add each argument to the rib.
541545
this.resolve_params(&declaration.inputs);
542546

src/tools/rustfmt/src/items.rs

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

198198
pub(crate) fn from_fn_kind(
199199
fn_kind: &'a visit::FnKind<'_>,
200-
generics: &'a ast::Generics,
201200
decl: &'a ast::FnDecl,
202201
defaultness: ast::Defaultness,
203202
) -> FnSig<'a> {
204203
match *fn_kind {
205-
visit::FnKind::Fn(fn_ctxt, _, fn_sig, vis, _) => match fn_ctxt {
204+
visit::FnKind::Fn(fn_ctxt, _, fn_sig, vis, generics, _) => match fn_ctxt {
206205
visit::FnCtxt::Assoc(..) => {
207206
let mut fn_sig = FnSig::from_method_sig(fn_sig, generics, vis);
208207
fn_sig.defaultness = defaultness;
@@ -3141,8 +3140,14 @@ impl Rewrite for ast::ForeignItem {
31413140
let inner_attrs = inner_attributes(&self.attrs);
31423141
let fn_ctxt = visit::FnCtxt::Foreign;
31433142
visitor.visit_fn(
3144-
visit::FnKind::Fn(fn_ctxt, self.ident, &sig, &self.vis, Some(body)),
3145-
generics,
3143+
visit::FnKind::Fn(
3144+
fn_ctxt,
3145+
self.ident,
3146+
&sig,
3147+
&self.vis,
3148+
generics,
3149+
Some(body),
3150+
),
31463151
&sig.decl,
31473152
self.span,
31483153
defaultness,

src/tools/rustfmt/src/visitor.rs

Lines changed: 4 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,7 @@ 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(fn_ctxt, item.ident, &sig, &item.vis, generics, Some(body)),
557555
&sig.decl,
558556
item.span,
559557
defaultness,
@@ -642,8 +640,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
642640
let inner_attrs = inner_attributes(&ai.attrs);
643641
let fn_ctxt = visit::FnCtxt::Assoc(assoc_ctxt);
644642
self.visit_fn(
645-
visit::FnKind::Fn(fn_ctxt, ai.ident, sig, &ai.vis, Some(body)),
646-
generics,
643+
visit::FnKind::Fn(fn_ctxt, ai.ident, sig, &ai.vis, generics, Some(body)),
647644
&sig.decl,
648645
ai.span,
649646
defaultness,

0 commit comments

Comments
 (0)