diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt index d56f5fc657c..7efdb25c84e 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt @@ -357,6 +357,13 @@ abstract class CommonCompilerArguments : CommonToolArguments() { ) var suppressVersionWarnings: Boolean by FreezableVar(false) + @Argument( + value = "-Xextended-compiler-checks", + description = "Enable additional compiler checks that might provide verbose diagnostic information for certain errors.\n" + + "Warning: this mode is not backward-compatible and might cause compilation errors in previously compiled code." + ) + var extendedCompilerChecks: Boolean by FreezableVar(false) + open fun configureAnalysisFlags(collector: MessageCollector): MutableMap, Any> { return HashMap, Any>().apply { put(AnalysisFlags.skipMetadataVersionCheck, skipMetadataVersionCheck) @@ -424,6 +431,10 @@ abstract class CommonCompilerArguments : CommonToolArguments() { put(LanguageFeature.InferenceCompatibility, LanguageFeature.State.ENABLED) } + if (extendedCompilerChecks) { + put(LanguageFeature.ExtendedCompilerChecks, LanguageFeature.State.ENABLED) + } + if (progressiveMode) { LanguageFeature.values().filter { it.kind.enabledInProgressiveMode }.forEach { // Don't overwrite other settings: users may want to turn off some particular diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/AnalyzerWithCompilerReport.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/AnalyzerWithCompilerReport.kt index d5e0db6b644..730a5558bd7 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/AnalyzerWithCompilerReport.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/AnalyzerWithCompilerReport.kt @@ -26,6 +26,7 @@ import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.* import org.jetbrains.kotlin.codegen.state.IncompatibleClassTrackerImpl import org.jetbrains.kotlin.config.CompilerConfiguration +import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.config.LanguageVersionSettings import org.jetbrains.kotlin.config.languageVersionSettings import org.jetbrains.kotlin.diagnostics.* @@ -72,6 +73,9 @@ class AnalyzerWithCompilerReport( .append(", unresolved supertypes: ").append(unresolved!!.joinToString()) .append("\n") } + if (!languageVersionSettings.supportsFeature(LanguageFeature.ExtendedCompilerChecks)) { + message.append("Adding -Xextended-compiler-checks argument might provide additional information.\n") + } messageCollector.report(ERROR, message.toString()) } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/MissingDependencySupertypeChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/MissingDependencySupertypeChecker.kt index cd5c71e2d89..7ea1aa45dcd 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/MissingDependencySupertypeChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/MissingDependencySupertypeChecker.kt @@ -6,6 +6,7 @@ package org.jetbrains.kotlin.resolve.checkers import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.diagnostics.Errors import org.jetbrains.kotlin.psi.KtDeclaration @@ -48,7 +49,11 @@ object MissingDependencySupertypeChecker { descriptor.dispatchReceiverParameter?.declaration, reportOn, context.trace, context.missingSupertypesResolver ) - if (descriptor !is ConstructorDescriptor && descriptor !is FakeCallableDescriptorForObject && !errorReported) { + + val eagerChecksAllowed = context.languageVersionSettings.supportsFeature(LanguageFeature.ExtendedCompilerChecks) + val unresolvedLazySupertypesByDefault = descriptor is ConstructorDescriptor || descriptor is FakeCallableDescriptorForObject + + if (eagerChecksAllowed || !unresolvedLazySupertypesByDefault && !errorReported) { // The constructed class' own supertypes are not resolved after constructor call, // so its containing declaration should not be checked. // Dispatch receiver is checked before for case of inner class constructor call. diff --git a/compiler/testData/cli/js/jsExtraHelp.out b/compiler/testData/cli/js/jsExtraHelp.out index 766636dd930..b6134d27332 100644 --- a/compiler/testData/cli/js/jsExtraHelp.out +++ b/compiler/testData/cli/js/jsExtraHelp.out @@ -44,6 +44,8 @@ where advanced options include: -Xexplicit-api={strict|warning|disable} Force compiler to report errors on all public API declarations without explicit visibility or return type. Use 'warning' level to issue warnings instead of errors. + -Xextended-compiler-checks Enable additional compiler checks that might provide verbose diagnostic information for certain errors. + Warning: this mode is not backward-compatible and might cause compilation errors in previously compiled code. -Xinference-compatibility Enable compatibility changes for generic type inference algorithm -Xinline-classes Enable experimental inline classes -Xintellij-plugin-root= Path to the kotlin-compiler.jar or directory where IntelliJ configuration files can be found diff --git a/compiler/testData/cli/jvm/extraHelp.out b/compiler/testData/cli/jvm/extraHelp.out index 8c5061724e1..374d9ca8499 100644 --- a/compiler/testData/cli/jvm/extraHelp.out +++ b/compiler/testData/cli/jvm/extraHelp.out @@ -152,6 +152,8 @@ where advanced options include: -Xexplicit-api={strict|warning|disable} Force compiler to report errors on all public API declarations without explicit visibility or return type. Use 'warning' level to issue warnings instead of errors. + -Xextended-compiler-checks Enable additional compiler checks that might provide verbose diagnostic information for certain errors. + Warning: this mode is not backward-compatible and might cause compilation errors in previously compiled code. -Xinference-compatibility Enable compatibility changes for generic type inference algorithm -Xinline-classes Enable experimental inline classes -Xintellij-plugin-root= Path to the kotlin-compiler.jar or directory where IntelliJ configuration files can be found diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyErrorPositions/output.txt b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyErrorPositions/output.txt index 98c12b14fef..ea5ffd56f74 100644 --- a/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyErrorPositions/output.txt +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyErrorPositions/output.txt @@ -1,5 +1,6 @@ error: supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath: class test.Sub, unresolved supertypes: test.Super +Adding -Xextended-compiler-checks argument might provide additional information. compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyErrorPositions/source.kt:3:1: error: cannot access 'test.Super' which is a supertype of 'SubSub'. Check your module classpath for missing or conflicting dependencies class SubSub : Sub() diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyInJava/output.txt b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyInJava/output.txt index c92ccf9dfb0..5c1236ca777 100644 --- a/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyInJava/output.txt +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyInJava/output.txt @@ -1,5 +1,6 @@ error: supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath: class test.Sub, unresolved supertypes: test.Super +Adding -Xextended-compiler-checks argument might provide additional information. compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyInJava/source.kt:3:1: error: cannot access 'test.Super' which is a supertype of 'SubSub'. Check your module classpath for missing or conflicting dependencies class SubSub : Sub() diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyInKotlin/output.txt b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyInKotlin/output.txt index faeca3c57dc..8171f0c3fed 100644 --- a/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyInKotlin/output.txt +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyInKotlin/output.txt @@ -1,5 +1,6 @@ error: supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath: class test.Sub, unresolved supertypes: test.Super +Adding -Xextended-compiler-checks argument might provide additional information. compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyInKotlin/source.kt:3:1: error: cannot access 'test.Super' which is a supertype of 'SubSub'. Check your module classpath for missing or conflicting dependencies class SubSub : Sub() diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyMissingInterface/output.txt b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyMissingInterface/output.txt index 71b5795f63e..29fad2bbc02 100644 --- a/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyMissingInterface/output.txt +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyMissingInterface/output.txt @@ -1,5 +1,6 @@ error: supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath: class test.B, unresolved supertypes: test.A +Adding -Xextended-compiler-checks argument might provide additional information. compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyMissingInterface/source.kt:5:15: error: cannot access 'test.A' which is a supertype of 'test.B'. Check your module classpath for missing or conflicting dependencies D.m(B.n()) diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyOfEnclosingClass/output.txt b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyOfEnclosingClass/output.txt index 0c1c50f7b34..c6a2ae58a40 100644 --- a/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyOfEnclosingClass/output.txt +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyOfEnclosingClass/output.txt @@ -1,6 +1,7 @@ error: supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath: class test.SubClass, unresolved supertypes: test.Super class test.SubObject, unresolved supertypes: test.Super +Adding -Xextended-compiler-checks argument might provide additional information. compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyOfEnclosingClass/source.kt:6:16: error: cannot access 'test.Super' which is a supertype of 'test.SubClass'. Check your module classpath for missing or conflicting dependencies SubClass().Inner() // Error - dispatch receiver misses supertype diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/library/test/Sub.java b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/library/test/Sub.java new file mode 100644 index 00000000000..aa08b2fb540 --- /dev/null +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/library/test/Sub.java @@ -0,0 +1,5 @@ +package test; + +public class Sub extends Super { + +} \ No newline at end of file diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/library/test/SubKt.kt b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/library/test/SubKt.kt new file mode 100644 index 00000000000..e14b52fe04f --- /dev/null +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/library/test/SubKt.kt @@ -0,0 +1,11 @@ +package test + +class SubKt : Super() { + companion object { + fun companionMethod() = "OK" + } + + object InnerObject { + fun objectMethod() = "OK" + } +} \ No newline at end of file diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/library/test/Super.java b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/library/test/Super.java new file mode 100644 index 00000000000..56493e0dee6 --- /dev/null +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/library/test/Super.java @@ -0,0 +1,7 @@ +package test; + +public class Super { + String foo() { + return "Super"; + } +} \ No newline at end of file diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/output.txt b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/output.txt new file mode 100644 index 00000000000..2ff691d93aa --- /dev/null +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/output.txt @@ -0,0 +1,20 @@ +error: supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath: + class test.Sub, unresolved supertypes: test.Super + class test.SubKt, unresolved supertypes: test.Super + +compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/source.kt:12:32: error: cannot access 'test.Super' which is a supertype of 'Sub'. Check your module classpath for missing or conflicting dependencies +fun simpleFun(arg: Sub): Sub = Sub() + ^ +compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/source.kt:19:18: error: cannot access 'test.Super' which is a supertype of 'Sub'. Check your module classpath for missing or conflicting dependencies + val x: Sub = Sub() + ^ +compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/source.kt:21:18: error: cannot access 'test.Super' which is a supertype of 'Sub'. Check your module classpath for missing or conflicting dependencies + useCallRef(::Sub) + ^ +compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/source.kt:22:15: error: cannot access 'test.Super' which is a supertype of 'Sub'. Check your module classpath for missing or conflicting dependencies + simpleFun(Sub()) + ^ +compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/source.kt:23:20: error: cannot access 'test.Super' which is a supertype of 'Sub'. Check your module classpath for missing or conflicting dependencies + inlineFun(Sub()) + ^ +COMPILATION_ERROR diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/source.kt b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/source.kt new file mode 100644 index 00000000000..d68fac51dae --- /dev/null +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/incompleteHierarchyWithExtendedCompilerChecks/source.kt @@ -0,0 +1,26 @@ +import test.Sub as MySub +import test.SubKt + +typealias Sub = MySub + +class Test + +@Suppress("UNUSED_PARAMETER") +fun useCallRef(ref: Any?) {} + +@Suppress("UNUSED_PARAMETER") +fun simpleFun(arg: Sub): Sub = Sub() + +inline fun inlineFun(t: T) = t + +// In conservative mode without -Xextended-compiler-checks flag these usages don't cause compilation error +fun test() { + @Suppress("UNUSED_VARIABLE") + val x: Sub = Sub() + Test() + useCallRef(::Sub) + simpleFun(Sub()) + inlineFun(Sub()) + SubKt.companionMethod() + SubKt.InnerObject.objectMethod() +} diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.kt b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.kt index c36330550c9..8b4940a5851 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.kt @@ -229,6 +229,14 @@ class CompileKotlinAgainstCustomBinariesTest : AbstractKotlinCompilerIntegration doTestBrokenLibrary("library", "test/Super.class") } + fun testIncompleteHierarchyWithExtendedCompilerChecks() { + doTestBrokenLibrary( + "library", + "test/Super.class", + additionalOptions = listOf("-Xextended-compiler-checks"), + ) + } + fun testIncompleteHierarchyErrorPositions() { doTestBrokenLibrary("library", "test/Super.class") } diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 1fd27a05cc3..e7eff53f676 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -168,6 +168,8 @@ enum class LanguageFeature( // Looks like we can't enable it until KT-26245 is fixed because otherwise some use cases become broken because of overrides ProhibitUsingNullableTypeParameterAgainstNotNullAnnotated(sinceVersion = null, kind = BUG_FIX), + ExtendedCompilerChecks(sinceVersion = null), + // Experimental features Coroutines(