eclipse-jdt / eclipse-jdt/eclipse.jdt.core

javac incompatible missing error: "Cannot cast from List<Object> to T"

Open
#1,332 10 comments 0 reactions 0 assignees View on GitHub
bug compiler javac
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 10h
Merged PRs (30d)
49

Description

The following code compiles with ecj but fails with javac `error: incompatible types: List cannot be converted to T`:

```
> T interfaced() {return (T) Collections.emptyList();}
```
Interestingly it seems only to be incompatible when an interface involved while slightly other variants behave same as in javac:
```
package a;

import java.util.Collections;
import java.util.List;

@SuppressWarnings("unchecked")
public class A {
//ecj: Cannot cast from List to T:
//javac: error: incompatible types: List cannot be converted to T:
> T custom() {return (T) Collections.emptyList();}
//ecj: Cannot cast from List to T:
//javac: error: incompatible types: List cannot be converted to T:
> T abstrct() {return (T) Collections.emptyList();}
//ecj: passes:
//javac: error: incompatible types: List cannot be converted to T:
> T interfaced() {return (T) Collections.emptyList();}
//ecj: passes:
//javac: passes:
> T objectable() {return (T) Collections.emptyList();}
}

```

Who is right jdt or javac?

I would also be interested in clean compile-able solution - which does not need `@SuppressWarnings("unchecked") `and/or typecast `(T)`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.