Files
openide/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/switchExpressions/SimpleInferenceCases.java
Anna Kozlova 8475ba3e51 switch expressions 2.0: update CFG & inference
GitOrigin-RevId: 8f83e7c2c57819e34c4cb8fa5d44a863b0b58532
2019-07-02 06:52:16 +03:00

121 lines
3.7 KiB
Java

import java.util.function.*;
class MyTest {
<T> T foo(T t) {
return t;
}
<T> T foo(Supplier<T> t) {
return t.get();
}
<T> T foo(IntSupplier t) {
return null;
}
static <K> K bar() {
return null;
}
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 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 s6 = switch (i) {
case 1 -> <error descr="Bad type in switch expression: int cannot be converted to java.lang.String">2</error>;
default -> {
yield <error descr="Bad type in switch expression: int cannot be converted to java.lang.String">1</error>;
}
};
Supplier<String> stringSupplier = switch (i) {
default -> {
yield () -> <error descr="Bad return type in lambda expression: int cannot be converted to String">1</error>;
}
};
String s7 = switch (i) {
case 1: {
yield <error descr="Bad type in switch expression: int cannot be converted to java.lang.String">1</error>;
}
default: {
int i1 = switch (0) {
default -> {
yield 1;
}
};
yield "";
}
};
}
void switchChain(final int i) {
String s = switch (i) {
default -> switch (0) {
default -> {
yield <error descr="Bad type in switch expression: int cannot be converted to java.lang.String">1</error>;
}
};
};
String s1 = switch (i) {
default -> {
yield switch (0) {
default -> {
yield <error descr="Bad type in switch expression: int cannot be converted to java.lang.String">1</error>;
}
};
}
};
String s2 = switch (i) {
default: {
yield switch (0) {
default -> {
yield <error descr="Bad type in switch expression: int cannot be converted to java.lang.String">1</error>;
}
};
}
};
String s3 = switch (0) {
default: {
yield switch (1) {
case 2: {
System.out.println();
int inside_switch = switch (8) {
default:
yield 1;
};
}
case 1:
if (i > 3) yield <error descr="Bad type in switch expression: int cannot be converted to java.lang.String">3</error>;
case 0:
try {
yield <error descr="Bad type in switch expression: int cannot be converted to java.lang.String">42</error>;
} finally {
//do nothing
}
default:
yield <error descr="Bad type in switch expression: int cannot be converted to java.lang.String">1</error>;
};
}
};
Runnable r = () -> <error descr="Target type for switch expression cannot be void">switch</error>(0) {
default -> throw new IllegalArgumentException();
};
}
static void test(boolean b, int i) {
Class<?> c = (b ?
switch (i) {
case 1 -> true;
default -> 0;
} : 1).getClass();
System.out.println(c.getCanonicalName());
}
}