From 02ea1a4d64bae16f101d03296795c1ed82c55cda Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 21 Dec 2016 11:43:51 +0300 Subject: [PATCH] JS: split error diagnostic about body of external declaration into three different diagnostics (additional ones: wrong initializer, wrong default value of parameter). --- .../testsWithJsStdLib/native/body.kt | 4 +-- .../diagnostics/DefaultErrorMessagesJs.kt | 5 +++- .../js/resolve/diagnostics/ErrorsJs.java | 4 ++- .../resolve/diagnostics/JsExternalChecker.kt | 29 ++++++++----------- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/native/body.kt b/compiler/testData/diagnostics/testsWithJsStdLib/native/body.kt index c8008fc6734..69856c4a6bc 100644 --- a/compiler/testData/diagnostics/testsWithJsStdLib/native/body.kt +++ b/compiler/testData/diagnostics/testsWithJsStdLib/native/body.kt @@ -8,7 +8,7 @@ external fun baz(): Int = 23 external fun f(x: Int, y: String = noImpl): Unit -external fun g(x: Int, y: String = ""): Unit +external fun g(x: Int, y: String = ""): Unit external var a: Int get() = noImpl @@ -21,7 +21,7 @@ external val b: Int external val c: Int = noImpl -external val d: Int = 23 +external val d: Int = 23 external class C { fun foo(): Int = noImpl diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/DefaultErrorMessagesJs.kt b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/DefaultErrorMessagesJs.kt index dc094112395..5796ee8de78 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/DefaultErrorMessagesJs.kt +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/DefaultErrorMessagesJs.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. + * 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. @@ -76,6 +76,9 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy { put(ErrorsJs.NON_ABSTRACT_MEMBER_OF_EXTERNAL_INTERFACE, "Only nullable properties of external interfaces are allowed to be non-abstract") put(ErrorsJs.WRONG_BODY_OF_EXTERNAL_DECLARATION, "Wrong body of external declaration. Must be either ' = noImpl' or { noImpl }") + put(ErrorsJs.WRONG_INITIALIZER_OF_EXTERNAL_DECLARATION, "Wrong initializer of external declaration. Must be ' = noImpl'") + put(ErrorsJs.WRONG_DEFAULT_VALUE_FOR_EXTERNAL_FUN_PARAMETER, + "Wrong default value for parameter of external function. Must be ' = noImpl'") this } diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/ErrorsJs.java b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/ErrorsJs.java index 5f395b01e4c..43c1d57818b 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/ErrorsJs.java +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/ErrorsJs.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. + * 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. @@ -81,6 +81,8 @@ public interface ErrorsJs { DiagnosticFactory0 NON_ABSTRACT_MEMBER_OF_EXTERNAL_INTERFACE = DiagnosticFactory0.create( ERROR, PositioningStrategies.DECLARATION_SIGNATURE_OR_DEFAULT); DiagnosticFactory0 WRONG_BODY_OF_EXTERNAL_DECLARATION = DiagnosticFactory0.create(ERROR); + DiagnosticFactory0 WRONG_INITIALIZER_OF_EXTERNAL_DECLARATION = DiagnosticFactory0.create(ERROR); + DiagnosticFactory0 WRONG_DEFAULT_VALUE_FOR_EXTERNAL_FUN_PARAMETER = DiagnosticFactory0.create(ERROR); @SuppressWarnings("UnusedDeclaration") Object _initializer = new Object() { diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsExternalChecker.kt b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsExternalChecker.kt index 6e9f0a407fe..9b94fc81df8 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsExternalChecker.kt +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsExternalChecker.kt @@ -90,35 +90,30 @@ object JsExternalChecker : SimpleDeclarationChecker { diagnosticHolder.report(ErrorsJs.NON_ABSTRACT_MEMBER_OF_EXTERNAL_INTERFACE.on(declaration)) } - checkBody(declaration, diagnosticHolder, bindingContext) + checkBody(declaration, descriptor, diagnosticHolder, bindingContext) } - private fun checkBody(declaration: KtDeclaration, diagnosticHolder: DiagnosticSink, bindingContext: BindingContext) { + private fun checkBody( + declaration: KtDeclaration, descriptor: DeclarationDescriptor, + diagnosticHolder: DiagnosticSink, bindingContext: BindingContext + ) { + if (declaration is KtProperty && descriptor is PropertyAccessorDescriptor) return + if (declaration is KtDeclarationWithBody && !declaration.hasValidExternalBody(bindingContext)) { - reportWrongBody(declaration.bodyExpression!!, diagnosticHolder, bindingContext) + diagnosticHolder.report(ErrorsJs.WRONG_BODY_OF_EXTERNAL_DECLARATION.on(declaration.bodyExpression!!)) } else if (declaration is KtDeclarationWithInitializer && declaration.initializer?.isNoImplExpression(bindingContext) == false) { - reportWrongBody(declaration.initializer!!, diagnosticHolder, bindingContext) + diagnosticHolder.report(ErrorsJs.WRONG_INITIALIZER_OF_EXTERNAL_DECLARATION.on(declaration.initializer!!)) } if (declaration is KtCallableDeclaration) { for (defaultValue in declaration.valueParameters.mapNotNull { it.defaultValue }) { - checkExternalExpression(defaultValue, diagnosticHolder, bindingContext) + if (!defaultValue.isNoImplExpression(bindingContext)) { + diagnosticHolder.report(ErrorsJs.WRONG_DEFAULT_VALUE_FOR_EXTERNAL_FUN_PARAMETER.on(defaultValue)) + } } } } - private fun checkExternalExpression(expression: KtExpression, diagnosticHolder: DiagnosticSink, bindingContext: BindingContext) { - if (!expression.isNoImplExpression(bindingContext)) { - reportWrongBody(expression, diagnosticHolder, bindingContext) - } - } - - private fun reportWrongBody(expression: KtExpression, diagnosticHolder: DiagnosticSink, bindingContext: BindingContext) { - if (bindingContext.diagnostics.forElement(expression).none { it.factory == ErrorsJs.WRONG_BODY_OF_EXTERNAL_DECLARATION }) { - diagnosticHolder.report(ErrorsJs.WRONG_BODY_OF_EXTERNAL_DECLARATION.on(expression)) - } - } - private fun isDirectlyExternal(declaration: KtDeclaration, descriptor: DeclarationDescriptor): Boolean { if (declaration is KtProperty && descriptor is PropertyAccessorDescriptor) return false