[groovy] cache closure/lambda type in current inference context

GitOrigin-RevId: 4f2826ac1f094d5ba74c1ed2156007b0ac21a003
This commit is contained in:
Daniil Ovchinnikov
2019-12-18 11:08:59 +00:00
committed by intellij-monorepo-bot
parent 3b06964315
commit fcb94c0f3a
2 changed files with 12 additions and 8 deletions
@@ -15,6 +15,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.GrLambdaBody
import org.jetbrains.plugins.groovy.lang.psi.api.GrLambdaExpression
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList
import org.jetbrains.plugins.groovy.lang.psi.dataFlow.types.TypeInferenceHelper
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.GrExpressionImpl
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.params.GrParameterListImpl
@@ -46,7 +47,9 @@ class GrLambdaExpressionImpl(node: ASTNode) : GrExpressionImpl(node), GrLambdaEx
override fun getReturnType(): PsiType? = body?.returnType
override fun getType(): PsiType? = GrClosureType.create(this, true)
override fun getType(): PsiType? = TypeInferenceHelper.getCurrentContext().getExpressionType(this) {
GrClosureType.create(it, true)
}
override fun toString(): String = "Lambda expression"
}
@@ -20,7 +20,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList;
import org.jetbrains.plugins.groovy.lang.psi.dataFlow.types.TypeInferenceHelper;
import org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType;
import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyPsiElementImpl;
import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyPsiManager;
import org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil;
@@ -30,10 +29,10 @@ import org.jetbrains.plugins.groovy.lang.resolve.MethodTypeInferencer;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import static com.intellij.psi.util.CachedValueProvider.Result.create;
import static org.jetbrains.plugins.groovy.lang.psi.impl.FunctionalExpressionsKt.*;
import static org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType.create;
/**
* @author ilyas
@@ -135,7 +134,7 @@ public class GrClosableBlockImpl extends GrBlockImpl implements GrClosableBlock
@Override
public PsiType getType() {
return GrClosureType.create(this, true);
return TypeInferenceHelper.getCurrentContext().getExpressionType(this, it -> create(it, true));
}
@Override
@@ -164,13 +163,15 @@ public class GrClosableBlockImpl extends GrBlockImpl implements GrClosableBlock
return PsiImplUtil.replaceExpression(this, newExpr, removeUnnecessaryParentheses);
}
private static final Function<GrClosableBlock, PsiType> ourTypesCalculator =
block -> GroovyPsiManager.inferType(block, new MethodTypeInferencer(block));
@Override
@Nullable
public PsiType getReturnType() {
return TypeInferenceHelper.getCurrentContext().getExpressionType(this, ourTypesCalculator);
return TypeInferenceHelper.getCurrentContext().getCachedValue(this, this::doGetReturnType);
}
@Nullable
private PsiType doGetReturnType() {
return GroovyPsiManager.inferType(this, new MethodTypeInferencer(this));
}
@Override