Skip to content

fix: resolve NameExpr to field when local variable with same name is declared later#5025

Merged
jlerbsc merged 2 commits into
javaparser:masterfrom
jlerbsc:master
May 26, 2026
Merged

fix: resolve NameExpr to field when local variable with same name is declared later#5025
jlerbsc merged 2 commits into
javaparser:masterfrom
jlerbsc:master

Conversation

@jlerbsc

@jlerbsc jlerbsc commented May 26, 2026

Copy link
Copy Markdown
Collaborator

Fixes #3674 .

BlockStmtContext.solveSymbol() was using the last statement of the block
as the reference point for localVariablesExposedToChild(), which exposed
all local variables declared anywhere in the block — including ones
declared after the expression being resolved. This caused a NameExpr
referring to a class field to be incorrectly resolved to a local variable
with the same name declared later in the same method.
The fix removes the solveSymbol() override entirely. StatementContext
(the parent class) already handles both cases correctly: direct child
statements walk backwards through the block and delegate to the method
context, while nested blocks use the NodeWithStatements branch to walk
backwards from their own position in the outer block.

@codecov

codecov Bot commented May 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.699%. Comparing base (6ad1e57) to head (d3c103b).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@               Coverage Diff               @@
##              master     #5025       +/-   ##
===============================================
- Coverage     58.709%   58.699%   -0.010%     
+ Complexity      2593      2587        -6     
===============================================
  Files            702       702               
  Lines          40232     40213       -19     
  Branches        7331      7325        -6     
===============================================
- Hits           23620     23605       -15     
+ Misses         13638     13636        -2     
+ Partials        2974      2972        -2     
Flag Coverage Δ
AlsoSlowTests 58.699% <ø> (-0.010%) ⬇️
javaparser-core 58.699% <ø> (-0.010%) ⬇️
javaparser-symbol-solver 58.699% <ø> (-0.010%) ⬇️
jdk-10 58.270% <ø> (-0.013%) ⬇️
jdk-11 58.271% <ø> (-0.010%) ⬇️
jdk-12 58.271% <ø> (-0.008%) ⬇️
jdk-13 58.269% <ø> (-0.013%) ⬇️
jdk-14 58.500% <ø> (-0.010%) ⬇️
jdk-15 58.502% <ø> (-0.010%) ⬇️
jdk-16 58.477% <ø> (-0.010%) ⬇️
jdk-17 58.624% <ø> (-0.010%) ⬇️
jdk-18 58.627% <ø> (-0.010%) ⬇️
jdk-8 58.108% <ø> (-0.010%) ⬇️
jdk-9 58.270% <ø> (-0.010%) ⬇️
macos-latest 58.675% <ø> (-0.010%) ⬇️
ubuntu-latest 58.670% <ø> (-0.010%) ⬇️
windows-latest 58.682% <ø> (-0.010%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ver/javaparsermodel/contexts/BlockStmtContext.java 75.862% <ø> (-9.555%) ⬇️

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1e2907d...d3c103b. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jlerbsc jlerbsc merged commit 670a7a1 into javaparser:master May 26, 2026
35 checks passed
@jlerbsc jlerbsc added this to the next release milestone May 26, 2026
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.

Solving incorrectly a NameExpr Symbol when there is a variable declared after with same name

1 participant