Fix context-sensitive template crash for anonymous class args in assignments#7177
Merged
Fix context-sensitive template crash for anonymous class args in assignments#7177
Conversation
…gnments
When a context-sensitive JavaTemplate targeted an expression inside an
anonymous class that was itself an argument to a method call (e.g.,
Preconditions.check()), the generated stub contained invalid Java: a
semicolon inside a method argument like `__M__.any(new Foo(){...};)`.
The parser's error recovery dropped the template comment markers,
causing listTemplatedTrees to return an empty list and crash with
IndexOutOfBoundsException.
Two fixes in BlockStatementTemplateGenerator:
- Don't append ';' after anonymous class body when parent is a MethodCall
- Detect __TEMPLATE__ comment in JLeftPadded.before padding for
assignment-context templates
Member
Author
|
Discussed on Slack with @greg-at-moderne : https://moderneinc.slack.com/archives/C02MMFD9DKJ/p1774613169108859?thread_ts=1774588052.282249&cid=C02MMFD9DKJ Consensus was looks fine and likely fixes the issue we had seen in today's run. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
IndexOutOfBoundsExceptioninJavaTemplateParser.parseExpression()when a context-sensitive template replaces a method invocation inside an assignment, where the containing anonymous class is an argument to another method call (e.g.,Preconditions.check())__M__.any(new Foo(){...};)— semicolon inside method argument) causing the parser to drop/*__TEMPLATE__*/comment markers during error recoveryMethodCall, consistent with the existing pattern used elsewhere in the same method__TEMPLATE__comment inJLeftPadded.beforepadding for assignment-context templates where the comment lands in the padding space rather than on a tree node prefixTest plan
replaceContextSensitiveMethodInvocationInsideAssignmenttest reproducing the exact crash scenarioJavaTemplateTest*tests passrewrite-java-testandrewrite-javatest suites pass