Lazy resolve: fix scope for primary constructor parameters
For classes on top level scope was getting for file, which may introduce resolution problems with generics #KT-8829 Fixed
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2016 JetBrains s.r.o.
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -524,7 +524,6 @@ class ResolveElementCache(
|
|||||||
|
|
||||||
private fun constructorAdditionalResolve(resolveSession: ResolveSession, klass: KtClassOrObject, file: KtFile, filter : BindingTraceFilter): BindingTrace {
|
private fun constructorAdditionalResolve(resolveSession: ResolveSession, klass: KtClassOrObject, file: KtFile, filter : BindingTraceFilter): BindingTrace {
|
||||||
val trace = createDelegatingTrace(klass, filter)
|
val trace = createDelegatingTrace(klass, filter)
|
||||||
val scope = resolveSession.declarationScopeProvider.getResolutionScopeForDeclaration(klass)
|
|
||||||
|
|
||||||
val classDescriptor = resolveSession.resolveToDescriptor(klass) as ClassDescriptor
|
val classDescriptor = resolveSession.resolveToDescriptor(klass) as ClassDescriptor
|
||||||
val constructorDescriptor = classDescriptor.unsubstitutedPrimaryConstructor
|
val constructorDescriptor = classDescriptor.unsubstitutedPrimaryConstructor
|
||||||
@@ -534,6 +533,7 @@ class ResolveElementCache(
|
|||||||
|
|
||||||
val primaryConstructor = klass.primaryConstructor
|
val primaryConstructor = klass.primaryConstructor
|
||||||
if (primaryConstructor != null) {
|
if (primaryConstructor != null) {
|
||||||
|
val scope = resolveSession.declarationScopeProvider.getResolutionScopeForDeclaration(primaryConstructor)
|
||||||
val bodyResolver = createBodyResolver(resolveSession, trace, file, StatementFilter.NONE)
|
val bodyResolver = createBodyResolver(resolveSession, trace, file, StatementFilter.NONE)
|
||||||
bodyResolver.resolveConstructorParameterDefaultValues(DataFlowInfo.EMPTY, trace, primaryConstructor, constructorDescriptor, scope)
|
bodyResolver.resolveConstructorParameterDefaultValues(DataFlowInfo.EMPTY, trace, primaryConstructor, constructorDescriptor, scope)
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea
|
|||||||
import com.intellij.psi.PsiDocumentManager
|
import com.intellij.psi.PsiDocumentManager
|
||||||
import junit.framework.TestCase
|
import junit.framework.TestCase
|
||||||
import org.intellij.lang.annotations.Language
|
import org.intellij.lang.annotations.Language
|
||||||
|
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
|
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||||
@@ -34,6 +35,7 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
|||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||||
|
import org.jetbrains.kotlin.types.typeUtil.containsError
|
||||||
|
|
||||||
class ResolveElementCacheTest : KotlinLightCodeInsightFixtureTestCase() {
|
class ResolveElementCacheTest : KotlinLightCodeInsightFixtureTestCase() {
|
||||||
override fun getProjectDescriptor() = KotlinLightProjectDescriptor.INSTANCE
|
override fun getProjectDescriptor() = KotlinLightProjectDescriptor.INSTANCE
|
||||||
@@ -510,6 +512,32 @@ class C(param1: String = "", param2: Int = 0) {
|
|||||||
assert(bindingContext[BindingContext.REFERENCE_TARGET, nameRef]?.fqNameSafe?.asString() == "kotlin.Int")
|
assert(bindingContext[BindingContext.REFERENCE_TARGET, nameRef]?.fqNameSafe?.asString() == "kotlin.Int")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testResolveDefaultValueInPrimaryConstructor() {
|
||||||
|
val file = myFixture.configureByText("Test.kt", """
|
||||||
|
class ClassA<N> (
|
||||||
|
messenger: ClassB<N> = object : ClassB<N> {
|
||||||
|
override fun methodOne(param: List<N>) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
interface ClassB<N> {
|
||||||
|
fun methodOne(param: List<N>)
|
||||||
|
}
|
||||||
|
""") as KtFile
|
||||||
|
|
||||||
|
val classA = file.declarations[0] as KtClass
|
||||||
|
val defaultValue = classA.primaryConstructor!!.valueParameters[0].defaultValue as KtObjectLiteralExpression
|
||||||
|
val methodOne = defaultValue.objectDeclaration.declarations[0] as KtFunction
|
||||||
|
|
||||||
|
val bindingContext = methodOne.analyze(BodyResolveMode.FULL)
|
||||||
|
|
||||||
|
val parameter = methodOne.valueParameters[0]
|
||||||
|
val parameterDescriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, parameter] as ValueParameterDescriptor
|
||||||
|
|
||||||
|
assert(!parameterDescriptor.type.containsError())
|
||||||
|
}
|
||||||
|
|
||||||
private fun checkResolveMultiple(mode: BodyResolveMode, vararg expressions: KtExpression): BindingContext {
|
private fun checkResolveMultiple(mode: BodyResolveMode, vararg expressions: KtExpression): BindingContext {
|
||||||
val resolutionFacade = expressions.first().getResolutionFacade()
|
val resolutionFacade = expressions.first().getResolutionFacade()
|
||||||
val bindingContext = resolutionFacade.analyze(expressions.asList(), mode)
|
val bindingContext = resolutionFacade.analyze(expressions.asList(), mode)
|
||||||
|
|||||||
Reference in New Issue
Block a user