Support inline classes as last expression in lambda

This commit is contained in:
Mikhail Zarechenskiy
2018-02-07 07:18:06 +03:00
parent 396cc7d02b
commit 913e4b42bb
12 changed files with 250 additions and 51 deletions
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2015 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 2000-2018 JetBrains s.r.o. 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.codegen;
@@ -208,7 +197,9 @@ public class ClosureCodegen extends MemberCodegen<KtElement> {
generateBridge(
typeMapper.mapAsmMethod(erasedInterfaceFunction),
typeMapper.mapAsmMethod(funDescriptor)
erasedInterfaceFunction.getReturnType(),
typeMapper.mapAsmMethod(funDescriptor),
funDescriptor.getReturnType()
);
//TODO: rewrite cause ugly hack
@@ -275,7 +266,12 @@ public class ClosureCodegen extends MemberCodegen<KtElement> {
);
}
protected void generateBridge(@NotNull Method bridge, @NotNull Method delegate) {
protected void generateBridge(
@NotNull Method bridge,
@Nullable KotlinType bridgeReturnType,
@NotNull Method delegate,
@Nullable KotlinType delegateReturnType
) {
if (bridge.equals(delegate)) return;
MethodVisitor mv =
@@ -306,7 +302,10 @@ public class ClosureCodegen extends MemberCodegen<KtElement> {
}
iv.invokevirtual(asmType.getInternalName(), delegate.getName(), delegate.getDescriptor(), false);
StackValue.onStack(delegate.getReturnType()).put(bridge.getReturnType(), iv);
StackValue
.onStack(delegate.getReturnType(), delegateReturnType)
.put(bridge.getReturnType(), bridgeReturnType, iv);
iv.areturn(bridge.getReturnType());
@@ -326,11 +326,15 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
}
public void gen(KtElement expr, Type type) {
StackValue value = Type.VOID_TYPE.equals(type) ? genStatement(expr) : gen(expr);
putStackValue(expr, type, value);
gen(expr, type, null);
}
private void putStackValue(@Nullable KtElement expr, @NotNull Type type, @NotNull StackValue value) {
public void gen(KtElement expr, Type type, KotlinType kotlinType) {
StackValue value = Type.VOID_TYPE.equals(type) ? genStatement(expr) : gen(expr);
putStackValue(expr, type, kotlinType, value);
}
private void putStackValue(@Nullable KtElement expr, @NotNull Type type, @Nullable KotlinType kotlinType, @NotNull StackValue value) {
// for repl store the result of the last line into special field
if (value.type != Type.VOID_TYPE && state.getReplSpecific().getShouldGenerateScriptResultValue()) {
ScriptContext context = getScriptContext();
@@ -342,12 +346,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
}
}
KotlinType ktType = null;
if (expr instanceof KtExpression) {
ktType = kotlinType((KtExpression) expr);
}
value.put(type, ktType, v);
value.put(type, kotlinType, v);
}
@NotNull
@@ -1511,9 +1510,10 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
}
Type returnType = isNonLocalReturn ? nonLocalReturn.returnType : this.returnType;
KotlinType returnKotlinType = isNonLocalReturn ? null : this.context.getFunctionDescriptor().getReturnType();
StackValue valueToReturn = returnedExpression != null ? gen(returnedExpression) : StackValue.none();
putStackValue(returnedExpression, returnType, valueToReturn);
putStackValue(returnedExpression, returnType, returnKotlinType, valueToReturn);
Label afterReturnLabel = new Label();
generateFinallyBlocksIfNeeded(returnType, afterReturnLabel);
@@ -1588,7 +1588,10 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
? Type.VOID_TYPE
: returnType;
gen(expr, typeForExpression);
KotlinType kotlinTypeForExpression =
isBlockedNamedFunction || isVoidCoroutineLambda ? null : context.getFunctionDescriptor().getReturnType();
gen(expr, typeForExpression, kotlinTypeForExpression);
// If it does not end with return we should return something
// because if we don't there can be VerifyError (specific cases with Nothing-typed expressions)
@@ -3389,7 +3392,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
v.mark(ifNull);
v.pop();
gen(expression.getRight(), exprType);
gen(expression.getRight(), exprType, exprKotlinType);
v.mark(end);
return null;
});
@@ -380,8 +380,11 @@ public abstract class StackValue {
);
}
private static void unboxInlineClass(@NotNull KotlinType kotlinType, @NotNull InstructionAdapter v) {
private static void unboxInlineClass(@NotNull Type type, @NotNull KotlinType kotlinType, @NotNull InstructionAdapter v) {
Type owner = KotlinTypeMapper.mapInlineClassTypeAsDeclaration(kotlinType);
coerce(type, owner, v);
Type resultType = KotlinTypeMapper.mapUnderlyingTypeOfInlineClassType(kotlinType);
v.invokevirtual(
owner.getInternalName(),
@@ -429,7 +432,7 @@ public abstract class StackValue {
boxInlineClass(fromKotlinType, v);
}
else if (!isFromTypeUnboxed) {
unboxInlineClass(fromKotlinType, v);
unboxInlineClass(fromType, fromKotlinType, v);
}
}
else {
@@ -48,7 +48,7 @@ class CoercionValue(
class StackValueWithLeaveTask(
val stackValue: StackValue,
val leaveTasks: (StackValue) -> Unit
) : StackValue(stackValue.type) {
) : StackValue(stackValue.type, stackValue.kotlinType) {
override fun putReceiver(v: InstructionAdapter, isRead: Boolean) {
stackValue.putReceiver(v, isRead)
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2000-2018 JetBrains s.r.o. 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.codegen.coroutines
@@ -199,7 +188,7 @@ class CoroutineCodegenForLambda private constructor(
val bridgeParameters = (1 until delegate.argumentTypes.size).map { AsmTypes.OBJECT_TYPE } + delegate.argumentTypes.last()
val bridge = Method(delegate.name, delegate.returnType, bridgeParameters.toTypedArray())
generateBridge(bridge, delegate)
generateBridge(bridge, createCoroutineDescriptor.returnType, delegate, createCoroutineDescriptor.returnType)
}
}
@@ -0,0 +1,42 @@
// !LANGUAGE: +InlineClasses
inline class Name(private val value: String) {
fun asValue(): String = value
}
fun concat(a: Name, b: Name) = a.asValue() + b.asValue()
inline class UInt(private val value: Int) {
fun asValue(): Int = value
}
fun box(): String {
val o = inlinedRun {
Name("O")
}
val k = notInlinedRun {
Name("K")
}
if (concat(o, k) != "OK") return "fail 1"
val a = UInt(1)
val one = inlinedRun {
a
}
if (one.asValue() != 1) return "fail 2"
val b = UInt(2)
val two = notInlinedRun {
b
}
if (two.asValue() != 2) return "fail 3"
return "OK"
}
inline fun <R> inlinedRun(block: () -> R): R = block()
fun <R> notInlinedRun(block: () -> R): R = block()
@@ -0,0 +1,19 @@
// !LANGUAGE: +InlineClasses
inline class UInt(private val u: Int)
fun test(x: UInt?, y: UInt) {
val a = run {
x!!
}
val b = run {
y
}
}
// 2 INVOKESTATIC UInt\$Erased.box
// 3 INVOKEVIRTUAL UInt.unbox
// 0 valueOf
// 0 intValue
@@ -10341,6 +10341,36 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
doTest(fileName);
}
@TestMetadata("checkBoxingAfterAssertionOperator.kt")
public void testCheckBoxingAfterAssertionOperator() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingAfterAssertionOperator.kt");
doTest(fileName);
}
@TestMetadata("checkBoxingFromReturnTypeForInlineClasses.kt")
public void testCheckBoxingFromReturnTypeForInlineClasses() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingFromReturnTypeForInlineClasses.kt");
doTest(fileName);
}
@TestMetadata("checkBoxingOnFunctionCalls.kt")
public void testCheckBoxingOnFunctionCalls() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingOnFunctionCalls.kt");
doTest(fileName);
}
@TestMetadata("checkBoxingOnLocalVariableAssignments.kt")
public void testCheckBoxingOnLocalVariableAssignments() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingOnLocalVariableAssignments.kt");
doTest(fileName);
}
@TestMetadata("checkCallingMembersInsideInlineClass.kt")
public void testCheckCallingMembersInsideInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkCallingMembersInsideInlineClass.kt");
doTest(fileName);
}
@TestMetadata("computablePropertyInsideInlineClass.kt")
public void testComputablePropertyInsideInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/computablePropertyInsideInlineClass.kt");
@@ -10353,6 +10383,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
doTest(fileName);
}
@TestMetadata("inlineClassAsLastExpressionInInLambda.kt")
public void testInlineClassAsLastExpressionInInLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/inlineClassAsLastExpressionInInLambda.kt");
doTest(fileName);
}
@TestMetadata("referToPropertyInCompanionObjectOfInlineClass.kt")
public void testReferToPropertyInCompanionObjectOfInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/referToPropertyInCompanionObjectOfInlineClass.kt");
@@ -10341,6 +10341,36 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("checkBoxingAfterAssertionOperator.kt")
public void testCheckBoxingAfterAssertionOperator() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingAfterAssertionOperator.kt");
doTest(fileName);
}
@TestMetadata("checkBoxingFromReturnTypeForInlineClasses.kt")
public void testCheckBoxingFromReturnTypeForInlineClasses() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingFromReturnTypeForInlineClasses.kt");
doTest(fileName);
}
@TestMetadata("checkBoxingOnFunctionCalls.kt")
public void testCheckBoxingOnFunctionCalls() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingOnFunctionCalls.kt");
doTest(fileName);
}
@TestMetadata("checkBoxingOnLocalVariableAssignments.kt")
public void testCheckBoxingOnLocalVariableAssignments() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingOnLocalVariableAssignments.kt");
doTest(fileName);
}
@TestMetadata("checkCallingMembersInsideInlineClass.kt")
public void testCheckCallingMembersInsideInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkCallingMembersInsideInlineClass.kt");
doTest(fileName);
}
@TestMetadata("computablePropertyInsideInlineClass.kt")
public void testComputablePropertyInsideInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/computablePropertyInsideInlineClass.kt");
@@ -10353,6 +10383,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("inlineClassAsLastExpressionInInLambda.kt")
public void testInlineClassAsLastExpressionInInLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/inlineClassAsLastExpressionInInLambda.kt");
doTest(fileName);
}
@TestMetadata("referToPropertyInCompanionObjectOfInlineClass.kt")
public void testReferToPropertyInCompanionObjectOfInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/referToPropertyInCompanionObjectOfInlineClass.kt");
@@ -1920,6 +1920,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
doTest(fileName);
}
@TestMetadata("boxUnboxInsideLambdaAsLastExpression.kt")
public void testBoxUnboxInsideLambdaAsLastExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/inlineClasses/boxUnboxInsideLambdaAsLastExpression.kt");
doTest(fileName);
}
@TestMetadata("callMemberMethodsInsideInlineClass.kt")
public void testCallMemberMethodsInsideInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/inlineClasses/callMemberMethodsInsideInlineClass.kt");
@@ -1950,12 +1956,6 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
doTest(fileName);
}
@TestMetadata("inlineClassCallsDefaultValue.kt")
public void testInlineClassCallsDefaultValue() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/inlineClasses/inlineClassCallsDefaultValue.kt");
doTest(fileName);
}
@TestMetadata("inlineClassesUnboxingAfterAssertionOperator.kt")
public void testInlineClassesUnboxingAfterAssertionOperator() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/inlineClasses/inlineClassesUnboxingAfterAssertionOperator.kt");
@@ -10341,6 +10341,36 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
doTest(fileName);
}
@TestMetadata("checkBoxingAfterAssertionOperator.kt")
public void testCheckBoxingAfterAssertionOperator() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingAfterAssertionOperator.kt");
doTest(fileName);
}
@TestMetadata("checkBoxingFromReturnTypeForInlineClasses.kt")
public void testCheckBoxingFromReturnTypeForInlineClasses() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingFromReturnTypeForInlineClasses.kt");
doTest(fileName);
}
@TestMetadata("checkBoxingOnFunctionCalls.kt")
public void testCheckBoxingOnFunctionCalls() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingOnFunctionCalls.kt");
doTest(fileName);
}
@TestMetadata("checkBoxingOnLocalVariableAssignments.kt")
public void testCheckBoxingOnLocalVariableAssignments() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingOnLocalVariableAssignments.kt");
doTest(fileName);
}
@TestMetadata("checkCallingMembersInsideInlineClass.kt")
public void testCheckCallingMembersInsideInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkCallingMembersInsideInlineClass.kt");
doTest(fileName);
}
@TestMetadata("computablePropertyInsideInlineClass.kt")
public void testComputablePropertyInsideInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/computablePropertyInsideInlineClass.kt");
@@ -10353,6 +10383,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
doTest(fileName);
}
@TestMetadata("inlineClassAsLastExpressionInInLambda.kt")
public void testInlineClassAsLastExpressionInInLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/inlineClassAsLastExpressionInInLambda.kt");
doTest(fileName);
}
@TestMetadata("referToPropertyInCompanionObjectOfInlineClass.kt")
public void testReferToPropertyInCompanionObjectOfInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/referToPropertyInCompanionObjectOfInlineClass.kt");
@@ -11301,6 +11301,36 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
doTest(fileName);
}
@TestMetadata("checkBoxingAfterAssertionOperator.kt")
public void testCheckBoxingAfterAssertionOperator() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingAfterAssertionOperator.kt");
doTest(fileName);
}
@TestMetadata("checkBoxingFromReturnTypeForInlineClasses.kt")
public void testCheckBoxingFromReturnTypeForInlineClasses() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingFromReturnTypeForInlineClasses.kt");
doTest(fileName);
}
@TestMetadata("checkBoxingOnFunctionCalls.kt")
public void testCheckBoxingOnFunctionCalls() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingOnFunctionCalls.kt");
doTest(fileName);
}
@TestMetadata("checkBoxingOnLocalVariableAssignments.kt")
public void testCheckBoxingOnLocalVariableAssignments() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkBoxingOnLocalVariableAssignments.kt");
doTest(fileName);
}
@TestMetadata("checkCallingMembersInsideInlineClass.kt")
public void testCheckCallingMembersInsideInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/checkCallingMembersInsideInlineClass.kt");
doTest(fileName);
}
@TestMetadata("computablePropertyInsideInlineClass.kt")
public void testComputablePropertyInsideInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/computablePropertyInsideInlineClass.kt");
@@ -11313,6 +11343,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
doTest(fileName);
}
@TestMetadata("inlineClassAsLastExpressionInInLambda.kt")
public void testInlineClassAsLastExpressionInInLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/inlineClassAsLastExpressionInInLambda.kt");
doTest(fileName);
}
@TestMetadata("referToPropertyInCompanionObjectOfInlineClass.kt")
public void testReferToPropertyInCompanionObjectOfInlineClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/inlineClasses/referToPropertyInCompanionObjectOfInlineClass.kt");