[NI] Disable SAM-conversions for Kotlin functions by default

#KT-30661 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2019-04-12 16:19:39 +03:00
parent c6f29cbf38
commit 705a8a2234
11 changed files with 76 additions and 20 deletions

View File

@@ -305,7 +305,6 @@ abstract class CommonCompilerArguments : CommonToolArguments() {
if (newInference) {
put(LanguageFeature.NewInference, LanguageFeature.State.ENABLED)
put(LanguageFeature.SamConversionForKotlinFunctions, LanguageFeature.State.ENABLED)
}
if (legacySmartCastAfterTry) {
@@ -343,22 +342,13 @@ abstract class CommonCompilerArguments : CommonToolArguments() {
private fun HashMap<LanguageFeature, LanguageFeature.State>.configureLanguageFeaturesFromInternalArgs(collector: MessageCollector) {
val featuresThatForcePreReleaseBinaries = mutableListOf<LanguageFeature>()
var samConversionsExplicitlyPassed = false
for ((feature, state) in internalArguments.filterIsInstance<ManualLanguageFeatureSetting>()) {
if (feature == LanguageFeature.SamConversionForKotlinFunctions) {
samConversionsExplicitlyPassed = true
}
put(feature, state)
if (state == LanguageFeature.State.ENABLED && feature.forcesPreReleaseBinariesIfEnabled()) {
featuresThatForcePreReleaseBinaries += feature
}
}
if (!samConversionsExplicitlyPassed && this[LanguageFeature.NewInference] == LanguageFeature.State.ENABLED) {
put(LanguageFeature.SamConversionForKotlinFunctions, LanguageFeature.State.ENABLED)
}
if (featuresThatForcePreReleaseBinaries.isNotEmpty()) {
collector.report(
CompilerMessageSeverity.STRONG_WARNING,

View File

@@ -17975,6 +17975,11 @@ public class FirDiagnosticsSmokeTestGenerated extends AbstractFirDiagnosticsSmok
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/samConversions"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("checkSamConversionsAreDisabledByDefault.kt")
public void testCheckSamConversionsAreDisabledByDefault() throws Exception {
runTest("compiler/testData/diagnostics/tests/samConversions/checkSamConversionsAreDisabledByDefault.kt");
}
@TestMetadata("DisabledForKTSimple.kt")
public void testDisabledForKTSimple() throws Exception {
runTest("compiler/testData/diagnostics/tests/samConversions/DisabledForKTSimple.kt");

View File

@@ -60,7 +60,8 @@ class SamAdapterFunctionsScope(
private val deprecationResolver: DeprecationResolver,
private val lookupTracker: LookupTracker
) : SyntheticScope.Default() {
private val samViaSyntheticScopeDisabled = languageVersionSettings.supportsFeature(LanguageFeature.NewInference)
private val samViaSyntheticScopeDisabled = languageVersionSettings.supportsFeature(LanguageFeature.NewInference) &&
languageVersionSettings.supportsFeature(LanguageFeature.SamConversionForKotlinFunctions)
private val extensionForFunction =
storageManager.createMemoizedFunctionWithNullableValues<FunctionDescriptor, FunctionDescriptor> { function ->

View File

@@ -7,7 +7,7 @@ This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
compiler/testData/cli/jvm/newInferenceImpliesSamConversions.kt:1:9: warning: parameter 'r' is never used
fun foo(r: Runnable) {}
compiler/testData/cli/jvm/newInferenceImpliesSamConversions.kt:4:9: error: type mismatch: inferred type is () -> TypeVariable(_L) but Runnable was expected
foo { }
^
OK
COMPILATION_ERROR

View File

@@ -1,4 +1,4 @@
compiler/testData/cli/jvm/newInferenceImpliesSamConversions.kt:1:9: warning: parameter 'r' is never used
fun foo(r: Runnable) {}
compiler/testData/cli/jvm/newInferenceImpliesSamConversions.kt:4:9: error: type mismatch: inferred type is () -> TypeVariable(_L) but Runnable was expected
foo { }
^
OK
COMPILATION_ERROR

View File

@@ -16,14 +16,14 @@ fun <T> bar(s: T) {}
fun <T> complex(t: T, f: (T) -> Unit) {}
fun test1() {
foo(1, <!NI;TYPE_MISMATCH!>A::invokeLater<!>) // KT-24507 SAM conversion accidentally applied to callable reference and incorrectly handled via BE
foo(1, A::invokeLater) // KT-24507 SAM conversion accidentally applied to callable reference and incorrectly handled via BE
foo(1, ::bar)
complex(1, ::bar)
}
fun <R> test2(x: R) {
foo(x, <!NI;TYPE_MISMATCH!>A::invokeLater<!>) // KT-24507 SAM conversion accidentally applied to callable reference and incorrectly handled via BE
foo(x, A::invokeLater) // KT-24507 SAM conversion accidentally applied to callable reference and incorrectly handled via BE
foo(x, ::bar)
complex(x, ::bar)

View File

@@ -0,0 +1,31 @@
// !LANGUAGE: +NewInference
// FILE: Runnable.java
public interface Runnable {
void run();
}
// FILE: 1.kt
interface K {
fun foo1(r: Runnable)
fun foo2(r1: Runnable, r2: Runnable)
fun foo3(r1: Runnable, r2: Runnable, r3: Runnable)
}
fun test(k: K, r: Runnable) {
k.foo1(r)
k.foo1(<!TYPE_MISMATCH!>{}<!>)
k.foo2(r, r)
k.foo2(<!TYPE_MISMATCH!>{}<!>, <!TYPE_MISMATCH!>{}<!>)
k.foo2(r, <!TYPE_MISMATCH!>{}<!>)
k.foo2(<!TYPE_MISMATCH!>{}<!>, r)
k.foo3(r, r, r)
k.foo3(r, r, <!TYPE_MISMATCH!>{}<!>)
k.foo3(r, <!TYPE_MISMATCH!>{}<!>, r)
k.foo3(r, <!TYPE_MISMATCH!>{}<!>, <!TYPE_MISMATCH!>{}<!>)
k.foo3(<!TYPE_MISMATCH!>{}<!>, r, r)
k.foo3(<!TYPE_MISMATCH!>{}<!>, r, <!TYPE_MISMATCH!>{}<!>)
k.foo3(<!TYPE_MISMATCH!>{}<!>, <!TYPE_MISMATCH!>{}<!>, r)
k.foo3(<!TYPE_MISMATCH!>{}<!>, <!TYPE_MISMATCH!>{}<!>, <!TYPE_MISMATCH!>{}<!>)
}

View File

@@ -0,0 +1,19 @@
package
public fun test(/*0*/ k: K, /*1*/ r: Runnable): kotlin.Unit
public interface K {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo1(/*0*/ r: Runnable): kotlin.Unit
public abstract fun foo2(/*0*/ r1: Runnable, /*1*/ r2: Runnable): kotlin.Unit
public abstract fun foo3(/*0*/ r1: Runnable, /*1*/ r2: Runnable, /*2*/ r3: Runnable): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface Runnable {
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 abstract fun run(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}

View File

@@ -21,7 +21,7 @@ class B : A() {
}
if (d.x is B) {
<!OI;SMARTCAST_IMPOSSIBLE!>d.x<!>.<!NI;INVISIBLE_MEMBER!>foo<!> {}
<!SMARTCAST_IMPOSSIBLE!>d.x<!>.<!NI;INVISIBLE_MEMBER!>foo<!> {}
}
}
}

View File

@@ -17987,6 +17987,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/samConversions"), Pattern.compile("^(.*)\\.kts?$"), TargetBackend.ANY, true);
}
@TestMetadata("checkSamConversionsAreDisabledByDefault.kt")
public void testCheckSamConversionsAreDisabledByDefault() throws Exception {
runTest("compiler/testData/diagnostics/tests/samConversions/checkSamConversionsAreDisabledByDefault.kt");
}
@TestMetadata("DisabledForKTSimple.kt")
public void testDisabledForKTSimple() throws Exception {
runTest("compiler/testData/diagnostics/tests/samConversions/DisabledForKTSimple.kt");

View File

@@ -17977,6 +17977,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/samConversions"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("checkSamConversionsAreDisabledByDefault.kt")
public void testCheckSamConversionsAreDisabledByDefault() throws Exception {
runTest("compiler/testData/diagnostics/tests/samConversions/checkSamConversionsAreDisabledByDefault.kt");
}
@TestMetadata("DisabledForKTSimple.kt")
public void testDisabledForKTSimple() throws Exception {
runTest("compiler/testData/diagnostics/tests/samConversions/DisabledForKTSimple.kt");