Added Native-specific frontend checker for @SharedImmutable (#3091)

This commit is contained in:
LepilkinaElena
2020-03-04 16:45:37 +03:00
committed by GitHub
parent 8b559b20e2
commit b031706ea9
7 changed files with 321 additions and 1 deletions
@@ -0,0 +1,145 @@
// FILE: annotation.kt
package kotlin.native.concurrent
import kotlin.reflect.KProperty
@Target(AnnotationTarget.PROPERTY)
@Retention(AnnotationRetention.BINARY)
annotation class SharedImmutable
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
@Retention(AnnotationRetention.BINARY)
annotation class ThreadLocal
// FILE: test.kt
import kotlin.native.concurrent.SharedImmutable
import kotlin.native.concurrent.ThreadLocal
import kotlin.reflect.KProperty
fun println(<!UNUSED_PARAMETER!>value<!>: Int) {}
fun println(<!UNUSED_PARAMETER!>value<!>: String) {}
fun println(<!UNUSED_PARAMETER!>value<!>: Point) {}
data class Point(val x: Double, val y: Double)
@SharedImmutable
val point1 = Point(1.0, 1.0)
<!INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY!>@SharedImmutable<!>
var point2 = Point(2.0, 2.0)
class Date(<!INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL!>@SharedImmutable<!> val month: Int, <!INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY, INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL!>@SharedImmutable<!> var day:Int)
class Person(val name: String) {
<!INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY, INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL!>@SharedImmutable<!>
var surname: String? = null
}
class Figure {
<!INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY, INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL!>@SharedImmutable<!>
val cornerPoint: Point
get() = point1
}
<!INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY!>@SharedImmutable<!>
var age = 20
get() {
println("Age is: $field")
return field
}
set(value) {
println(value)
}
var globalAge = 30
<!INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY!>@SharedImmutable<!>
var age1 = 20
get() {
println("Age is: $field")
return field
}
set(value) {
globalAge = value
}
@SharedImmutable
val age2 = 20
get() {
println("Age is: $field")
return field
}
<!INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY!>@SharedImmutable<!>
var point3: Point
get() = point2
set(value) {
point2 = value
}
<!INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY!>@SharedImmutable<!>
var point4: Point
get() = point2
set(value) {
println(value)
}
@ThreadLocal
var point0 = Point(2.0, 2.0)
<!INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY!>@SharedImmutable<!>
var point5: Point
get() = point0
set(value) {
point0 = value
}
class Delegate {
var value = 20
operator fun getValue(thisRef: Any?, property: KProperty<*>): Int {
println("Get")
return value
}
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Int) {
println("Set")
}
}
@SharedImmutable
var property: Int by Delegate()
class Delegate1 {
var value = 20
operator fun getValue(thisRef: Any?, property: KProperty<*>): Int {
return value
}
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Int) {
this.value = value
}
}
@SharedImmutable
var property1: Int by Delegate1()
var globalValue: Int = 20
class Delegate2 {
operator fun getValue(thisRef: Any?, property: KProperty<*>): Int {
return globalValue
}
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Int) {
println(value)
}
}
@SharedImmutable
var property2: Int by Delegate2()
<!INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY!>@SharedImmutable<!>
val someValue: Int
get() = 20
@SharedImmutable
val someValueWithDelegate by Delegate()
@@ -0,0 +1,111 @@
package
@kotlin.native.concurrent.SharedImmutable public var age: kotlin.Int
@kotlin.native.concurrent.SharedImmutable public var age1: kotlin.Int
@kotlin.native.concurrent.SharedImmutable public val age2: kotlin.Int = 20
public var globalAge: kotlin.Int
public var globalValue: kotlin.Int
@kotlin.native.concurrent.ThreadLocal public var point0: Point
@kotlin.native.concurrent.SharedImmutable public val point1: Point
@kotlin.native.concurrent.SharedImmutable public var point2: Point
@kotlin.native.concurrent.SharedImmutable public var point3: Point
@kotlin.native.concurrent.SharedImmutable public var point4: Point
@kotlin.native.concurrent.SharedImmutable public var point5: Point
@kotlin.native.concurrent.SharedImmutable public var property: kotlin.Int
@kotlin.native.concurrent.SharedImmutable public var property1: kotlin.Int
@kotlin.native.concurrent.SharedImmutable public var property2: kotlin.Int
@kotlin.native.concurrent.SharedImmutable public val someValue: kotlin.Int
@kotlin.native.concurrent.SharedImmutable public val someValueWithDelegate: kotlin.Int
public fun println(/*0*/ value: Point): kotlin.Unit
public fun println(/*0*/ value: kotlin.Int): kotlin.Unit
public fun println(/*0*/ value: kotlin.String): kotlin.Unit
public final class Date {
public constructor Date(/*0*/ month: kotlin.Int, /*1*/ day: kotlin.Int)
@kotlin.native.concurrent.SharedImmutable public final var day: kotlin.Int
@kotlin.native.concurrent.SharedImmutable public final val month: kotlin.Int
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 Delegate {
public constructor Delegate()
public final var value: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final operator fun getValue(/*0*/ thisRef: kotlin.Any?, /*1*/ property: kotlin.reflect.KProperty<*>): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final operator fun setValue(/*0*/ thisRef: kotlin.Any?, /*1*/ property: kotlin.reflect.KProperty<*>, /*2*/ value: kotlin.Int): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Delegate1 {
public constructor Delegate1()
public final var value: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final operator fun getValue(/*0*/ thisRef: kotlin.Any?, /*1*/ property: kotlin.reflect.KProperty<*>): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final operator fun setValue(/*0*/ thisRef: kotlin.Any?, /*1*/ property: kotlin.reflect.KProperty<*>, /*2*/ value: kotlin.Int): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Delegate2 {
public constructor Delegate2()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final operator fun getValue(/*0*/ thisRef: kotlin.Any?, /*1*/ property: kotlin.reflect.KProperty<*>): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final operator fun setValue(/*0*/ thisRef: kotlin.Any?, /*1*/ property: kotlin.reflect.KProperty<*>, /*2*/ value: kotlin.Int): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Figure {
public constructor Figure()
@kotlin.native.concurrent.SharedImmutable public final val cornerPoint: Point
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 Person {
public constructor Person(/*0*/ name: kotlin.String)
public final val name: kotlin.String
@kotlin.native.concurrent.SharedImmutable public final var surname: kotlin.String?
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 data class Point {
public constructor Point(/*0*/ x: kotlin.Double, /*1*/ y: kotlin.Double)
public final val x: kotlin.Double
public final val y: kotlin.Double
public final operator /*synthesized*/ fun component1(): kotlin.Double
public final operator /*synthesized*/ fun component2(): kotlin.Double
public final /*synthesized*/ fun copy(/*0*/ x: kotlin.Double = ..., /*1*/ y: kotlin.Double = ...): Point
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
package kotlin {
package kotlin.native {
package kotlin.native.concurrent {
@kotlin.annotation.Target(allowedTargets = {AnnotationTarget.PROPERTY}) @kotlin.annotation.Retention(value = AnnotationRetention.BINARY) public final annotation class SharedImmutable : kotlin.Annotation {
public constructor SharedImmutable()
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.annotation.Target(allowedTargets = {AnnotationTarget.PROPERTY, AnnotationTarget.CLASS}) @kotlin.annotation.Retention(value = AnnotationRetention.BINARY) public final annotation class ThreadLocal : kotlin.Annotation {
public constructor ThreadLocal()
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
}
}
}
}
@@ -28,6 +28,11 @@ public class DiagnosticsNativeTestGenerated extends AbstractDiagnosticsNativeTes
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/nativeTests"), Pattern.compile("^(.+)\\.kt$"), null, true); KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/nativeTests"), Pattern.compile("^(.+)\\.kt$"), null, true);
} }
@TestMetadata("sharedImmutable.kt")
public void testSharedImmutable() throws Exception {
runTest("compiler/testData/diagnostics/nativeTests/sharedImmutable.kt");
}
@TestMetadata("throws.kt") @TestMetadata("throws.kt")
public void testThrows() throws Exception { public void testThrows() throws Exception {
runTest("compiler/testData/diagnostics/nativeTests/throws.kt"); runTest("compiler/testData/diagnostics/nativeTests/throws.kt");
@@ -20,6 +20,11 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy {
ErrorsNative.INCOMPATIBLE_THROWS_INHERITED, "Member inherits different @Throws filters from {0}", ErrorsNative.INCOMPATIBLE_THROWS_INHERITED, "Member inherits different @Throws filters from {0}",
Renderers.commaSeparated(Renderers.NAME) Renderers.commaSeparated(Renderers.NAME)
) )
put(
ErrorsNative.INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY,
"@SharedImmutable is applicable only to val with backing field or to property with delegation"
)
put(ErrorsNative.INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL, "@SharedImmutable is applicable only to top level declarations")
} }
} }
@@ -20,6 +20,10 @@ object ErrorsNative {
val INCOMPATIBLE_THROWS_OVERRIDE = DiagnosticFactory1.create<KtElement, DeclarationDescriptor>(Severity.ERROR) val INCOMPATIBLE_THROWS_OVERRIDE = DiagnosticFactory1.create<KtElement, DeclarationDescriptor>(Severity.ERROR)
@JvmField @JvmField
val INCOMPATIBLE_THROWS_INHERITED = DiagnosticFactory1.create<KtDeclaration, Collection<DeclarationDescriptor>>(Severity.ERROR) val INCOMPATIBLE_THROWS_INHERITED = DiagnosticFactory1.create<KtDeclaration, Collection<DeclarationDescriptor>>(Severity.ERROR)
@JvmField
val INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY = DiagnosticFactory0.create<KtElement>(Severity.ERROR)
@JvmField
val INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL = DiagnosticFactory0.create<KtElement>(Severity.ERROR)
init { init {
Errors.Initializer.initializeFactoryNames(ErrorsNative::class.java) Errors.Initializer.initializeFactoryNames(ErrorsNative::class.java)
@@ -0,0 +1,49 @@
/*
* Copyright 2010-2020 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.resolve.konan.diagnostics
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory0
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.checkers.DeclarationChecker
import org.jetbrains.kotlin.resolve.checkers.DeclarationCheckerContext
import org.jetbrains.kotlin.resolve.hasBackingField
object NativeSharedImmutableChecker : DeclarationChecker {
private val sharedImmutableFqName = FqName("kotlin.native.concurrent.SharedImmutable")
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
check(declaration, descriptor, context, ErrorsNative.INAPPLICABLE_SHARED_IMMUTABLE_PROPERTY) {
val isVariable = descriptor is VariableDescriptor && descriptor.isVar
val hasBackingField = descriptor is PropertyDescriptor && descriptor.hasBackingField(context.trace.bindingContext)
val hasDelegate = declaration is KtProperty && declaration.delegate != null
!isVariable && hasBackingField || hasDelegate
}
check(declaration, descriptor, context, ErrorsNative.INAPPLICABLE_SHARED_IMMUTABLE_TOP_LEVEL) {
DescriptorUtils.isTopLevelDeclaration(descriptor)
}
}
fun check(
declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext,
error: DiagnosticFactory0<KtElement>, successCondition: (DeclarationDescriptor) -> Boolean
) {
if (successCondition(descriptor)) return
val sharedImmutableAnnotation = descriptor.annotations.findAnnotation(sharedImmutableFqName)
sharedImmutableAnnotation?.let {
val reportLocation = DescriptorToSourceUtils.getSourceFromAnnotation(it) ?: declaration
context.trace.report(error.on(reportLocation))
}
}
}
@@ -14,11 +14,12 @@ import org.jetbrains.kotlin.resolve.*
import org.jetbrains.kotlin.resolve.checkers.ExpectedActualDeclarationChecker import org.jetbrains.kotlin.resolve.checkers.ExpectedActualDeclarationChecker
import org.jetbrains.kotlin.resolve.inline.ReasonableInlineRule import org.jetbrains.kotlin.resolve.inline.ReasonableInlineRule
import org.jetbrains.kotlin.resolve.jvm.checkers.SuperCallWithDefaultArgumentsChecker import org.jetbrains.kotlin.resolve.jvm.checkers.SuperCallWithDefaultArgumentsChecker
import org.jetbrains.kotlin.resolve.konan.diagnostics.NativeSharedImmutableChecker
import org.jetbrains.kotlin.resolve.konan.diagnostics.NativeThrowsChecker import org.jetbrains.kotlin.resolve.konan.diagnostics.NativeThrowsChecker
object NativePlatformConfigurator : PlatformConfiguratorBase( object NativePlatformConfigurator : PlatformConfiguratorBase(
additionalCallCheckers = listOf(SuperCallWithDefaultArgumentsChecker()), additionalCallCheckers = listOf(SuperCallWithDefaultArgumentsChecker()),
additionalDeclarationCheckers = listOf(NativeThrowsChecker) additionalDeclarationCheckers = listOf(NativeThrowsChecker, NativeSharedImmutableChecker)
) { ) {
override fun configureModuleComponents(container: StorageComponentContainer) { override fun configureModuleComponents(container: StorageComponentContainer) {
container.useInstance(NativeInliningRule) container.useInstance(NativeInliningRule)