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

Fixes for type attribution and TypeUtils#isAssignableTo() #4427

Merged
merged 4 commits into from
Aug 21, 2024
Merged

Conversation

knutwannheden
Copy link
Contributor

@knutwannheden knutwannheden commented Aug 18, 2024

Try to fix #4423 by correcting the type attribution for some generics and the corresponding type assignability check.

Try to fix #4423 by correcting the type attribution for some generics and the corresponding type assignability check.

Fixes: #4423
@knutwannheden
Copy link
Contributor Author

knutwannheden commented Aug 18, 2024

@timtebeek I believe this will cause some test failures, but I do believe that there is currently something missing in the type attribution, which causes the test from #4423 to fail.

This is WIP and I am not sure when I will be able to pick this up. Maybe you find the time 😉

@timtebeek timtebeek self-requested a review August 19, 2024 14:15
@timtebeek
Copy link
Contributor

Indeed seeing three new falures; quick link to one of them here for reference.
:rewrite-java-test:test org.openrewrite.java.JavaTemplateSubstitutionsTest

diff --git a/Test.java b/Test.java	
index b025685..8109464 100644	
--- a/Test.java	
+++ b/Test.java	
@@ -1,6 +1,6 @@ 	
 import java.util.Map;	
 class Test {	
  void test(Map<String, ?> map) {	
-     System.out.println(map.get("test"));	
+     System.out.println(__P__.<error> << captured wildcard>>/*__p0__*/p();	
  }	
 }	

@timtebeek timtebeek added bug Something isn't working enhancement New feature or request labels Aug 19, 2024
@knutwannheden
Copy link
Contributor Author

@timtebeek I pushed another commit. Let's see if this helps. Got any idea if any recipes have bugs which can be explained by this?

@timtebeek
Copy link
Contributor

timtebeek commented Aug 20, 2024

@timtebeek I pushed another commit. Let's see if this helps. Got any idea if any recipes have bugs which can be explained by this?

Thanks a lot! I don't recall us using JavaTemplate.matches much outside of Refaster recipes, and within Refaster recipes I think we've mostly then had cases that failed to match. That's still a bug, but not as impactful as a false positive match would be. I do expect to see improvement to the cases matched through the ErrorProne Support set of recipes, and being able to leverage those more for our migrations as we've started to do in rewrite-testing-frameworks.

There had been a few earlier attempts at recipes that failed to match that could now be reevaluated:

Would you need me to apply the same fixes to the other versions of Java parsers?

@knutwannheden
Copy link
Contributor Author

Yes, I believe we should apply the same fix to the other Java parsers.

anandfresh

This comment was marked as spam.

@knutwannheden
Copy link
Contributor Author

knutwannheden commented Aug 21, 2024

Also note that the bug could affect any recipes which direcly or indirectly use TypeUtils.isAssignableTo(). So this isn't restricted to Refaster or JavaTemplate.matches(), but it is still an uncommon corner case.

@timtebeek timtebeek marked this pull request as ready for review August 21, 2024 07:43
Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Thanks a lot for finding some time to squeeze this in! Should make those matchers a lot more effective, and unlocks more reuse through Refaster.

@timtebeek
Copy link
Contributor

Seeing one downstream test failure (so far) in rewrite-static-analysis
https://github.com/openrewrite/rewrite-static-analysis/blob/195fbf806ce214305d59d92e81bf2bef7401cf9c/src/test/java/org/openrewrite/staticanalysis/ExplicitLambdaArgumentTypesTest.java#L618-L648

ExplicitLambdaArgumentTypesTest > extendsConstraint() FAILED
    org.opentest4j.AssertionFailedError: [Unexpected result in "com/test/A.java":
    diff --git a/com/test/A.java b/com/test/A.java
    index b70fd0e..46a199b 100644
    --- a/com/test/A.java
    +++ b/com/test/A.java
    @@ -4,6 +4,6 @@ 
 
     class A {
         void foo(List<? extends A> a) {
    -        a.forEach((A it) -> { });
    +        a.forEach((<captured wildcard> it) -> { });
         }
     }
    \ No newline at end of file
    ] 
    expected: 
      "package com.test;
  
      import java.util.List;
  
      class A {
          void foo(List<? extends A> a) {
              a.forEach((A it) -> { });
          }
      }"
     but was: 
      "package com.test;
  
      import java.util.List;
  
      class A {
          void foo(List<? extends A> a) {
              a.forEach((<captured wildcard> it) -> { });
          }
      }"
        at app//org.openrewrite.test.RewriteTest.assertContentEquals(RewriteTest.java:616)
        at app//org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:507)
        at app//org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:132)
        at app//org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:127)
        at app//org.openrewrite.staticanalysis.ExplicitLambdaArgumentTypesTest.extendsConstraint(ExplicitLambdaArgumentTypesTest.java:621)

timtebeek added a commit to openrewrite/rewrite-static-analysis that referenced this pull request Aug 21, 2024
@knutwannheden
Copy link
Contributor Author

Looks like it requires slightly more work.

knutwannheden added a commit that referenced this pull request Aug 23, 2024
The change in the recent PR #4427 was a bit overly cautious. We should be able to use `?` as the name in the signature for all captured types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

JavaTemplateSemanticallyEqual fails to match AssertJ's assertThat("foo").isEqualTo("")
3 participants