Open jubax opened 2 years ago
This code uses a feature introduced in our implementation via https://bugs.eclipse.org/bugs/show_bug.cgi?id=562392.
Perhaps @jarthana can provide some insights?
One of my student stumble on the same issue yesterday but with an instanceof and a type pattern.
The JLS section 15.20.2 [1] says The RelationalExpression must be downcast compatible with the ReferenceType (5.5 [1]), or a compile-time error occurs.
i.e. expr instanceof B
The problem is that ecj considers that it's an unsafe cast
List<? extends A
But it should not.
[1] https://docs.oracle.com/javase/specs/jls/se19/html/jls-5.html#jls-5.5
Will consider this for 4.27.
Thanks Jay !
OK, I see the mistake i did when introducing the type parameters in instanceof expresssion: I should have gone with the same error message that we used for unsafeCast rather than introducing a new one. The new one is not configurable and will always result in an error. I will fix this shortly.
@stephan-herrmann I was thinking of picking some of the code from CastExpression for this (around line number 655 inside if (isLegal) {
and noticed some null annotation related code. Should we do the same for instanceof expression as well? If it's simply a matter of copy-pasting or just making the code common for reuse, I could include that as well in my patch.
I am thinking of splitting these two conditions with the latter being used for issuing the unsafe cast warning:
if (!isLegal || (this.bits & ASTNode.UnsafeCast) != 0) {
@stephan-herrmann Stephan, I am a bit baffled by this testcase:
public void foo(A<? extends T> obj) { if (obj instanceof B<T> ) { // } }
Javac issues an error here and with my above fix, we will only get the warning. I am using the existing checkCastTypesCompatibility()
for this check. Is this expected that this method returns true
for this code?
I've unset 4.27 milestone because it is too late for 4.27. Please consider to set a new one if you plan to contribute to 4.28.
Sorry, couldn't complete this in 4.27. Will keep it on my radar for 4.28.
The following code compiles with
javac
, but not with eclipse:Is there a workaround without changing the code?