Partial body resolve: to take source Kotlin callables with Nothing type from caches

This commit is contained in:
Valentin Kipyatkov
2014-11-19 18:41:58 +03:00
parent 992cdd9fe2
commit e5579bcf32
23 changed files with 301 additions and 13 deletions
@@ -28,4 +28,6 @@ public interface KotlinFunctionStub extends KotlinStubWithFqName<JetNamedFunctio
boolean hasBody();
boolean hasTypeParameterListBeforeFunctionName();
boolean isPossiblyNothingType();
}
@@ -26,4 +26,6 @@ public interface KotlinPropertyStub extends KotlinStubWithFqName<JetProperty> {
boolean hasInitializer();
boolean hasReceiverTypeRef();
boolean hasReturnTypeRef();
boolean isPossiblyNothingType();
}
@@ -36,7 +36,7 @@ import org.jetbrains.jet.plugin.JetLanguage;
import java.io.IOException;
public class JetFileElementType extends IStubFileElementType<KotlinFileStub> {
public static final int STUB_VERSION = 30;
public static final int STUB_VERSION = 31;
public JetFileElementType() {
super("jet.FILE", JetLanguage.INSTANCE);
@@ -23,8 +23,7 @@ import com.intellij.psi.stubs.StubOutputStream;
import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.psi.stubs.KotlinFunctionStub;
import org.jetbrains.jet.lang.psi.stubs.impl.KotlinFunctionStubImpl;
import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils;
@@ -45,8 +44,10 @@ public class JetFunctionElementType extends JetStubElementType<KotlinFunctionStu
FqName fqName = ResolveSessionUtils.safeFqNameForLazyResolve(psi);
boolean hasBlockBody = psi.hasBlockBody();
boolean hasBody = psi.hasBody();
boolean isNothing = ElementsPackage.isPossiblyNothing(psi.getTypeReference());
return new KotlinFunctionStubImpl(parentStub, StringRef.fromString(psi.getName()), isTopLevel, fqName,
isExtension, hasBlockBody, hasBody, psi.hasTypeParameterListBeforeFunctionName());
isExtension, hasBlockBody, hasBody, psi.hasTypeParameterListBeforeFunctionName(),
isNothing);
}
@Override
@@ -61,6 +62,7 @@ public class JetFunctionElementType extends JetStubElementType<KotlinFunctionStu
dataStream.writeBoolean(stub.hasBlockBody());
dataStream.writeBoolean(stub.hasBody());
dataStream.writeBoolean(stub.hasTypeParameterListBeforeFunctionName());
dataStream.writeBoolean(stub.isPossiblyNothingType());
}
@NotNull
@@ -76,9 +78,10 @@ public class JetFunctionElementType extends JetStubElementType<KotlinFunctionStu
boolean hasBlockBody = dataStream.readBoolean();
boolean hasBody = dataStream.readBoolean();
boolean hasTypeParameterListBeforeFunctionName = dataStream.readBoolean();
boolean possiblyNothingType = dataStream.readBoolean();
return new KotlinFunctionStubImpl(parentStub, name, isTopLevel, fqName, isExtension, hasBlockBody, hasBody,
hasTypeParameterListBeforeFunctionName);
hasTypeParameterListBeforeFunctionName, possiblyNothingType);
}
@Override
@@ -47,6 +47,7 @@ public class JetPropertyElementType extends JetStubElementType<KotlinPropertyStu
psi.isVar(), psi.isTopLevel(), psi.hasDelegate(),
psi.hasDelegateExpression(), psi.hasInitializer(),
psi.getReceiverTypeReference() != null, psi.getTypeReference() != null,
ElementsPackage.isPossiblyNothing(psi.getTypeReference()),
ResolveSessionUtils.safeFqNameForLazyResolve(psi)
);
}
@@ -61,6 +62,7 @@ public class JetPropertyElementType extends JetStubElementType<KotlinPropertyStu
dataStream.writeBoolean(stub.hasInitializer());
dataStream.writeBoolean(stub.hasReceiverTypeRef());
dataStream.writeBoolean(stub.hasReturnTypeRef());
dataStream.writeBoolean(stub.isPossiblyNothingType());
FqName fqName = stub.getFqName();
dataStream.writeName(fqName != null ? fqName.asString() : null);
@@ -77,12 +79,14 @@ public class JetPropertyElementType extends JetStubElementType<KotlinPropertyStu
boolean hasInitializer = dataStream.readBoolean();
boolean hasReceiverTypeRef = dataStream.readBoolean();
boolean hasReturnTypeRef = dataStream.readBoolean();
boolean possiblyNothing = dataStream.readBoolean();
StringRef fqNameAsString = dataStream.readName();
FqName fqName = fqNameAsString != null ? new FqName(fqNameAsString.toString()) : null;
return new KotlinPropertyStubImpl(parentStub, name, isVar, isTopLevel, hasDelegate,
hasDelegateExpression, hasInitializer, hasReceiverTypeRef, hasReturnTypeRef, fqName);
hasDelegateExpression, hasInitializer, hasReceiverTypeRef, hasReturnTypeRef, possiblyNothing,
fqName);
}
@Override
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.psi.stubs.elements
import org.jetbrains.jet.lang.psi.JetUserType
import org.jetbrains.jet.lang.psi.JetTypeReference
fun JetTypeReference?.isPossiblyNothing()
= (this?.getTypeElement() as? JetUserType)?.getReferencedName() == "Nothing"
@@ -34,6 +34,7 @@ public class KotlinFunctionStubImpl extends KotlinStubBaseImpl<JetNamedFunction>
private final boolean hasBlockBody;
private final boolean hasBody;
private final boolean hasTypeParameterListBeforeFunctionName;
private final boolean possiblyNothingType;
public KotlinFunctionStubImpl(
@NotNull StubElement parent,
@@ -43,7 +44,8 @@ public class KotlinFunctionStubImpl extends KotlinStubBaseImpl<JetNamedFunction>
boolean isExtension,
boolean hasBlockBody,
boolean hasBody,
boolean hasTypeParameterListBeforeFunctionName
boolean hasTypeParameterListBeforeFunctionName,
boolean possiblyNothingType
) {
super(parent, JetStubElementTypes.FUNCTION);
@@ -58,6 +60,7 @@ public class KotlinFunctionStubImpl extends KotlinStubBaseImpl<JetNamedFunction>
this.hasBlockBody = hasBlockBody;
this.hasBody = hasBody;
this.hasTypeParameterListBeforeFunctionName = hasTypeParameterListBeforeFunctionName;
this.possiblyNothingType = possiblyNothingType;
}
@Override
@@ -95,4 +98,9 @@ public class KotlinFunctionStubImpl extends KotlinStubBaseImpl<JetNamedFunction>
public FqName getFqName() {
return fqName;
}
@Override
public boolean isPossiblyNothingType() {
return possiblyNothingType;
}
}
@@ -33,6 +33,7 @@ public class KotlinPropertyStubImpl extends KotlinStubBaseImpl<JetProperty> impl
private final boolean hasInitializer;
private final boolean hasReceiverTypeRef;
private final boolean hasReturnTypeRef;
private final boolean possiblyNothingType;
private final FqName fqName;
public KotlinPropertyStubImpl(
@@ -45,6 +46,7 @@ public class KotlinPropertyStubImpl extends KotlinStubBaseImpl<JetProperty> impl
boolean hasInitializer,
boolean hasReceiverTypeRef,
boolean hasReturnTypeRef,
boolean possiblyNothingType,
@Nullable FqName fqName
) {
super(parent, JetStubElementTypes.PROPERTY);
@@ -64,6 +66,7 @@ public class KotlinPropertyStubImpl extends KotlinStubBaseImpl<JetProperty> impl
this.hasInitializer = hasInitializer;
this.hasReceiverTypeRef = hasReceiverTypeRef;
this.hasReturnTypeRef = hasReturnTypeRef;
this.possiblyNothingType = possiblyNothingType;
this.fqName = fqName;
}
@@ -102,6 +105,11 @@ public class KotlinPropertyStubImpl extends KotlinStubBaseImpl<JetProperty> impl
return hasReturnTypeRef;
}
@Override
public boolean isPossiblyNothingType() {
return possiblyNothingType;
}
@Nullable
@Override
public FqName getFqName() {
@@ -69,6 +69,11 @@ public abstract class ElementResolver {
return resolveToElement(jetElement, false);
}
@NotNull
protected PossiblyNothingCallableNamesService possiblyNothingCallableNamesService() {
return DefaultNothingCallableNamesService.INSTANCE$;
}
@NotNull
public BindingContext resolveToElement(@NotNull JetElement jetElement, boolean partialBodyResolve) {
@SuppressWarnings("unchecked") JetElement elementOfAdditionalResolve = (JetElement) JetPsiUtil.getTopmostParentOfTypes(
@@ -96,7 +101,7 @@ public abstract class ElementResolver {
boolean inBody = body != null && PsiTreeUtil.isAncestor(body, jetElement, false);
Function1<JetElement, Boolean> filter;
if (inBody) {
filter = new PartialBodyResolveFilter(jetElement, body);
filter = new PartialBodyResolveFilter(jetElement, body, possiblyNothingCallableNamesService());
}
else { // do as less as possible body-resolve
filter = new Function1<JetElement, Boolean>() {
@@ -27,10 +27,16 @@ import com.intellij.psi.PsiElement
import org.jetbrains.jet.JetNodeTypes
import org.jetbrains.jet.lang.psi.psiUtil.isAncestor
class PartialBodyResolveFilter(elementToResolve: JetElement, private val body: JetExpression) : (JetElement) -> Boolean {
class PartialBodyResolveFilter(
elementToResolve: JetElement,
private val body: JetExpression,
possiblyNothingCallableNamesService: PossiblyNothingCallableNamesService
) : (JetElement) -> Boolean {
private val statementsToResolve = HashSet<JetExpression>()
private val processedBlocks = HashSet<JetBlockExpression>()
private val possiblyNothingFunctionNames = possiblyNothingCallableNamesService.functionNames()
private val possiblyNothingPropertyNames = possiblyNothingCallableNamesService.propertyNames()
;{
assert(body.isAncestor(elementToResolve, strict = false))
@@ -286,6 +292,13 @@ class PartialBodyResolveFilter(elementToResolve: JetElement, private val body: J
super.visitCallExpression(expression)
}
override fun visitSimpleNameExpression(expression: JetSimpleNameExpression) {
val name = expression.getReferencedName()
if (name in possiblyNothingPropertyNames) {
result.add(expression)
}
}
override fun visitBinaryExpression(expression: JetBinaryExpression) {
if (expression.getOperationToken() == JetTokens.ELVIS) {
// do not search exits after "?:"
@@ -368,9 +381,5 @@ class PartialBodyResolveFilter(elementToResolve: JetElement, private val body: J
private fun JetBlockExpression.lastStatement(): JetExpression?
= getLastChild().siblings(forward = false).filterIsInstance<JetExpression>().firstOrNull()
class object {
private val possiblyNothingFunctionNames = setOf("error") // currently hard-coded
}
}
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.lazy
public trait PossiblyNothingCallableNamesService {
public fun functionNames(): Set<String>
public fun propertyNames(): Set<String>
}
public object DefaultNothingCallableNamesService : PossiblyNothingCallableNamesService {
private val hardcodedNames = setOf("error")
override fun functionNames() = hardcodedNames
override fun propertyNames(): Set<String> = setOf()
}
@@ -28,16 +28,26 @@ import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.AdditionalCheckerProvider;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.lazy.DefaultNothingCallableNamesService;
import org.jetbrains.jet.lang.resolve.lazy.ElementResolver;
import org.jetbrains.jet.lang.resolve.lazy.PossiblyNothingCallableNamesService;
import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
import org.jetbrains.jet.plugin.stubindex.JetPossiblyNothingFunctionShortNameIndex;
import org.jetbrains.jet.plugin.stubindex.JetPossiblyNothingPropertyShortNameIndex;
import org.jetbrains.jet.storage.LazyResolveStorageManager;
import org.jetbrains.jet.storage.MemoizedFunctionToNotNull;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
public class ResolveElementCache extends ElementResolver {
private final Project project;
private final CachedValue<MemoizedFunctionToNotNull<JetElement, BindingContext>> additionalResolveCache;
public ResolveElementCache(ResolveSession resolveSession, Project project) {
super(resolveSession);
this.project = project;
// Recreate internal cache after change of modification count
this.additionalResolveCache =
@@ -74,4 +84,29 @@ public class ResolveElementCache extends ElementResolver {
public AdditionalCheckerProvider getAdditionalCheckerProvider(@NotNull JetFile jetFile) {
return TargetPlatformDetector.getPlatform(jetFile).getAdditionalCheckerProvider();
}
@NotNull
@Override
protected PossiblyNothingCallableNamesService possiblyNothingCallableNamesService() {
return new PossiblyNothingCallableNamesService() {
@NotNull
@Override
public Set<String> functionNames() {
// we have to add hardcoded-names until we have Kotlin compiled classes in caches
Set<String> hardcodedNames = DefaultNothingCallableNamesService.INSTANCE$.functionNames();
Collection<String> indexedNames = JetPossiblyNothingFunctionShortNameIndex.getInstance().getAllKeys(project);
Set<String> set = new HashSet<String>(hardcodedNames.size() + indexedNames.size());
set.addAll(hardcodedNames);
set.addAll(indexedNames);
return set;
//TODO: what about local declarations?
}
@NotNull
@Override
public Set<String> propertyNames() {
return new HashSet<String>(JetPossiblyNothingPropertyShortNameIndex.getInstance().getAllKeys(project));
}
};
}
}
@@ -0,0 +1,50 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.stubindex;
import com.intellij.openapi.project.Project;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.stubs.StringStubIndexExtension;
import com.intellij.psi.stubs.StubIndexKey;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import java.util.Collection;
public class JetPossiblyNothingFunctionShortNameIndex extends StringStubIndexExtension<JetNamedFunction> {
private static final StubIndexKey<String, JetNamedFunction> KEY = KotlinIndexUtil.createIndexKey(JetPossiblyNothingFunctionShortNameIndex.class);
private static final JetPossiblyNothingFunctionShortNameIndex ourInstance = new JetPossiblyNothingFunctionShortNameIndex();
public static JetPossiblyNothingFunctionShortNameIndex getInstance() {
return ourInstance;
}
private JetPossiblyNothingFunctionShortNameIndex() {}
@NotNull
@Override
public StubIndexKey<String, JetNamedFunction> getKey() {
return KEY;
}
@NotNull
@Override
public Collection<JetNamedFunction> get(@NotNull String s, @NotNull Project project, @NotNull GlobalSearchScope scope) {
return super.get(s, project, JetSourceFilterScope.kotlinSourcesAndLibraries(scope, project));
}
}
@@ -0,0 +1,50 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.stubindex;
import com.intellij.openapi.project.Project;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.stubs.StringStubIndexExtension;
import com.intellij.psi.stubs.StubIndexKey;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetProperty;
import java.util.Collection;
public class JetPossiblyNothingPropertyShortNameIndex extends StringStubIndexExtension<JetProperty> {
private static final StubIndexKey<String, JetProperty> KEY = KotlinIndexUtil.createIndexKey(JetPossiblyNothingPropertyShortNameIndex.class);
private static final JetPossiblyNothingPropertyShortNameIndex ourInstance = new JetPossiblyNothingPropertyShortNameIndex();
public static JetPossiblyNothingPropertyShortNameIndex getInstance() {
return ourInstance;
}
private JetPossiblyNothingPropertyShortNameIndex() {}
@NotNull
@Override
public StubIndexKey<String, JetProperty> getKey() {
return KEY;
}
@NotNull
@Override
public Collection<JetProperty> get(@NotNull String s, @NotNull Project project, @NotNull GlobalSearchScope scope) {
return super.get(s, project, JetSourceFilterScope.kotlinSourcesAndLibraries(scope, project));
}
}
@@ -113,6 +113,10 @@ public class StubIndexServiceImpl implements StubIndexService {
}
}
sink.occurrence(JetFunctionShortNameIndex.getInstance().getKey(), name);
if (stub.isPossiblyNothingType()) {
sink.occurrence(JetPossiblyNothingFunctionShortNameIndex.getInstance().getKey(), name);
}
}
// can have special fq name in case of syntactically incorrect function with no name
FqName topFQName = stub.getFqName();
@@ -136,6 +140,10 @@ public class StubIndexServiceImpl implements StubIndexService {
}
sink.occurrence(JetPropertyShortNameIndex.getInstance().getKey(), name);
if (stub.isPossiblyNothingType()) {
sink.occurrence(JetPossiblyNothingPropertyShortNameIndex.getInstance().getKey(), name);
}
}
// can have special fq name in case of syntactically incorrect function with no name
if (stub.isTopLevel()) {
+2
View File
@@ -395,6 +395,8 @@
<stubIndex implementation="org.jetbrains.jet.plugin.stubindex.JetTopLevelPropertiesFqnNameIndex"/>
<stubIndex implementation="org.jetbrains.jet.plugin.stubindex.JetAnnotationsIndex"/>
<stubIndex implementation="org.jetbrains.jet.plugin.stubindex.JetTopLevelObjectShortNameIndex"/>
<stubIndex implementation="org.jetbrains.jet.plugin.stubindex.JetPossiblyNothingFunctionShortNameIndex"/>
<stubIndex implementation="org.jetbrains.jet.plugin.stubindex.JetPossiblyNothingPropertyShortNameIndex"/>
<psi.classFileDecompiler implementation="org.jetbrains.jet.plugin.decompiler.JetClassFileDecompiler"/>
@@ -0,0 +1,3 @@
Resolve target: value-parameter val p: kotlin.Any
Skipped statements:
if (p !is String) { print(error) }
@@ -0,0 +1,7 @@
val error = "error"
fun foo(p: Any) {
if (p !is String) {
print(error)
}
println(<caret>p.size)
}
@@ -0,0 +1,2 @@
Resolve target: value-parameter val p: kotlin.Any smart-casted to kotlin.String
Skipped statements:
@@ -0,0 +1,8 @@
fun myError(): Nothing = throw Exception()
fun foo(p: Any) {
if (p !is String) {
myError()
}
println(<caret>p.size)
}
@@ -0,0 +1,2 @@
Resolve target: value-parameter val p: kotlin.Any smart-casted to kotlin.String
Skipped statements:
@@ -0,0 +1,9 @@
val prop: Nothing
get() = throw Exception()
fun foo(p: Any) {
if (p !is String) {
prop
}
println(<caret>p.size)
}
@@ -19,6 +19,7 @@ package org.jetbrains.jet.resolve;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.jet.JUnit3RunnerWithInners;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.test.InnerTestClasses;
import org.jetbrains.jet.test.TestMetadata;
import org.junit.runner.RunWith;
@@ -107,6 +108,24 @@ public class PartialBodyResolveTestGenerated extends AbstractPartialBodyResolveT
doTest(fileName);
}
@TestMetadata("IfNotIsErrorVariable.kt")
public void testIfNotIsErrorVariable() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/resolve/partialBodyResolve/IfNotIsErrorVariable.kt");
doTest(fileName);
}
@TestMetadata("IfNotIsMyError.kt")
public void testIfNotIsMyError() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/resolve/partialBodyResolve/IfNotIsMyError.kt");
doTest(fileName);
}
@TestMetadata("IfNotIsNothingProp.kt")
public void testIfNotIsNothingProp() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/resolve/partialBodyResolve/IfNotIsNothingProp.kt");
doTest(fileName);
}
@TestMetadata("IfNotIsReturn.kt")
public void testIfNotIsReturn() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/resolve/partialBodyResolve/IfNotIsReturn.kt");