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

Update fix structor to contain multiple annotation changes #242

Merged
merged 23 commits into from
Oct 8, 2024

Conversation

nimakarimipour
Copy link
Member

@nimakarimipour nimakarimipour commented Oct 2, 2024

This PR introduces a necessary change in preparation for the upcoming major PR that will add support for taint checker inference.

The key change in this PR is the update to the Fix structure, which now contains a list of changes rather than a single change. With this new design, a Fix instance represents the smallest unit of annotation changes that the Annotator will evaluate for its impact. If approved, these changes will be applied to the source code. It’s important to note that resolving an error may require adding multiple annotations, and a Fix instance can include a subset or all of these changes.

In NullAway inference, we evaluate each annotation individually and apply it if approved. For example, resolving an initialization error may require annotating multiple fields as @Nullable, but each annotation is evaluated and applied one at a time. In contrast, taint inference requires evaluating all suggested annotations together for a given error.

This updated design lays the groundwork for supporting the taint checker inference by accommodating the evaluation of grouped annotations for each error.

Set<Location> otherTree = found.tree.stream().map(Fix::toLocation).collect(Collectors.toSet());
Set<Location> thisTree =
this.tree.stream().flatMap(fix -> fix.toLocations().stream()).collect(Collectors.toSet());

Copy link
Member Author

Choose a reason for hiding this comment

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

Remove white line

Copy link
Member Author

Choose a reason for hiding this comment

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

@@ -187,7 +187,7 @@ private Set<String> extractUninitializedFieldNames(String errorMessage) {
* @param region Region where the error is reported.
* @return Set of fixes for uninitialized fields to resolve the given error.
*/
protected ImmutableSet<Fix> generateFixesForUninitializedFields(
protected ImmutableSet<AddAnnotation> computeAddAnnotationInstancesForUninitializedFields(
Copy link
Member Author

Choose a reason for hiding this comment

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

Update javadoc

Copy link
Member Author

Choose a reason for hiding this comment

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

@@ -174,6 +175,10 @@ public boolean declaredInModule(Location location) {
return methodRegistry.declaredInModule(location);
}

public boolean declaredInModule(Set<Location> locations) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Add javadoc

Copy link
Member Author

Choose a reason for hiding this comment

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

/** The fixes which can resolve this error (possibly empty). */
protected final ImmutableSet<Fix> resolvingFixes;
/** The fixes which can resolve this error (possibly null). */
protected final Set<Fix> resolvingFixes;
Copy link
Member Author

Choose a reason for hiding this comment

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

Make immutable

Copy link
Member Author

Choose a reason for hiding this comment

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


public ImmutableSet<Fix> getResolvingFixes() {
public Set<Fix> getFixes() {
Copy link
Member Author

Choose a reason for hiding this comment

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

Update return type

Copy link
Member Author

Choose a reason for hiding this comment

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

@nimakarimipour nimakarimipour self-assigned this Oct 4, 2024
@nimakarimipour nimakarimipour added the enhancement New feature or request label Oct 4, 2024
Copy link
Member

@msridhar msridhar left a comment

Choose a reason for hiding this comment

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

The change LGTM. I have some high-level questions and minor comments but they don't need to block landing the PR.

@@ -157,7 +157,7 @@ private void executeNextIteration(
injector.injectFixes(selectedFixes);
// Update log.
context.log.updateInjectedAnnotations(
selectedFixes.stream().map(fix -> fix.change).collect(Collectors.toSet()));
selectedFixes.stream().flatMap(fix -> fix.changes.stream()).collect(Collectors.toSet()));
Copy link
Member

Choose a reason for hiding this comment

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

For after this PR: it'd be good to add Google Java Format formatting to this repo, enforced via Spotless. It will get rid of long lines like this one that are hard to read.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, will do #248

@@ -51,19 +52,19 @@ public BaseCache(S store) {

@Override
public boolean isUnknown(Fix fix) {
return !this.store.containsKey(fix.toLocation());
return !this.store.containsKey(fix.toLocations());
Copy link
Member

Choose a reason for hiding this comment

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

High-level question: why not use Fix as the key type for the maps, instead of Set<Location>?

Copy link
Member Author

Choose a reason for hiding this comment

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

@msridhar That was a very good catch. It should be "Fix" with the new changes on Fix declaration. In previous design "Fix" contained some other properties except Set of locations. This is much better now. d4f0854

*/
protected ImmutableSet<Fix> generateFixesForUninitializedFields(
String errorMessage, Region region, ModuleInfo module) {
private ImmutableSet<AddAnnotation> computeAddAnnotationInstancesForUninitializedFields(
Copy link
Member

Choose a reason for hiding this comment

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

We don't make this ImmutableSet<Fix> because we want to evaluate each annotation individually, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes exactly, we evaluate each annotation (uninitialized field in this case) individually.

Comment on lines 484 to 486
.changes
.iterator()
.next()
Copy link
Member

Choose a reason for hiding this comment

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

Should we also assert that the size of changes is equal to 1?

Copy link
Member Author

@nimakarimipour nimakarimipour Oct 8, 2024

Choose a reason for hiding this comment

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

Actually based on your earlier comment comment mentioning that instead of working with set of locations, we can simply use the fix instance, I updated this method to just check the equality of fixes directly rather than the locations. ee0af76

@nimakarimipour nimakarimipour changed the title update fix structor to contain multiple annotation changes Update fix structor to contain multiple annotation changes Oct 8, 2024
@nimakarimipour nimakarimipour merged commit 5e3437e into master Oct 8, 2024
7 checks passed
@nimakarimipour nimakarimipour deleted the nimak/update-root branch October 8, 2024 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants