[FIR] Create synthetic properties for members of java annotations

#KT-41939 Fixed
This commit is contained in:
Dmitriy Novozhilov
2020-09-18 13:31:32 +03:00
parent da3a676c2a
commit 0e91c8f048
6 changed files with 127 additions and 2 deletions
@@ -0,0 +1,14 @@
// ISSUE: KT-41939
// FILE: Ann.java
public @interface Ann {
String value()
}
// FILE: main.kt
fun test(ann: Ann) {
ann.value
ann.<!UNRESOLVED_REFERENCE!>value<!>() // should be an error
}
@@ -0,0 +1,5 @@
FILE: main.kt
public final fun test(ann: R|Ann|): R|kotlin/Unit| {
R|<local>/ann|.R|/Ann.value|
R|<local>/ann|.<Unresolved name: value>#()
}
@@ -2031,6 +2031,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
public void testNoBackingFieldForExtension() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/properties/noBackingFieldForExtension.kt");
}
@TestMetadata("syntheticPropertiesForJavaAnnotations.kt")
public void testSyntheticPropertiesForJavaAnnotations() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/properties/syntheticPropertiesForJavaAnnotations.kt");
}
}
@TestMetadata("compiler/fir/analysis-tests/testData/resolve/references")
@@ -2031,6 +2031,11 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
public void testNoBackingFieldForExtension() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/properties/noBackingFieldForExtension.kt");
}
@TestMetadata("syntheticPropertiesForJavaAnnotations.kt")
public void testSyntheticPropertiesForJavaAnnotations() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/properties/syntheticPropertiesForJavaAnnotations.kt");
}
}
@TestMetadata("compiler/fir/analysis-tests/testData/resolve/references")
@@ -35,8 +35,25 @@ class JavaScopeProvider(
klass: FirClass<*>,
useSiteSession: FirSession,
scopeSession: ScopeSession
): FirTypeScope =
buildJavaEnhancementScope(useSiteSession, klass.symbol as FirRegularClassSymbol, scopeSession, mutableSetOf())
): FirTypeScope {
val symbol = klass.symbol as FirRegularClassSymbol
val enhancementScope = buildJavaEnhancementScope(useSiteSession, symbol, scopeSession, mutableSetOf())
if (klass.classKind == ClassKind.ANNOTATION_CLASS) {
return buildSyntheticScopeForAnnotations(useSiteSession, symbol, scopeSession, enhancementScope)
}
return enhancementScope
}
private fun buildSyntheticScopeForAnnotations(
session: FirSession,
symbol: FirRegularClassSymbol,
scopeSession: ScopeSession,
enhancementScope: JavaClassMembersEnhancementScope
): FirTypeScope {
return scopeSession.getOrBuild(symbol, JAVA_SYNTHETIC_FOR_ANNOTATIONS) {
JavaAnnotationSyntheticPropertiesScope(session, symbol, enhancementScope)
}
}
private fun buildJavaEnhancementScope(
useSiteSession: FirSession,
@@ -196,6 +213,7 @@ class JavaScopeProvider(
}
}
private val JAVA_SYNTHETIC_FOR_ANNOTATIONS = scopeSessionKey<FirRegularClassSymbol, JavaAnnotationSyntheticPropertiesScope>()
private val JAVA_ENHANCEMENT_FOR_STATIC = scopeSessionKey<FirRegularClassSymbol, JavaClassStaticEnhancementScope>()
private val JAVA_ENHANCEMENT = scopeSessionKey<FirRegularClassSymbol, JavaClassMembersEnhancementScope>()
private val JAVA_USE_SITE = scopeSessionKey<FirRegularClassSymbol, JavaClassUseSiteMemberScope>()
@@ -0,0 +1,78 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.fir.java.scopes
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction
import org.jetbrains.kotlin.fir.declarations.synthetic.FirSyntheticProperty
import org.jetbrains.kotlin.fir.declarations.synthetic.FirSyntheticPropertyAccessor
import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor
import org.jetbrains.kotlin.fir.scopes.FirTypeScope
import org.jetbrains.kotlin.fir.scopes.ProcessorAction
import org.jetbrains.kotlin.fir.symbols.CallableId
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
class JavaAnnotationSyntheticPropertiesScope(
private val session: FirSession,
owner: FirRegularClassSymbol,
private val delegateScope: JavaClassMembersEnhancementScope
) : FirTypeScope() {
private val classId: ClassId = owner.classId
private val names: Set<Name> = owner.fir.declarations.mapNotNullTo(mutableSetOf()) { (it as? FirSimpleFunction)?.name }
private val syntheticPropertiesCache = mutableMapOf<FirFunctionSymbol<*>, FirVariableSymbol<*>>()
override fun processDeclaredConstructors(processor: (FirConstructorSymbol) -> Unit) {
delegateScope.processDeclaredConstructors(processor)
}
override fun processFunctionsByName(name: Name, processor: (FirFunctionSymbol<*>) -> Unit) {
if (name in names) return
delegateScope.processFunctionsByName(name, processor)
}
override fun processPropertiesByName(name: Name, processor: (FirVariableSymbol<*>) -> Unit) {
if (name !in names) return
delegateScope.processFunctionsByName(name) { functionSymbol ->
val function = functionSymbol.fir as? FirSimpleFunction ?: return@processFunctionsByName
val symbol = syntheticPropertiesCache.getOrPut(functionSymbol) {
val callableId = CallableId(classId, name)
FirAccessorSymbol(callableId, callableId).also {
val accessor = FirSyntheticPropertyAccessor(function, isGetter = true)
FirSyntheticProperty(session, name, isVar = false, it, function.status, function.resolvePhase, accessor)
}
}
processor(symbol)
}
}
override fun processClassifiersByNameWithSubstitution(name: Name, processor: (FirClassifierSymbol<*>, ConeSubstitutor) -> Unit) {
delegateScope.processClassifiersByNameWithSubstitution(name, processor)
}
override fun processDirectOverriddenFunctionsWithBaseScope(
functionSymbol: FirFunctionSymbol<*>,
processor: (FirFunctionSymbol<*>, FirTypeScope) -> ProcessorAction
): ProcessorAction {
return ProcessorAction.NONE
}
override fun processDirectOverriddenPropertiesWithBaseScope(
propertySymbol: FirPropertySymbol,
processor: (FirPropertySymbol, FirTypeScope) -> ProcessorAction
): ProcessorAction {
return ProcessorAction.NONE
}
override fun getCallableNames(): Set<Name> {
return delegateScope.getCallableNames()
}
override fun getClassifierNames(): Set<Name> {
return delegateScope.getClassifierNames()
}
}