RFR: 8262889: Compiler implementation for Record Patterns [v2]

Jan Lahoda jlahoda at openjdk.java.net
Fri May 6 17:43:04 UTC 2022


On Fri, 6 May 2022 15:44:22 GMT, Gavin Bierman <gbierman at openjdk.org> wrote:

> > From the JLS specdiff
> > > If the type R names a generic record class then it is a compile-time error if R is not a parameterized type.
> > 
> > 
> > The following snippet raises a `MatchException`. Shouldn't it be a compile-time error?
> > ```
> >         Box<String> r = new Box<>(null);
> > 
> >         switch (r) {
> >             case Box(String s):
> >                 System.out.println("match");
> >         }
> > ```
> > 
> > 
> >     
> >       
> >     
> > 
> >       
> >     
> > 
> >     
> >   
> > If this is Ok and my understanding is wrong, then why that raises an exception at all? I can make it work (as an unconditional) if I define the Box as `record Box<T extends String>` and now I am confused...
> > ping @GavinBierman @lahodaj
> 
> A couple of issues here. (1) This should be a compile-time error. (2) upon investigation I think there is a bug with the pattern matching code, because the compiler is currently saying that the pattern match here: `Box<String> bs = new Box<>(null); if (bs instanceof Box<String>(String s)) { System.out.println("match!"); }` does not succeed. (It should do). The `MatchException` you are seeing is that the exhaustive pattern switch has no matching label (if you put in a default, you don't get the exception).

Right. Will fix. Sorry for that.

-------------

PR: https://git.openjdk.java.net/jdk/pull/8516


More information about the compiler-dev mailing list