[FE] Fix source element duplication in object literal expressions

This commit is contained in:
Yan Zhulanow
2022-09-27 02:24:54 +09:00
committed by teamcity
parent ea3f550b58
commit 61ce9fe524
14 changed files with 63 additions and 64 deletions
@@ -45,7 +45,6 @@ internal fun KtExpression.unwrap(): KtExpression {
return when (this) {
is KtLabeledExpression -> baseExpression?.unwrap()
is KtAnnotatedExpression -> baseExpression?.unwrap()
is KtObjectLiteralExpression -> objectDeclaration
is KtFunctionLiteral -> (parent as? KtLambdaExpression)?.unwrap()
else -> this
} ?: this
@@ -44,7 +44,6 @@ internal class FirElementBuilder(
// null will be return in case of invalid KtValueArgument
deparenthesized.getArgumentExpression()
}
deparenthesized is KtObjectLiteralExpression -> deparenthesized.objectDeclaration
deparenthesized is KtStringTemplateEntryWithExpression -> deparenthesized.expression
deparenthesized is KtUserType && deparenthesized.parent is KtNullableType -> deparenthesized.parent as KtNullableType
else -> deparenthesized
@@ -0,0 +1,5 @@
// LOOK_UP_FOR_ELEMENT_OF_TYPE: org.jetbrains.kotlin.psi.KtObjectDeclaration
fun test() {
<expr>object {}</expr>
}
@@ -0,0 +1,11 @@
KT element: KtObjectDeclaration
FIR element: FirAnonymousObjectImpl
FIR source kind: KtRealSourceElementKind
FIR element rendered:
object : R|kotlin/Any| {
private constructor(): R|<anonymous>| {
super<R|kotlin/Any|>()
}
}
@@ -0,0 +1,5 @@
// LOOK_UP_FOR_ELEMENT_OF_TYPE: org.jetbrains.kotlin.psi.KtObjectLiteralExpression
fun test() {
<expr>object {}</expr>
}
@@ -0,0 +1,11 @@
KT element: KtObjectLiteralExpression
FIR element: FirAnonymousObjectExpressionImpl
FIR source kind: KtRealSourceElementKind
FIR element rendered:
object : R|kotlin/Any| {
private constructor(): R|<anonymous>| {
super<R|kotlin/Any|>()
}
}
@@ -215,6 +215,18 @@ public class GetOrBuildFirTestGenerated extends AbstractGetOrBuildFirTest {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/low-level-api-fir/testdata/getOrBuildFir/declarations"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@Test
@TestMetadata("objectLiteral.kt")
public void testObjectLiteral() throws Exception {
runTest("analysis/low-level-api-fir/testdata/getOrBuildFir/declarations/objectLiteral.kt");
}
@Test
@TestMetadata("objectLiteralExpression.kt")
public void testObjectLiteralExpression() throws Exception {
runTest("analysis/low-level-api-fir/testdata/getOrBuildFir/declarations/objectLiteralExpression.kt");
}
@Test
@TestMetadata("propertyDelegate.kt")
public void testPropertyDelegate() throws Exception {
@@ -25,12 +25,15 @@ object FirThrowableSubclassChecker : FirClassChecker() {
if (declaration.typeParameters.isNotEmpty()) {
reporter.reportOn(declaration.typeParameters.firstOrNull()?.source, FirErrors.GENERIC_THROWABLE_SUBCLASS, context)
val source = when {
(declaration as? FirRegularClass)?.let { it.isInner || it.isLocal } == true -> declaration.source
declaration is FirAnonymousObject -> (declaration.declarations.firstOrNull())?.source
else -> null
val shouldReport = when (declaration) {
is FirRegularClass -> declaration.isInner || declaration.isLocal
is FirAnonymousObject -> true
else -> false
}
if (shouldReport) {
reporter.reportOn(declaration.source, FirErrors.INNER_CLASS_OF_GENERIC_THROWABLE_SUBCLASS, context)
}
reporter.reportOn(source, FirErrors.INNER_CLASS_OF_GENERIC_THROWABLE_SUBCLASS, context)
} else if (declaration.hasGenericOuterDeclaration(context)) {
reporter.reportOn(declaration.source, FirErrors.INNER_CLASS_OF_GENERIC_THROWABLE_SUBCLASS, context)
}
@@ -634,10 +634,9 @@ class DeclarationsConverter(
return withChildClassName(SpecialNames.ANONYMOUS, isExpect = false) {
var delegatedFieldsMap: Map<Int, FirFieldSymbol>? = null
buildAnonymousObjectExpression {
val objectDeclaration = objectLiteral.getChildNodesByType(OBJECT_DECLARATION).first()
val sourceElement = objectDeclaration.toFirSourceElement()
source = sourceElement
source = objectLiteral.toFirSourceElement()
anonymousObject = buildAnonymousObject {
val objectDeclaration = objectLiteral.getChildNodesByType(OBJECT_DECLARATION).first()
source = objectDeclaration.toFirSourceElement()
origin = FirDeclarationOrigin.Source
moduleData = baseModuleData
@@ -1226,14 +1226,13 @@ open class RawFirBuilder(
}
override fun visitObjectLiteralExpression(expression: KtObjectLiteralExpression, data: Unit): FirElement {
val objectDeclaration = expression.objectDeclaration
return withChildClassName(SpecialNames.ANONYMOUS, forceLocalContext = true, isExpect = false) {
var delegatedFieldsMap: Map<Int, FirFieldSymbol>?
buildAnonymousObjectExpression {
val sourceElement = objectDeclaration.toFirSourceElement()
source = sourceElement
source = expression.toFirSourceElement()
anonymousObject = buildAnonymousObject {
source = sourceElement
val objectDeclaration = expression.objectDeclaration
source = objectDeclaration.toFirSourceElement()
moduleData = baseModuleData
origin = FirDeclarationOrigin.Source
classKind = ClassKind.OBJECT
@@ -35,8 +35,10 @@ object LightTreePositioningStrategies {
): List<TextRange> {
when (node.tokenType) {
KtNodeTypes.OBJECT_LITERAL -> {
val objectKeyword = tree.findDescendantByType(node, KtTokens.OBJECT_KEYWORD)!!
return markElement(objectKeyword, startOffset, endOffset, tree, node)
val objectDeclaration = tree.findDescendantByType(node, KtNodeTypes.OBJECT_DECLARATION)!!
val objectKeyword = tree.objectKeyword(objectDeclaration)!!
val supertypeList = tree.supertypesList(objectDeclaration)
return markRange(objectKeyword, supertypeList ?: objectKeyword, startOffset, endOffset, tree, node)
}
KtNodeTypes.OBJECT_DECLARATION -> {
val objectKeyword = tree.objectKeyword(node)!!
@@ -1,47 +0,0 @@
// !DIAGNOSTICS: -UNUSED_EXPRESSION -UNUSED_PARAMETER -UNUSED_VARIABLE -NOTHING_TO_INLINE
// !LANGUAGE: +InlineDefaultFunctionalParameters
fun test() = "OK"
val prop = "OK"
class Foo {
fun test() = "OK"
val prop = "OK"
}
object FooObject {
fun test() = "OK"
val prop = "OK"
}
inline fun default1(s : () -> String = {"OK"}) {}
inline fun default2(s : () -> String = ::test) {}
inline fun default3(s : () -> String = ::prop) {}
inline fun default4(s : () -> String = FooObject::test) {}
inline fun default5(s : () -> String = FooObject::prop) {}
inline fun default6(s : (a: Foo) -> String = Foo::test) {}
inline fun default7(s : (a: Foo) -> String = Foo::prop) {}
val a = Foo()
inline fun default8(s : () -> String = a::test) {}
inline fun default9(s : () -> String = a::prop) {}
inline fun default10(s : () -> String = <!INVALID_DEFAULT_FUNCTIONAL_PARAMETER_FOR_INLINE!>object<!> : Function0<String> {
override fun invoke(): String {
return "FAIL"
}
}) {}
abstract class Base {
abstract fun foo(f: () -> Unit = { })
}
class Derived : Base() {
<!OVERRIDE_BY_INLINE!>override final inline fun foo(<!NOT_YET_SUPPORTED_IN_INLINE!>f: () -> Unit<!>)<!> {
f()
}
}
inline fun default11(s : () -> Derived = ::Derived) {}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_EXPRESSION -UNUSED_PARAMETER -UNUSED_VARIABLE -NOTHING_TO_INLINE
// !LANGUAGE: +InlineDefaultFunctionalParameters
@@ -1,7 +1,7 @@
interface Base {
fun foo()
}
val String.test: Base = <!EXTENSION_PROPERTY_WITH_BACKING_FIELD!>object<!>: Base {
val String.test: Base = <!EXTENSION_PROPERTY_WITH_BACKING_FIELD!>object: Base<!> {
override fun foo() {
this<!UNRESOLVED_LABEL!>@test<!>
}