Skip to content

Refactor visit.rs with a Visitor trait #7081

Closed
@pnkfelix

Description

@pnkfelix

Currently libsyntax/visit.rs provides a record that holds a collection of @fn's, using a mix of lambda-lifting and an explicit virtual method table to define traversals over the AST.

pub struct Visitor<E> {
    visit_mod: @fn(&_mod, span, node_id, E, vt<E>),
    visit_view_item: @fn(@view_item, E, vt<E>),
    ...
}

where

pub enum vt<E> { mk_vt(visitor<E>), }

So then a use of the visitor looks like this (taken from freevars.rs):

    let v = visit::mk_vt(@visit::Visitor {visit_item: ignore_item,
                                          visit_expr: walk_expr,
                                          .. *visit::default_visitor()});
    (v.visit_block)(blk, 1, v);

where ignore_item and walk_expr are defined accordingly.

The above is awkward. A more natural way to encode this would be via a Visitor trait that defined the visit methods; then instances of the trait would correspond to the accumulated state denoted above by E in the struct definition and 1 in the freevars example.

(I originally had written a @Visitor object, but there is no need to commit to using objects in the Visitor interface.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions