KT-61245 [LL API] Collect correct context for fields

There is a fake field created for the supertypes delegation,
and it needs to be visited by the collector
This commit is contained in:
Roman Golyshev
2023-09-04 19:14:37 +02:00
committed by Space Team
parent f47a09e55e
commit b44632b92f
4 changed files with 94 additions and 0 deletions
@@ -460,6 +460,35 @@ private class ContextCollectorVisitor(
}
}
/**
* We visit fields to properly handle supertypes delegation:
*
* ```kt
* class Foo : Bar by baz
* ```
*
* In the code above, `baz` expression is saved into a separate synthetic field.
* It's not accessible from the delegated constructor, it's just added to the
* `Foo` class body.
*/
override fun visitField(field: FirField) = withProcessor {
dumpContext(field, ContextKind.SELF)
processSignatureAnnotations(field)
onActiveBody {
field.lazyResolveToPhase(FirResolvePhase.BODY_RESOLVE)
context.withField(field) {
dumpContext(field, ContextKind.BODY)
onActive {
process(field.initializer)
}
}
}
}
override fun visitPropertyAccessor(propertyAccessor: FirPropertyAccessor) = withProcessor {
dumpContext(propertyAccessor, ContextKind.SELF)
@@ -0,0 +1,16 @@
/*
* Copyright 2010-2023 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 test
interface MyInterface {
operator fun getValue(thisRef: Any, property: Any): MyInterface
}
class Foo(constructorParam: MyInterface) {
val regularProperty: MyInterface
val property: MyInterface by <expr>constructorParam</expr>
}
@@ -0,0 +1,43 @@
Tower Data Context:
Element 0
Scope: FirDefaultStarImportingScope
Element 1
Scope: FirDefaultSimpleImportingScope
Element 2
Scope: FirExplicitStarImportingScope
Element 3
Scope: FirDefaultSimpleImportingScope
Element 4
Scope: FirDefaultSimpleImportingScope
Element 5
Scope: FirPackageMemberScope
Element 6
Scope: FirExplicitSimpleImportingScope
Element 7
Implicit receiver:
FirRegularClassSymbol public final class Foo : R|kotlin/Any|
Type: test/Foo
Element 8
Scope: FirLocalScope
Properties:
FirValueParameterSymbol constructorParam: R|test/MyInterface|
FILE: [ResolvedTo(IMPORTS)] propertyDelegateInitializer.kt
public abstract [ResolvedTo(STATUS)] interface MyInterface : R|kotlin/Any| {
public abstract operator [ResolvedTo(CONTRACTS)] fun getValue([ResolvedTo(CONTRACTS)] thisRef: R|kotlin/Any|, [ResolvedTo(CONTRACTS)] property: R|kotlin/Any|): R|test/MyInterface|
}
public final [ResolvedTo(STATUS)] class Foo : R|kotlin/Any| {
public [ResolvedTo(STATUS)] constructor([ResolvedTo(STATUS)] constructorParam: R|test/MyInterface|): R|test/Foo| {
LAZY_super<R|kotlin/Any|>
}
public final [ResolvedTo(STATUS)] val regularProperty: R|test/MyInterface|
public [ResolvedTo(STATUS)] get(): R|test/MyInterface|
public final [ResolvedTo(BODY_RESOLVE)] val property: R|test/MyInterface|by R|<local>/constructorParam|
public [ResolvedTo(BODY_RESOLVE)] get(): R|test/MyInterface| {
^ this@R|test/Foo|.D|test/Foo.property|.R|test/MyInterface.getValue|(this@R|test/Foo|, ::R|test/Foo.property|)
}
}
@@ -90,6 +90,12 @@ public class ContextCollectorSourceTestGenerated extends AbstractContextCollecto
runTest("analysis/low-level-api-fir/testdata/contextCollector/primaryConstructorParameter.kt");
}
@Test
@TestMetadata("propertyDelegateInitializer.kt")
public void testPropertyDelegateInitializer() throws Exception {
runTest("analysis/low-level-api-fir/testdata/contextCollector/propertyDelegateInitializer.kt");
}
@Test
@TestMetadata("simple.kt")
public void testSimple() throws Exception {