Fix NPE in VarWithPrimitive when lambda parameters have inferred primitive types#5873
Open
copybara-service[bot] wants to merge 1 commit into
Open
Fix NPE in VarWithPrimitive when lambda parameters have inferred primitive types#5873copybara-service[bot] wants to merge 1 commit into
VarWithPrimitive when lambda parameters have inferred primitive types#5873copybara-service[bot] wants to merge 1 commit into
Conversation
cdfcdfb to
1fff859
Compare
…imitive types ## Problem Fixes #5857 `VarWithPrimitive` throws a `NullPointerException` when a lambda has an implicit parameter whose inferred type is a primitive: ```java byte[] bar = new byte[6]; Map<Byte, List<Byte>> indicesMap = IntStream.range(0, 6) .mapToObj(n -> n) .collect(Collectors.groupingBy( n -> bar[n], Collectors.mapping( n -> (byte) n.intValue(), Collectors.toList()))); // Exception: java.lang.NullPointerException: Cannot invoke "com.sun.tools.javac.tree.JCTree.getStartPosition()" because "tree" is null ``` ## Root Cause `hasImplicitType()` returns `true` for both: - `var` declarations - Implicit lambda parameters (when `VariableTree.getType()` is `null`, per JDK-8268850) `VarWithPrimitive` used `hasImplicitType()` as its guard, so lambda parameters with inferred primitive types passed the check and reached `replaceVariableType()`. Inside `replaceVariableType()`, `tree.getType()` returns `null` for these parameters. `hasExplicitSource(null, state)` was then called unconditionally, which invokes: ```java getStartPosition(null) ``` resulting in a `NullPointerException`. ## Fix Add a null guard for `type` in `SuggestedFixes.replaceVariableType()` at the actual crash site. If `type == null`: - Skip the `hasExplicitSource()` call. - Fall through to `getStartPosition(tree)`. The subsequent token scan finds no `var` keyword for implicit lambda parameters and correctly returns `Optional.empty()`, producing `NO_MATCH` without throwing an exception. This is the minimal fix because: - It addresses the actual crash site. - It is defensive against any other callers that may pass a `VariableTree` with a null type. ## Tests ### `implicitLambdaParameter_noMatch` Regression test reproducing the exact code from the bug report: - Single-parameter lambdas - Inferred `int` and `byte` types - Verifies `NO_MATCH` and no exception ### `multiParamImplicitLambda_noMatch` Tests a multi-parameter implicit lambda: ```java IntBinaryOperator op = (a, b) -> a + b; ``` - Both parameters inferred as `int` - Verifies `NO_MATCH` and no exception ### `explicitLambdaParam_noMatch` Tests an explicit lambda parameter: ```java (int n) -> n ``` - `hasImplicitType()` returns `false` - Verifies no match is reported ### `enhancedForLoop` Tests: ```java for (var x : intArray) { // ... } ``` Expected fix: ```java for (int x : intArray) { // ... } ``` ### `forLoopInitializer` Tests: ```java for (var i = 0; i < 10; i++) { // ... } ``` Expected fix: ```java for (int i = 0; i < 10; i++) { // ... } ``` Fixes #5868 FUTURE_COPYBARA_INTEGRATE_REVIEW=#5868 from HarshMehta112:master 5881069 PiperOrigin-RevId: 931039338
1fff859 to
b568f51
Compare
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.
Fix NPE in
VarWithPrimitivewhen lambda parameters have inferred primitive typesProblem
Fixes #5857
VarWithPrimitivethrows aNullPointerExceptionwhen a lambda has an implicitparameter whose inferred type is a primitive:
Root Cause
hasImplicitType()returnstruefor both:vardeclarationsVariableTree.getType()isnull, per JDK-8268850)VarWithPrimitiveusedhasImplicitType()as its guard, so lambda parameterswith inferred primitive types passed the check and reached
replaceVariableType().Inside
replaceVariableType(),tree.getType()returnsnullfor theseparameters.
hasExplicitSource(null, state)was then called unconditionally,which invokes:
resulting in a
NullPointerException.Fix
Add a null guard for
typeinSuggestedFixes.replaceVariableType()at theactual crash site.
If
type == null:hasExplicitSource()call.getStartPosition(tree).The subsequent token scan finds no
varkeyword for implicit lambdaparameters and correctly returns
Optional.empty(), producingNO_MATCHwithout throwing an exception.
This is the minimal fix because:
VariableTreewith a null type.
Tests
implicitLambdaParameter_noMatchRegression test reproducing the exact code from the bug report:
intandbytetypesNO_MATCHand no exceptionmultiParamImplicitLambda_noMatchTests a multi-parameter implicit lambda:
intNO_MATCHand no exceptionexplicitLambdaParam_noMatchTests an explicit lambda parameter:
hasImplicitType()returnsfalseenhancedForLoopTests:
Expected fix:
forLoopInitializerTests:
Expected fix:
Fixes #5868
FUTURE_COPYBARA_INTEGRATE_REVIEW=#5868 from HarshMehta112:master 5881069