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

Java 10+ Local-Variable Type Inference #218

Merged
merged 24 commits into from
Jun 16, 2023

Conversation

MBoegers
Copy link
Contributor

@MBoegers MBoegers commented May 3, 2023

Implement a recipe for local variable type inference as discussed here: #217

This Draft contains the test suite, as tim suggested.

@timtebeek timtebeek added the recipe Recipe requested label May 3, 2023
@timtebeek timtebeek linked an issue May 3, 2023 that may be closed by this pull request
@timtebeek
Copy link
Contributor

Might want to explicitly state what we want to do with primitives through tests; that could help indicate what we end up doing is intentional; great start!

Perhaps as a suggestion: right now your tests are a mix of should & should not apply recipe. Could be helpful to separate those into nested classes each such that you can drop the prefix.

@MBoegers
Copy link
Contributor Author

MBoegers commented May 3, 2023

Yes agree primitives are missing, see my comment here.

You suggest grouping the test by applicability instead, currently they are grouped as in the issue.
I can live with both ;)

@timtebeek
Copy link
Contributor

Yes agree primitives are missing, see my comment here.

👍🏻

You suggest grouping the test by applicability instead, currently they are grouped as in the issue. I can live with both ;)

Hah; we can do both with Nested within Nested. Especially helpful if there's a lot of cases to visually separate, which it appears we might end up doing here: both covering cases where we do want to make changes and exclusions where we do not want to make changes. And then logically group those based on their applicability to primitives, generics, etc.. at the top level.

@MBoegers
Copy link
Contributor Author

GH and I had issues the last days.. But now I updated the Test as we discussed

@timtebeek timtebeek requested review from rpau and knutwannheden May 10, 2023 11:37
@timtebeek
Copy link
Contributor

Thanks a lot @MBoegers ; I'm asking my colleagues to have a look! :)

@MBoegers
Copy link
Contributor Author

@timtebeek cheers! I have not yet implemented generics, because I felt it really hard to detect them securely. I'd like to get a review for the simple parts and aim for generics a bit later ;)

Copy link
Contributor

@knutwannheden knutwannheden left a comment

Choose a reason for hiding this comment

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

I think the PR looks really good! Some small comments:

  • I found a problem with compound definitions, which currently would do the wrong thing.
  • The null-type checking can also be improved
  • I also noticed that it would currently not apply to code inside static and non-static initializers
  • It might be cool to also support the variables of enhanced for-loops (e.g. for (var s : List.of("a", "b")))

@knutwannheden
Copy link
Contributor

@MBoegers Thanks for looking into this! Regarding your question:

Would I be an acceptable way to implement tests as marker and postpone the implementation to a followup?

Definitely! The recipe doesn't need to be perfect from day one. We do however have the do no harm guideline which boils down to that a recipe should rather not make any change than a wrong change. This can in itself already by quite a challenge (and we certainly still have some recipes which violate this rule), but we should make a best effort to not produce any wrong code. So if we can fix the few cases I think I found, I think we can go ahead and integrate this recipe and do improvements in follow-up commits.

@MBoegers
Copy link
Contributor Author

I've updated the PR, as you may see I excluded some features for now. They should be a new issue, otherwise this PRs lifetime explodes :D

What is possible by now:

  • direct assignments and reassignments for primitive and boxed primitive
  • inside static and instance initializer blocks as well as methods
  • skipping of var usage, fields definitions, method parameters, multiple variable declarations and null assignments

The leftovers:

  • Generics and Method invocation in general, I think we should spend some more time defining what should work for generics and especially what not
  • Ternary operator, similar to generics, this needs some more spec work
  • Enhanced for loop, is tightly tied to generics
  • Splitting the recipe into small recipes, handling each sub feature. Like Methods, Primitives, Ternary and the easy direct assignments

Should I write some issues like I have them in mind for further refinement?

@MBoegers MBoegers force-pushed the 217-usage_of_var branch from 14f0bcd to 55a3d4b Compare May 24, 2023 12:48
@MBoegers
Copy link
Contributor Author

MBoegers commented Jun 14, 2023

I've extracted to open work for ternary operator support to #237 and support for method invocation to #236.
Further, I refactored the recipe into two separate recipes and a combining yml recipe and rebases.
I need a review to aim for merging

…tor DeclarationCheck to be package private Utility. Prio used UseVarKeywordTest remains as prove of correct refactoring.
Copy link
Contributor

@knutwannheden knutwannheden left a comment

Choose a reason for hiding this comment

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

Looks very good. It will be interesting to see what turns up when we run this recipe in the SaaS.

@knutwannheden
Copy link
Contributor

@MBoegers Looks like you just committed some changes. Some of my comments may not apply anymore...

Copy link
Contributor

@knutwannheden knutwannheden left a comment

Choose a reason for hiding this comment

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

Very nice! @timtebeek Anything else you want to add or should we merge it?

@knutwannheden knutwannheden merged commit b38a99b into openrewrite:main Jun 16, 2023
@knutwannheden
Copy link
Contributor

@MBoegers Thanks again for your contribution!

@MBoegers
Copy link
Contributor Author

Thanks to you both for your help

@timtebeek
Copy link
Contributor

Yes thanks a lot! I know it was a long one to get through, with a lot of discovery along the way, but really appreciate the effort!

@knutwannheden
Copy link
Contributor

@MBoegers FYI: I integrated two small fixes for the recipe: ff4d3dc and 688ce47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe Recipe requested
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Java 10+ Local-Variable Type Inference should be used RSPEC-6212
3 participants