eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
158 stars 126 forks source link

[23] JEP 455: Primitive Types in Patterns, instanceof, and switch (Preview) #2298

Closed mpalat closed 1 month ago

mpalat commented 6 months ago

ref: https://openjdk.org/jeps/455

Summary Enhance pattern matching by allowing primitive type patterns in all pattern contexts, and extend instanceof and switch to work with all primitive types.

spec delta: https://cr.openjdk.org/~abimpoudis/instanceof/latest/

srikanth-sankaran commented 5 months ago

Please include me in all design and implementation discussions and reviews.

As a first step, I suggest we figure out if parser/scanner changes are required by capturing all snippets from the JEP document into test cases - do we have any parse failures if we do that ?

jarthana commented 5 months ago

As a first step, I suggest we figure out if parser/scanner changes are required by capturing all snippets from the JEP document into test cases - do we have any parse failures if we do that ?

Is there an easy/standard way to do that or are you saying we just look for syntax errors?

srikanth-sankaran commented 5 months ago

As a first step, I suggest we figure out if parser/scanner changes are required by capturing all snippets from the JEP document into test cases - do we have any parse failures if we do that ?

Is there an easy/standard way to do that or are you saying we just look for syntax errors?

We can see what errors we get, or better yet whether diagnose parse gets entered while compiling the snippets.

stephan-herrmann commented 5 months ago

Please include me in all design and implementation discussions and reviews.

... and include me on all spec issues :)

srikanth-sankaran commented 3 months ago

When this is scheduled for standardization I will assume ownership - for now, please feel free to engage me for any design or implementation consultation and code review as needed. Thanks

stephan-herrmann commented 3 months ago

When this is scheduled for standardization I will assume ownership

by "standardization" you mean having a stable spec, or maturing from preview to standard?

srikanth-sankaran commented 3 months ago

When this is scheduled for standardization I will assume ownership

by "standardization" you mean having a stable spec, or maturing from preview to standard?

I meant the latter.

mpalat commented 2 months ago

@stephan-herrmann - Would you be able to take this up (partly/completely rest of it?) - I am off for the next few days on an important personal work and cannot spend time on work until I am back [Aug 5th] and may have to take a few days later too. In case you are able to spare time to work on this next week, it would be of great help- Please feel free to take it up partly/completely depending on your availability. [@srikanth-sankaran - open to you as well but since you are out I am not sure about your availability]. Thanks.

mpalat commented 1 month ago

Additional issues:

stephan-herrmann commented 1 month ago

Some - perhaps academic - observations:

Let's hope that such issues will clear up when the feature leaves preview!

mpalat commented 1 month ago

Some - perhaps academic - observations:

  • we already use this class since 988d0b8, @mpalat how did you find out about that class, has it been mentioned in some document or post on the mailing list?

@stephan-herrmann initially I tried implementing this by myself in the generated code which met with limited success. And I should confess that on a comparative check with javac generated code I saw this method and hence decided to use the same since it is already available at runtime.

stephan-herrmann commented 1 month ago

Here's a first attempt of a mapping between spec changes and implementation already done:

stephan-herrmann commented 1 month ago

Let's check for completeness of individual parts:

§5.7 conversions: the part concerning primitives is represented by PrimitiveConversionRoute added in https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2298

Enum PrimitiveConversionRoute :heavy_check_mark: Covers all conversions relating to primitives

Computation in Pattern.findPrimitiveConversionRoute() :heavy_check_mark: Had 3 TODOs, which I'm fleshing out in my current PR #2878. Note that "a widening reference conversion followed by an unboxing conversion" is more an academic thing, see https://mail.openjdk.org/pipermail/amber-spec-experts/2024-August/004204.html and answers by Brian and Rémi.

Code gen for all conversion routes :heavy_check_mark:

Test coverage :heavy_check_mark: Findings from running all compiler regression tests at compliance 23 with coverage enabled:

stephan-herrmann commented 1 month ago

While working on all those type tests and conversions, I repeatedly got entangled in confusing terminology, particularly where the type of a switch selector was named the "expectedType" for each case statement. One such confusion actually surfaced in bogus error messages like this:

        final short s = 100;
        Integer i1 = ...;
        switch (i1)  {
            case s:
        }

ecj reported:

    case s:
         ^
Type mismatch: cannot convert from short to Integer

Nobody has any business converting any short value to Integer. An Integer value is provided and to be matched against (not even converted to) a short value. Taking inspiration from javac I'm correcting the error to

Case constant of type short is incompatible with switch selector type Integer
stephan-herrmann commented 1 month ago

Much has been argued in this issue and in #2869.

At this point I've run out of obvious things to implement or fix, so let's call victory for now - until playing with the feature will reveal shortcomings, but those will live in their own issues.

mpalat commented 1 month ago

Much has been argued in this issue and in #2869.

At this point I've run out of obvious things to implement or fix, so let's call victory for now - until playing with the feature will reveal shortcomings, but those will live in their own issues.

Thanks a lot @stephan-herrmann for stepping in for this issue and also for the exceptionally fast code implementations. [maybe I should fall sick more often :)]. Going through the code and the comments - In case of any (odd) cases found, will raise individual issues.

stephan-herrmann commented 1 month ago

Mentioning #2891 for the story of nested patterns (primitive type pattern nested in record pattern).