Skip to content

Fix several issues in InstanceOfPatternMatch#489

Merged
greg-at-moderne merged 11 commits into
openrewrite:mainfrom
cortlepp:fix/instance-of-pattern-match
Apr 7, 2025
Merged

Fix several issues in InstanceOfPatternMatch#489
greg-at-moderne merged 11 commits into
openrewrite:mainfrom
cortlepp:fix/instance-of-pattern-match

Conversation

@cortlepp

@cortlepp cortlepp commented Mar 26, 2025

Copy link
Copy Markdown
Contributor

What's changed?

  • always use the type in the cast for the new pattern and not the previously checked instanceof type.
  • when a variable name is already present in the source, don't search for duplicates and just take it as it is. Otherwise we would have to make unnecessary changes when the variable happens to be shadowing something else.
  • refuse to pattern-match non-wildcard generic types, as this cannot be checked due to type erasure.

What's your motivation?

Anything in particular you'd like reviewers to focus on?

I'll annotate some areas in the files themselves.

Anyone you would like to review specifically?

@greg-at-moderne

Have you considered any alternatives or workarounds?

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

- always set the clazz of a InstanceOfPatternMatch to the casted class (don't keep the one from the original type check)
- back off if the casted class is a non-wildcard generic
- adapt `Arrays#string()` test to new behavior
…es (openrewrite#483,openrewrite#484)

- doing so causes problems when the preexisting name shadows another variable, because our duplicate checking does not consider shadowing.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Mar 26, 2025
null));
typeCastTypeTree.getType(),
null))
.withClazz(typeCastTypeTree.withPrefix(currentTypeTree.getPrefix()).withId(Tree.randomId()));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It seems superfluous or possibly even wrong that we have to pass the type to the Identifier and also set it as clazz on the J.InstanceOf. Maybe this could be avoided with Java template, but I was unsure how one would look for this case.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I assume you mean a reference to line 263 here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It seems superfluous or possibly even wrong that we have to pass the type to the Identifier and also set it as clazz on the J.InstanceOf

Not sure I understand why you find it superfluous. All identifiers need a type (so that our LST is typed - i.e. all elements have proper type information attributed). And arguably o instanceof String s1 is a declaration of o Identifier and this needs type.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I guess I thought that by giving the identifier a type in line 263, this should determine the type for the entire expression and I should not have to set it again as the clazz. To me it seems like clazz should always be the same as the type passed in line 263, therefore I thought maybe I was using the class wrong or I misunderstood something generally.

Comment thread src/main/java/org/openrewrite/staticanalysis/InstanceOfPatternMatch.java Outdated
@timtebeek timtebeek added the bug Something isn't working label Mar 26, 2025

@timtebeek timtebeek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks a lot for the multiple fixes here @cortlepp ! @greg-at-moderne will have a look shortly and see if we can get this in before the release

@greg-at-moderne greg-at-moderne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I appreciate the great contribution. I consider it a piece of high quality work. Well tested, thoughtful and solving a few overlapping cases in one go!

There's one minor outstanding issue and I think we are good to merge.

(Also, I acknowledge your questions on some of the linked issues, I will get to them).

null));
typeCastTypeTree.getType(),
null))
.withClazz(typeCastTypeTree.withPrefix(currentTypeTree.getPrefix()).withId(Tree.randomId()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I assume you mean a reference to line 263 here.

null));
typeCastTypeTree.getType(),
null))
.withClazz(typeCastTypeTree.withPrefix(currentTypeTree.getPrefix()).withId(Tree.randomId()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It seems superfluous or possibly even wrong that we have to pass the type to the Identifier and also set it as clazz on the J.InstanceOf

Not sure I understand why you find it superfluous. All identifiers need a type (so that our LST is typed - i.e. all elements have proper type information attributed). And arguably o instanceof String s1 is a declaration of o Identifier and this needs type.

Comment thread src/main/java/org/openrewrite/staticanalysis/InstanceOfPatternMatch.java Outdated
Comment thread src/main/java/org/openrewrite/staticanalysis/InstanceOfPatternMatch.java Outdated
@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Mar 26, 2025
- no longer discover generic assignments if they aren't wildcards
- adapt `catTooSpecific` test to again cover the originally reported issue
@greg-at-moderne

Copy link
Copy Markdown
Contributor

Does it make sense to you?
Not trying to put any pressure or anything like this. Just wondering if you are blocked by anything here?

@cortlepp

cortlepp commented Apr 2, 2025

Copy link
Copy Markdown
Contributor Author

Hey, I'm currently on vacation until next week, I'll take a look at it as soon as I return. (Sorry I should've said something, I just forgot)

@greg-at-moderne

greg-at-moderne commented Apr 7, 2025

Copy link
Copy Markdown
Contributor

Not sure what's happening with Github, might be some glitch. I have clicked the button to approve CI run a few times already for this PR.

@cortlepp

cortlepp commented Apr 7, 2025

Copy link
Copy Markdown
Contributor Author

I think the run went through now (on your merge commit).

@greg-at-moderne greg-at-moderne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am satisfied with this change. I appreciate the contribution.

@arodionov

Copy link
Copy Markdown
Contributor

It looks like the current PR create an issue #528

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Archived in project

4 participants