rust-lang / rust-lang/rust

Detect situations where a visitor implementation is skipped by accidentally directly calling the corresponding walk function

Open
#129,859 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lints C-cleanup E-hard T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

For the AST (but also other visitors in the compiler), visitor functions typically come in two flavors:

  • visit_X - Overrideable part of the trait invocation. If you override it, you should call walk_X if you want to recurse or do visit_Y calls on the sub-components (for some other Y) you want to walk into, skipping the rest.
  • walk_X - Non-overrideable, free function which actually recurses on the structure by calling visit_Y on all of the sub-components (for some other Y) of the thing. Sometimes this is called super_visit_X.

It is typically a bug to call walk_Y on a sub-component within a visit_X invocation. For example, see:

https://github.com/rust-lang/rust/pull/129858

Which fixed a bug where we called walk_expr on the async closure's body when visiting the async closure in visit_fn. This should've been a visit_expr, since visit_expr had meaningful side-effects (collecting macro invocations).

We should have some sort of lint to prevent this pattern outside of regular visit_X -> walk_X situation, and compiler devs who need to do something special can do #[allow(whatever_lint_name_for_sketchy_visitor_pattern)] to justify that what they're doing is okay. We already have some comments that document when this happens (I think, I feel like I've seen them around), but it can be done by accident which causes bugs!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the AST visitor pattern described in the issue and the linked pull request, comparing visit_X and walk_X calls in compiler visitors. Define the lint so the regular visit_X -> walk_X pattern remains valid, while accidental direct walk_Y calls from other visitor implementations are diagnosed and can be explicitly allowed when justified.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.