diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/FirDefaultErrorMessages.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/FirDefaultErrorMessages.kt index 624296c6e19..0cbe7945d13 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/FirDefaultErrorMessages.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/FirDefaultErrorMessages.kt @@ -5,12 +5,10 @@ package org.jetbrains.kotlin.fir.analysis.diagnostics -import org.jetbrains.kotlin.diagnostics.Errors -import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages import org.jetbrains.kotlin.diagnostics.rendering.LanguageFeatureMessageRenderer -import org.jetbrains.kotlin.diagnostics.rendering.Renderers.RENDER_POSITION_VARIANCE -import org.jetbrains.kotlin.diagnostics.rendering.Renderers.STRING -import org.jetbrains.kotlin.diagnostics.rendering.Renderers.commaSeparated +import org.jetbrains.kotlin.diagnostics.rendering.CommonRenderers.RENDER_POSITION_VARIANCE +import org.jetbrains.kotlin.diagnostics.rendering.CommonRenderers.STRING +import org.jetbrains.kotlin.diagnostics.rendering.CommonRenderers.commaSeparated import org.jetbrains.kotlin.fir.analysis.diagnostics.FirDiagnosticRenderers.AMBIGUOUS_CALLS import org.jetbrains.kotlin.fir.analysis.diagnostics.FirDiagnosticRenderers.COLLECTION import org.jetbrains.kotlin.fir.analysis.diagnostics.FirDiagnosticRenderers.DECLARATION_NAME diff --git a/compiler/frontend.common-psi/src/org/jetbrains/kotlin/diagnostics/rendering/CommonRenderers.kt b/compiler/frontend.common-psi/src/org/jetbrains/kotlin/diagnostics/rendering/CommonRenderers.kt new file mode 100644 index 00000000000..7332e95c8a6 --- /dev/null +++ b/compiler/frontend.common-psi/src/org/jetbrains/kotlin/diagnostics/rendering/CommonRenderers.kt @@ -0,0 +1,49 @@ +/* + * 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.diagnostics.rendering + +import com.intellij.openapi.util.text.StringUtil +import org.jetbrains.kotlin.types.Variance +import java.io.PrintWriter +import java.io.StringWriter + +object CommonRenderers { + @JvmField + val EMPTY = Renderer { "" } + + @JvmField + val STRING = Renderer { it } + + @JvmField + val THROWABLE = Renderer { + val writer = StringWriter() + it.printStackTrace(PrintWriter(writer)) + StringUtil.first(writer.toString(), 2048, true) + } + + @JvmField + val RENDER_POSITION_VARIANCE = Renderer { variance: Variance -> + when (variance) { + Variance.INVARIANT -> "invariant" + Variance.IN_VARIANCE -> "in" + Variance.OUT_VARIANCE -> "out" + } + } + + @JvmStatic + fun commaSeparated(itemRenderer: DiagnosticParameterRenderer) = ContextDependentRenderer> { collection, context -> + buildString { + val iterator = collection.iterator() + while (iterator.hasNext()) { + val next = iterator.next() + append(itemRenderer.render(next, context)) + if (iterator.hasNext()) { + append(", ") + } + } + } + } +} diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/LanguageFeatureMessageRenderer.kt b/compiler/frontend.common-psi/src/org/jetbrains/kotlin/diagnostics/rendering/LanguageFeatureMessageRenderer.kt similarity index 76% rename from compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/LanguageFeatureMessageRenderer.kt rename to compiler/frontend.common-psi/src/org/jetbrains/kotlin/diagnostics/rendering/LanguageFeatureMessageRenderer.kt index 285467ef842..dade8820be0 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/LanguageFeatureMessageRenderer.kt +++ b/compiler/frontend.common-psi/src/org/jetbrains/kotlin/diagnostics/rendering/LanguageFeatureMessageRenderer.kt @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 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. + * 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.diagnostics.rendering @@ -65,4 +54,4 @@ class LanguageFeatureMessageRenderer @JvmOverloads constructor( return sb.toString() } -} \ No newline at end of file +} diff --git a/compiler/frontend.common/src/org/jetbrains/kotlin/diagnostics/rendering/DiagnosticParameterRenderer.kt b/compiler/frontend.common/src/org/jetbrains/kotlin/diagnostics/rendering/DiagnosticParameterRenderer.kt index 8c61c2910ab..044587bbf36 100644 --- a/compiler/frontend.common/src/org/jetbrains/kotlin/diagnostics/rendering/DiagnosticParameterRenderer.kt +++ b/compiler/frontend.common/src/org/jetbrains/kotlin/diagnostics/rendering/DiagnosticParameterRenderer.kt @@ -32,4 +32,7 @@ fun Renderer(block: (O) -> String) = object : ContextIndependentParameterRen fun ContextDependentRenderer(block: (O, RenderingContext) -> String) = object : DiagnosticParameterRenderer { override fun render(obj: O, renderingContext: RenderingContext): String = block(obj, renderingContext) -} \ No newline at end of file +} + +fun

renderParameter(parameter: P, renderer: DiagnosticParameterRenderer

?, context: RenderingContext): Any? = + renderer?.render(parameter, context) ?: parameter 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 7e087b6b68a..a60c8af155e 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 @@ -15,6 +15,7 @@ import java.util.List; import static kotlin.collections.CollectionsKt.*; import static org.jetbrains.kotlin.diagnostics.rendering.Renderers.*; +import static org.jetbrains.kotlin.diagnostics.rendering.CommonRenderers.*; import static org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm.*; public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java index 6081599bacd..837a9791d7f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -28,6 +28,7 @@ import java.lang.reflect.Modifier; import java.util.*; import static org.jetbrains.kotlin.diagnostics.Errors.*; +import static org.jetbrains.kotlin.diagnostics.rendering.CommonRenderers.*; import static org.jetbrains.kotlin.diagnostics.rendering.Renderers.*; import static org.jetbrains.kotlin.diagnostics.rendering.RenderingContext.of; diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DiagnosticRendererUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DiagnosticRendererUtil.kt index d8817d254fa..dfe697018fe 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DiagnosticRendererUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DiagnosticRendererUtil.kt @@ -20,9 +20,6 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters import org.jetbrains.kotlin.renderer.DescriptorRenderer -fun

renderParameter(parameter: P, renderer: DiagnosticParameterRenderer

?, context: RenderingContext): Any? = - renderer?.render(parameter, context) ?: parameter - fun ClassifierDescriptorWithTypeParameters.renderKindWithName(): String = DescriptorRenderer.getClassifierKindPrefix(this) + " '" + name + "'" diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/Renderers.kt b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/Renderers.kt index b9ceabc6b7b..b8d1cca9087 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/Renderers.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/Renderers.kt @@ -66,19 +66,6 @@ object Renderers { element.toString() } - @JvmField - val EMPTY = Renderer { "" } - - @JvmField - val STRING = Renderer { it } - - @JvmField - val THROWABLE = Renderer { - val writer = StringWriter() - it.printStackTrace(PrintWriter(writer)) - StringUtil.first(writer.toString(), 2048, true) - } - @JvmField val NAME = Renderer { it.name.asString() } @@ -180,15 +167,6 @@ object Renderers { } } - @JvmField - val RENDER_POSITION_VARIANCE = Renderer { variance: Variance -> - when (variance) { - Variance.INVARIANT -> "invariant" - Variance.IN_VARIANCE -> "in" - Variance.OUT_VARIANCE -> "out" - } - } - @JvmField val AMBIGUOUS_CALLS = Renderer { calls: Collection> -> val descriptors = calls.map { it.resultingDescriptor } @@ -214,20 +192,6 @@ object Renderers { } } - @JvmStatic - fun commaSeparated(itemRenderer: DiagnosticParameterRenderer) = ContextDependentRenderer> { collection, context -> - buildString { - val iterator = collection.iterator() - while (iterator.hasNext()) { - val next = iterator.next() - append(itemRenderer.render(next, context)) - if (iterator.hasNext()) { - append(", ") - } - } - } - } - @JvmField val TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS_RENDERER = Renderer { renderConflictingSubstitutionsInferenceError(it, TabledDescriptorRenderer.create()).toString() 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 5050d058ab9..341c47611c5 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 @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.js.resolve.diagnostics +import org.jetbrains.kotlin.diagnostics.rendering.CommonRenderers.STRING import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages import org.jetbrains.kotlin.diagnostics.rendering.DiagnosticFactoryToRendererMap import org.jetbrains.kotlin.diagnostics.rendering.Renderers @@ -15,27 +16,27 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy { put(ErrorsJs.NATIVE_ANNOTATIONS_ALLOWED_ONLY_ON_MEMBER_OR_EXTENSION_FUN, "Annotation ''{0}'' is allowed only on member functions of declaration annotated as ''kotlin.js.native'' or on toplevel extension functions", RENDER_TYPE) - put(ErrorsJs.NATIVE_INDEXER_KEY_SHOULD_BE_STRING_OR_NUMBER, "Native {0}''s first parameter type should be ''kotlin.String'' or subtype of ''kotlin.Number''", Renderers.STRING) - put(ErrorsJs.NATIVE_INDEXER_CAN_NOT_HAVE_DEFAULT_ARGUMENTS, "Native {0}''s parameter can not have default value", Renderers.STRING) + put(ErrorsJs.NATIVE_INDEXER_KEY_SHOULD_BE_STRING_OR_NUMBER, "Native {0}''s first parameter type should be ''kotlin.String'' or subtype of ''kotlin.Number''", STRING) + put(ErrorsJs.NATIVE_INDEXER_CAN_NOT_HAVE_DEFAULT_ARGUMENTS, "Native {0}''s parameter can not have default value", STRING) put(ErrorsJs.NATIVE_GETTER_RETURN_TYPE_SHOULD_BE_NULLABLE, "Native getter's return type should be nullable") put(ErrorsJs.NATIVE_SETTER_WRONG_RETURN_TYPE, "Native setter's return type should be 'Unit' or a supertype of the second parameter's type") - put(ErrorsJs.NATIVE_INDEXER_WRONG_PARAMETER_COUNT, "Expected {0} parameters for native {1}", Renderers.TO_STRING, Renderers.STRING) + put(ErrorsJs.NATIVE_INDEXER_WRONG_PARAMETER_COUNT, "Expected {0} parameters for native {1}", Renderers.TO_STRING, STRING) put(ErrorsJs.JSCODE_ERROR, "JavaScript: {0}", JsCallDataTextRenderer) put(ErrorsJs.JSCODE_WARNING, "JavaScript: {0}", JsCallDataTextRenderer) put(ErrorsJs.JSCODE_ARGUMENT_SHOULD_BE_CONSTANT, "Argument must be string constant") put(ErrorsJs.NOT_SUPPORTED, "Cannot translate (not supported yet): ''{0}''", RenderFirstLineOfElementText) put(ErrorsJs.JSCODE_NO_JAVASCRIPT_PRODUCED, "Argument must be non-empty JavaScript code") put(ErrorsJs.NESTED_EXTERNAL_DECLARATION, "Non top-level `external` declaration") - put(ErrorsJs.WRONG_EXTERNAL_DECLARATION, "Declaration of such kind ({0}) can''t be external", Renderers.STRING) + put(ErrorsJs.WRONG_EXTERNAL_DECLARATION, "Declaration of such kind ({0}) can''t be external", STRING) put(ErrorsJs.EXTENSION_FUNCTION_IN_EXTERNAL_DECLARATION, "Function types with receiver are prohibited in external declarations") put(ErrorsJs.INLINE_CLASS_IN_EXTERNAL_DECLARATION, "Using inline classes as parameter type or return type of external declarations is not supported") put(ErrorsJs.JS_NAME_CLASH, "JavaScript name ({0}) generated for this declaration clashes with another declaration: {1}", - Renderers.STRING, Renderers.COMPACT) + STRING, Renderers.COMPACT) put(ErrorsJs.JS_FAKE_NAME_CLASH, "JavaScript name {0} is generated for different inherited members: {1} and {2}", - Renderers.STRING, Renderers.COMPACT, Renderers.COMPACT) + STRING, Renderers.COMPACT, Renderers.COMPACT) put(ErrorsJs.JS_BUILTIN_NAME_CLASH, "JavaScript name generated for this declaration clashes with built-in declaration {1}", - Renderers.STRING) + STRING) put(ErrorsJs.JS_NAME_ON_PRIMARY_CONSTRUCTOR_PROHIBITED, "@JsName annotation is prohibited for primary constructors") put(ErrorsJs.JS_NAME_ON_ACCESSOR_AND_PROPERTY, "@JsName can be either on a property or its accessors, not both of them") put(ErrorsJs.JS_NAME_IS_NOT_ON_ALL_ACCESSORS, "@JsName should be on all of the property accessors") @@ -67,7 +68,7 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy { put(ErrorsJs.EXTERNAL_INTERFACE_AS_CLASS_LITERAL, "Can't refer to external interface from class literal") put(ErrorsJs.EXTERNAL_TYPE_EXTENDS_NON_EXTERNAL_TYPE, "External type extends non-external type") - put(ErrorsJs.WRONG_OPERATION_WITH_DYNAMIC, "Wrong operation with dynamic value: {0}", Renderers.STRING) + put(ErrorsJs.WRONG_OPERATION_WITH_DYNAMIC, "Wrong operation with dynamic value: {0}", STRING) put(ErrorsJs.SPREAD_OPERATOR_IN_DYNAMIC_CALL, "Can't apply spread operator in dynamic call") put(ErrorsJs.DELEGATION_BY_DYNAMIC, "Can't delegate to dynamic value") put(ErrorsJs.PROPERTY_DELEGATION_BY_DYNAMIC, "Can't apply property delegation by dynamic handler") @@ -101,8 +102,8 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy { Renderers.DECLARATION_NAME_WITH_KIND) put(ErrorsJs.NESTED_JS_EXPORT, "@JsExport is only allowed on files and top-level declarations") - put(ErrorsJs.WRONG_EXPORTED_DECLARATION, "Declaration of such kind ({0}) can''t be exported to JS", Renderers.STRING) - put(ErrorsJs.NON_EXPORTABLE_TYPE, "Exported declaration uses non-exportable {0} type: {1}", Renderers.STRING, RENDER_TYPE) + put(ErrorsJs.WRONG_EXPORTED_DECLARATION, "Declaration of such kind ({0}) can''t be exported to JS", STRING) + put(ErrorsJs.NON_EXPORTABLE_TYPE, "Exported declaration uses non-exportable {0} type: {1}", STRING, RENDER_TYPE) this } diff --git a/native/frontend/src/org/jetbrains/kotlin/resolve/konan/diagnostics/DefaultErrorMessagesNative.kt b/native/frontend/src/org/jetbrains/kotlin/resolve/konan/diagnostics/DefaultErrorMessagesNative.kt index 96b1f350ca2..8ea3e6f6cbc 100644 --- a/native/frontend/src/org/jetbrains/kotlin/resolve/konan/diagnostics/DefaultErrorMessagesNative.kt +++ b/native/frontend/src/org/jetbrains/kotlin/resolve/konan/diagnostics/DefaultErrorMessagesNative.kt @@ -5,10 +5,7 @@ package org.jetbrains.kotlin.resolve.konan.diagnostics -import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages -import org.jetbrains.kotlin.diagnostics.rendering.DiagnosticFactoryToRendererMap -import org.jetbrains.kotlin.diagnostics.rendering.Renderer -import org.jetbrains.kotlin.diagnostics.rendering.Renderers +import org.jetbrains.kotlin.diagnostics.rendering.* private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy { DiagnosticFactoryToRendererMap("Native").apply { @@ -19,7 +16,7 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy { ) put( ErrorsNative.INCOMPATIBLE_THROWS_INHERITED, "Member inherits different @Throws filters from {0}", - Renderers.commaSeparated(Renderers.NAME) + CommonRenderers.commaSeparated(Renderers.NAME) ) put( ErrorsNative.MISSING_EXCEPTION_IN_THROWS_ON_SUSPEND, @@ -41,7 +38,7 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy { "@ThreadLocal is applicable only to property with backing field, to property with delegation or to objects" ) put(ErrorsNative.INAPPLICABLE_THREAD_LOCAL_TOP_LEVEL, "@ThreadLocal is applicable only to top level declarations") - put(ErrorsNative.INVALID_CHARACTERS_NATIVE, "Name {0}", Renderers.STRING); + put(ErrorsNative.INVALID_CHARACTERS_NATIVE, "Name {0}", CommonRenderers.STRING); } } diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/diagnostic/SerializationPluginErrorsRendering.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/diagnostic/SerializationPluginErrorsRendering.kt index b090a0843bc..e96aea16016 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/diagnostic/SerializationPluginErrorsRendering.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/diagnostic/SerializationPluginErrorsRendering.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlinx.serialization.compiler.diagnostic +import org.jetbrains.kotlin.diagnostics.rendering.CommonRenderers import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages import org.jetbrains.kotlin.diagnostics.rendering.DiagnosticFactoryToRendererMap import org.jetbrains.kotlin.diagnostics.rendering.Renderers @@ -17,8 +18,8 @@ object SerializationPluginErrorsRendering : DefaultErrorMessages.Extension { MAP.put( SerializationErrors.INLINE_CLASSES_NOT_SUPPORTED, "Inline classes require runtime serialization library version at least {0}, while your classpath has {1}.", - Renderers.STRING, - Renderers.STRING, + CommonRenderers.STRING, + CommonRenderers.STRING, ) MAP.put( SerializationErrors.PLUGIN_IS_NOT_ENABLED, @@ -53,7 +54,7 @@ object SerializationPluginErrorsRendering : DefaultErrorMessages.Extension { MAP.put( SerializationErrors.DUPLICATE_SERIAL_NAME, "Serializable class has duplicate serial name of property ''{0}'', either in the class itself or its supertypes", - Renderers.STRING + CommonRenderers.STRING ) MAP.put( SerializationErrors.SERIALIZER_NOT_FOUND, @@ -103,18 +104,18 @@ object SerializationPluginErrorsRendering : DefaultErrorMessages.Extension { SerializationErrors.REQUIRED_KOTLIN_TOO_HIGH, "Your current Kotlin version is {0}, while kotlinx.serialization core runtime {1} requires at least Kotlin {2}. " + "Please update your Kotlin compiler and IDE plugin.", - Renderers.STRING, - Renderers.STRING, - Renderers.STRING + CommonRenderers.STRING, + CommonRenderers.STRING, + CommonRenderers.STRING ) MAP.put( SerializationErrors.PROVIDED_RUNTIME_TOO_LOW, "Your current kotlinx.serialization core version is {0}, while current Kotlin compiler plugin {1} requires at least {2}. " + "Please update your kotlinx.serialization runtime dependency.", - Renderers.STRING, - Renderers.STRING, - Renderers.STRING + CommonRenderers.STRING, + CommonRenderers.STRING, + CommonRenderers.STRING ) MAP.put(