diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java
index e8531f644de0..13d596ca7df8 100644
--- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java
+++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java
@@ -450,7 +450,7 @@ public class HighlightUtil extends HighlightUtilBase {
.range(variable).create();
}
- if (lType instanceof PsiArrayType) {
+ if (lType instanceof PsiArrayType && !lType.equals(typeElement.getType())) {
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR)
.descriptionAndTooltip("'var' is not allowed as an element type of an array")
.range(variable)
diff --git a/java/java-psi-api/src/com/intellij/psi/PsiTypeElement.java b/java/java-psi-api/src/com/intellij/psi/PsiTypeElement.java
index d91da5e91f8b..1be650b01200 100644
--- a/java/java-psi-api/src/com/intellij/psi/PsiTypeElement.java
+++ b/java/java-psi-api/src/com/intellij/psi/PsiTypeElement.java
@@ -58,7 +58,7 @@ public interface PsiTypeElement extends PsiElement, PsiAnnotationOwner {
/**
- * Returns true when variable is declared as var name;
+ * Returns {@code true} when a variable is declared as {@code var name;}
*
* The actual type should be inferred according to the JEP 286: Local-Variable Type Inference
* (http://openjdk.java.net/jeps/286).
diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/PsiTypeElementImpl.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/PsiTypeElementImpl.java
index 5432fc09ea04..d0ca28db4fb5 100644
--- a/java/java-psi-impl/src/com/intellij/psi/impl/source/PsiTypeElementImpl.java
+++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/PsiTypeElementImpl.java
@@ -98,26 +98,7 @@ public class PsiTypeElementImpl extends CompositePsiElement implements PsiTypeEl
}
else if (PsiUtil.isJavaToken(child, JavaTokenType.VAR_KEYWORD)) {
assert type == null : this;
- if (parent instanceof PsiParameter) {
- PsiElement declarationScope = ((PsiParameter)parent).getDeclarationScope();
- if (declarationScope instanceof PsiForeachStatement) {
- PsiExpression iteratedValue = ((PsiForeachStatement)declarationScope).getIteratedValue();
- if (iteratedValue != null) {
- type = JavaGenericsUtil.getCollectionItemType(iteratedValue);
- }
- }
- }
- else {
- for (PsiElement e = this; e != null; e = e.getNextSibling()) {
- if (e instanceof PsiExpression) {
- if (!(e instanceof PsiArrayInitializerExpression) &&
- !isSelfReferenced((PsiExpression)e, parent)) {
- type = ((PsiExpression)e).getType();
- }
- break;
- }
- }
- }
+ type = inferVarType(parent);
}
else if (child instanceof PsiJavaCodeReferenceElement) {
assert type == null : this;
@@ -174,6 +155,30 @@ public class PsiTypeElementImpl extends CompositePsiElement implements PsiTypeEl
return type;
}
+ private PsiType inferVarType(PsiElement parent) {
+ if (parent instanceof PsiParameter) {
+ PsiElement declarationScope = ((PsiParameter)parent).getDeclarationScope();
+ if (declarationScope instanceof PsiForeachStatement) {
+ PsiExpression iteratedValue = ((PsiForeachStatement)declarationScope).getIteratedValue();
+ if (iteratedValue != null) {
+ return JavaGenericsUtil.getCollectionItemType(iteratedValue);
+ }
+ }
+ }
+ else {
+ for (PsiElement e = this; e != null; e = e.getNextSibling()) {
+ if (e instanceof PsiExpression) {
+ if (!(e instanceof PsiArrayInitializerExpression) &&
+ !isSelfReferenced((PsiExpression)e, parent)) {
+ return ((PsiExpression)e).getType();
+ }
+ return null;
+ }
+ }
+ }
+ return null;
+ }
+
private static boolean isSelfReferenced(PsiExpression initializer, PsiElement parent) {
class SelfReferenceVisitor extends JavaRecursiveElementVisitor {
private boolean referenced = false;
diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advLVTI/SimpleAvailability.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advLVTI/SimpleAvailability.java
index 2bf4984053c7..623df4cc8f88 100644
--- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advLVTI/SimpleAvailability.java
+++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advLVTI/SimpleAvailability.java
@@ -4,6 +4,8 @@ class Main {
var a = 1;
var b = 2, c = 3.0;
var d[] = new int[4];
+ var d1 = new int[] {4};
+ var d2 = new int[4];
var e;
var f = { 6 };
var g = (g = 7);