java highlighting: provide better incompatible types message on failed inference

GitOrigin-RevId: 5f97ec808f753d9ca40c417704ec93a802512745
This commit is contained in:
Anna Kozlova
2019-07-08 10:43:57 +02:00
committed by intellij-monorepo-bot
parent 6a7856d5c6
commit afa0706bfc
28 changed files with 87 additions and 101 deletions
@@ -7,7 +7,6 @@ abstract class Test {
abstract <T> T test(Serializable type);
private void call(){
String s = <error descr="Incompatible types. Required String but 'test' was inferred to T:
no instance(s) of type variable(s) exist so that String[] conforms to String">test(String[].class);</error>
String s = <error descr="Incompatible types. Found: 'java.lang.String[]', required: 'java.lang.String'">test(String[].class);</error>
}
}
@@ -3,7 +3,6 @@ import java.util.Map;
class C1<K,V> {
class C2<T> {C2(Comparator<? super T> pComparator) {}}
C1(Comparator<? super K> pComparator) {new C2<Map.Entry<K,V>>(<error descr="Incompatible types. Required Comparator<? super Entry<K, V>> but 'm' was inferred to Comparator<Entry<T, ?>>:
Incompatible equality constraint: K and capture of ? super K">m(pComparator)</error>);}
C1(Comparator<? super K> pComparator) {new C2<Map.Entry<K,V>>(<error descr="Incompatible types. Found: 'java.util.Comparator<java.util.Map.Entry<capture<? super K>,?>>', required: 'java.util.Comparator<? super java.util.Map.Entry<K,V>>'">m(pComparator)</error>);}
static <T> Comparator<Map.Entry<T,?>> m(Comparator<T> pKeyComparator) {return null;}
}
@@ -1,6 +1,5 @@
class Foo<T extends Enum> {
public T <error descr="Invalid return type">bar</error>(Class<? extends T> type, String str) {
return <error descr="Incompatible types. Required T but 'valueOf' was inferred to T:
Incompatible types: Enum is not convertible to T">Enum.valueOf(type, str);</error>
return <error descr="Incompatible types: Enum is not convertible to T">Enum.valueOf(type, str);</error>
}
}
@@ -11,6 +11,6 @@ abstract class A1{
abstract <T> T baz(List<? super T> a);
void bar(List<?> x){
String o = <error descr="Type parameter T has incompatible upper bounds: capture of ? and String">baz(x);</error>
String o = <error descr="Incompatible types. Found: 'capture<?>', required: 'java.lang.String'">baz(x);</error>
}
}
@@ -10,8 +10,7 @@ class Test {
public void test(Set<MyConsumer> set) {
@SuppressWarnings("unchecked")
Map<Parent, MyConsumer<Parent>> map = <error descr="Incompatible types. Required Map<Parent, MyConsumer<Parent>> but 'create' was inferred to Map<S, T>:
Incompatible equality constraint: MyConsumer<Test.Parent> and MyConsumer">create(set);</error>
Map<Parent, MyConsumer<Parent>> map = <error descr="Incompatible types. Found: 'java.util.Map<java.lang.Object,Test.MyConsumer>', required: 'java.util.Map<Test.Parent,Test.MyConsumer<Test.Parent>>'">create(set);</error>
}
@@ -4,8 +4,7 @@ abstract class Group {
}
public <T extends Category> T <error descr="Invalid return type">get</error>(Key<T> key) {
return <error descr="Incompatible types. Required T but 'getCategory' was inferred to R:
Incompatible types: Category is not convertible to T">getCategory(key);</error>
return <error descr="Incompatible types: Category is not convertible to T">getCategory(key);</error>
}
public abstract <R extends Category<R>> R getCategory(Key<R> key);
@@ -10,8 +10,7 @@ class NachCollections<K,V> {
Collection<? super Map.Entry<K,V>> c2,
Consumer<Map.Entry<K, V>> a) {
c1.forEach(consumer(a));
c2.forEach(<error descr="Incompatible types. Required Consumer<? super capture of ? super Entry<K, V>> but 'consumer' was inferred to Consumer<Entry<K1, V1>>:
no instance(s) of type variable(s) K1, V1 exist so that capture of ? super Entry<K, V> conforms to Entry<K1, V1>">consumer(a)</error>);
c2.forEach(<error descr="Incompatible types. Found: 'java.util.function.Consumer<java.util.Map.Entry<K,V>>', required: 'java.util.function.Consumer<? super capture<? super java.util.Map.Entry<K,V>>>'">consumer(a)</error>);
}
}
@@ -7,10 +7,8 @@ public class Sample {
<B> B bar(G<B> gb) {return null;}
void f(G1 g1) {
G<String> l11 = <error descr="Incompatible types. Required G<String> but 'bar' was inferred to B:
Incompatible types: Object is not convertible to G<String>">bar(g1);</error>
String l1 = <error descr="Incompatible types. Required String but 'bar' was inferred to B:
Incompatible types: Object is not convertible to String">bar(g1);</error>
G<String> l11 = <error descr="Incompatible types. Found: 'java.lang.Object', required: 'Sample.G<java.lang.String>'">bar(g1);</error>
String l1 = <error descr="Incompatible types. Found: 'java.lang.Object', required: 'java.lang.String'">bar(g1);</error>
Object o = bar(g1);
}
}
@@ -2,26 +2,14 @@ class Test {
{
Holder h = null;
Result<String> r1 = new Result<error descr="Cannot infer arguments"><></error>(h);
Result<String> r2 = <error descr="Incompatible types. Required Result<String> but 'create' was inferred to Result<K>:
no instance(s) of type variable(s) exist so that Holder conforms to String
inference variable K has incompatible bounds:
equality constraints: String
lower bounds: Holder">Result.create(h);</error>
Result<String> r2 = <error descr="Incompatible types. Found: 'Result<Holder>', required: 'Result<java.lang.String>'">Result.create(h);</error>
Holder dataHolder = null;
Result<String> r3 = new Result<error descr="Cannot infer arguments"><></error>(new Holder<>(dataHolder));
Result<String> r4 = <error descr="Incompatible types. Required Result<String> but 'create' was inferred to Result<K>:
no instance(s) of type variable(s) exist so that Holder conforms to String
inference variable K has incompatible bounds:
equality constraints: String
lower bounds: Holder">Result.create(new Holder<>(dataHolder));</error>
Result<String> r4 = <error descr="Incompatible types. Found: 'Result<Holder>', required: 'Result<java.lang.String>'">Result.create(new Holder<>(dataHolder));</error>
Result<String> r5 = new Result<error descr="Cannot infer arguments"><></error>(Holder.create(dataHolder));
Result<String> r6 = <error descr="Incompatible types. Required Result<String> but 'create' was inferred to Result<K>:
no instance(s) of type variable(s) exist so that Holder conforms to String
inference variable K has incompatible bounds:
equality constraints: String
lower bounds: Holder">Result.create(Holder.create(dataHolder));</error>
Result<String> r6 = <error descr="Incompatible types. Found: 'Result<Holder>', required: 'Result<java.lang.String>'">Result.create(Holder.create(dataHolder));</error>
}
}
@@ -3,14 +3,10 @@ import java.util.Map;
class Test {
public static void main(String[] args) {
Map<Object, Object> b = <error descr="Incompatible types. Required Map<Object, Object> but 'newMapTrie' was inferred to T:
Incompatible equality constraint: Byte and Object">newMapTrie();</error>
Map<Object, Map<Object, Object>> c = <error descr="Incompatible types. Required Map<Object, Map<Object, Object>> but 'newMapTrie' was inferred to T:
Incompatible equality constraint: Byte and Object">newMapTrie();</error>
Map<Object, Map<Object, Map<Object, Object>>> d = <error descr="Incompatible types. Required Map<Object, Map<Object, Map<Object, Object>>> but 'newMapTrie' was inferred to T:
Incompatible equality constraint: Byte and Object">newMapTrie();</error>
Map<Object, Map<Object, Map<Object, Map<Object, Object>>>> e = <error descr="Incompatible types. Required Map<Object, Map<Object, Map<Object, Map<Object, Object>>>> but 'newMapTrie' was inferred to T:
Incompatible equality constraint: Byte and Object">newMapTrie();</error>
Map<Object, Object> b = <error descr="Incompatible types. Found: 'T', required: 'java.util.Map<java.lang.Object,java.lang.Object>'">newMapTrie();</error>
Map<Object, Map<Object, Object>> c = <error descr="Incompatible types. Found: 'T', required: 'java.util.Map<java.lang.Object,java.util.Map<java.lang.Object,java.lang.Object>>'">newMapTrie();</error>
Map<Object, Map<Object, Map<Object, Object>>> d = <error descr="Incompatible types. Found: 'T', required: 'java.util.Map<java.lang.Object,java.util.Map<java.lang.Object,java.util.Map<java.lang.Object,java.lang.Object>>>'">newMapTrie();</error>
Map<Object, Map<Object, Map<Object, Map<Object, Object>>>> e = <error descr="Incompatible types. Found: 'T', required: 'java.util.Map<java.lang.Object,java.util.Map<java.lang.Object,java.util.Map<java.lang.Object,java.util.Map<java.lang.Object,java.lang.Object>>>>'">newMapTrie();</error>
}
public static <T extends Map<Byte, T>> T newMapTrie() {
@@ -6,8 +6,7 @@ class Test {
Factory factory = new Factory();
final Class<? extends ClassB> bClass = null;
ClassB b = factory.create(bClass);
String str = <error descr="Incompatible types. Required String but 'create' was inferred to T:
no instance(s) of type variable(s) exist so that capture of ? extends ClassB conforms to String">factory.create(bClass);</error>
String str = <error descr="Incompatible types. Found: 'capture<? extends Test.ClassB>', required: 'java.lang.String'">factory.create(bClass);</error>
}
public static class Factory {
@@ -6,9 +6,7 @@ class Test {
}
void m(List l){
boolean foo = <error descr="Incompatible types. Required boolean but 'foo' was inferred to T:
Incompatible types: Object is not convertible to boolean">foo(l);</error>
String s = <error descr="Incompatible types. Required String but 'foo' was inferred to T:
Incompatible types: Object is not convertible to String">foo(l);</error>
boolean foo = <error descr="Incompatible types. Found: 'java.lang.Object', required: 'boolean'">foo(l);</error>
String s = <error descr="Incompatible types. Found: 'java.lang.Object', required: 'java.lang.String'">foo(l);</error>
}
}
@@ -37,11 +37,7 @@ class Test1 {
if (s.equals("1")) {
return Option.option(1);
} else {
return <error descr="Incompatible types. Required Option<Integer> but 'option' was inferred to Option<T>:
no instance(s) of type variable(s) exist so that String conforms to Integer
inference variable T has incompatible bounds:
equality constraints: Integer
lower bounds: String">Option.option("2");</error>
return <error descr="Incompatible types. Found: 'Test1.Option<java.lang.String>', required: 'Test1.Option<java.lang.Integer>'">Option.option("2");</error>
}
};
}
@@ -12,10 +12,7 @@ class TypeArgsConsistency {
I<Integer> i1 = (i, j) -> i + j;
foo((i, j) -> i + j);
I<Integer> i2 = bar((i, j) -> i + j);
I<Integer> i3 = bar(<error descr="no instance(s) of type variable(s) exist so that String conforms to Integer
inference variable X has incompatible bounds:
equality constraints: Integer
lower bounds: String">(i, j) -> "" + i + j</error>);
I<Integer> i3 = bar(<error descr="Incompatible types. Found: 'TypeArgsConsistency.I<java.lang.Object>', required: 'TypeArgsConsistency.I<java.lang.Integer>'">(i, j) -> "" + i + j</error>);
}
}
@@ -46,10 +43,7 @@ class TypeArgsConsistency2 {
I<Integer> i1 = bar(x -> x);
I1<Integer> i2 = bar1(x -> 1);
I2<String> aI2 = bar2(x -> "");
I2<Integer> aI28 = bar2( <error descr="no instance(s) of type variable(s) exist so that String conforms to Integer
inference variable T has incompatible bounds:
equality constraints: Integer
lower bounds: String">x-> ""</error>);
I2<Integer> aI28 = bar2( <error descr="Incompatible types. Found: 'TypeArgsConsistency2.I2<java.lang.Object>', required: 'TypeArgsConsistency2.I2<java.lang.Integer>'">x-> ""</error>);
I2<Integer> i3 = bar2(x -> x);
I2<Integer> i4 = bar2(x -> foooI());
System.out.println(i4.foo(2));
@@ -6,7 +6,7 @@ import java.util.function.Function;
class Test {
{
final Map<Comparable, List<Collection<?>>> families = sortingMerge(<error descr="Incompatible equality constraint: Integer and Comparable">(s) -> 0</error>);
final Map<Comparable, List<Collection<?>>> families = sortingMerge(<error descr="Incompatible types. Found: 'java.util.Map<C,java.util.List<java.lang.Object>>', required: 'java.util.Map<java.lang.Comparable,java.util.List<java.util.Collection<?>>>'">(s) -> 0</error>);
}
private <C extends Comparable<C>, T> Map<C, List<T>> sortingMerge(Function<T, C> keyFunction) {
@@ -22,11 +22,7 @@ abstract class FooBar<M> {
}
class Test {
<T> List<List<Object>> foo(List<T> objects, Function<T, ?>... functions) {
return <error descr="Incompatible types. Required List<List<Object>> but 'collect' was inferred to R:
no instance(s) of type variable(s) exist so that List<capture of ?> conforms to List<Object>
inference variable T has incompatible bounds:
equality constraints: List<Object>
lower bounds: List<capture of ?>">objects.stream()
return <error descr="Incompatible types. Found: 'java.util.List<java.util.List<capture<?>>>', required: 'java.util.List<java.util.List<java.lang.Object>>'">objects.stream()
.map(object -> Arrays.stream(functions)
.map(fn -> fn.apply(object))
.collect(toList()))
@@ -9,8 +9,7 @@ class Test {
<R> SuperFoo<R> foo(I<R> ax) { return null; }
SuperFoo<String> ls = foo(<error descr="Incompatible types. Required SuperFoo<String> but 'foo' was inferred to SuperFoo<R>:
no instance(s) of type variable(s) exist so that String conforms to Number">() -> new Foo<>()</error>);
SuperFoo<String> ls = foo(<error descr="Incompatible types. Found: 'Test.SuperFoo<java.lang.Number>', required: 'Test.SuperFoo<java.lang.String>'">() -> new Foo<>()</error>);
SuperFoo<Integer> li = foo(() -> new Foo<>());
SuperFoo<?> lw = foo(() -> new Foo<>());
}
@@ -9,8 +9,7 @@ class A {
<error descr="Missing return statement">}</error>);
Map<Integer, Integer> map = Stream.iterate(5, <error descr="Incompatible types. Required Map<Integer, Integer> but 'iterate' was inferred to Stream<T>:
no instance(s) of type variable(s) T exist so that Stream<T> conforms to Map<Integer, Integer>">t -> t + 5</error>);
Map<Integer, Integer> map = Stream.iterate(5, <error descr="Incompatible types. Found: 'java.util.stream.Stream<java.lang.Integer>', required: 'java.util.Map<java.lang.Integer,java.lang.Integer>'">t -> t + 5</error>);
}
@@ -26,7 +26,7 @@ abstract class NoFormalParamTypeInferenceNeeded {
{
map(a -> zip(text -> text));
zip(a -> zip(text -> text));
Integer zip = zip(<error descr="no instance(s) of type variable(s) exist so that Object conforms to Integer">a -> zip(text -> text)</error>);
Integer zip = zip(<error descr="Incompatible types. Found: 'java.lang.Object', required: 'java.lang.Integer'">a -> zip(text -> text)</error>);
}
}
@@ -4,8 +4,7 @@ import java.util.stream.Collectors;
class MyTest {
static Map<String, Meeting> <error descr="Invalid return type">getMeetingsById</error>(List<Meeting> meetings){
return <error descr="Incompatible types. Required Map<String, Meeting> but 'collect' was inferred to R:
no instance(s) of type variable(s) A, A, K, R, T exist so that List<T> conforms to Meeting">meetings.stream()
return <error descr="Incompatible types. Found: 'java.util.Map<java.lang.String,java.util.List<Meeting>>', required: 'java.util.Map<java.lang.String,Meeting>'">meetings.stream()
.collect(Collectors.groupingBy(Meeting::getId));</error>
}
@@ -12,7 +12,7 @@ import java.util.function.Function;
class Test {
{
valueOf(processFirst(<error descr="no instance(s) of type variable(s) exist so that Integer conforms to char[]">x -> x</error>));
valueOf(processFirst(<error descr="Incompatible types. Found: 'java.lang.Object', required: 'char[]'">x -> x</error>));
}
public static <V> V processFirst(Function<Integer,V> f){
@@ -9,7 +9,6 @@ class MyTest {
}
{
new MyTest("", <error descr="Incompatible types. Required int but 'emptyList' was inferred to List<T>:
no instance(s) of type variable(s) T exist so that List<T> conforms to Integer">Collections.emptyList()</error>);
new MyTest("", <error descr="Incompatible types. Found: 'java.util.List<java.lang.Object>', required: 'int'">Collections.emptyList()</error>);
}
}
@@ -6,7 +6,7 @@ import java.util.stream.Stream;
class Test {
void foo() {
log(<error descr="no instance(s) of type variable(s) exist so that TreeSet<String> conforms to String[]">get(TreeSet<String>::new)</error>);
log(<error descr="Incompatible types. Found: 'java.lang.Object', required: 'java.lang.String[]'">get(TreeSet<String>::new)</error>);
}
private void log(String params[]) {
@@ -17,15 +17,13 @@ class MyTest {
void m(int i) {
String s = foo(switch (i) {default -> "str";});
String s1 = <error descr="Incompatible types. Required String but 'foo' was inferred to T:
no instance(s) of type variable(s) exist so that Object conforms to String">foo(switch (i) {case 1 -> new Object(); default -> "str";});</error>
String s1 = <error descr="Incompatible types. Found: 'java.lang.Object', required: 'java.lang.String'">foo(switch (i) {case 1 -> new Object(); default -> "str";});</error>
String s2 = foo(() -> switch (i) {
default -> "str";
});
String s3 = foo(() -> switch (i) {default -> bar();});
String s4 = foo(() -> switch (i) {default -> { yield bar();}});
String s5 = foo(<error descr="Incompatible types. Required String but 'foo' was inferred to T:
no instance(s) of type variable(s) exist so that Integer conforms to String">() -> switch (i) {default -> { yield 1;}}</error>);
String s5 = foo(<error descr="Incompatible types. Found: 'java.lang.Integer', required: 'java.lang.String'">() -> switch (i) {default -> { yield 1;}}</error>);
String s6 = switch (i) {
case 1 -> <error descr="Bad type in switch expression: int cannot be converted to java.lang.String">2</error>;
default -> {