KT-4351 Cannot resolve reference to self in init of class local to function
#KT-4351 fixed
This commit is contained in:
@@ -20,6 +20,7 @@ import com.google.common.base.Predicates;
|
|||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
import com.intellij.psi.PsiElement;
|
import com.intellij.psi.PsiElement;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.di.InjectorForTopDownAnalyzerBasic;
|
import org.jetbrains.jet.di.InjectorForTopDownAnalyzerBasic;
|
||||||
import org.jetbrains.jet.lang.PlatformToKotlinClassMap;
|
import org.jetbrains.jet.lang.PlatformToKotlinClassMap;
|
||||||
import org.jetbrains.jet.lang.descriptors.*;
|
import org.jetbrains.jet.lang.descriptors.*;
|
||||||
@@ -157,6 +158,7 @@ public class TopDownAnalyzer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void processClassOrObject(
|
public static void processClassOrObject(
|
||||||
|
@Nullable final WritableScope scope,
|
||||||
@NotNull ExpressionTypingContext context,
|
@NotNull ExpressionTypingContext context,
|
||||||
@NotNull final DeclarationDescriptor containingDeclaration,
|
@NotNull final DeclarationDescriptor containingDeclaration,
|
||||||
@NotNull JetClassOrObject object
|
@NotNull JetClassOrObject object
|
||||||
@@ -185,7 +187,9 @@ public class TopDownAnalyzer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addClassifierDescriptor(@NotNull MutableClassDescriptorLite classDescriptor) {
|
public void addClassifierDescriptor(@NotNull MutableClassDescriptorLite classDescriptor) {
|
||||||
|
if (scope != null) {
|
||||||
|
scope.addClassifierDescriptor(classDescriptor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+2
-1
@@ -85,7 +85,8 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
|
|||||||
};
|
};
|
||||||
ObservableBindingTrace traceAdapter = new ObservableBindingTrace(temporaryTrace);
|
ObservableBindingTrace traceAdapter = new ObservableBindingTrace(temporaryTrace);
|
||||||
traceAdapter.addHandler(CLASS, handler);
|
traceAdapter.addHandler(CLASS, handler);
|
||||||
TopDownAnalyzer.processClassOrObject(context.replaceBindingTrace(traceAdapter).replaceContextDependency(INDEPENDENT),
|
TopDownAnalyzer.processClassOrObject(null, // don't need to add classifier of object literal to any scope
|
||||||
|
context.replaceBindingTrace(traceAdapter).replaceContextDependency(INDEPENDENT),
|
||||||
context.scope.getContainingDeclaration(),
|
context.scope.getContainingDeclaration(),
|
||||||
expression.getObjectDeclaration());
|
expression.getObjectDeclaration());
|
||||||
|
|
||||||
|
|||||||
+2
-10
@@ -85,11 +85,7 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
|
|||||||
@Override
|
@Override
|
||||||
public JetTypeInfo visitObjectDeclaration(@NotNull JetObjectDeclaration declaration, ExpressionTypingContext context) {
|
public JetTypeInfo visitObjectDeclaration(@NotNull JetObjectDeclaration declaration, ExpressionTypingContext context) {
|
||||||
TopDownAnalyzer.processClassOrObject(
|
TopDownAnalyzer.processClassOrObject(
|
||||||
context.replaceScope(scope).replaceContextDependency(INDEPENDENT), scope.getContainingDeclaration(), declaration);
|
scope, context.replaceScope(scope).replaceContextDependency(INDEPENDENT), scope.getContainingDeclaration(), declaration);
|
||||||
ClassDescriptor classDescriptor = context.trace.getBindingContext().get(BindingContext.CLASS, declaration);
|
|
||||||
if (classDescriptor != null) {
|
|
||||||
scope.addClassifierDescriptor(classDescriptor);
|
|
||||||
}
|
|
||||||
return DataFlowUtils.checkStatementType(declaration, context, context.dataFlowInfo);
|
return DataFlowUtils.checkStatementType(declaration, context, context.dataFlowInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,11 +177,7 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
|
|||||||
@Override
|
@Override
|
||||||
public JetTypeInfo visitClass(@NotNull JetClass klass, ExpressionTypingContext context) {
|
public JetTypeInfo visitClass(@NotNull JetClass klass, ExpressionTypingContext context) {
|
||||||
TopDownAnalyzer.processClassOrObject(
|
TopDownAnalyzer.processClassOrObject(
|
||||||
context.replaceScope(scope).replaceContextDependency(INDEPENDENT), scope.getContainingDeclaration(), klass);
|
scope, context.replaceScope(scope).replaceContextDependency(INDEPENDENT), scope.getContainingDeclaration(), klass);
|
||||||
ClassDescriptor classDescriptor = context.trace.getBindingContext().get(BindingContext.CLASS, klass);
|
|
||||||
if (classDescriptor != null) {
|
|
||||||
scope.addClassifierDescriptor(classDescriptor);
|
|
||||||
}
|
|
||||||
return DataFlowUtils.checkStatementType(klass, context, context.dataFlowInfo);
|
return DataFlowUtils.checkStatementType(klass, context, context.dataFlowInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
// KT-4351 Cannot resolve reference to self in init of class local to function
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
var accessedFromConstructor: Class<*>? = null
|
||||||
|
|
||||||
|
class MyClass() {
|
||||||
|
{
|
||||||
|
accessedFromConstructor = javaClass<MyClass>()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MyClass()
|
||||||
|
if (accessedFromConstructor!!.getName().endsWith("MyClass")) {
|
||||||
|
return "OK"
|
||||||
|
} else {
|
||||||
|
return accessedFromConstructor.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
// KT-4351 Cannot resolve reference to self in init of class local to function
|
||||||
|
|
||||||
|
fun f() {
|
||||||
|
class MyClass() {
|
||||||
|
{
|
||||||
|
val <!UNUSED_VARIABLE!>x<!>: MyClass = MyClass()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun member() {
|
||||||
|
val x: MyClass = MyClass()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
object MyObject {
|
||||||
|
{
|
||||||
|
val <!UNUSED_VARIABLE!>obj<!>: MyObject = MyObject
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val <!UNUSED_VARIABLE!>x<!>: MyClass = MyClass()
|
||||||
|
}
|
||||||
|
|
||||||
|
val closure = {
|
||||||
|
class MyClass {
|
||||||
|
{
|
||||||
|
val x: MyClass = MyClass()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4331,6 +4331,11 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
|
|||||||
doTest("compiler/testData/diagnostics/tests/inner/outerSuperClassMember.kt");
|
doTest("compiler/testData/diagnostics/tests/inner/outerSuperClassMember.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("referenceToSelfInLocal.kt")
|
||||||
|
public void testReferenceToSelfInLocal() throws Exception {
|
||||||
|
doTest("compiler/testData/diagnostics/tests/inner/referenceToSelfInLocal.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("resolvePackageClassInObjects.kt")
|
@TestMetadata("resolvePackageClassInObjects.kt")
|
||||||
public void testResolvePackageClassInObjects() throws Exception {
|
public void testResolvePackageClassInObjects() throws Exception {
|
||||||
doTest("compiler/testData/diagnostics/tests/inner/resolvePackageClassInObjects.kt");
|
doTest("compiler/testData/diagnostics/tests/inner/resolvePackageClassInObjects.kt");
|
||||||
|
|||||||
+5
@@ -1013,6 +1013,11 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
|
|||||||
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/regressions/kt864.kt");
|
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/regressions/kt864.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("referenceToSelfInLocal.kt")
|
||||||
|
public void testReferenceToSelfInLocal() throws Exception {
|
||||||
|
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/regressions/referenceToSelfInLocal.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("typeCastException.kt")
|
@TestMetadata("typeCastException.kt")
|
||||||
public void testTypeCastException() throws Exception {
|
public void testTypeCastException() throws Exception {
|
||||||
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/regressions/typeCastException.kt");
|
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/regressions/typeCastException.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user