bad code green: reject return type constraint if unchecked conversion was applied during applicability check and return type is type parameter

This commit is contained in:
Anna.Kozlova
2016-04-11 21:51:18 +02:00
parent da6de99b11
commit 1466b0155e
5 changed files with 24 additions and 2 deletions
@@ -689,6 +689,12 @@ public class InferenceSession {
public void registerReturnTypeConstraints(PsiType returnType, PsiType targetType) {
returnType = substituteWithInferenceVariables(returnType);
if (myErased) {
final InferenceVariable inferenceVariable = getInferenceVariable(returnType);
if (inferenceVariable != null) {
final PsiSubstitutor substitutor = resolveSubset(Collections.singletonList(inferenceVariable), mySiteSubstitutor);
returnType = substitutor.substitute(inferenceVariable);
if (returnType == null) return;
}
addConstraint(new TypeCompatibilityConstraint(targetType, TypeConversionUtil.erasure(returnType)));
}
else if (FunctionalInterfaceParameterizationUtil.isWildcardParameterized(returnType)) {
@@ -1,5 +1,5 @@
class Foo<T extends Enum> {
public T bar(Class<? extends T> type, String str) {
<error descr="Incompatible types. Found: 'java.lang.Enum', required: 'T'">return Enum.valueOf(type, str);</error>
return Enum.valueOf(<error descr="'valueOf(java.lang.Class<T>, java.lang.String)' in 'java.lang.Enum' cannot be applied to '(java.lang.Class<capture<? extends T>>, java.lang.String)'">type</error>, str);
}
}
@@ -4,7 +4,7 @@ abstract class Group {
}
public <T extends Category> T get(Key<T> key) {
<error descr="Incompatible types. Found: 'Category', required: 'T'">return getCategory(key);</error>
return getCategory<error descr="'getCategory(Key<R>)' in 'Group' cannot be applied to '(Key<T>)'">(key)</error>;
}
public abstract <R extends Category<R>> R getCategory(Key<R> key);
@@ -0,0 +1,12 @@
import java.util.List;
class Test {
<T> T foo(List<T> l) {
return l.get(0);
}
void m(List l){
<error descr="Incompatible types. Found: 'java.lang.Object', required: 'boolean'">boolean foo = foo(l);</error>
<error descr="Incompatible types. Found: 'java.lang.Object', required: 'java.lang.String'">String s = foo(l);</error>
}
}
@@ -429,6 +429,10 @@ public class GraphInferenceHighlightingTest extends LightDaemonAnalyzerTestCase
doTest();
}
public void testPrimitiveTypeInReturnConstraintWithUncheckedConversion() throws Exception {
doTest();
}
public void testVariableNamesOfNestedCalls() throws Exception {
IdeaTestUtil.setTestVersion(JavaSdkVersion.JDK_1_8, getModule(), getTestRootDisposable());
String filePath = BASE_PATH + "/" + getTestName(false) + ".java";