LVTI: accept explicit array types, javadoc (follow IDEA-CR-24690)

This commit is contained in:
Anna Kozlova
2017-09-19 09:28:42 +03:00
parent 6e8bc5e4c1
commit 2f165da164
4 changed files with 29 additions and 22 deletions
@@ -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)
@@ -58,7 +58,7 @@ public interface PsiTypeElement extends PsiElement, PsiAnnotationOwner {
/**
* Returns true when variable is declared as <code>var name;</code>
* 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).
@@ -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;
@@ -4,6 +4,8 @@ class Main {
var a = 1;
<error descr="'var' is not allowed in a compound declaration">var b = 2</error>, <error descr="'var' is not allowed in a compound declaration">c = 3.0;</error>
<error descr="'var' is not allowed as an element type of an array">var d[] = new int[4];</error>
var d1 = new int[] {4};
var d2 = new int[4];
<error descr="Cannot infer type: 'var' on variable without initializer">var e;</error>
var f = <error descr="Array initializer is not allowed here">{ 6 }</error>;
var g = (<error descr="Incompatible types. Found: 'int', required: 'null'">g = 7</error>);