Skip to content

HIR/THIR visitors should deconstruct all values #141849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nnethercote opened this issue Jun 1, 2025 · 2 comments
Open

HIR/THIR visitors should deconstruct all values #141849

nnethercote opened this issue Jun 1, 2025 · 2 comments
Assignees
Labels
C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nnethercote
Copy link
Contributor

nnethercote commented Jun 1, 2025

The AST visitor (in compiler/rustc_ast/src/visit.rs) uses struct/enum deconstruction to access fields in its walk_* methods, e.g.:

pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V::Result {
    let Expr { id, kind, span, attrs, tokens: _ } = expression; 
    walk_list!(visitor, visit_attribute, attrs);
    match kind {
        ...
    }
    ...
}

This is good, because it's impossible to add a new field to an AST type and fail to update the appropriate visitor -- you'll get a compile error. (I swear there was a comment about this, but I can't find it right now.)

In contrast, the HIR visitor (in compiler/rustc_hir/src/intravisit.rs) uses field access:

pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) -> V::Result {
    try_visit!(visitor.visit_id(expression.hir_id));
    match expression.kind {
        ...
    }
    ...
}

It would be good to change the HIR visitor to use deconstruction everywhere. This would identify any fields that are currently erroneously unvisited, and prevent future occurrences.

Likewise for the THIR visitor (in compiler/rustc_middle/src/thir/visit.rs) which is a lot smaller and simpler.

This would be a good task for a new contributor, maybe even a good first bug.

@nnethercote nnethercote added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. C-cleanup Category: PRs that clean code up or issues documenting cleanup. labels Jun 1, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 1, 2025
@nnethercote nnethercote changed the title HIR visitor should deconstruct all values HIR/THIR visitors should deconstruct all values Jun 1, 2025
@Noratrieb Noratrieb added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 1, 2025
@krikera
Copy link

krikera commented Jun 1, 2025

@rustbot claim

@ArtemIsmagilov
Copy link

// N.B., deliberately force a compilation error if/when new fields are added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants