fix casting to primitives according to the spec (IDEA-131107; IDEA-148880)

This commit is contained in:
Anna Kozlova
2015-12-10 17:01:55 +01:00
parent 3f72d428a8
commit 81a6cceb35
5 changed files with 63 additions and 3 deletions
@@ -137,9 +137,9 @@ public class TypeConversionUtil {
if (languageLevel.isAtLeast(LanguageLevel.JDK_1_7)) {
final PsiClassType classType = (PsiClassType)fromType;
final PsiClass psiClass = classType.resolve();
if (psiClass == null || psiClass instanceof PsiTypeParameter) return false;
if (psiClass == null) return false;
final PsiClassType boxedType = ((PsiPrimitiveType)toType).getBoxedType(psiClass.getManager(), psiClass.getResolveScope());
if (boxedType != null && isAssignable(fromType, boxedType)) {
if (boxedType != null && isNarrowingReferenceConversionAllowed(fromType, boxedType)) {
return true;
}
}
@@ -0,0 +1,35 @@
class Test {
public <T> void foo(T valIn){
double val = (double ) valIn;
}
public <T extends Double> void foo1(T valIn){
double val = (double ) valIn;
}
public <T extends String> void foo2(T valIn){
double val = <error descr="Inconvertible types; cannot cast 'T' to 'double'">(double ) valIn</error>;
}
public <T extends S, S extends Double> void foo2(T valIn){
double val = (double ) valIn;
}
}
class Foo<T> {
private T _value;
T getValue() {
return _value;
}
static Foo<?> getFoo() {
return new Foo<>();
}
public static void main(String[] args) {
Foo<?> foo = getFoo();
double value = (double) foo.getValue();
}
}
@@ -0,0 +1,17 @@
class Foo<T> {
private T _value;
T getValue() {
return _value;
}
static Foo<?> getFoo() {
return new Foo<>();
}
public static void main(String[] args) {
Foo<?> foo = getFoo();
double value = <error descr="Inconvertible types; cannot cast 'capture<?>' to 'double'">(double) foo.getValue()</error>;
}
}
@@ -546,7 +546,11 @@ public class GenericsHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testIDEA130243() throws Exception {
doTest(LanguageLevel.JDK_1_7, JavaSdkVersion.JDK_1_7, false);
}
public void testCastingToPrimitive() throws Exception {
doTest(LanguageLevel.JDK_1_7, JavaSdkVersion.JDK_1_7, false);
}
public void testProvablyDistinctForWildcardsWithArrayBounds() throws Exception {
doTest(LanguageLevel.JDK_1_7, JavaSdkVersion.JDK_1_7, false);
}
@@ -942,4 +942,8 @@ public class GenericsHighlighting8Test extends LightDaemonAnalyzerTestCase {
public void testIDEA139096() throws Exception {
doTest();
}
public void testCastingCapturedWildcardToPrimitive() throws Exception {
doTest();
}
}