From 9ea4afe7c82ec48bcdb78da8a7791e48d87358ed Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Wed, 22 Nov 2023 09:40:22 +0100 Subject: [PATCH] [FIR] Fix isValidTypeParameterFromOuterDeclaration check for local class This fixes an IllegalArgumentException when symbolProvider.getClassLikeSymbolByClassId is called with a local class ID. #KT-63656 Fixed --- .../kotlin/fir/resolve/DeclarationUtils.kt | 9 ++-- .../outerArgumentsRequired.fir.kt | 20 ++++++++ .../innerClasses/outerArgumentsRequired.kt | 20 ++++++++ .../innerClasses/outerArgumentsRequired.txt | 49 ------------------- 4 files changed, 46 insertions(+), 52 deletions(-) delete mode 100644 compiler/testData/diagnostics/tests/generics/innerClasses/outerArgumentsRequired.txt diff --git a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/DeclarationUtils.kt b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/DeclarationUtils.kt index 80f5c6d90e3..c4170ba717e 100644 --- a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/DeclarationUtils.kt +++ b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/DeclarationUtils.kt @@ -7,12 +7,15 @@ package org.jetbrains.kotlin.fir.resolve import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.containingClassForLocalAttr +import org.jetbrains.kotlin.fir.containingClassLookupTag import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.utils.isInner import org.jetbrains.kotlin.fir.declarations.utils.isLocal import org.jetbrains.kotlin.fir.resolve.dfa.RealVariable import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider -import org.jetbrains.kotlin.fir.symbols.impl.* +import org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol +import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol +import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.name.ClassId @@ -53,8 +56,8 @@ fun isValidTypeParameterFromOuterDeclaration( } if (currentDeclaration is FirCallableDeclaration) { - val containingClassId = currentDeclaration.symbol.callableId.classId ?: return true - return containsTypeParameter(session.symbolProvider.getClassLikeSymbolByClassId(containingClassId)?.fir) + val containingClassLookupTag = currentDeclaration.containingClassLookupTag() ?: return true + return containsTypeParameter(containingClassLookupTag.toSymbol(session)?.fir) } else if (currentDeclaration is FirClass) { for (superTypeRef in currentDeclaration.superTypeRefs) { val superClassFir = superTypeRef.firClassLike(session) ?: return true diff --git a/compiler/testData/diagnostics/tests/generics/innerClasses/outerArgumentsRequired.fir.kt b/compiler/testData/diagnostics/tests/generics/innerClasses/outerArgumentsRequired.fir.kt index 7df2d4765d9..646c6cd945d 100644 --- a/compiler/testData/diagnostics/tests/generics/innerClasses/outerArgumentsRequired.fir.kt +++ b/compiler/testData/diagnostics/tests/generics/innerClasses/outerArgumentsRequired.fir.kt @@ -1,3 +1,4 @@ +// WITH_STDLIB import A.B.D import A.B.C import A.B.D.Innermost @@ -19,5 +20,24 @@ class A { val d: D? = null val innerMost: Innermost? = null + + fun foo() { + object { + val something = listOf<B>() + } + } + } + + fun foo() { + object { + val something = listOf>() // False positive in K1 KT-63732 + } + } +} + +fun bar() { + data class Example(val foo: Int) + object { + val something = listOf() } } diff --git a/compiler/testData/diagnostics/tests/generics/innerClasses/outerArgumentsRequired.kt b/compiler/testData/diagnostics/tests/generics/innerClasses/outerArgumentsRequired.kt index 10f529147c9..faefa70116c 100644 --- a/compiler/testData/diagnostics/tests/generics/innerClasses/outerArgumentsRequired.kt +++ b/compiler/testData/diagnostics/tests/generics/innerClasses/outerArgumentsRequired.kt @@ -1,3 +1,4 @@ +// WITH_STDLIB import A.B.D import A.B.C import A.B.D.Innermost @@ -19,5 +20,24 @@ class A { val d: D? = null val innerMost: Innermost? = null + + fun foo() { + object { + val something = listOf<B>() + } + } + } + + fun foo() { + object { + val something = listOf<B>() // False positive in K1 KT-63732 + } + } +} + +fun bar() { + data class Example(val foo: Int) + object { + val something = listOf() } } diff --git a/compiler/testData/diagnostics/tests/generics/innerClasses/outerArgumentsRequired.txt b/compiler/testData/diagnostics/tests/generics/innerClasses/outerArgumentsRequired.txt deleted file mode 100644 index 77017ab4c3b..00000000000 --- a/compiler/testData/diagnostics/tests/generics/innerClasses/outerArgumentsRequired.txt +++ /dev/null @@ -1,49 +0,0 @@ -package - -public final class A { - public constructor A() - public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean - public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int - public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String - - public final inner class B /*captured type parameters: /*1*/ T*/ { - public constructor B() - public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean - public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int - public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String - - public final inner class C /*captured type parameters: /*1*/ F, /*2*/ T*/ { - public constructor C() - public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean - public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int - public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String - } - - public final inner class D /*captured type parameters: /*0*/ F, /*1*/ T*/ { - public constructor D() - public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean - public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int - public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String - - public final inner class Innermost /*captured type parameters: /*1*/ F, /*2*/ T*/ { - public constructor Innermost() - public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean - public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int - public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String - } - } - } - - public final class Nested { - public constructor Nested() - public final val c: [Error type: Type for error type constructor (C)]? - public final val d: [Error type: Type for error type constructor (D)]? - public final val innerMost: [Error type: Type for error type constructor (Innermost)]? - public final val x: [Error type: Type for error type constructor (B)]? - public final val y: [Error type: Type for error type constructor (C)]? - public final val z: [Error type: Type for error type constructor (D)]? - public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean - public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int - public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String - } -}