Issue with Comparator.comparing

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Dec 17 12:49:38 UTC 2018


On 17/12/2018 12:36, B. Blaser wrote:
> This assertion being wrong in your example as 'T = ? super Foo<?>' and
> keys aren't always comparable between themselves.

This is where we disagree. You are treating this as "you are calling a 
method with the bad input" whereas I'm saying "it's an issue you are 
able to call the method in the first place".

As you say, I have a ? super Foo<?> on my hand, but, thanks to capture 
conversion, we can trick the compiler into thinking that all things 
coming out of the function we pass to Comparator::comparing will _all_ 
have the same captured type, which is wrong. In reality, '? super 
Foo<?>' gets re-captured on every call of the method reference, so 
assuming that all method reference call will yield the same captured 
type is simply wrong.

Challenge: can you find an example which ends up with same behavior 
(ClassCastException) that does _not_ involve method references and/or 
lambdas?

Maurizio




More information about the compiler-dev mailing list