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
Unexpected cast compile error for generic type argument #837
Comments
|
The cast is determined to be between "provably distinct" types by this stack trace: For whatever reason When using I.e. this code exits for the For the When using I.e. this change "fixes" the bug: Will have to check why the parameters are turned around for the The method comment explaining this: I'm not sure how much sense it makes to check whether
|
|
Makes me wonder what The compile of the method invocation seems to be comparing different things (different to what the cast check compares): At the deepest stack frame, And Whereas the cast check ends up comparing And |
Given a generic type defined as "<Z extends Comparable<? super Z>>", the compiler fails to cast List<Z> to e.g. List<String>. Despite String fulfilling the bound requirements for Z (String implements Comparable<String>). For this particular case, the bug occurs due to: For the "? super Z" bound, TypeBinding.isTypeArgumentContainedBy() calls isCompatibleWith() on String, with argument the lower bound "?". Then in ReferenceBinding.isCompatibleWith0() the equivalence check fails, since String doesn't evaluate to being equivalent to "?" - String is not a parameterized type. This change adds an extra equivalence check, ensuring "?" is tested for equivalence to String (or any other type that would fit the generic argument bounds). Fixes: eclipse-jdt#837 Signed-off-by: Simeon Andreev <simeon.danailov.andreev@gmail.com>
|
FYI, cast compatibility is specified in JLS 5.1.6.1 which refers to the definition of "provably distinct" inside JLS 4.5. Here's a group of bugs that motivated our existing code:
Apparently, Philipe was struggling with a fuzzy spec and worked from additional information about intended spec changes. To find out the intended semantics it should be worth looking into https://bugs.openjdk.org/browse/JDK-6790039 and children. E.g., Mauricio mentions replacing captures with wildcards as essential in javac's implementation, which is absent from the spec as far as I can see. |
See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=562230
To reproduce, paste the following snippet in Eclipse:
Compile and run with
javac/java(OpenJDK 17), observe no errors:With
ecj/Eclipse though, observe compile error:The problem is created here:
Changing the generics type argument to
Z extends Comparable<? extends Z>results in no compile error withecj.The text was updated successfully, but these errors were encountered: