From f76efb68f29e466b4c31022a1039aa1be9e6c50c Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 11 Mar 2016 19:45:28 +0300 Subject: [PATCH] Strictfp on a class is now a warning #KT-11109 Fixed --- .../checkers/StrictfpApplicabilityChecker.kt | 43 +++++++++++++++++++ .../diagnostics/DefaultErrorMessagesJvm.java | 2 + .../resolve/jvm/diagnostics/ErrorsJvm.java | 2 + .../jvm/platform/JvmPlatformConfigurator.kt | 3 +- .../kotlin/resolve/AnnotationUtil.kt | 5 +++ .../annotations/strictfpOnClass.kt | 12 ++++-- .../annotations/strictfpOnClass.txt | 2 + .../jvm/annotations/JvmFlagAnnotations.kt | 2 +- 8 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/StrictfpApplicabilityChecker.kt diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/StrictfpApplicabilityChecker.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/StrictfpApplicabilityChecker.kt new file mode 100644 index 00000000000..bd125af0b38 --- /dev/null +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/StrictfpApplicabilityChecker.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.resolve.jvm.checkers + +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.diagnostics.DiagnosticSink +import org.jetbrains.kotlin.psi.KtClassOrObject +import org.jetbrains.kotlin.psi.KtDeclaration +import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.DeclarationChecker +import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils +import org.jetbrains.kotlin.resolve.annotations.findStrictfpAnnotation +import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm + +class StrictfpApplicabilityChecker : DeclarationChecker { + override fun check( + declaration: KtDeclaration, + descriptor: DeclarationDescriptor, + diagnosticHolder: DiagnosticSink, + bindingContext: BindingContext + ) { + val annotation = descriptor.findStrictfpAnnotation() ?: return + if (declaration is KtClassOrObject && descriptor is ClassDescriptor) { + val annotationEntry = DescriptorToSourceUtils.getSourceFromAnnotation(annotation) ?: return + diagnosticHolder.report(ErrorsJvm.STRICTFP_ON_CLASS.on(annotationEntry)) + } + } +} diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/DefaultErrorMessagesJvm.java b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/DefaultErrorMessagesJvm.java index dbfd19788f5..90912d7dea9 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/DefaultErrorMessagesJvm.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/DefaultErrorMessagesJvm.java @@ -100,6 +100,8 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension { MAP.put(ErrorsJvm.JVM_SYNTHETIC_ON_DELEGATE, "''@JvmSynthetic'' annotation cannot be used on delegated properties"); + MAP.put(ErrorsJvm.STRICTFP_ON_CLASS, "''@Strictfp'' annotation on classes is unsupported yet"); + MAP.put(ErrorsJvm.SUPER_CALL_WITH_DEFAULT_PARAMETERS, "Super-calls with default arguments are not allowed. Please specify all arguments of ''super.{0}'' explicitly", Renderers.TO_STRING); } diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/ErrorsJvm.java b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/ErrorsJvm.java index ce580f47872..d61e9bd45a5 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/ErrorsJvm.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/diagnostics/ErrorsJvm.java @@ -52,6 +52,8 @@ public interface ErrorsJvm { DiagnosticFactory0 JVM_SYNTHETIC_ON_DELEGATE = DiagnosticFactory0.create(ERROR); + DiagnosticFactory0 STRICTFP_ON_CLASS = DiagnosticFactory0.create(WARNING); + DiagnosticFactory0 VOLATILE_ON_VALUE = DiagnosticFactory0.create(ERROR); DiagnosticFactory0 VOLATILE_ON_DELEGATE = DiagnosticFactory0.create(ERROR); DiagnosticFactory0 SYNCHRONIZED_ON_ABSTRACT = DiagnosticFactory0.create(ERROR); diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt index 2f257178d66..6b78131bb89 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt @@ -41,7 +41,8 @@ object JvmPlatformConfigurator : PlatformConfigurator( OverloadsAnnotationChecker(), JvmFieldApplicabilityChecker(), TypeParameterBoundIsNotArrayChecker(), - JvmSyntheticApplicabilityChecker() + JvmSyntheticApplicabilityChecker(), + StrictfpApplicabilityChecker() ), additionalCallCheckers = listOf( diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUtil.kt index 14801917be2..7098b636f62 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUtil.kt @@ -39,6 +39,11 @@ fun DeclarationDescriptor.hasJvmSyntheticAnnotation() = findJvmSyntheticAnnotati fun DeclarationDescriptor.findJvmSyntheticAnnotation() = DescriptorUtils.getAnnotationByFqName(annotations, JVM_SYNTHETIC_ANNOTATION_FQ_NAME) +private val STRICTFP_ANNOTATION_FQ_NAME = FqName("kotlin.jvm.Strictfp") + +fun DeclarationDescriptor.findStrictfpAnnotation() = + DescriptorUtils.getAnnotationByFqName(annotations, STRICTFP_ANNOTATION_FQ_NAME) + fun CallableDescriptor.isPlatformStaticInObjectOrClass(): Boolean = isPlatformStaticIn { DescriptorUtils.isNonCompanionObject(it) || DescriptorUtils.isClassOrEnumClass(it) } diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/strictfpOnClass.kt b/compiler/testData/diagnostics/testsWithStdLib/annotations/strictfpOnClass.kt index 3424a976dc5..a6c7381b7de 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/strictfpOnClass.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/strictfpOnClass.kt @@ -1,11 +1,17 @@ -@kotlin.jvm.Strictfp class A { +@kotlin.jvm.Strictfp class A { } -@kotlin.jvm.Strictfp object B { +@kotlin.jvm.Strictfp object B { } -@kotlin.jvm.Strictfp interface C { +@kotlin.jvm.Strictfp interface C { +} + +fun foo() { + @kotlin.jvm.Strictfp class D + + @kotlin.jvm.Strictfp object: Any() {} } \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/strictfpOnClass.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/strictfpOnClass.txt index f80355a1167..495ea2cb7aa 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/strictfpOnClass.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/strictfpOnClass.txt @@ -1,5 +1,7 @@ package +public fun foo(): kotlin.Unit + @kotlin.jvm.Strictfp() public final class A { public constructor A() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean diff --git a/core/runtime.jvm/src/kotlin/jvm/annotations/JvmFlagAnnotations.kt b/core/runtime.jvm/src/kotlin/jvm/annotations/JvmFlagAnnotations.kt index 7ab39d9f8b4..fcd5c1bf32a 100644 --- a/core/runtime.jvm/src/kotlin/jvm/annotations/JvmFlagAnnotations.kt +++ b/core/runtime.jvm/src/kotlin/jvm/annotations/JvmFlagAnnotations.kt @@ -41,7 +41,7 @@ annotation class Transient * of floating point operations performed inside the method needs to be restricted in order to * achieve better portability. */ -@Target(FUNCTION, CONSTRUCTOR, PROPERTY_GETTER, PROPERTY_SETTER) +@Target(FUNCTION, CONSTRUCTOR, PROPERTY_GETTER, PROPERTY_SETTER, CLASS) @Retention(AnnotationRetention.SOURCE) @MustBeDocumented annotation class Strictfp