From bf5a0f81f495ef0c6a28ad4a32ea951d3182b7bd Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 24 May 2019 11:51:27 +0200 Subject: [PATCH] assert that types computed via JavaResolveCache on different threads are the same GitOrigin-RevId: 17e0b308f4c47d963d42806ec7bb8aaced48f02e --- .../com/intellij/psi/PsiLambdaParameterType.java | 10 ++++++++++ .../psi/impl/source/resolve/JavaResolveCache.java | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/java/java-psi-api/src/com/intellij/psi/PsiLambdaParameterType.java b/java/java-psi-api/src/com/intellij/psi/PsiLambdaParameterType.java index bdef7534dbd8..086dd4d4e8de 100644 --- a/java/java-psi-api/src/com/intellij/psi/PsiLambdaParameterType.java +++ b/java/java-psi-api/src/com/intellij/psi/PsiLambdaParameterType.java @@ -70,4 +70,14 @@ public class PsiLambdaParameterType extends PsiType { public PsiParameter getParameter() { return myParameter; } + + @Override + public boolean equals(Object o) { + return this == o || o instanceof PsiLambdaParameterType && myParameter.equals(((PsiLambdaParameterType)o).myParameter); + } + + @Override + public int hashCode() { + return myParameter.hashCode(); + } } \ No newline at end of file diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/JavaResolveCache.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/JavaResolveCache.java index 15f657e56bd0..6b2966cd7b25 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/JavaResolveCache.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/JavaResolveCache.java @@ -20,6 +20,8 @@ package com.intellij.psi.impl.source.resolve; import com.intellij.openapi.components.ServiceManager; +import com.intellij.openapi.diagnostic.Attachment; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.NotNullLazyKey; @@ -46,6 +48,7 @@ import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicReference; public class JavaResolveCache { + private static final Logger LOG = Logger.getInstance(JavaResolveCache.class); private static final NotNullLazyKey INSTANCE_KEY = ServiceManager.createLazyKey(JavaResolveCache.class); public static JavaResolveCache getInstance(Project project) { @@ -99,7 +102,10 @@ public class JavaResolveCache { } if (type == null) type = TypeConversionUtil.NULL_TYPE; - map.put(expr, type); + PsiType alreadyCached = map.put(expr, type); + if (alreadyCached != null && !type.equals(alreadyCached)) { + reportUnstableType(expr, type, alreadyCached); + } if (type instanceof PsiClassReferenceType) { // convert reference-based class type to the PsiImmediateClassType, since the reference may become invalid @@ -115,6 +121,13 @@ public class JavaResolveCache { return type == TypeConversionUtil.NULL_TYPE ? null : type; } + private static void reportUnstableType(@NotNull PsiExpression expr, @NotNull PsiType type, @NotNull PsiType alreadyCached) { + PsiFile file = expr.getContainingFile(); + LOG.error("Different types returned for the same PSI " + expr.getTextRange() + " on different threads: " + + type + " != " + alreadyCached, + new Attachment(file.getName(), file.getText())); + } + @Nullable public Object computeConstantValueWithCaching(@NotNull PsiVariable variable, @NotNull ConstValueComputer computer, Set visitedVars){ boolean physical = variable.isPhysical();