Skip to content

Document that ASTHelpers.getSymbol(Tree) returns null for a parenthesized expression - #6295

Closed
vlsi wants to merge 1 commit into
google:masterfrom
vlsi:claude/javadoc-getsymbol-improvement-ba25fb
Closed

vlsi wants to merge 1 commit into
google:masterfrom
vlsi:claude/javadoc-getsymbol-improvement-ba25fb

Conversation

@vlsi

@vlsi vlsi commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

ASTHelpers.getSymbol((x)) returns null where getSymbol(x) returns the symbol of x, and the Javadoc did not say so. A caller that forgets stripParentheses gets no symbol for a parenthesized operand, including the expression of every synchronized statement, and usually treats that as "not a variable" without any error.

As discussed in #6223, the method keeps returning null: callers treat a non-null result as a sign that the tree itself is the identifier, member select, or invocation, and some cast it or pass it to getReceiver, which throws IllegalStateException when given a parenthesized expression. The Javadoc now states the null result and points at stripParentheses(Tree). A new test in ASTHelpersTest pins it, with that reason in its comment, so that a change making getSymbol look through parentheses fails it.

The old Javadoc also promised null when a compilation error left the symbol unresolved. Error Prone does not run checks on a compilation unit with errors, and an unresolved invocation, new expression, or member reference throws IllegalArgumentException from its typed overload instead of returning null. The comment now states what the code returns: null for an identifier or member select that javac left without a symbol. The inline comment in getSymbol names module names as one such case.

check_api/pom.xml lists ASTHelpersTest.java in testExcludes, so the GitHub Maven build does not compile or run the new test. I compiled and ran ASTHelpersTest by hand on JDK 25.0.4: all 67 tests pass. With a ParenthesizedTree case added at the top of getSymbol(Tree), the new test fails with expected: null / but was : x. javadoc -Xdoclint:all on ASTHelpers.java reports no errors; both {@link} targets resolve.

🤖 Generated with Claude Code

@google-cla

google-cla Bot commented Nov 4, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@vlsi
vlsi force-pushed the claude/javadoc-getsymbol-improvement-ba25fb branch from 87290b0 to e56cd4d Compare November 4, 2026 12:10
* Gets the symbol that a tree declares or refers to.
*
* <p>Returns null if {@code tree} is null, if it is a kind of tree that has no symbol, or if it
* is an identifier or member select with no resolved symbol, such as after a compilation error.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

such as after a compilation error

Error Prone isn't supposed to run checks over compilation units with errors, so that isn't supposed to happen:

if (JavaCompiler.instance(context).errorCount() > errorProneErrors) {

There have been bugs in that area before, and ASTHelpers could be used outside of Error Prone checks, but it could be slightly misleading to mention compilation errors.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I removed it. The clause came from the previous Javadoc ("or if the symbol cannot be found due to a compilation error"); I had kept it and only narrowed it to identifiers and member selects, since an unresolved invocation, new, or member reference throws instead. The Javadoc now says "or if javac attached no symbol to an identifier or member select, as for some module names", following the inline comment in getSymbol about module names.

…esized expression.

`getSymbol((x))` returns null where `getSymbol(x)` returns the symbol of `x`, and the Javadoc did not say so. A caller that passes an operand without `stripParentheses` silently gets no symbol when the operand is written in parentheses, including the expression of every `synchronized` statement, which javac always parenthesizes.

The method keeps returning null for a `ParenthesizedTree`: callers treat a non-null result as a sign that the tree itself is the identifier, member select, or invocation, and some then cast it or pass it to `getReceiver`, which throws `IllegalStateException` when given a parenthesized expression. A test in `ASTHelpersTest` pins the null result and states this reason, so that a change making `getSymbol` look through parentheses fails it.

The old Javadoc also promised null when a compilation error left the symbol unresolved. Error Prone does not run checks on a compilation unit with errors, and an unresolved invocation, `new` expression, or member reference throws `IllegalArgumentException` from its typed overload instead of returning null. The comment now states what the code returns: null for an identifier or member select that javac left without a symbol. The inline comment in `getSymbol` names module names as one such case.

See google#6223

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
@vlsi
vlsi force-pushed the claude/javadoc-getsymbol-improvement-ba25fb branch from e56cd4d to 4f5b241 Compare November 4, 2026 12:20
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.

2 participants