From 5ff82addc93b19c31ad0aa4b8e1cb9e4bc69aade Mon Sep 17 00:00:00 2001 From: Andrey Zinovyev Date: Sun, 11 Apr 2021 20:18:34 +0300 Subject: [PATCH] [FIR] Add REIFIED_TYPE_PARAMETER_NO_INLINE check --- .../FirReifiedTypeParameterChecker.kt | 36 +++++++++++++++++++ .../fir/checkers/CommonDeclarationCheckers.kt | 1 + .../generics/tpAsReified/InConstructor.fir.kt | 2 +- .../generics/tpAsReified/InProperty.fir.kt | 2 -- .../tests/generics/tpAsReified/InProperty.kt | 1 + .../tests/generics/tpAsReified/InType.fir.kt | 4 --- .../tests/generics/tpAsReified/InType.kt | 1 + .../generics/tpAsReified/LocalFun.fir.kt | 9 ----- .../tests/generics/tpAsReified/LocalFun.kt | 1 + .../tpAsReified/NotInlineableReified.fir.kt | 4 --- .../tpAsReified/NotInlineableReified.kt | 1 + .../generics/tpAsReified/ReifiedClass.fir.kt | 3 -- .../generics/tpAsReified/ReifiedClass.kt | 1 + .../tests/resolve/invoke/KT-4372.fir.kt | 22 ------------ .../tests/resolve/invoke/KT-4372.kt | 1 + .../testsWithStdLib/native/reified.fir.kt | 4 +-- 16 files changed, 46 insertions(+), 47 deletions(-) create mode 100644 compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirReifiedTypeParameterChecker.kt delete mode 100644 compiler/testData/diagnostics/tests/generics/tpAsReified/InProperty.fir.kt delete mode 100644 compiler/testData/diagnostics/tests/generics/tpAsReified/InType.fir.kt delete mode 100644 compiler/testData/diagnostics/tests/generics/tpAsReified/LocalFun.fir.kt delete mode 100644 compiler/testData/diagnostics/tests/generics/tpAsReified/NotInlineableReified.fir.kt delete mode 100644 compiler/testData/diagnostics/tests/generics/tpAsReified/ReifiedClass.fir.kt delete mode 100644 compiler/testData/diagnostics/tests/resolve/invoke/KT-4372.fir.kt diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirReifiedTypeParameterChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirReifiedTypeParameterChecker.kt new file mode 100644 index 00000000000..6b587292bc8 --- /dev/null +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirReifiedTypeParameterChecker.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2010-2021 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 org.jetbrains.kotlin.fir.analysis.checkers.declaration + +import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext +import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter +import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors +import org.jetbrains.kotlin.fir.analysis.diagnostics.reportOn +import org.jetbrains.kotlin.fir.declarations.* +import org.jetbrains.kotlin.lexer.KtTokens + +object FirReifiedTypeParameterChecker : FirTypeParameterChecker() { + override fun check(declaration: FirTypeParameter, context: CheckerContext, reporter: DiagnosticReporter) { + if (!declaration.isReified) return + val containingDeclaration = context.containingDeclarations.lastOrNull() ?: return + + val forbidReified = (containingDeclaration is FirRegularClass) || + (containingDeclaration is FirSimpleFunction && !containingDeclaration.isInline) || + (containingDeclaration is FirProperty && !containingDeclaration.areAccessorsInline()) + + if (forbidReified) { + val reportTarget = declaration.getModifier(KtTokens.REIFIED_KEYWORD)?.source + reporter.reportOn(reportTarget, FirErrors.REIFIED_TYPE_PARAMETER_NO_INLINE, context) + } + } + + private fun FirProperty.areAccessorsInline(): Boolean { + if (getter?.isInline != true) return false + if (isVar && setter?.isInline != true) return false + return true + } + +} diff --git a/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/checkers/CommonDeclarationCheckers.kt b/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/checkers/CommonDeclarationCheckers.kt index 395452accda..4cb7b76638f 100644 --- a/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/checkers/CommonDeclarationCheckers.kt +++ b/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/checkers/CommonDeclarationCheckers.kt @@ -94,5 +94,6 @@ object CommonDeclarationCheckers : DeclarationCheckers() { override val typeParameterCheckers: Set = setOf( FirTypeParameterBoundsChecker, FirTypeParameterVarianceChecker, + FirReifiedTypeParameterChecker, ) } diff --git a/compiler/testData/diagnostics/tests/generics/tpAsReified/InConstructor.fir.kt b/compiler/testData/diagnostics/tests/generics/tpAsReified/InConstructor.fir.kt index 9016284cedf..edd4554f33d 100644 --- a/compiler/testData/diagnostics/tests/generics/tpAsReified/InConstructor.fir.kt +++ b/compiler/testData/diagnostics/tests/generics/tpAsReified/InConstructor.fir.kt @@ -1,5 +1,5 @@ // !WITH_NEW_INFERENCE -class C +class C<reified T> fun id(p: T): T = p diff --git a/compiler/testData/diagnostics/tests/generics/tpAsReified/InProperty.fir.kt b/compiler/testData/diagnostics/tests/generics/tpAsReified/InProperty.fir.kt deleted file mode 100644 index b1f7e802c2b..00000000000 --- a/compiler/testData/diagnostics/tests/generics/tpAsReified/InProperty.fir.kt +++ /dev/null @@ -1,2 +0,0 @@ -val T.v: T - get() = throw UnsupportedOperationException() diff --git a/compiler/testData/diagnostics/tests/generics/tpAsReified/InProperty.kt b/compiler/testData/diagnostics/tests/generics/tpAsReified/InProperty.kt index 0bbc77f3ef1..99e5534b570 100644 --- a/compiler/testData/diagnostics/tests/generics/tpAsReified/InProperty.kt +++ b/compiler/testData/diagnostics/tests/generics/tpAsReified/InProperty.kt @@ -1,2 +1,3 @@ +// FIR_IDENTICAL val <reified T> T.v: T get() = throw UnsupportedOperationException() diff --git a/compiler/testData/diagnostics/tests/generics/tpAsReified/InType.fir.kt b/compiler/testData/diagnostics/tests/generics/tpAsReified/InType.fir.kt deleted file mode 100644 index 6f5cac1d5c2..00000000000 --- a/compiler/testData/diagnostics/tests/generics/tpAsReified/InType.fir.kt +++ /dev/null @@ -1,4 +0,0 @@ -class C - -fun main(p1: C, p2: C) { -} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/generics/tpAsReified/InType.kt b/compiler/testData/diagnostics/tests/generics/tpAsReified/InType.kt index b6e2755b8da..9ae56144678 100644 --- a/compiler/testData/diagnostics/tests/generics/tpAsReified/InType.kt +++ b/compiler/testData/diagnostics/tests/generics/tpAsReified/InType.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL class C<reified T> fun main(p1: C, p2: C) { diff --git a/compiler/testData/diagnostics/tests/generics/tpAsReified/LocalFun.fir.kt b/compiler/testData/diagnostics/tests/generics/tpAsReified/LocalFun.fir.kt deleted file mode 100644 index e984a060a04..00000000000 --- a/compiler/testData/diagnostics/tests/generics/tpAsReified/LocalFun.fir.kt +++ /dev/null @@ -1,9 +0,0 @@ -// !DIAGNOSTICS: -UNUSED_PARAMETER -NOT_YET_SUPPORTED_IN_INLINE - -inline fun foo(x: T) { - fun bar() { - - } - - bar() -} diff --git a/compiler/testData/diagnostics/tests/generics/tpAsReified/LocalFun.kt b/compiler/testData/diagnostics/tests/generics/tpAsReified/LocalFun.kt index 6892134bc2d..0a80ad2a666 100644 --- a/compiler/testData/diagnostics/tests/generics/tpAsReified/LocalFun.kt +++ b/compiler/testData/diagnostics/tests/generics/tpAsReified/LocalFun.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_PARAMETER -NOT_YET_SUPPORTED_IN_INLINE inline fun foo(x: T) { diff --git a/compiler/testData/diagnostics/tests/generics/tpAsReified/NotInlineableReified.fir.kt b/compiler/testData/diagnostics/tests/generics/tpAsReified/NotInlineableReified.fir.kt deleted file mode 100644 index c9a6bc6c2ba..00000000000 --- a/compiler/testData/diagnostics/tests/generics/tpAsReified/NotInlineableReified.fir.kt +++ /dev/null @@ -1,4 +0,0 @@ - -fun foo() { - -} diff --git a/compiler/testData/diagnostics/tests/generics/tpAsReified/NotInlineableReified.kt b/compiler/testData/diagnostics/tests/generics/tpAsReified/NotInlineableReified.kt index c1154e70574..ea4a0683c97 100644 --- a/compiler/testData/diagnostics/tests/generics/tpAsReified/NotInlineableReified.kt +++ b/compiler/testData/diagnostics/tests/generics/tpAsReified/NotInlineableReified.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL funreified T2, T3, reified T4> foo() { diff --git a/compiler/testData/diagnostics/tests/generics/tpAsReified/ReifiedClass.fir.kt b/compiler/testData/diagnostics/tests/generics/tpAsReified/ReifiedClass.fir.kt deleted file mode 100644 index d12c8d6b0a8..00000000000 --- a/compiler/testData/diagnostics/tests/generics/tpAsReified/ReifiedClass.fir.kt +++ /dev/null @@ -1,3 +0,0 @@ -class A { - fun foo(): T2 = throw UnsupportedOperationException() -} diff --git a/compiler/testData/diagnostics/tests/generics/tpAsReified/ReifiedClass.kt b/compiler/testData/diagnostics/tests/generics/tpAsReified/ReifiedClass.kt index ba9e188b4f8..3e66639cf3c 100644 --- a/compiler/testData/diagnostics/tests/generics/tpAsReified/ReifiedClass.kt +++ b/compiler/testData/diagnostics/tests/generics/tpAsReified/ReifiedClass.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL class Areified T2, T3, reified T4> { fun<reified R> foo(): T2 = throw UnsupportedOperationException() } diff --git a/compiler/testData/diagnostics/tests/resolve/invoke/KT-4372.fir.kt b/compiler/testData/diagnostics/tests/resolve/invoke/KT-4372.fir.kt deleted file mode 100644 index b33f336a2af..00000000000 --- a/compiler/testData/diagnostics/tests/resolve/invoke/KT-4372.fir.kt +++ /dev/null @@ -1,22 +0,0 @@ -// !DIAGNOSTICS: -UNUSED_PARAMETER - -//KT-4372 Invalid error position and failing to resolve invoke with receiver from other module - -class Foo { - fun invoke(content: TInner.() -> Unit) { - } -} - -// comment this function to fix the error below -fun Foo.invoke(name: String, content: TInner.() -> Unit) {} - -enum class EnumClass(val x: String) {} -object Y { - val x = javaClass() // javaClass unresolved in any file in this module -} - -//declarations from library -val T.javaClass : Class - get() = throw Exception() - -fun javaClass() : Class = throw Exception() diff --git a/compiler/testData/diagnostics/tests/resolve/invoke/KT-4372.kt b/compiler/testData/diagnostics/tests/resolve/invoke/KT-4372.kt index 96cf43ff1cb..c61d8f2ae2f 100644 --- a/compiler/testData/diagnostics/tests/resolve/invoke/KT-4372.kt +++ b/compiler/testData/diagnostics/tests/resolve/invoke/KT-4372.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_PARAMETER //KT-4372 Invalid error position and failing to resolve invoke with receiver from other module diff --git a/compiler/testData/diagnostics/testsWithStdLib/native/reified.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/native/reified.fir.kt index 1dd7ee2ac95..c398f496c97 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/native/reified.fir.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/native/reified.fir.kt @@ -1,4 +1,4 @@ import kotlin.jvm.* -external fun foo() -inline external fun bar() \ No newline at end of file +external fun <reified T> foo() +inline external fun bar()