Skip to content

Duplicate cursor while working with Java visitors #3641

Closed

Description

@BoykoAlex

The change in TreeVisitor:

            setCursor(cursor.getParent());

            if (topLevel) {
                sample.stop(Timer.builder("rewrite.visitor.visit").tag("visitor.class", getClass().getName()).register(Metrics.globalRegistry));
                visitCountSummary.record(visitCount);

                if (t != null && afterVisit != null) {
                    for (TreeVisitor<?, P> v : afterVisit) {
                        if (v != null) {
                             v.setCursor(new Cursor(cursor, tree)); // <---- This change
                            //noinspection unchecked
                            t = (T) v.visit(t, p);
                        }
                    }
                }

                sample.stop(Timer.builder("rewrite.visitor.visit.cumulative").tag("visitor.class", getClass().getName()).register(Metrics.globalRegistry));
                afterVisit = null;
                visitCount = 0;
            }

seems problematic and may lead to the creation of two cursors (child and parent) pointing to the same AST node.
The following call t = (T) v.visit(t, p); would create another cursor with the same tree element it seems:

    @Nullable
    public T visit(@Nullable Tree tree, P p) {
        if (tree == null) {
            return defaultValue(null, p);
        }

        Timer.Sample sample = null;
        boolean topLevel = false;
        if (visitCount == 0) {
            topLevel = true;
            sample = Timer.start();
        }

        visitCount++;
        setCursor(new Cursor(cursor, tree));

Having a duplicate cursor leads to problems with indents in the auto formatting visitor.

Activity

  1. added
    bug
    on Oct 23, 2023
  2. BoykoAlex commented on Oct 23, 2023

    @BoykoAlex
    ContributorAuthor

    I propose to switch the cursor creation call:

    from

    v.setCursor(new Cursor(cursor, tree));

    to

    v.setCursor(cursor == null ? null : cursor.fork());
  3. knutwannheden commented on Oct 26, 2023

    @knutwannheden
    Contributor

    @BoykoAlex Thanks for reporting. This seems to be about commit 3086e52. It indeed looks like the after visitor's cursor will end up with a parent with the same value.

    @sambsnyd If I understand your commit correctly, you made a change to fix TreeVisitorTest#scheduleAfterOnVisitWithCursor(). But I think that what that test is doing is somewhat dangerous: Its visitor overrides visit(Tree, P). This is of course possible, but that method (in TreeVisitor) is the one which contains all the "important" bits about setting up the cursor etc. So I think a visitor doing that can't expect the cursor to point to the element being visited. In that way I think this test is wrong. What are your thoughts on this?

  4. knutwannheden commented on Oct 27, 2023

    @knutwannheden
    Contributor

    @BoykoAlex Rather than calling Cursor#fork() I would like to propose to switch back to using the original cursor, as we had it before. I will update your PR accordingly.

  5. knutwannheden commented on Oct 27, 2023

    @knutwannheden
    Contributor

    Using Cursor#fork(), as you propose, could also make sense in a second step to avoid any "pollution" between visitors.

  6. linked a pull request that will close this issueFix after visitor cursor propagation #3642on Oct 27, 2023
  7. moved this to In Progress in OpenRewriteon Oct 27, 2023
  8. moved this from In Progress to Ready to Review in OpenRewriteon Oct 27, 2023
  9. BoykoAlex commented on Oct 27, 2023

    @BoykoAlex
    ContributorAuthor

    @knutwannheden reverting the commit or switching to fork() either should fix the issue for me. thanks very much for looking into this!-)

  10. moved this from Ready to Review to Done in OpenRewriteon Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions