Repository navigation
Conversation
|
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. |
87290b0 to
e56cd4d
Compare
| * 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. |
There was a problem hiding this comment.
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:
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.
There was a problem hiding this comment.
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>
e56cd4d to
4f5b241
Compare
ASTHelpers.getSymbol((x))returns null wheregetSymbol(x)returns the symbol ofx, and the Javadoc did not say so. A caller that forgetsstripParenthesesgets no symbol for a parenthesized operand, including the expression of everysynchronizedstatement, 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 throwsIllegalStateExceptionwhen given a parenthesized expression. The Javadoc now states the null result and points atstripParentheses(Tree). A new test inASTHelpersTestpins it, with that reason in its comment, so that a change makinggetSymbollook 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,
newexpression, or member reference throwsIllegalArgumentExceptionfrom 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 ingetSymbolnames module names as one such case.check_api/pom.xmllistsASTHelpersTest.javaintestExcludes, so the GitHub Maven build does not compile or run the new test. I compiled and ranASTHelpersTestby hand on JDK 25.0.4: all 67 tests pass. With aParenthesizedTreecase added at the top ofgetSymbol(Tree), the new test fails withexpected: null/but was : x.javadoc -Xdoclint:allonASTHelpers.javareports no errors; both{@link}targets resolve.🤖 Generated with Claude Code