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

Owning fields should not be compared with string containment #6280

Merged
merged 8 commits into from
Nov 3, 2023

Conversation

kelloggm
Copy link
Contributor

@kelloggm kelloggm commented Nov 1, 2023

fixes #6276

sorry in advance for the mildly annoying merge with #6271 @Calvin-L

private boolean expressionEqualsField(String e, VariableElement field) {
try {
JavaExpression je = StringToJavaExpression.atFieldDecl(e, field, this.checker);
return je instanceof FieldAccess && ((FieldAccess) je).getField().equals(field);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we also need to check if the receiver is this?

Copy link
Contributor Author

@kelloggm kelloggm Nov 1, 2023

Choose a reason for hiding this comment

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

StringToJavaExpression should handle that for us: it uses the supplied location (field, the declaration of the owning field, in this case) and the given string to find the appropriate program element, and associates that with the result.

A potential point of confusion in my implementation is that field is used for two purposes:

  • it's the element corresponding to the owning field, so the object that StringToJavaExpression converts the input string into is compared against it
  • it is used as the location at which StringToJavaExpression should consider the string e to occur in the code: that is, the context

Copy link
Contributor

Choose a reason for hiding this comment

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

Could I write something like

@MustCall("close")
class SneakyDestructor {
  private final @Owning Closeable resource;

  // ...

  @EnsuresCalledMethods(value = "#1.resource", methods = "close")
  void close(SneakyDestructor other) {
    other.close();
  }
}

?

In that case, the field referred to by the expression really is SneakyDestructor.resource, but the destructor clearly doesn't close its own @Owning field.

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 understand what you mean now, thanks. I fleshed out your test case and added it to this PR, and a bit to my surprise the tests are passing.

@Calvin-L
Copy link
Contributor

Calvin-L commented Nov 1, 2023

sorry in advance for the mildly annoying merge

It's no problem. I'm happy to see this fixed. :)

Copy link
Contributor

@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.

LGTM, nice fix!

@msridhar msridhar merged commit c251419 into typetools:master Nov 3, 2023
29 checks passed
@msridhar msridhar deleted the owning-field-string-comparison branch November 3, 2023 04:38
wmdietl pushed a commit to eisop/checker-framework that referenced this pull request Dec 5, 2023
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.

Resource Leak Checker: false negative related to owning fields
3 participants