K2: don't use outer class type parameters during nested class TP resolve

Before this commit, we allowed access to outer class type parameters
during resolve of type parameter bounds of a nested class.
In fact, outer type parameters are accessible in this situation
only if it's an inner class (or a local class).
This commit forbids such a usage. In the earlier fix of KT-57209
the same was done for regular type reference resolve.

#KT-61459 Fixed
#KT-61959 Fixed
This commit is contained in:
Mikhail Glukhikh
2023-09-08 17:08:56 +02:00
committed by Space Team
parent cdc197c723
commit 9cf1d36e5e
7 changed files with 10 additions and 40 deletions
@@ -1,6 +1,5 @@
package test
class Outer<MyParam> {
// TODO this test has an error, MyParam should not be resolved here. See KT-61959
class Nested<T : <expr>MyParam</expr>>
}
@@ -20,7 +20,7 @@ Tower Data Context:
Element 8
Scope: FirNestedClassifierScopeImpl
Classifiers:
FirRegularClassSymbol public final class Nested<T : R|MyParam|> : R|kotlin/Any|
FirRegularClassSymbol public final class Nested<T : <ERROR TYPE REF: Symbol not found for MyParam>> : R|kotlin/Any|
Element 9
Implicit receiver:
FirRegularClassSymbol public final class Outer<MyParam> : R|kotlin/Any|
@@ -28,7 +28,7 @@ Tower Data Context:
Element 10
Scope: FirMemberTypeParameterScope
Classifiers:
FirTypeParameterSymbol T : R|MyParam|
FirTypeParameterSymbol T : <ERROR TYPE REF: Symbol not found for MyParam>
FILE: [ResolvedTo(IMPORTS)] declaredInClass_fromNested_typeBound.kt
public final [ResolvedTo(STATUS)] class Outer<[ResolvedTo(STATUS)] MyParam> : R|kotlin/Any| {
@@ -36,8 +36,8 @@ FILE: [ResolvedTo(IMPORTS)] declaredInClass_fromNested_typeBound.kt
super<R|kotlin/Any|>()
}
public final [ResolvedTo(STATUS)] class Nested<[ResolvedTo(STATUS)] T : R|MyParam|> : R|kotlin/Any| {
public [ResolvedTo(BODY_RESOLVE)] constructor<[ResolvedTo(STATUS)] T : R|MyParam|>(): R|test/Outer.Nested<T>| {
public final [ResolvedTo(STATUS)] class Nested<[ResolvedTo(STATUS)] T : <ERROR TYPE REF: Symbol not found for MyParam>> : R|kotlin/Any| {
public [ResolvedTo(BODY_RESOLVE)] constructor<[ResolvedTo(STATUS)] T : <ERROR TYPE REF: Symbol not found for MyParam>>(): R|test/Outer.Nested<T>| {
super<R|kotlin/Any|>()
}
@@ -125,6 +125,10 @@ open class FirTypeResolveTransformer(
fun transformClassTypeParameters(regularClass: FirRegularClass, data: Any?) {
withScopeCleanup {
// Remove type parameter scopes for classes that are neither inner nor local
if (removeOuterTypeParameterScope(regularClass)) {
this.scopes = staticScopes
}
addTypeParametersScope(regularClass)
regularClass.typeParameters.forEach {
it.accept(this, data)
@@ -1,22 +0,0 @@
// ISSUE: KT-61459
interface Alarm {
interface Builder<Self : Builder<Self>> {
fun build(): Alarm
}
}
abstract class AbstractAlarm<Self : AbstractAlarm<Self, Builder>, Builder : AbstractAlarm.Builder<Builder, Self>>(
val identifier: String,
) : Alarm {
abstract class Builder<Self : Builder<!TYPE_ARGUMENTS_NOT_ALLOWED!><Self, Built><!>, Built : AbstractAlarm<Built, <!UPPER_BOUND_VIOLATED!>Self<!>>> : Alarm.Builder<<!UPPER_BOUND_VIOLATED!>Self<!>> {
private var identifier: String = ""
fun setIdentifier(text: String): Self {
this.identifier = text
return this <!UNCHECKED_CAST!>as Self<!>
}
final override fun build(): Built = TODO()
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// ISSUE: KT-61459
interface Alarm {
@@ -1,13 +0,0 @@
// ISSUE: KT-61959
package test
interface OuterParam
class Outer<OuterParam> {
class Nested<NestedParam : OuterParam>
}
fun main() {
Outer.Nested<<!UPPER_BOUND_VIOLATED!>OuterParam<!>>()
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// ISSUE: KT-61959
package test