-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Conversation
There was a problem hiding this 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;
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ 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
🚀 New features to boost your workflow:
|
There was a problem hiding this 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
groovy/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/Groovysh.groovy
Line 294 in 8739056
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
https://issues.apache.org/jira/browse/GROOVY-4721