You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Duplicate cursor while working with Java visitors #3641
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(newCursor(cursor, tree)); // <---- This change//noinspection uncheckedt = (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:
@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?
@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.
Activity
BoykoAlex commentedon Oct 23, 2023
I propose to switch the cursor creation call:
from
to
knutwannheden commentedon Oct 26, 2023
@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 overridesvisit(Tree, P). This is of course possible, but that method (inTreeVisitor) 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?knutwannheden commentedon Oct 27, 2023
@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.knutwannheden commentedon Oct 27, 2023
Using
Cursor#fork(), as you propose, could also make sense in a second step to avoid any "pollution" between visitors.BoykoAlex commentedon Oct 27, 2023
@knutwannheden reverting the commit or switching to
fork()either should fix the issue for me. thanks very much for looking into this!-)