-
Notifications
You must be signed in to change notification settings - Fork 353
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
Model try-with-resources semantics during CFG construction #6273
Conversation
@msridhar Ordinarily me, but I am overwhelmed at the moment, so I will ask Suzanne to take a look. |
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 just had a few minor comments.
|
||
/** | ||
* This represents a resource declaration in a try-with-resources tree. A resource declaration can | ||
* be either a variable declaration or just a final (or effectively final) local variable. |
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 change just a
to a use of a
.
|
||
@Override | ||
public Collection<Node> getOperands() { | ||
// TODO what is the right answer here? |
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 should return a singleton of assignOrLocalVarNode
.
@@ -1241,6 +1242,28 @@ private void updateObligationsForAssignment( | |||
} | |||
} | |||
|
|||
/** | |||
* Updates a set of obligations to account for a resource declaration in a try-with-resources | |||
* tree. If the resource is a local variable (<em>not</em> a variable declaration), remove all |
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 change the resource is a local variable (<em>not</em> a variable declaration)
to the resource is a use of a local variable
.
* @param p the argument for the operation implemented by this visitor | ||
* @return the return value of the operation implemented by this visitor | ||
*/ | ||
R visitResource(ResourceNode n, P p); |
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.
Please add a change log entry for the new node subclass.
I realized that in Java 9 and after try-with-resources can also be used to close a final field. Will need to rework this PR a bit to handle that. Will address comments also and then re-request a review. |
@smillst @kelloggm @mernst I'm thinking about re-working the approach here and would like your feedback. Right now, this PR adds Here is my alternative proposal. Instead of putting the Let me know your thoughts on the above; thanks! |
I think this alternative proposal is sensible. I would consider changing the name from |
This sounds good to me. I also agree with Martin's naming suggestion, too. |
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.
Thanks for all these improvements. I have a few questions/comments.
dataflow/src/main/java/org/checkerframework/dataflow/cfg/builder/CFGTranslationPhaseOne.java
Outdated
Show resolved
Hide resolved
dataflow/src/main/java/org/checkerframework/dataflow/cfg/builder/CFGTranslationPhaseOne.java
Outdated
Show resolved
Hide resolved
dataflow/src/main/java/org/checkerframework/dataflow/cfg/builder/CFGTranslationPhaseOne.java
Outdated
Show resolved
Hide resolved
dataflow/src/main/java/org/checkerframework/dataflow/cfg/builder/CFGTranslationPhaseOne.java
Outdated
Show resolved
Hide resolved
dataflow/src/main/java/org/checkerframework/dataflow/cfg/builder/CFGTranslationPhaseOne.java
Outdated
Show resolved
Hide resolved
dataflow/src/main/java/org/checkerframework/dataflow/cfg/builder/CFGTranslationPhaseOne.java
Outdated
Show resolved
Hide resolved
dataflow/src/main/java/org/checkerframework/dataflow/cfg/builder/CFGTranslationPhaseOne.java
Outdated
Show resolved
Hide resolved
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.
LGTM. I didn't merge because I'm not sure if I'm the last to review it.
@msridhar I think this can be merged. Thank you! |
…#6273) Co-authored-by: Michael Ernst <mernst@cs.washington.edu>
Fixes #6037
We modify CFG construction to inject calls to
close()
at the appropriate place for all resource variables declared or used in a try-with-resources tree. Our modeling is based on the desugaring given in JLS 14.20.3.1:https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.20.3.1
Essentially, the closing of each resource is modeled using a nested try-finally block, where the finally block includes the
close()
call. We currently elide the null checks and tracking of the primary exception from the JLS desugaring.This explicit modeling in the CFG enables deletion of a good amount of custom code related to resource variables in the Must Call Checker and the Resource Leak Checker, while also enabling those checkers to handle more cases.