Allow to turn the first parameter of a SAM-converted lambda into the receiver (KT-12848)

This commit is contained in:
Yan Zhulanow
2016-12-06 20:03:27 +03:00
parent fc8cc217dc
commit cbef0250aa
57 changed files with 935 additions and 148 deletions
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="kotlin-runtime" level="project" />
<orderEntry type="module" module-name="frontend" />
<orderEntry type="module" module-name="frontend.java" />
</component>
</module>
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="kotlin-runtime" level="project" />
<orderEntry type="module" module-name="frontend" />
</component>
</module>
@@ -0,0 +1,71 @@
/*
* 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.samWithReceiver
import com.intellij.mock.MockProject
import org.jetbrains.kotlin.analyzer.ModuleInfo
import org.jetbrains.kotlin.compiler.plugin.CliOption
import org.jetbrains.kotlin.compiler.plugin.CliOptionProcessingException
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.CompilerConfigurationKey
import org.jetbrains.kotlin.container.ComponentProvider
import org.jetbrains.kotlin.container.get
import org.jetbrains.kotlin.extensions.StorageComponentContainerContributor
import org.jetbrains.kotlin.load.java.sam.SamWithReceiverResolver
object SamWithReceiverConfigurationKeys {
val ANNOTATION: CompilerConfigurationKey<List<String>> =
CompilerConfigurationKey.create("annotation qualified name")
}
class SamWithReceiverCommandLineProcessor : CommandLineProcessor {
companion object {
val ANNOTATION_OPTION = CliOption("annotation", "<fqname>", "Annotation qualified names",
required = false, allowMultipleOccurrences = true)
val PLUGIN_ID = "org.jetbrains.kotlin.samWithReceiver"
}
override val pluginId = PLUGIN_ID
override val pluginOptions = listOf(ANNOTATION_OPTION)
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) = when (option) {
ANNOTATION_OPTION -> {
val paths = configuration.getList(SamWithReceiverConfigurationKeys.ANNOTATION).toMutableList()
paths.add(value)
configuration.put(SamWithReceiverConfigurationKeys.ANNOTATION, paths)
}
else -> throw CliOptionProcessingException("Unknown option: ${option.name}")
}
}
class SamWithReceiverComponentRegistrar : ComponentRegistrar {
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
val annotations = configuration.get(SamWithReceiverConfigurationKeys.ANNOTATION) ?: return
if (annotations.isEmpty()) return
StorageComponentContainerContributor.registerExtension(project, CliSamWithReceiverComponentContributor(annotations))
}
}
class CliSamWithReceiverComponentContributor(val annotations: List<String>): StorageComponentContainerContributor {
override fun onContainerComposed(container: ComponentProvider, moduleInfo: ModuleInfo?) {
container.get<SamWithReceiverResolver>().registerExtension(SamWithReceiverResolverExtension(annotations))
}
}
@@ -0,0 +1,31 @@
/*
* 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.samWithReceiver
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.extensions.AnnotationBasedExtension
import org.jetbrains.kotlin.load.java.sam.SamWithReceiverResolver
import org.jetbrains.kotlin.psi.KtModifierListOwner
class SamWithReceiverResolverExtension(val annotations: List<String>) : SamWithReceiverResolver.Extension, AnnotationBasedExtension {
override fun getAnnotationFqNames(modifierListOwner: KtModifierListOwner?) = annotations
override fun shouldConvertFirstSamParameterToReceiver(function: FunctionDescriptor): Boolean {
return (function.containingDeclaration as? ClassDescriptor)?.hasSpecialAnnotation(null) ?: false
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.samWithReceiver.SamWithReceiverCommandLineProcessor
@@ -0,0 +1 @@
org.jetbrains.kotlin.samWithReceiver.SamWithReceiverComponentRegistrar
@@ -0,0 +1,21 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER,-UNUSED_VARIABLE
// FILE: Sam.java
@SamWithReceiver
public interface Sam {
void run();
}
// FILE: test.kt
annotation class SamWithReceiver
fun test() {
Sam {
System.out.println("Hello, world!")
}
Sam {
val a: String = <!NO_THIS!>this<!>
System.out.println(a)
}
}
@@ -0,0 +1,18 @@
package
@SamWithReceiver public /*synthesized*/ fun Sam(/*0*/ function: () -> kotlin.Unit): Sam
public fun test(): kotlin.Unit
@SamWithReceiver public interface Sam {
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
}
public final annotation class SamWithReceiver : kotlin.Annotation {
public constructor SamWithReceiver()
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 open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,21 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER,-UNUSED_VARIABLE
// FILE: Sam.java
@SamWithReceiver
public interface Sam {
void run(String a, String b);
}
// FILE: test.kt
annotation class SamWithReceiver
fun test() {
Sam { <!EXPECTED_PARAMETERS_NUMBER_MISMATCH!>a, <!CANNOT_INFER_PARAMETER_TYPE!>b<!><!> ->
System.out.println(a)
}
Sam { b ->
val a: String = this
System.out.println(a)
}
}
@@ -0,0 +1,18 @@
package
@SamWithReceiver public /*synthesized*/ fun Sam(/*0*/ function: kotlin.String!.(b: kotlin.String!) -> kotlin.Unit): Sam
public fun test(): kotlin.Unit
@SamWithReceiver public interface Sam {
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(/*0*/ a: kotlin.String!, /*1*/ b: kotlin.String!): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final annotation class SamWithReceiver : kotlin.Annotation {
public constructor SamWithReceiver()
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 open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,28 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER,-UNUSED_VARIABLE
// FILE: Sam.java
@SamWithReceiver
public interface Sam {
void run();
}
// FILE: Exec.java
public class Exec {
void exec(Sam sam) {}
}
// FILE: test.kt
annotation class SamWithReceiver
fun test() {
val e = Exec()
e.exec {
System.out.println("Hello, world!")
}
e.exec {
val a: String = <!NO_THIS!>this<!>
System.out.println(a)
}
}
@@ -0,0 +1,26 @@
package
@SamWithReceiver public /*synthesized*/ fun Sam(/*0*/ function: () -> kotlin.Unit): Sam
public fun test(): kotlin.Unit
public open class Exec {
public constructor Exec()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public/*package*/ open fun exec(/*0*/ sam: Sam!): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@SamWithReceiver public interface Sam {
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
}
public final annotation class SamWithReceiver : kotlin.Annotation {
public constructor SamWithReceiver()
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 open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,22 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER,-UNUSED_VARIABLE
// FILE: Sam.java
@SamWithReceiver
public interface Sam {
void run(String a);
}
// FILE: Exec.java
public class Exec {
void exec(Sam sam) {}
}
// FILE: test.kt
annotation class SamWithReceiver
fun test() {
val e = Exec()
e.exec <!TYPE_MISMATCH!>{ <!CANNOT_INFER_PARAMETER_TYPE!>a<!> -> System.out.println(<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>a<!>) }<!>
e.exec { System.out.println(this) }
}
@@ -0,0 +1,26 @@
package
@SamWithReceiver public /*synthesized*/ fun Sam(/*0*/ function: kotlin.String!.() -> kotlin.Unit): Sam
public fun test(): kotlin.Unit
public open class Exec {
public constructor Exec()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public/*package*/ open fun exec(/*0*/ sam: Sam!): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@SamWithReceiver public interface Sam {
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(/*0*/ a: kotlin.String!): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final annotation class SamWithReceiver : kotlin.Annotation {
public constructor SamWithReceiver()
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 open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,19 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER,-UNUSED_VARIABLE
// FILE: Sam.java
public interface Sam {
void run(String a);
}
// FILE: Exec.java
public class Exec {
void exec(Sam sam) {}
}
// FILE: test.kt
fun test() {
val e = Exec()
e.exec { a -> System.out.println(a) }
e.exec { System.out.println(<!NO_THIS!>this<!>) }
}
@@ -0,0 +1,19 @@
package
public /*synthesized*/ fun Sam(/*0*/ function: (a: kotlin.String!) -> kotlin.Unit): Sam
public fun test(): kotlin.Unit
public open class Exec {
public constructor Exec()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public/*package*/ open fun exec(/*0*/ sam: Sam!): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface Sam {
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(/*0*/ a: kotlin.String!): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,23 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER,-UNUSED_VARIABLE
// FILE: Sam.java
@SamWithReceiver
public interface Sam {
String run(String a, String b);
}
// FILE: test.kt
annotation class SamWithReceiver
fun test() {
Sam { <!EXPECTED_PARAMETERS_NUMBER_MISMATCH!>a, <!CANNOT_INFER_PARAMETER_TYPE!>b<!><!> ->
System.out.println(a)
""
}
Sam { b ->
val a: String = this
System.out.println(a)
""
}
}
@@ -0,0 +1,18 @@
package
@SamWithReceiver public /*synthesized*/ fun Sam(/*0*/ function: kotlin.String!.(b: kotlin.String!) -> kotlin.String!): Sam
public fun test(): kotlin.Unit
@SamWithReceiver public interface Sam {
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(/*0*/ a: kotlin.String!, /*1*/ b: kotlin.String!): kotlin.String!
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final annotation class SamWithReceiver : kotlin.Annotation {
public constructor SamWithReceiver()
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 open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,20 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER,-UNUSED_VARIABLE
// FILE: Sam.java
public interface Sam {
String run(String a, String b);
}
// FILE: test.kt
fun test() {
Sam { a, b ->
System.out.println(a)
""
}
Sam { <!EXPECTED_PARAMETERS_NUMBER_MISMATCH!>b<!> ->
val a = <!NO_THIS!>this@Sam<!>
System.out.println(<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>a<!>)
""
}
}
@@ -0,0 +1,11 @@
package
public /*synthesized*/ fun Sam(/*0*/ function: (a: kotlin.String!, b: kotlin.String!) -> kotlin.String!): Sam
public fun test(): kotlin.Unit
public interface Sam {
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(/*0*/ a: kotlin.String!, /*1*/ b: kotlin.String!): kotlin.String!
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,22 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER,-UNUSED_VARIABLE
// FILE: Sam.java
@SamWithReceiver
public interface Sam {
void run(String a);
}
// FILE: test.kt
annotation class SamWithReceiver
fun test() {
Sam { <!EXPECTED_PARAMETERS_NUMBER_MISMATCH, CANNOT_INFER_PARAMETER_TYPE!>a<!> ->
System.out.println(<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>a<!>)
}
Sam {
val a: String = this
val a2: String = <!UNRESOLVED_REFERENCE!>it<!>
System.out.println(a)
}
}
@@ -0,0 +1,18 @@
package
@SamWithReceiver public /*synthesized*/ fun Sam(/*0*/ function: kotlin.String!.() -> kotlin.Unit): Sam
public fun test(): kotlin.Unit
@SamWithReceiver public interface Sam {
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(/*0*/ a: kotlin.String!): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final annotation class SamWithReceiver : kotlin.Annotation {
public constructor SamWithReceiver()
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 open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,18 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER,-UNUSED_VARIABLE
// FILE: Sam.java
public interface Sam {
void run(String a);
}
// FILE: test.kt
fun test() {
Sam { a ->
System.out.println(a)
}
Sam {
val a = <!NO_THIS!>this<!>
System.out.println(<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>a<!>)
}
}
@@ -0,0 +1,11 @@
package
public /*synthesized*/ fun Sam(/*0*/ function: (a: kotlin.String!) -> kotlin.Unit): Sam
public fun test(): kotlin.Unit
public interface Sam {
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(/*0*/ a: kotlin.String!): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}