Skip to content
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

Second Phase of RLC inference #6278

Merged
merged 182 commits into from
Dec 5, 2023
Merged

Conversation

Nargeshdb
Copy link
Contributor

This PR infers:

  • @NotOwning for return types
  • @MustCallAlias for both return types and parameter types
  • @Owning for constructor parameters when there are multiple owning fields.

Copy link
Member

@mernst mernst left a comment

Choose a reason for hiding this comment

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

Thanks for these enhancements. I have a few comments.

* inference. For example, if a field's must-call method is called, it is added to this set. If,
* in a later statement in the same method, the same field is re-assigned, it will be removed from
* this set (since the field assignment invalidated the previously-inferred closing of the
* obligation).
*/
private final Set<VariableElement> disposedFields = new HashSet<>();
Copy link
Member

Choose a reason for hiding this comment

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

Does the use of a HashSet make the algorithm nondeterministic? (I'm not sure; don't make it a LinkedHashSet unless it needs to be.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This might cause nondeterministic ordering of fields in @EnsuresCalledMethods annotations, and to prevent this, I've switched to using LinkedHashSet.

* <li>the inferred owning fields in this analysis.
* </ul>
*
* This set is a superset of the {@code disposedFields} set.
*/
private final Set<VariableElement> owningFields = new HashSet<>();
Copy link
Member

Choose a reason for hiding this comment

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

Does the use of a HashSet make the algorithm nondeterministic? (I'm not sure; don't make it a LinkedHashSet unless it needs to be.)

Copy link
Contributor Author

@Nargeshdb Nargeshdb Nov 16, 2023

Choose a reason for hiding this comment

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

Since we don't iterate over this field in our computation, I think using a HashSet should be fine.

* The owned fields. This includes:
*
* <ul>
* <li>fields with written {@code @Owning} annotations at the entry point of the CFG currently
Copy link
Member

Choose a reason for hiding this comment

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

What does it mean to have an @Owning annotation at the entry point? Does that mean @Owning is written on a field or formal parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's pointing to the fields, I removed "at the entry ..." to prevent future confusion.

* The keys are the obligation of return nodes, and the values are the index of current method
* formal parameter (1-based) that is aliased with the return node.
*/
private final Map<Obligation, Integer> returnNodeToParameter = new HashMap<>();
Copy link
Member

Choose a reason for hiding this comment

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

Should this be named "returnObligationToParameter"?

More generally, please discuss the choice to use obligations rather than nodes as the keys. The documentation says that the map is from return nodes to method parameters, but the implementation is different. Why are obligations needed? Is it because they indicate resource aliasing relationships? Or is it for some other reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are needed to check aliasing relationships of return nodes with parameters.

// methods should be called. This map is used to create a single @EnsuresCalledMethods
// annotation for fields that share the same must-call obligation.
// methods should be called. This map is used to create a @EnsuresCalledMethods annotation for
// each set of fields that share the same must-call obligation.
Map<String, Set<String>> methodToFields = new LinkedHashMap<>();
for (VariableElement disposedField : disposedFields) {
List<String> mustCallValues = resourceLeakAtf.getMustCallValues(disposedField);
Copy link
Member

Choose a reason for hiding this comment

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

The value of mustCallValues was known when constructing disposedFields. Would it make sense to record it it dispasedFields so it does not need to be recomputed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the deposedFields set to a map to keep the must-call value and save some computation here. Thanks for the suggestion.

@@ -493,7 +610,7 @@ private void addOrUpdateClassMustCall() {
}

/**
* Computes ownership transfer at the method call to infer @Owning annotation for the arguments
* Computes ownership transfer at the method call to infer @Owning annotations for the arguments
Copy link
Member

Choose a reason for hiding this comment

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

I am confused by this comment. Should it be, "Computes ownership transfer at the method call to infer @owning annotations for formal parameters of the current method, if the parameter is passed into the call and the corresponding formal parameter of the callee is @owning."?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is correct, I updated the comment to what you suggested.

@mernst mernst assigned Nargeshdb and unassigned mernst Nov 14, 2023
mernst
mernst previously approved these changes Dec 4, 2023
mernst
mernst previously approved these changes Dec 4, 2023
@msridhar msridhar enabled auto-merge (squash) December 4, 2023 18:16
@msridhar
Copy link
Contributor

msridhar commented Dec 4, 2023

@mernst can I get one more approval from you? I had to remove some logic related to resource variables that should be unnecessary now that #6273 has landed

@msridhar msridhar enabled auto-merge (squash) December 4, 2023 18:25
Copy link
Member

@mernst mernst left a comment

Choose a reason for hiding this comment

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

Thanks for the simplifictation!

@msridhar msridhar merged commit ff3cca9 into typetools:master Dec 5, 2023
29 checks passed
@msridhar msridhar deleted the inference-phase-two branch December 11, 2023 17:02
wmdietl pushed a commit to eisop/checker-framework that referenced this pull request Jan 1, 2024
Co-authored-by: Michael Ernst <mernst@cs.washington.edu>
Co-authored-by: Manu Sridharan <msridhar@gmail.com>
Co-authored-by: Martin Kellogg <martin.kellogg@njit.edu>
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.

4 participants