[FIR] do not transform propagated annotations from place other than declaration side

We shouldn't transform annotations not from declaration side due to
a possible different context and to avoid unexpected transformation of
unrelated declarations

Example:
```kotlin
fun implicitType1() = TopLevelObject.expectedType()

object TopLevelObject {
    private const val privateConstVal = "privateConstVal"
    fun expectedType(): @Anno(privateConstVal) Int = 4
}
```
Here we will try to transform the annotation from `expectedType`
during `implicitType1` and as the result, we will see unresolved
reference on the declaration side. This commit fixes this issue.

This solution is based on the fact that the compiler anyway will
resolve the propagated annotation on the declaration side.
And it doesn't matter if it is resolved before or after the call site
declaration transformation, because as a global result, we will observe
that all declarations are resolved correctly in the right context.

Hence, this commit fixes the issue in the case of "full resolution"
which is true for the compiler, but it is not correct for Low Level
FIR where we resolve declarations on demand. It will be solved in
the next commits

^KT-63042
This commit is contained in:
Dmitrii Gridin
2023-12-01 13:29:09 +01:00
committed by Space Team
parent 6f0d52f991
commit 9888cbbfcd
98 changed files with 1768 additions and 1754 deletions
@@ -4,7 +4,7 @@ FIR source kind: KtRealSourceElementKind
FIR element rendered:
public final [ResolvedTo(BODY_RESOLVE)] fun resolveMe(): R|kotlin/Unit| {
R|foo/testMe|(<L> = SAM([ResolvedTo(BODY_RESOLVE)] [MatchingParameterFunctionTypeKey=@R|foo/Anno|(<strcat>(String(testMe param type ), constant#)) foo/Foo] testMe@fun <anonymous>([ResolvedTo(BODY_RESOLVE)] b: R|@R|foo/Anno|(position = <strcat>(String(foo param type ), R|foo/constant|)) foo/Arg|): R|@R|foo/Anno|(position = <strcat>(String(foo return type ), R|foo/constant|)) foo/Arg| <inline=NoInline> {
R|foo/testMe|(<L> = SAM([ResolvedTo(BODY_RESOLVE)] [MatchingParameterFunctionTypeKey=@R|foo/Anno|(<strcat>(String(testMe param type ), constant#)) foo/Foo] testMe@fun <anonymous>([ResolvedTo(BODY_RESOLVE)] b: R|@R|foo/Anno|(<strcat>(String(foo param type ), constant#)) foo/Arg|): R|@R|foo/Anno|(<strcat>(String(foo return type ), constant#)) foo/Arg| <inline=NoInline> {
^ R|<local>/b|
}
))
@@ -20,25 +20,25 @@ FILE: [ResolvedTo(IMPORTS)] lambdaAsSAMInterfaceWithAnnotation.kt
}
}
@R|kotlin/annotation/Target|[Types](Q|kotlin/annotation/AnnotationTarget|.R|kotlin/annotation/AnnotationTarget.TYPE|) public final [ResolvedTo(STATUS)] annotation class Anno : R|kotlin/Annotation| {
public [ResolvedTo(STATUS)] [ContainingClassKey=Anno] constructor([ResolvedTo(STATUS)] [CorrespondingProperty=foo/Anno.position] position: R|kotlin/String|): R|foo/Anno| {
@R|kotlin/annotation/Target|[CompilerRequiredAnnotations](Q|kotlin/annotation/AnnotationTarget|.R|kotlin/annotation/AnnotationTarget.TYPE|) public? final? [ResolvedTo(COMPILER_REQUIRED_ANNOTATIONS)] annotation class Anno : R|kotlin/Annotation| {
public? [ResolvedTo(RAW_FIR)] [ContainingClassKey=Anno] constructor([ResolvedTo(RAW_FIR)] [CorrespondingProperty=foo/Anno.position] position: String): R|foo/Anno| {
LAZY_super<R|kotlin/Any|>
}
public final [ResolvedTo(STATUS)] [IsFromPrimaryConstructor=true] val position: R|kotlin/String| = R|<local>/position|
public [ResolvedTo(STATUS)] [ContainingClassKey=Anno] get(): R|kotlin/String|
public? final? [ResolvedTo(RAW_FIR)] [IsFromPrimaryConstructor=true] val position: String = R|<local>/position|
public? [ResolvedTo(RAW_FIR)] [ContainingClassKey=Anno] get(): String
}
public final const [ResolvedTo(IMPLICIT_TYPES_BODY_RESOLVE)] val constant: R|kotlin/Int| = Int(0)
public [ResolvedTo(IMPLICIT_TYPES_BODY_RESOLVE)] get(): R|kotlin/Int|
public? final? const [ResolvedTo(RAW_FIR)] val constant: <implicit> = LAZY_EXPRESSION
public? [ResolvedTo(RAW_FIR)] get(): <implicit>
public abstract fun [ResolvedTo(STATUS)] interface Foo : R|kotlin/Any| {
public abstract [ResolvedTo(STATUS)] fun foo([ResolvedTo(STATUS)] a: R|@R|foo/Anno|(position = <strcat>(String(foo param type ), R|foo/constant|)) foo/Arg|): R|@R|foo/Anno|(position = <strcat>(String(foo return type ), R|foo/constant|)) foo/Arg|
public abstract [ResolvedTo(STATUS)] fun foo([ResolvedTo(STATUS)] a: R|@R|foo/Anno|(<strcat>(String(foo param type ), constant#)) foo/Arg|): R|@R|foo/Anno|(<strcat>(String(foo return type ), constant#)) foo/Arg|
}
public final [ResolvedTo(CONTRACTS)] fun testMe([ResolvedTo(CONTRACTS)] f: R|@R|foo/Anno|(<strcat>(String(testMe param type ), constant#)) foo/Foo|): R|kotlin/Unit| {
}
public final [ResolvedTo(BODY_RESOLVE)] fun resolveMe(): R|kotlin/Unit| {
R|foo/testMe|(<L> = SAM([ResolvedTo(BODY_RESOLVE)] [MatchingParameterFunctionTypeKey=@R|foo/Anno|(<strcat>(String(testMe param type ), constant#)) foo/Foo] testMe@fun <anonymous>([ResolvedTo(BODY_RESOLVE)] b: R|@R|foo/Anno|(position = <strcat>(String(foo param type ), R|foo/constant|)) foo/Arg|): R|@R|foo/Anno|(position = <strcat>(String(foo return type ), R|foo/constant|)) foo/Arg| <inline=NoInline> {
R|foo/testMe|(<L> = SAM([ResolvedTo(BODY_RESOLVE)] [MatchingParameterFunctionTypeKey=@R|foo/Anno|(<strcat>(String(testMe param type ), constant#)) foo/Foo] testMe@fun <anonymous>([ResolvedTo(BODY_RESOLVE)] b: R|@R|foo/Anno|(<strcat>(String(foo param type ), constant#)) foo/Arg|): R|@R|foo/Anno|(<strcat>(String(foo return type ), constant#)) foo/Arg| <inline=NoInline> {
^ R|<local>/b|
}
))
@@ -3,7 +3,7 @@ FIR element: FirPropertyImpl
FIR source kind: KtRealSourceElementKind
FIR element rendered:
@R|foo/Anno|[Types](position = <strcat>(String(property ), R|foo/secondConstant|)) [ResolvedTo(BODY_RESOLVE)] lval localProperty: R|@R|foo/Anno|(position = <strcat>(String(return type: ), R|foo/constant|)) kotlin/collections/List<@R|foo/Anno|(position = <strcat>(String(nested return type: ), R|foo/constant|)) kotlin/collections/Collection<@R|foo/Anno|(position = <strcat>(String(nested nested return type: ), R|foo/constant|)) kotlin/String>>|by R|kotlin/lazy|<R|@R|foo/Anno|(position = <strcat>(String(return type: ), R|foo/constant|)) kotlin/collections/List<@R|foo/Anno|(position = <strcat>(String(nested return type: ), R|foo/constant|)) kotlin/collections/Collection<@R|foo/Anno|(position = <strcat>(String(nested nested return type: ), R|foo/constant|)) kotlin/String>>|>(<L> = [ResolvedTo(BODY_RESOLVE)] [MatchingParameterFunctionTypeKey=kotlin/Function0<T>] lazy@fun <anonymous>(): R|@R|foo/Anno|(position = <strcat>(String(return type: ), R|foo/constant|)) kotlin/collections/List<@R|foo/Anno|(position = <strcat>(String(nested return type: ), R|foo/constant|)) kotlin/collections/Collection<@R|foo/Anno|(position = <strcat>(String(nested nested return type: ), R|foo/constant|)) kotlin/String>>| <inline=NoInline> {
@R|foo/Anno|[Types](position = <strcat>(String(property ), R|foo/secondConstant|)) [ResolvedTo(BODY_RESOLVE)] lval localProperty: R|@R|foo/Anno|(<strcat>(String(return type: ), constant#)) kotlin/collections/List<@R|foo/Anno|(<strcat>(String(nested return type: ), constant#)) kotlin/collections/Collection<@R|foo/Anno|(<strcat>(String(nested nested return type: ), constant#)) kotlin/String>>|by R|kotlin/lazy|<R|@R|foo/Anno|(<strcat>(String(return type: ), constant#)) kotlin/collections/List<@R|foo/Anno|(<strcat>(String(nested return type: ), constant#)) kotlin/collections/Collection<@R|foo/Anno|(<strcat>(String(nested nested return type: ), constant#)) kotlin/String>>|>(<L> = [ResolvedTo(BODY_RESOLVE)] [MatchingParameterFunctionTypeKey=kotlin/Function0<T>] lazy@fun <anonymous>(): R|@R|foo/Anno|(<strcat>(String(return type: ), constant#)) kotlin/collections/List<@R|foo/Anno|(<strcat>(String(nested return type: ), constant#)) kotlin/collections/Collection<@R|foo/Anno|(<strcat>(String(nested nested return type: ), constant#)) kotlin/String>>| <inline=NoInline> {
^ R|foo/explicitType|()
}
)
@@ -21,13 +21,13 @@ FILE: [ResolvedTo(IMPORTS)] localDelegatedPropertyWithPropagatedType.kt
public [ResolvedTo(STATUS)] [ContainingClassKey=Anno] get(): R|kotlin/String|
}
public final const [ResolvedTo(IMPLICIT_TYPES_BODY_RESOLVE)] val constant: R|kotlin/Int| = Int(0)
public [ResolvedTo(IMPLICIT_TYPES_BODY_RESOLVE)] get(): R|kotlin/Int|
public final [ResolvedTo(CONTRACTS)] fun explicitType(): R|@R|foo/Anno|(position = <strcat>(String(return type: ), R|foo/constant|)) kotlin/collections/List<@R|foo/Anno|(position = <strcat>(String(nested return type: ), R|foo/constant|)) kotlin/collections/Collection<@R|foo/Anno|(position = <strcat>(String(nested nested return type: ), R|foo/constant|)) kotlin/String>>| {
public? final? const [ResolvedTo(RAW_FIR)] val constant: <implicit> = LAZY_EXPRESSION
public? [ResolvedTo(RAW_FIR)] get(): <implicit>
public final [ResolvedTo(CONTRACTS)] fun explicitType(): R|@R|foo/Anno|(<strcat>(String(return type: ), constant#)) kotlin/collections/List<@R|foo/Anno|(<strcat>(String(nested return type: ), constant#)) kotlin/collections/Collection<@R|foo/Anno|(<strcat>(String(nested nested return type: ), constant#)) kotlin/String>>| {
^explicitType IntegerLiteral(0)
}
public final [ResolvedTo(BODY_RESOLVE)] fun resolveMe(): R|kotlin/Unit| {
@R|foo/Anno|[Types](position = <strcat>(String(property ), R|foo/secondConstant|)) [ResolvedTo(BODY_RESOLVE)] lval localProperty: R|@R|foo/Anno|(position = <strcat>(String(return type: ), R|foo/constant|)) kotlin/collections/List<@R|foo/Anno|(position = <strcat>(String(nested return type: ), R|foo/constant|)) kotlin/collections/Collection<@R|foo/Anno|(position = <strcat>(String(nested nested return type: ), R|foo/constant|)) kotlin/String>>|by R|kotlin/lazy|<R|@R|foo/Anno|(position = <strcat>(String(return type: ), R|foo/constant|)) kotlin/collections/List<@R|foo/Anno|(position = <strcat>(String(nested return type: ), R|foo/constant|)) kotlin/collections/Collection<@R|foo/Anno|(position = <strcat>(String(nested nested return type: ), R|foo/constant|)) kotlin/String>>|>(<L> = [ResolvedTo(BODY_RESOLVE)] [MatchingParameterFunctionTypeKey=kotlin/Function0<T>] lazy@fun <anonymous>(): R|@R|foo/Anno|(position = <strcat>(String(return type: ), R|foo/constant|)) kotlin/collections/List<@R|foo/Anno|(position = <strcat>(String(nested return type: ), R|foo/constant|)) kotlin/collections/Collection<@R|foo/Anno|(position = <strcat>(String(nested nested return type: ), R|foo/constant|)) kotlin/String>>| <inline=NoInline> {
@R|foo/Anno|[Types](position = <strcat>(String(property ), R|foo/secondConstant|)) [ResolvedTo(BODY_RESOLVE)] lval localProperty: R|@R|foo/Anno|(<strcat>(String(return type: ), constant#)) kotlin/collections/List<@R|foo/Anno|(<strcat>(String(nested return type: ), constant#)) kotlin/collections/Collection<@R|foo/Anno|(<strcat>(String(nested nested return type: ), constant#)) kotlin/String>>|by R|kotlin/lazy|<R|@R|foo/Anno|(<strcat>(String(return type: ), constant#)) kotlin/collections/List<@R|foo/Anno|(<strcat>(String(nested return type: ), constant#)) kotlin/collections/Collection<@R|foo/Anno|(<strcat>(String(nested nested return type: ), constant#)) kotlin/String>>|>(<L> = [ResolvedTo(BODY_RESOLVE)] [MatchingParameterFunctionTypeKey=kotlin/Function0<T>] lazy@fun <anonymous>(): R|@R|foo/Anno|(<strcat>(String(return type: ), constant#)) kotlin/collections/List<@R|foo/Anno|(<strcat>(String(nested return type: ), constant#)) kotlin/collections/Collection<@R|foo/Anno|(<strcat>(String(nested nested return type: ), constant#)) kotlin/String>>| <inline=NoInline> {
^ R|foo/explicitType|()
}
)
@@ -3,7 +3,7 @@ FIR element: FirPropertyImpl
FIR source kind: KtRealSourceElementKind
FIR element rendered:
@R|foo/Anno|[Types](position = <strcat>(String(property ), R|foo/secondConstant|)) [ResolvedTo(BODY_RESOLVE)] lval localProperty: R|@R|foo/Anno|(position = <strcat>(String(return type: ), R|foo/constant|)) kotlin/collections/List<@R|foo/Anno|(position = <strcat>(String(nested return type: ), R|foo/constant|)) kotlin/collections/Collection<@R|foo/Anno|(position = <strcat>(String(nested nested return type: ), R|foo/constant|)) kotlin/String>>| = R|foo/explicitType|()
@R|foo/Anno|[Types](position = <strcat>(String(property ), R|foo/secondConstant|)) [ResolvedTo(BODY_RESOLVE)] lval localProperty: R|@R|foo/Anno|(<strcat>(String(return type: ), constant#)) kotlin/collections/List<@R|foo/Anno|(<strcat>(String(nested return type: ), constant#)) kotlin/collections/Collection<@R|foo/Anno|(<strcat>(String(nested nested return type: ), constant#)) kotlin/String>>| = R|foo/explicitType|()
FIR FILE:
FILE: [ResolvedTo(IMPORTS)] localPropertyWithPropagatedType.kt
@@ -18,13 +18,13 @@ FILE: [ResolvedTo(IMPORTS)] localPropertyWithPropagatedType.kt
public [ResolvedTo(STATUS)] [ContainingClassKey=Anno] get(): R|kotlin/String|
}
public final const [ResolvedTo(IMPLICIT_TYPES_BODY_RESOLVE)] val constant: R|kotlin/Int| = Int(0)
public [ResolvedTo(IMPLICIT_TYPES_BODY_RESOLVE)] get(): R|kotlin/Int|
public final [ResolvedTo(CONTRACTS)] fun explicitType(): R|@R|foo/Anno|(position = <strcat>(String(return type: ), R|foo/constant|)) kotlin/collections/List<@R|foo/Anno|(position = <strcat>(String(nested return type: ), R|foo/constant|)) kotlin/collections/Collection<@R|foo/Anno|(position = <strcat>(String(nested nested return type: ), R|foo/constant|)) kotlin/String>>| {
public? final? const [ResolvedTo(RAW_FIR)] val constant: <implicit> = LAZY_EXPRESSION
public? [ResolvedTo(RAW_FIR)] get(): <implicit>
public final [ResolvedTo(CONTRACTS)] fun explicitType(): R|@R|foo/Anno|(<strcat>(String(return type: ), constant#)) kotlin/collections/List<@R|foo/Anno|(<strcat>(String(nested return type: ), constant#)) kotlin/collections/Collection<@R|foo/Anno|(<strcat>(String(nested nested return type: ), constant#)) kotlin/String>>| {
^explicitType IntegerLiteral(0)
}
public final [ResolvedTo(BODY_RESOLVE)] fun resolveMe(): R|kotlin/Unit| {
@R|foo/Anno|[Types](position = <strcat>(String(property ), R|foo/secondConstant|)) [ResolvedTo(BODY_RESOLVE)] lval localProperty: R|@R|foo/Anno|(position = <strcat>(String(return type: ), R|foo/constant|)) kotlin/collections/List<@R|foo/Anno|(position = <strcat>(String(nested return type: ), R|foo/constant|)) kotlin/collections/Collection<@R|foo/Anno|(position = <strcat>(String(nested nested return type: ), R|foo/constant|)) kotlin/String>>| = R|foo/explicitType|()
@R|foo/Anno|[Types](position = <strcat>(String(property ), R|foo/secondConstant|)) [ResolvedTo(BODY_RESOLVE)] lval localProperty: R|@R|foo/Anno|(<strcat>(String(return type: ), constant#)) kotlin/collections/List<@R|foo/Anno|(<strcat>(String(nested return type: ), constant#)) kotlin/collections/Collection<@R|foo/Anno|(<strcat>(String(nested nested return type: ), constant#)) kotlin/String>>| = R|foo/explicitType|()
}
public final const [ResolvedTo(IMPLICIT_TYPES_BODY_RESOLVE)] val secondConstant: R|kotlin/String| = String(str)
public [ResolvedTo(IMPLICIT_TYPES_BODY_RESOLVE)] get(): R|kotlin/String|