Skip to content

fix: retain comments without specialized placements#6813

Open
martinfrancois wants to merge 2 commits into
INRIA:masterfrom
martinfrancois:agent/issue-6812-comment-parent-fallback
Open

fix: retain comments without specialized placements#6813
martinfrancois wants to merge 2 commits into
INRIA:masterfrom
martinfrancois:agent/issue-6812-comment-parent-fallback

Conversation

@martinfrancois

@martinfrancois martinfrancois commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Summary

Keep comments in the model when a specialized comment-placement visitor cannot attach them to a more specific child node.

Fixes #6812.

Problem

Spoon first tries to attach each source comment to the most specific matching AST node. For some valid comment positions, the specialized placement visitor does not find a child that accepts the comment. Examples include a comment next to an array index or operator and a comment inside a module declaration with no directive. In those cases, JDTCommentBuilder.java logged an error and left the comment outside the model.

Spoon issue #2482 originally reported comments assigned to the wrong type and a missing end-of-type comment. Spoon PR #2636 fixed that original type-header reproduction by handling comments in declaration modifier ranges. Later comments on #2482 reported the array-access and binary-operator cases covered here. This pull request fixes the remaining path shared by those cases and the annotation and module examples: specialized placement runs, but no child accepts the comment.

This happened while StoneDetector used Spoon and the Eclipse Java compiler (ECJ) to analyze these OpenJDK sources:

The eight files produced 21 comment-placement errors.

Change

  • Keep the existing specialized placement logic.
  • If that logic does not give the comment a parent, attach the comment to the enclosing AST element instead of dropping it.
  • Keep the error log with accurate fallback wording and the issue-reporting link so users continue to report unsupported placements.

Tests

  • Enabled the existing array-access and binary-operator regressions in CommentTest.java.
  • Added cases for a comment in an empty annotation and a comment in a module with no directives.
  • mvn -q -Dtest=spoon.test.comment.CommentTest,spoon.test.compilation.CompilationTest test passes: 58 tests, 0 failures, 0 errors.
  • mvn -q -DskipTests verify passes after restoring the diagnostic.

Integration validation

I built the fallback behavior with the combined local ECJ fixes and used the result in StoneDetector.

All eight linked OpenJDK files completed successfully: AST 8/8, CFG 159/159, dominator trees 159/159, and encoded paths 159/159. The run exited with code 0 and the functional result was complete. The current branch intentionally restores the Spoon error diagnostic when fallback placement is used, so equivalent runs retain comments while continuing to report unsupported placements.

Full test suite

The complete Java 25 suite passes 4,462 tests with 0 failures, 0 errors, and 16 skipped.

Spoon issue INRIA#2482's original reproduction was fixed by PR INRIA#2636.
This change handles later cases where specialized placement finds no child.

Refs INRIA#6812
@martinfrancois
martinfrancois force-pushed the agent/issue-6812-comment-parent-fallback branch from 9426d0b to a6d8ef6 Compare July 17, 2026 17:17
@martinfrancois
martinfrancois marked this pull request as ready for review July 17, 2026 18:42
@MartinWitt

Copy link
Copy Markdown
Collaborator

@martinfrancois could we keep the log so we get some issues from users? The rest looks good

@martinfrancois

Copy link
Copy Markdown
Contributor Author

@martinfrancois could we keep the log so we get some issues from users? The rest looks good

Sure, done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Comments are dropped when specialized placement has no child

2 participants