From 62f847da516cb661547f8d134821bd5a88151fe6 Mon Sep 17 00:00:00 2001 From: "alexey.afanasiev" Date: Wed, 22 Feb 2017 12:23:22 +0300 Subject: [PATCH] IDEA-168578 Broken type inference in range expressions --- .../src/com/intellij/psi/PsiPrimitiveType.java | 5 +++++ .../impl/statements/expressions/TypesUtil.java | 6 +++--- .../groovy/lang/resolve/TypeInferenceTest.groovy | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/java/java-psi-api/src/com/intellij/psi/PsiPrimitiveType.java b/java/java-psi-api/src/com/intellij/psi/PsiPrimitiveType.java index fdc7e16ac80a..8619b80f6c50 100644 --- a/java/java-psi-api/src/com/intellij/psi/PsiPrimitiveType.java +++ b/java/java-psi-api/src/com/intellij/psi/PsiPrimitiveType.java @@ -139,6 +139,11 @@ public class PsiPrimitiveType extends PsiType.Stub { return unboxed.annotate(type.getAnnotationProvider()); } + @Nullable + public static PsiPrimitiveType getOptionallyUnboxedType(PsiType type) { + return type instanceof PsiPrimitiveType ? (PsiPrimitiveType)type : getUnboxedType(type); + } + public String getBoxedTypeName() { return ourUnboxedToQName.get(this); } diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/TypesUtil.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/TypesUtil.java index 9291b03bd660..8f408a1af873 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/TypesUtil.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/TypesUtil.java @@ -558,14 +558,14 @@ public class TypesUtil implements TypeConstants { @Nullable private static PsiType getNumericLUB(@Nullable PsiType type1, @Nullable PsiType type2) { - PsiPrimitiveType unboxedType1 = PsiPrimitiveType.getUnboxedType(type1); - PsiPrimitiveType unboxedType2 = PsiPrimitiveType.getUnboxedType(type2); + PsiPrimitiveType unboxedType1 = PsiPrimitiveType.getOptionallyUnboxedType(type1); + PsiPrimitiveType unboxedType2 = PsiPrimitiveType.getOptionallyUnboxedType(type2); if (unboxedType1 != null && unboxedType2 != null) { int i1 = LUB_NUMERIC_TYPES.indexOf(unboxedType1); int i2 = LUB_NUMERIC_TYPES.indexOf(unboxedType2); if (i1 >= 0 && i2 >= 0) { if (i1 > i2) return type1; - if (i2 > i1) return type2; + if (i2 >= i1) return type2; } } return null; diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/TypeInferenceTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/TypeInferenceTest.groovy index 870b99f5dc02..7c8c545a83cf 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/TypeInferenceTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/TypeInferenceTest.groovy @@ -597,6 +597,22 @@ class Any { ''', 'java.lang.Object') } + void testRange() { + doTest('''\ + def m = new int[3] + for (ii in 0..i + } +''', 'java.lang.Integer') + + doTest('''\ + def m = new int[3] + for (ii in m.size()..< m[0]) { + print ii + } +''', 'java.lang.Integer') + } + void testUnary() { doExprTest('~/abc/', 'java.util.regex.Pattern') }