diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/OptInUsageChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/OptInUsageChecker.kt index 1fc8a0d5f61..ca4c6a36ac4 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/OptInUsageChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/OptInUsageChecker.kt @@ -462,11 +462,14 @@ class OptInUsageChecker(project: Project) : CallChecker { } if (element.getParentOfType(false) == null) { + val containingClass = element.getParentOfType(strict = true) val descriptions = targetDescriptor.loadOptIns( moduleAnnotationsResolver, bindingContext, context.languageVersionSettings, - fromSupertype = element.getParentOfType(strict = true) != null + fromSupertype = containingClass != null && containingClass.superTypeListEntries.any { + it.typeAsUserType == element.parent + } ) reportNotAllowedOptIns(descriptions, element, context) } diff --git a/compiler/testData/diagnostics/testsWithStdLib/experimental/subClassOptInRequired.kt b/compiler/testData/diagnostics/testsWithStdLib/experimental/subClassOptInRequired.kt index 4d4638b3acf..0721c40f68a 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/experimental/subClassOptInRequired.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/experimental/subClassOptInRequired.kt @@ -25,7 +25,7 @@ class GrandDerivedFourth : DerivedFourth() open class Marked @SubclassOptInRequired(Marker::class) -class DerivedMarked : Marked() +open class DerivedMarked : Marked() fun test() { val b = Base() @@ -34,3 +34,18 @@ fun test() { val d3 = DerivedThird() val d4 = DerivedFourth() } + +fun test2(b: Base, g: Generic) { + object : Base() {} +} + +open class Generic + +class DerivedGeneric : Generic() + +@SubclassOptInRequired(Marker::class) +interface BaseInterface + +interface DerivedInterface : BaseInterface + +class Delegated(val bi: BaseInterface) : BaseInterface by bi diff --git a/compiler/testData/diagnostics/testsWithStdLib/experimental/subClassOptInRequired.txt b/compiler/testData/diagnostics/testsWithStdLib/experimental/subClassOptInRequired.txt index 9230f125b4e..b4ed3fb898d 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/experimental/subClassOptInRequired.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/experimental/subClassOptInRequired.txt @@ -1,6 +1,7 @@ package public fun test(): kotlin.Unit +public fun test2(/*0*/ b: Base, /*1*/ g: Generic): kotlin.Unit @kotlin.SubclassOptInRequired(markerClass = Marker::class) public open class Base { public constructor Base() @@ -9,6 +10,20 @@ public fun test(): kotlin.Unit public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } +@kotlin.SubclassOptInRequired(markerClass = Marker::class) public interface BaseInterface { + 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 Delegated : BaseInterface { + public constructor Delegated(/*0*/ bi: BaseInterface) + public final val bi: BaseInterface + 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 +} + @Marker public final class DerivedFirst : Base { public constructor DerivedFirst() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean @@ -23,7 +38,20 @@ public open class DerivedFourth : Base { public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } -@kotlin.SubclassOptInRequired(markerClass = Marker::class) public final class DerivedMarked : Marked { +public final class DerivedGeneric : Generic { + public constructor DerivedGeneric() + 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 interface DerivedInterface : BaseInterface { + 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 +} + +@kotlin.SubclassOptInRequired(markerClass = Marker::class) public open class DerivedMarked : Marked { public constructor DerivedMarked() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int @@ -44,6 +72,13 @@ public open class DerivedFourth : Base { public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } +public open class Generic { + public constructor Generic() + 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 GrandDerivedFourth : DerivedFourth { public constructor GrandDerivedFourth() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean