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

Add guardrails to prevent and correct wrong output from JavaTypeCorrect #339

Merged
merged 37 commits into from
Jul 30, 2024

Conversation

kelloggm
Copy link
Collaborator

@kelloggm kelloggm commented Jul 26, 2024

Incorporates #338, so don't review until that is merged.

I'm not sure if we want to merge this. It avoids some bad behavior on the NA-176 test, and we could probably expand it in the future for other, similar kinds of problems. However, it definitely does not address the root cause: why is Specimin putting itself in a situation where javac insists that a synthetic class must be compatible with String?

I'm going to continue my investigation, but I'm leaving this PR here because I may want to merge it later if the fix for the root cause is too complex and/or impracticle.

This PR makes a series of changes to how our javac type correction algorithm (in the JavaTypeCorrect class) handles complex cases. That algorithm is fundamentally imprecise, so the changes in this PR should be thought of as incremental improvements to the existing design that avoid some of its worst pathologies. In particular, this PR:

  • prevents JavaTypeCorrect from ever attempting to make a class extend java.lang.String, which is final;
  • makes it generally more difficult for the SyntheticUnconstrainedType type variable to propagate beyond the return type to which it is applied; and,
  • replaces parameters whose types are synthetic return types that get replaced by SyntheticUnconstrainedType with java.lang.Object. Previously, these types would hang around and make the output non-compilable, since they'd been marked as unused.

Together, these changes make NullAway-176 reproducible, based on my local testing.

@kelloggm kelloggm changed the title Add guardrail to prevent incorrect output from JavaTypeCorrect Add guardrails to prevent and correct wrong output from JavaTypeCorrect Jul 29, 2024
@kelloggm kelloggm requested a review from theron-wang July 29, 2024 18:02
@kelloggm kelloggm marked this pull request as ready for review July 29, 2024 18:03
Copy link
Collaborator

@theron-wang theron-wang left a comment

Choose a reason for hiding this comment

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

Looks good to me, aside from one minor comment!

@@ -104,6 +107,24 @@ public Map<String, String> getClassAndUnresolvedInterface() {
* @return the map described above.
*/
public Map<String, String> getExtendedTypes() {
// Before returning, purge any entries that are obviously bad according to
// the following simple heuristic(s):
// * don't extend known-final classes from the JDK, like java.lang.String.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This comment sounds like java.lang.String isn't the only known-final class in the JDK, but the code below only includes that case. Are there supposed to be more or should this comment be adjusted for clarity?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I thought we might want to add more in the future, so I future-proofed the comment. Your point is a good one, though - what we should do is look up a list, and then add that to JavaLangUtils. I'll do that now.

Copy link
Collaborator

@theron-wang theron-wang left a comment

Choose a reason for hiding this comment

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

LGTM, one small comment change.

@kelloggm kelloggm enabled auto-merge (squash) July 29, 2024 21:46
@kelloggm kelloggm merged commit 5ca6b72 into main Jul 30, 2024
2 checks passed
@kelloggm kelloggm deleted the extendsString branch July 30, 2024 15:04
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.

2 participants