Skip to content

GROOVY-4721: variable declared in try block is in scope in finally block #2172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2025

Conversation

daniellansun
Copy link
Contributor

@daniellansun daniellansun requested a review from Copilot April 2, 2025 16:57
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request addresses GROOVY-4721 by adjusting the scope handling of variables declared in try blocks to ensure their correct accessibility within the corresponding finally block.

  • Updated imports for new AST classes
  • Refactored makeBlockRecorder to utilize a consistent compileStack instance and incorporate explicit handling for empty finally statements
Files not reviewed (2)
  • src/test/groovy/bugs/Groovy4721.groovy: Language not supported
  • subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/GroovyshTest.groovy: Language not supported
Comments suppressed due to low confidence (1)

src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java:409

  • By returning early for a null or EmptyStatement, the lambda may bypass necessary compileStack state management that is performed in the non-empty path. Consider verifying whether any push/pop operations should still occur to maintain a consistent compileStack state.
if (finallyStatement == null || finallyStatement instanceof EmptyStatement) return;

@daniellansun daniellansun requested a review from paulk-asert April 2, 2025 16:58
@codecov-commenter
Copy link

codecov-commenter commented Apr 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 69.9776%. Comparing base (6ce967f) to head (fef0b8c).
Report is 25 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #2172        +/-   ##
==================================================
+ Coverage     68.8443%   69.9776%   +1.1333%     
- Complexity      29533      31746      +2213     
==================================================
  Files            1421       1422         +1     
  Lines          113424     119714      +6290     
  Branches        19644      21835      +2191     
==================================================
+ Hits            78086      83773      +5687     
- Misses          28754      29326       +572     
- Partials         6584       6615        +31     
Files with missing lines Coverage Δ
.../codehaus/groovy/classgen/asm/StatementWriter.java 97.5104% <100.0000%> (+0.3757%) ⬆️
.../groovy/org/apache/groovy/groovysh/Groovysh.groovy 60.0790% <100.0000%> (ø)

... and 27 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@eric-milles eric-milles left a comment

Choose a reason for hiding this comment

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

Seems reasonable. Nice compact change in StatementWriter.

// groovy:000> y
// Unknown property: y
// groovy:000>
@NotYetImplemented
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't the existing behavior of groovysh with interpreterMode set.

groovy:000> :set interpreterMode
groovy:000> int x = 3
===> 3
groovy:000> x
===> 3
groovy:000> int a, b, c
===> 0
groovy:000> b
===> 0

Copy link
Contributor

Choose a reason for hiding this comment

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

I would like to fix the original issue, but it would be good to not break interpreterMode. I'll try to find time to investigate a little more.

Copy link
Contributor Author

@daniellansun daniellansun Apr 4, 2025

Choose a reason for hiding this comment

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

buff = [importsSpec] + ['try {', 'true'] + current + ['} finally {' + variableBlocks + '}']

Groovysh will try to collect variables in the finally block, but variables declared in try block can not be accessed in finally block after GROOVY-4721 is fixed.

Copy link
Contributor

Choose a reason for hiding this comment

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

I added the change to StatementWriter and changed groovysh to just have the "variableBlocks" execute directly after "current" (i.e. just removed the try...finally) then interpreterMode still seemed to work. The current test suite and the new tests all passed. There could be variations that we don't have coverage for in our test suite that may become broken with that change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if the code of current throws an exception, the variables will still fail to collect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@paulk-asert I've polished the PR as you suggested:
fef0b8c

Co-authored-by: Daniel Sun <sunlan@apache.org>
@daniellansun daniellansun merged commit e6b7fc8 into master Apr 10, 2025
39 checks passed
@daniellansun daniellansun deleted the GROOVY-4721 branch April 10, 2025 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants