LVTI: shrink the highlighting range

move functional expression errors to var declaration
This commit is contained in:
Anna.Kozlova
2017-10-02 11:52:51 +02:00
parent bceb7a9a1c
commit aa8576f3ce
3 changed files with 28 additions and 11 deletions
@@ -426,7 +426,14 @@ public class HighlightUtil extends HighlightUtilBase {
if (initializer == null) {
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR)
.descriptionAndTooltip("Cannot infer type: 'var' on variable without initializer")
.range(variable).create();
.range(typeElement).create();
}
if (initializer instanceof PsiFunctionalExpression) {
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR)
.descriptionAndTooltip("Cannot infer type: " + (initializer instanceof PsiLambdaExpression ? "lambda expression" : "method reference") +
" requires an explicit target type")
.range(typeElement).create();
}
PsiLocalVariable[] localVariables = PsiTreeUtil.getChildrenOfType(parent, PsiLocalVariable.class);
@@ -440,14 +447,14 @@ public class HighlightUtil extends HighlightUtilBase {
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)
.range(typeElement)
.create();
}
if (PsiType.NULL.equals(lType)) {
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR)
.descriptionAndTooltip("Cannot infer type: variable initializer is 'null'")
.range(variable).create();
.range(typeElement).create();
}
}
}
@@ -320,7 +320,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
public void visitLambdaExpression(PsiLambdaExpression expression) {
myHolder.add(checkFeature(expression, Feature.LAMBDA_EXPRESSIONS));
final PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
if (parent instanceof PsiExpressionStatement) return;
if (toReportFunctionalExpressionProblemOnParent(parent)) return;
if (!myHolder.hasErrorResults() && !LambdaUtil.isValidLambdaContext(parent)) {
myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression)
.descriptionAndTooltip("Lambda expression not expected here").create());
@@ -1311,7 +1311,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) {
myHolder.add(checkFeature(expression, Feature.METHOD_REFERENCES));
final PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
if (parent instanceof PsiExpressionStatement) return;
if (toReportFunctionalExpressionProblemOnParent(parent)) return;
final JavaResolveResult result;
final JavaResolveResult[] results;
@@ -1460,6 +1460,16 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
}
}
/**
* @return true for {@code functional_expression;} or {@code var l = functional_expression;}
*/
private static boolean toReportFunctionalExpressionProblemOnParent(PsiElement parent) {
if (parent instanceof PsiLocalVariable) {
return ((PsiLocalVariable)parent).getTypeElement().isInferredType();
}
return parent instanceof PsiExpressionStatement;
}
// 15.13 | 15.27
// It is a compile-time error if any class or interface mentioned by either U or the function type of U
// is not accessible from the class or interface in which the method reference expression appears.
@@ -3,10 +3,10 @@ class Main {
private static void localVariableDeclaration() {
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>
<error descr="'var' is not allowed as an element type of an array">var</error> d[] = new int[4];
var d1 = new int[] {4};
var d2 = new int[4];
<error descr="Cannot infer type: 'var' on variable without initializer">var e;</error>
<error descr="Cannot infer type: 'var' on variable without initializer">var</error> e;
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>);
}
@@ -26,9 +26,9 @@ class Main {
var e = (CharSequence & Comparable<String>) "x";
int el = e.compareTo("");
var f = <error descr="<lambda expression> is not a functional interface">() -> "hello"</error>;
var m = <error descr="<method reference> is not a functional interface">Main::localVariableDeclaration</error>;
<error descr="Cannot infer type: variable initializer is 'null'">var g = null;</error>
<error descr="Cannot infer type: lambda expression requires an explicit target type">var</error> f = () -> "hello";
<error descr="Cannot infer type: method reference requires an explicit target type">var</error> m = Main::localVariableDeclaration;
<error descr="Cannot infer type: variable initializer is 'null'">var</error> g = null;
}
private void forEachType(String[] strs, Iterable<String> it, Iterable raw) {
@@ -53,7 +53,7 @@ class Main {
}
private void tryWithResources(AutoCloseable c) throws Exception {
try (<error descr="Cannot infer type: variable initializer is 'null'">var v = null</error>) { }
try (<error descr="Cannot infer type: variable initializer is 'null'">var</error> v = null) { }
try (var v = c) { }
}