Generate proper exception table

Don't forget to split nested try blocks without finally block
 on generating finally blocks from outer ones.

 #KT-31923 InProgress
This commit is contained in:
Mikhael Bogdanov
2019-06-11 11:29:52 +02:00
parent 078cfc02a5
commit c335015c05
20 changed files with 752 additions and 65 deletions
@@ -186,18 +186,20 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
}
}
static class FinallyBlockStackElement extends BlockStackElement {
static class TryBlockStackElement extends BlockStackElement {
List<Label> gaps = new ArrayList<>();
void addGapLabel(Label label){
gaps.add(label);
}
}
static class TryWithFinallyBlockStackElement extends TryBlockStackElement {
final KtTryExpression expression;
FinallyBlockStackElement(KtTryExpression expression) {
TryWithFinallyBlockStackElement(KtTryExpression expression) {
this.expression = expression;
}
private void addGapLabel(Label label){
gaps.add(label);
}
}
@NotNull
@@ -701,19 +703,20 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
@Override
public StackValue visitBreakExpression(@NotNull KtBreakExpression expression, StackValue receiver) {
return generateBreakOrContinueExpression(expression, true, new Label());
return generateBreakOrContinueExpression(expression, true, new Label(), new ArrayList<>());
}
@Override
public StackValue visitContinueExpression(@NotNull KtContinueExpression expression, StackValue receiver) {
return generateBreakOrContinueExpression(expression, false, new Label());
return generateBreakOrContinueExpression(expression, false, new Label(), new ArrayList<>());
}
@NotNull
private StackValue generateBreakOrContinueExpression(
@NotNull KtExpressionWithLabel expression,
boolean isBreak,
@NotNull Label afterBreakContinueLabel
@NotNull Label afterBreakContinueLabel,
@NotNull List<TryBlockStackElement> nestedTryBlocksWithoutFinally
) {
assert expression instanceof KtContinueExpression || expression instanceof KtBreakExpression;
@@ -723,9 +726,13 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
BlockStackElement stackElement = blockStackElements.peek();
if (stackElement instanceof FinallyBlockStackElement) {
FinallyBlockStackElement finallyBlockStackElement = (FinallyBlockStackElement) stackElement;
genFinallyBlockOrGoto(finallyBlockStackElement, null, afterBreakContinueLabel);
if (stackElement instanceof TryWithFinallyBlockStackElement) {
TryWithFinallyBlockStackElement tryWithFinallyBlockStackElement = (TryWithFinallyBlockStackElement) stackElement;
genFinallyBlockOrGoto(tryWithFinallyBlockStackElement, null, afterBreakContinueLabel, nestedTryBlocksWithoutFinally);
nestedTryBlocksWithoutFinally.clear();
}
else if (stackElement instanceof TryBlockStackElement) {
nestedTryBlocksWithoutFinally.add((TryBlockStackElement) stackElement);
}
else if (stackElement instanceof LoopBlockStackElement) {
LoopBlockStackElement loopBlockStackElement = (LoopBlockStackElement) stackElement;
@@ -748,7 +755,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
}
blockStackElements.pop();
StackValue result = generateBreakOrContinueExpression(expression, isBreak, afterBreakContinueLabel);
StackValue result = generateBreakOrContinueExpression(expression, isBreak, afterBreakContinueLabel, nestedTryBlocksWithoutFinally);
blockStackElements.push(stackElement);
return result;
}
@@ -1508,12 +1515,16 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
return myLastLineNumber;
}
private void doFinallyOnReturn(@NotNull Label afterReturnLabel) {
private boolean doFinallyOnReturn(@NotNull Label afterReturnLabel, @NotNull List<TryBlockStackElement> nestedTryBlocksWithoutFinally) {
if(!blockStackElements.isEmpty()) {
BlockStackElement stackElement = blockStackElements.peek();
if (stackElement instanceof FinallyBlockStackElement) {
FinallyBlockStackElement finallyBlockStackElement = (FinallyBlockStackElement) stackElement;
genFinallyBlockOrGoto(finallyBlockStackElement, null, afterReturnLabel);
if (stackElement instanceof TryWithFinallyBlockStackElement) {
TryWithFinallyBlockStackElement tryWithFinallyBlockStackElement = (TryWithFinallyBlockStackElement) stackElement;
genFinallyBlockOrGoto(tryWithFinallyBlockStackElement, null, afterReturnLabel, nestedTryBlocksWithoutFinally);
nestedTryBlocksWithoutFinally.clear();
}
else if (stackElement instanceof TryBlockStackElement) {
nestedTryBlocksWithoutFinally.add((TryBlockStackElement) stackElement);
}
else if (stackElement instanceof LoopBlockStackElement) {
@@ -1522,14 +1533,18 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
}
blockStackElements.pop();
doFinallyOnReturn(afterReturnLabel);
blockStackElements.push(stackElement);
try {
return doFinallyOnReturn(afterReturnLabel, nestedTryBlocksWithoutFinally);
} finally {
blockStackElements.push(stackElement);
}
}
return false;
}
public boolean hasFinallyBlocks() {
for (BlockStackElement element : blockStackElements) {
if (element instanceof FinallyBlockStackElement) {
if (element instanceof TryWithFinallyBlockStackElement) {
return true;
}
}
@@ -1537,21 +1552,23 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
}
private void genFinallyBlockOrGoto(
@Nullable FinallyBlockStackElement finallyBlockStackElement,
@Nullable TryWithFinallyBlockStackElement tryWithFinallyBlockStackElement,
@Nullable Label tryCatchBlockEnd,
@Nullable Label afterJumpLabel
@Nullable Label afterJumpLabel,
@NotNull List<TryBlockStackElement> nestedTryBlocksWithoutFinally
) {
if (finallyBlockStackElement != null) {
if (tryWithFinallyBlockStackElement != null) {
finallyDepth++;
assert finallyBlockStackElement.gaps.size() % 2 == 0 : "Finally block gaps are inconsistent";
assert tryWithFinallyBlockStackElement.gaps.size() % 2 == 0 : "Finally block gaps are inconsistent";
BlockStackElement topOfStack = blockStackElements.pop();
assert topOfStack == finallyBlockStackElement : "Top element of stack doesn't equals processing finally block";
assert topOfStack == tryWithFinallyBlockStackElement : "Top element of stack doesn't equals processing finally block";
KtTryExpression jetTryExpression = finallyBlockStackElement.expression;
KtTryExpression jetTryExpression = tryWithFinallyBlockStackElement.expression;
Label finallyStart = new Label();
v.mark(finallyStart);
finallyBlockStackElement.addGapLabel(finallyStart);
tryWithFinallyBlockStackElement.addGapLabel(finallyStart);
addGapLabelsForNestedTryCatchWithoutFinally(state, nestedTryBlocksWithoutFinally, finallyStart);
if (isFinallyMarkerRequired(context)) {
generateFinallyMarker(v, finallyDepth, true);
}
@@ -1564,22 +1581,35 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
}
if (tryCatchBlockEnd != null) {
if (finallyBlockStackElement != null) {
markLineNumber(finallyBlockStackElement.expression, true);
if (tryWithFinallyBlockStackElement != null) {
markLineNumber(tryWithFinallyBlockStackElement.expression, true);
}
v.goTo(tryCatchBlockEnd);
}
if (finallyBlockStackElement != null) {
if (tryWithFinallyBlockStackElement != null) {
finallyDepth--;
Label finallyEnd = afterJumpLabel != null ? afterJumpLabel : new Label();
if (afterJumpLabel == null) {
v.mark(finallyEnd);
}
finallyBlockStackElement.addGapLabel(finallyEnd);
tryWithFinallyBlockStackElement.addGapLabel(finallyEnd);
addGapLabelsForNestedTryCatchWithoutFinally(state, nestedTryBlocksWithoutFinally, finallyEnd);
blockStackElements.push(finallyBlockStackElement);
blockStackElements.push(tryWithFinallyBlockStackElement);
}
}
private static void addGapLabelsForNestedTryCatchWithoutFinally(
@NotNull GenerationState state,
@NotNull List<TryBlockStackElement> nestedTryBlocksWithoutFinally,
@NotNull Label label
) {
if (state.getLanguageVersionSettings().supportsFeature(LanguageFeature.ProperFinally)) {
for (TryBlockStackElement tryBlock : nestedTryBlocksWithoutFinally) {
tryBlock.addGapLabel(label);
}
}
}
@@ -1637,12 +1667,12 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
int returnValIndex = myFrameMap.enterTemp(returnType);
StackValue.Local localForReturnValue = StackValue.local(returnValIndex, returnType, returnKotlinType);
localForReturnValue.store(StackValue.onStack(returnType, returnKotlinType), v);
doFinallyOnReturn(afterReturnLabel);
doFinallyOnReturn(afterReturnLabel, new ArrayList<>());
localForReturnValue.put(returnType, null, v);
myFrameMap.leaveTemp(returnType);
}
else {
doFinallyOnReturn(afterReturnLabel);
doFinallyOnReturn(afterReturnLabel, new ArrayList<>());
}
}
}
@@ -4703,10 +4733,16 @@ The "returned" value of try expression with no finally is either the last expres
return StackValue.operation(expectedAsmType, expectedKotlinType, v -> {
KtFinallySection finallyBlock = expression.getFinallyBlock();
FinallyBlockStackElement finallyBlockStackElement = null;
TryWithFinallyBlockStackElement tryWithFinallyBlockStackElement = null;
TryBlockStackElement element;
if (finallyBlock != null) {
finallyBlockStackElement = new FinallyBlockStackElement(expression);
blockStackElements.push(finallyBlockStackElement);
tryWithFinallyBlockStackElement = new TryWithFinallyBlockStackElement(expression);
element = tryWithFinallyBlockStackElement;
blockStackElements.push(tryWithFinallyBlockStackElement);
}
else {
element = new TryBlockStackElement();
blockStackElements.push(element);
}
//PseudoInsnsPackage.saveStackBeforeTryExpr(v);
@@ -4727,11 +4763,11 @@ The "returned" value of try expression with no finally is either the last expres
v.mark(tryEnd);
//do it before finally block generation
List<Label> tryBlockRegions = getCurrentCatchIntervals(finallyBlockStackElement, tryStart, tryEnd);
List<Label> tryBlockRegions = getCurrentCatchIntervals(element, tryStart, tryEnd);
Label end = new Label();
genFinallyBlockOrGoto(finallyBlockStackElement, end, null);
genFinallyBlockOrGoto(tryWithFinallyBlockStackElement, end, null, new ArrayList<>());
List<KtCatchClause> clauses = expression.getCatchClauses();
for (int i = 0, size = clauses.size(); i < size; i++) {
@@ -4769,7 +4805,7 @@ The "returned" value of try expression with no finally is either the last expres
v.visitLocalVariable(descriptor.getName().asString(), descriptorType.getDescriptor(), null,
catchVariableStart, clauseEnd, index);
genFinallyBlockOrGoto(finallyBlockStackElement, i != size - 1 || finallyBlock != null ? end : null, null);
genFinallyBlockOrGoto(tryWithFinallyBlockStackElement, i != size - 1 || finallyBlock != null ? end : null, null, new ArrayList<>());
generateExceptionTable(clauseStart, tryBlockRegions, descriptorType.getInternalName());
}
@@ -4787,10 +4823,10 @@ The "returned" value of try expression with no finally is either the last expres
//do it before finally block generation
//javac also generates entry in exception table for default catch clause too!!!! so defaultCatchEnd as end parameter
List<Label> defaultCatchRegions = getCurrentCatchIntervals(finallyBlockStackElement, tryStart, defaultCatchEnd);
List<Label> defaultCatchRegions = getCurrentCatchIntervals(tryWithFinallyBlockStackElement, tryStart, defaultCatchEnd);
genFinallyBlockOrGoto(finallyBlockStackElement, null, null);
genFinallyBlockOrGoto(tryWithFinallyBlockStackElement, null, null, new ArrayList<>());
v.load(savedException, JAVA_THROWABLE_TYPE);
myFrameMap.leaveTemp(JAVA_THROWABLE_TYPE);
@@ -4808,9 +4844,9 @@ The "returned" value of try expression with no finally is either the last expres
myFrameMap.leaveTemp(expectedAsmType);
}
if (finallyBlock != null) {
blockStackElements.pop();
}
blockStackElements.pop();
return Unit.INSTANCE;
});
}
@@ -4830,7 +4866,7 @@ The "returned" value of try expression with no finally is either the last expres
@NotNull
private static List<Label> getCurrentCatchIntervals(
@Nullable FinallyBlockStackElement finallyBlockStackElement,
@Nullable TryBlockStackElement finallyBlockStackElement,
@NotNull Label blockStart,
@NotNull Label blockEnd
) {
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.codegen.pseudoInsns.fakeAlwaysFalseIfeq
import org.jetbrains.kotlin.codegen.pseudoInsns.fixStackAndJump
import org.jetbrains.kotlin.codegen.signature.BothSignatureWriter
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.isReleaseCoroutines
import org.jetbrains.kotlin.config.languageVersionSettings
import org.jetbrains.kotlin.descriptors.ClassDescriptor
@@ -59,17 +60,19 @@ sealed class ExpressionInfo
class LoopInfo(val loop: IrLoop, val continueLabel: Label, val breakLabel: Label) : ExpressionInfo()
class TryInfo(val onExit: IrExpression) : ExpressionInfo() {
open class TryInfo : ExpressionInfo() {
// Regions corresponding to copy-pasted contents of the `finally` block.
// These should not be covered by `catch` clauses.
val gaps = mutableListOf<Pair<Label, Label>>()
}
class TryWithFinallyInfo(val onExit: IrExpression) : TryInfo()
class BlockInfo(val parent: BlockInfo? = null) {
val variables = mutableListOf<VariableInfo>()
private val infos: Stack<ExpressionInfo> = parent?.infos ?: Stack()
fun hasFinallyBlocks(): Boolean = infos.firstIsInstanceOrNull<TryInfo>() != null
fun hasFinallyBlocks(): Boolean = infos.firstIsInstanceOrNull<TryWithFinallyInfo>() != null
internal inline fun <T : ExpressionInfo, R> withBlock(info: T, f: (T) -> R): R {
infos.add(info)
@@ -735,11 +738,20 @@ class ExpressionCodegen(
return immaterialUnitValue
}
private fun unwindBlockStack(endLabel: Label, data: BlockInfo, stop: (ExpressionInfo) -> Boolean = { false }): ExpressionInfo? {
private fun unwindBlockStack(
endLabel: Label,
data: BlockInfo,
nestedTryWithoutFinally: MutableList<TryInfo> = arrayListOf(),
stop: (ExpressionInfo) -> Boolean = { false }
): ExpressionInfo? {
return data.handleBlock {
if (it is TryInfo)
genFinallyBlock(it, null, endLabel, data)
return if (stop(it)) it else unwindBlockStack(endLabel, data, stop)
if (it is TryWithFinallyInfo) {
genFinallyBlock(it, null, endLabel, data, nestedTryWithoutFinally)
nestedTryWithoutFinally.clear()
} else if (it is TryInfo) {
nestedTryWithoutFinally.add(it)
}
return if (stop(it)) it else unwindBlockStack(endLabel, data, nestedTryWithoutFinally, stop)
}
}
@@ -755,13 +767,13 @@ class ExpressionCodegen(
override fun visitTry(aTry: IrTry, data: BlockInfo): PromisedValue {
aTry.markLineNumber(startOffset = true)
return if (aTry.finallyExpression != null)
data.withBlock(TryInfo(aTry.finallyExpression!!)) { visitTryWithInfo(aTry, data, it) }
else
visitTryWithInfo(aTry, data, null)
return data.withBlock(if (aTry.finallyExpression != null) TryWithFinallyInfo(aTry.finallyExpression!!) else TryInfo()) {
visitTryWithInfo(aTry, data, it)
}
}
private fun visitTryWithInfo(aTry: IrTry, data: BlockInfo, tryInfo: TryInfo?): PromisedValue {
private fun visitTryWithInfo(aTry: IrTry, data: BlockInfo, tryInfo: TryInfo): PromisedValue {
val tryBlockStart = markNewLabel()
mv.nop()
val tryAsmType = aTry.asmType
@@ -777,9 +789,9 @@ class ExpressionCodegen(
}
val tryBlockEnd = markNewLabel()
val tryBlockGaps = tryInfo?.gaps?.toList() ?: listOf()
val tryBlockGaps = tryInfo.gaps.toList()
val tryCatchBlockEnd = Label()
if (tryInfo != null) {
if (tryInfo is TryWithFinallyInfo) {
data.handleBlock { genFinallyBlock(tryInfo, tryCatchBlockEnd, null, data) }
} else {
mv.goTo(tryCatchBlockEnd)
@@ -812,7 +824,7 @@ class ExpressionCodegen(
index
)
if (tryInfo != null) {
if (tryInfo is TryWithFinallyInfo) {
data.handleBlock { genFinallyBlock(tryInfo, tryCatchBlockEnd, null, data) }
} else if (clause != catches.last()) {
mv.goTo(tryCatchBlockEnd)
@@ -821,7 +833,7 @@ class ExpressionCodegen(
genTryCatchCover(clauseStart, tryBlockStart, tryBlockEnd, tryBlockGaps, descriptorType.internalName)
}
if (tryInfo != null) {
if (tryInfo is TryWithFinallyInfo) {
// Generate `try { ... } catch (e: Any?) { <finally>; throw e }` around every part of
// the try-catch that is not a copy-pasted `finally` block.
val defaultCatchStart = markNewLabel()
@@ -860,22 +872,34 @@ class ExpressionCodegen(
mv.visitTryCatchBlock(lastRegionStart, tryEnd, catchStart, type)
}
private fun genFinallyBlock(tryInfo: TryInfo, tryCatchBlockEnd: Label?, afterJumpLabel: Label?, data: BlockInfo) {
private fun genFinallyBlock(
tryWithFinallyInfo: TryWithFinallyInfo,
tryCatchBlockEnd: Label?,
afterJumpLabel: Label?,
data: BlockInfo,
nestedTryWithoutFinally: MutableList<TryInfo> = arrayListOf()
) {
val gapStart = markNewLabel()
finallyDepth++
if (isFinallyMarkerRequired()) {
generateFinallyMarker(mv, finallyDepth, true)
}
tryInfo.onExit.accept(this, data).discard()
tryWithFinallyInfo.onExit.accept(this, data).discard()
if (isFinallyMarkerRequired()) {
generateFinallyMarker(mv, finallyDepth, false)
}
finallyDepth--
if (tryCatchBlockEnd != null) {
tryInfo.onExit.markLineNumber(startOffset = false)
tryWithFinallyInfo.onExit.markLineNumber(startOffset = false)
mv.goTo(tryCatchBlockEnd)
}
tryInfo.gaps.add(gapStart to (afterJumpLabel ?: markNewLabel()))
val gapEnd = afterJumpLabel ?: markNewLabel()
tryWithFinallyInfo.gaps.add(gapStart to gapEnd)
if (state.languageVersionSettings.supportsFeature(LanguageFeature.ProperFinally)) {
for (it in nestedTryWithoutFinally) {
it.gaps.add(gapStart to gapEnd)
}
}
}
fun generateFinallyBlocksIfNeeded(returnType: Type, afterReturnLabel: Label, data: BlockInfo) {
@@ -0,0 +1,31 @@
// IGNORE_BACKEND_FIR: JVM_IR
var result = ""
fun test() {
try {
for (z in 1..2) {
try {
result += "try"
break
} catch (fail: Throwable) {
result += " catch"
}
}
result += " after loop"
} finally {
result += " finally"
throw RuntimeException()
}
}
fun box(): String {
try {
test()
return "fail: expected exception"
} catch (e: RuntimeException) {
}
return if (result == "try after loop finally") "OK" else "fail: $result"
}
@@ -0,0 +1,31 @@
// IGNORE_BACKEND_FIR: JVM_IR
var result = ""
fun test() {
try {
for (z in 1..2) {
try {
result += "try "
continue
} catch (fail: Throwable) {
result += " catch"
}
}
result += "after loop"
} finally {
result += " finally"
throw RuntimeException()
}
}
fun box(): String {
try {
test()
return "fail: expected exception"
} catch (e: RuntimeException) {
}
return if (result == "try try after loop finally") "OK" else "fail: $result"
}
+30
View File
@@ -0,0 +1,30 @@
// !LANGUAGE: +ProperFinally
// IGNORE_BACKEND_FIR: JVM_IR
var result = ""
fun test() {
for (z in 1..2) {
try {
try {
result += "try"
break
} catch (fail: Throwable) {
result += " catch"
}
} finally {
result += " finally"
throw RuntimeException()
}
}
}
fun box(): String {
try {
test()
return "fail: expected exception"
} catch (e: RuntimeException) {
}
return if (result == "try finally") "OK" else "fail: $result"
}
@@ -0,0 +1,30 @@
// !LANGUAGE: +ProperFinally
// IGNORE_BACKEND_FIR: JVM_IR
var result = ""
fun test() {
for (z in 1..2) {
try {
try {
result += "try"
continue
} catch (fail: Throwable) {
result += "catch"
}
} finally {
result += " finally"
throw RuntimeException()
}
}
}
fun box(): String {
try {
test()
return "fail: expected exception"
} catch (e: RuntimeException) {
}
return if (result == "try finally") "OK" else "fail: $result"
}
+27
View File
@@ -0,0 +1,27 @@
// !LANGUAGE: +ProperFinally
var result = ""
fun test() {
try {
try {
result += "try"
return
} catch (fail: Throwable) {
result += " catch"
}
} finally {
result += " finally"
throw RuntimeException()
}
}
fun box(): String {
try {
test()
return "fail: expected exception"
} catch (e: RuntimeException) {
}
return if (result == "try finally") "OK" else "fail: $result"
}
+28
View File
@@ -0,0 +1,28 @@
// !LANGUAGE: -ProperFinally
// TARGET_BACKEND: JVM
var result = ""
fun test() {
try {
try {
result += "try"
return
} catch (fail: Throwable) {
result += " catch"
}
} finally {
result += " finally"
throw RuntimeException()
}
}
fun box(): String {
try {
test()
return "fail: expected exception"
} catch (e: RuntimeException) {
}
return if (result == "try finally catch finally") "OK" else "fail: $result"
}
@@ -0,0 +1,32 @@
// !LANGUAGE: +ProperFinally
var result = ""
fun test() {
try {
try {
try {
result += "try"
return
} catch (fail: Throwable) {
result += " catch"
}
} finally {
result += " finally 1"
throw RuntimeException("Fail 1")
}
} finally {
result += " finally 2"
throw RuntimeException("Fail 2")
}
}
fun box(): String {
try {
test()
return "fail: expected exception"
} catch (e: RuntimeException) {
if (e.message != "Fail 2") return "wrong exception: ${e.message}"
}
return if (result == "try finally 1 finally 2") "OK" else "fail: $result"
}
@@ -0,0 +1,34 @@
// !LANGUAGE: +ProperFinally
var result = ""
fun test() {
try {
try {
try {
result += "try"
return
} catch (fail: Throwable) {
result += " catch"
}
} catch (e: Throwable) {
result += " finally_catch"
} finally {
result += " finally 1"
throw RuntimeException("Fail 1")
}
} finally {
result += " finally 2"
throw RuntimeException("Fail 2")
}
}
fun box(): String {
try {
test()
return "fail: expected exception"
} catch (e: RuntimeException) {
if (e.message != "Fail 2") return "wrong exception: ${e.message}"
}
return if (result == "try finally 1 finally 2") "OK" else "fail: $result"
}
@@ -0,0 +1,37 @@
// !LANGUAGE: +ProperFinally
var result = ""
fun test() {
try {
try {
try {
try {
result += "try"
return
} catch (fail: Throwable) {
result += " catch"
}
} finally {
result += " finally 1"
throw RuntimeException(" exception from finally 1")
}
} catch (e: Throwable) {
result += " catch 2"
result += e.message
}
} finally {
result += " finally 2"
throw RuntimeException("Fail 2")
}
}
fun box(): String {
try {
test()
return "fail: expected exception"
} catch (e: RuntimeException) {
if (e.message != "Fail 2") return "wrong exception: ${e.message}"
}
return if (result == "try finally 1 catch 2 exception from finally 1 finally 2") "OK" else "fail: $result"
}
@@ -0,0 +1,36 @@
// !LANGUAGE: +ProperFinally
var result = ""
fun test() {
try {
try {
try {
try {
result += "try"
return
} catch (fail: Throwable) {
result += " catch"
}
} finally {
result += " finally 1"
}
} catch (e: Throwable) {
result += " catch 2"
result += e.message
}
} finally {
result += " finally 2"
throw RuntimeException("Fail 2")
}
}
fun box(): String {
try {
test()
return "fail: expected exception"
} catch (e: RuntimeException) {
if (e.message != "Fail 2") return "wrong exception: ${e.message}"
}
return if (result == "try finally 1 finally 2") "OK" else "fail: $result"
}
@@ -11239,11 +11239,41 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/finally"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
}
@TestMetadata("breakAndOuterFinally.kt")
public void testBreakAndOuterFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/breakAndOuterFinally.kt");
}
@TestMetadata("continueAndOuterFinally.kt")
public void testContinueAndOuterFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/continueAndOuterFinally.kt");
}
@TestMetadata("finallyAndFinally.kt")
public void testFinallyAndFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/finallyAndFinally.kt");
}
@TestMetadata("kt31923_break.kt")
public void testKt31923_break() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_break.kt");
}
@TestMetadata("kt31923_continue.kt")
public void testKt31923_continue() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_continue.kt");
}
@TestMetadata("kt31923_return.kt")
public void testKt31923_return() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_return.kt");
}
@TestMetadata("kt31923_wrong.kt")
public void testKt31923_wrong() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_wrong.kt");
}
@TestMetadata("kt3549.kt")
public void testKt3549() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt3549.kt");
@@ -11279,6 +11309,26 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/finally/loopAndFinally.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry.kt")
public void testNestedFinallyAndNonFinallyTry() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry2.kt")
public void testNestedFinallyAndNonFinallyTry2() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry2.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry3.kt")
public void testNestedFinallyAndNonFinallyTry3() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry3.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry4.kt")
public void testNestedFinallyAndNonFinallyTry4() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry4.kt");
}
@TestMetadata("notChainCatch.kt")
public void testNotChainCatch() throws Exception {
runTest("compiler/testData/codegen/box/finally/notChainCatch.kt");
@@ -11239,11 +11239,41 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/finally"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
}
@TestMetadata("breakAndOuterFinally.kt")
public void testBreakAndOuterFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/breakAndOuterFinally.kt");
}
@TestMetadata("continueAndOuterFinally.kt")
public void testContinueAndOuterFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/continueAndOuterFinally.kt");
}
@TestMetadata("finallyAndFinally.kt")
public void testFinallyAndFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/finallyAndFinally.kt");
}
@TestMetadata("kt31923_break.kt")
public void testKt31923_break() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_break.kt");
}
@TestMetadata("kt31923_continue.kt")
public void testKt31923_continue() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_continue.kt");
}
@TestMetadata("kt31923_return.kt")
public void testKt31923_return() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_return.kt");
}
@TestMetadata("kt31923_wrong.kt")
public void testKt31923_wrong() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_wrong.kt");
}
@TestMetadata("kt3549.kt")
public void testKt3549() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt3549.kt");
@@ -11279,6 +11309,26 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/finally/loopAndFinally.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry.kt")
public void testNestedFinallyAndNonFinallyTry() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry2.kt")
public void testNestedFinallyAndNonFinallyTry2() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry2.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry3.kt")
public void testNestedFinallyAndNonFinallyTry3() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry3.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry4.kt")
public void testNestedFinallyAndNonFinallyTry4() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry4.kt");
}
@TestMetadata("notChainCatch.kt")
public void testNotChainCatch() throws Exception {
runTest("compiler/testData/codegen/box/finally/notChainCatch.kt");
@@ -10089,11 +10089,41 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/finally"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true);
}
@TestMetadata("breakAndOuterFinally.kt")
public void testBreakAndOuterFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/breakAndOuterFinally.kt");
}
@TestMetadata("continueAndOuterFinally.kt")
public void testContinueAndOuterFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/continueAndOuterFinally.kt");
}
@TestMetadata("finallyAndFinally.kt")
public void testFinallyAndFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/finallyAndFinally.kt");
}
@TestMetadata("kt31923_break.kt")
public void testKt31923_break() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_break.kt");
}
@TestMetadata("kt31923_continue.kt")
public void testKt31923_continue() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_continue.kt");
}
@TestMetadata("kt31923_return.kt")
public void testKt31923_return() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_return.kt");
}
@TestMetadata("kt31923_wrong.kt")
public void testKt31923_wrong() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_wrong.kt");
}
@TestMetadata("kt3549.kt")
public void testKt3549() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt3549.kt");
@@ -10129,6 +10159,26 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/finally/loopAndFinally.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry.kt")
public void testNestedFinallyAndNonFinallyTry() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry2.kt")
public void testNestedFinallyAndNonFinallyTry2() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry2.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry3.kt")
public void testNestedFinallyAndNonFinallyTry3() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry3.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry4.kt")
public void testNestedFinallyAndNonFinallyTry4() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry4.kt");
}
@TestMetadata("notChainCatch.kt")
public void testNotChainCatch() throws Exception {
runTest("compiler/testData/codegen/box/finally/notChainCatch.kt");
@@ -10089,11 +10089,41 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/finally"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true);
}
@TestMetadata("breakAndOuterFinally.kt")
public void testBreakAndOuterFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/breakAndOuterFinally.kt");
}
@TestMetadata("continueAndOuterFinally.kt")
public void testContinueAndOuterFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/continueAndOuterFinally.kt");
}
@TestMetadata("finallyAndFinally.kt")
public void testFinallyAndFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/finallyAndFinally.kt");
}
@TestMetadata("kt31923_break.kt")
public void testKt31923_break() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_break.kt");
}
@TestMetadata("kt31923_continue.kt")
public void testKt31923_continue() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_continue.kt");
}
@TestMetadata("kt31923_return.kt")
public void testKt31923_return() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_return.kt");
}
@TestMetadata("kt31923_wrong.kt")
public void testKt31923_wrong() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_wrong.kt");
}
@TestMetadata("kt3549.kt")
public void testKt3549() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt3549.kt");
@@ -10129,6 +10159,26 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/finally/loopAndFinally.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry.kt")
public void testNestedFinallyAndNonFinallyTry() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry2.kt")
public void testNestedFinallyAndNonFinallyTry2() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry2.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry3.kt")
public void testNestedFinallyAndNonFinallyTry3() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry3.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry4.kt")
public void testNestedFinallyAndNonFinallyTry4() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry4.kt");
}
@TestMetadata("notChainCatch.kt")
public void testNotChainCatch() throws Exception {
runTest("compiler/testData/codegen/box/finally/notChainCatch.kt");
@@ -2433,6 +2433,16 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC
KotlinTestUtils.runTestWithCustomIgnoreDirective(this::doTest, TargetBackend.JVM_IR, testDataFilePath, "// IGNORE_BACKEND_MULTI_MODULE: ");
}
@TestMetadata("31929.kt")
public void test31929() throws Exception {
runTest("compiler/testData/codegen/boxInline/nonLocalReturns/tryFinally/exceptionTable/31929.kt");
}
@TestMetadata("31929_2.kt")
public void test31929_2() throws Exception {
runTest("compiler/testData/codegen/boxInline/nonLocalReturns/tryFinally/exceptionTable/31929_2.kt");
}
public void testAllFilesPresentInExceptionTable() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/nonLocalReturns/tryFinally/exceptionTable"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true);
}
@@ -2472,6 +2482,16 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC
runTest("compiler/testData/codegen/boxInline/nonLocalReturns/tryFinally/exceptionTable/innerAndExternalSimple.kt");
}
@TestMetadata("kt31923.kt")
public void testKt31923() throws Exception {
runTest("compiler/testData/codegen/boxInline/nonLocalReturns/tryFinally/exceptionTable/kt31923.kt");
}
@TestMetadata("kt31923_2.kt")
public void testKt31923_2() throws Exception {
runTest("compiler/testData/codegen/boxInline/nonLocalReturns/tryFinally/exceptionTable/kt31923_2.kt");
}
@TestMetadata("nested.kt")
public void testNested() throws Exception {
runTest("compiler/testData/codegen/boxInline/nonLocalReturns/tryFinally/exceptionTable/nested.kt");
@@ -115,6 +115,7 @@ enum class LanguageFeature(
ProhibitInvisibleAbstractMethodsInSuperclasses(KOTLIN_1_4, kind = BUG_FIX),
ProhibitNonReifiedArraysAsReifiedTypeArguments(KOTLIN_1_4, kind = BUG_FIX),
ProhibitProtectedCallFromInline(KOTLIN_1_4, kind = BUG_FIX),
ProperFinally(KOTLIN_1_4, kind = BUG_FIX),
ProperVisibilityForCompanionObjectInstanceField(sinceVersion = null, kind = BUG_FIX),
// Temporarily disabled, see KT-27084/KT-22379
@@ -8754,11 +8754,36 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/finally"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS_IR, true);
}
@TestMetadata("breakAndOuterFinally.kt")
public void testBreakAndOuterFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/breakAndOuterFinally.kt");
}
@TestMetadata("continueAndOuterFinally.kt")
public void testContinueAndOuterFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/continueAndOuterFinally.kt");
}
@TestMetadata("finallyAndFinally.kt")
public void testFinallyAndFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/finallyAndFinally.kt");
}
@TestMetadata("kt31923_break.kt")
public void testKt31923_break() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_break.kt");
}
@TestMetadata("kt31923_continue.kt")
public void testKt31923_continue() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_continue.kt");
}
@TestMetadata("kt31923_return.kt")
public void testKt31923_return() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_return.kt");
}
@TestMetadata("kt3549.kt")
public void testKt3549() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt3549.kt");
@@ -8794,6 +8819,26 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/finally/loopAndFinally.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry.kt")
public void testNestedFinallyAndNonFinallyTry() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry2.kt")
public void testNestedFinallyAndNonFinallyTry2() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry2.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry3.kt")
public void testNestedFinallyAndNonFinallyTry3() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry3.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry4.kt")
public void testNestedFinallyAndNonFinallyTry4() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry4.kt");
}
@TestMetadata("notChainCatch.kt")
public void testNotChainCatch() throws Exception {
runTest("compiler/testData/codegen/box/finally/notChainCatch.kt");
@@ -9829,11 +9829,36 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/finally"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
}
@TestMetadata("breakAndOuterFinally.kt")
public void testBreakAndOuterFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/breakAndOuterFinally.kt");
}
@TestMetadata("continueAndOuterFinally.kt")
public void testContinueAndOuterFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/continueAndOuterFinally.kt");
}
@TestMetadata("finallyAndFinally.kt")
public void testFinallyAndFinally() throws Exception {
runTest("compiler/testData/codegen/box/finally/finallyAndFinally.kt");
}
@TestMetadata("kt31923_break.kt")
public void testKt31923_break() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_break.kt");
}
@TestMetadata("kt31923_continue.kt")
public void testKt31923_continue() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_continue.kt");
}
@TestMetadata("kt31923_return.kt")
public void testKt31923_return() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt31923_return.kt");
}
@TestMetadata("kt3549.kt")
public void testKt3549() throws Exception {
runTest("compiler/testData/codegen/box/finally/kt3549.kt");
@@ -9869,6 +9894,26 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/finally/loopAndFinally.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry.kt")
public void testNestedFinallyAndNonFinallyTry() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry2.kt")
public void testNestedFinallyAndNonFinallyTry2() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry2.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry3.kt")
public void testNestedFinallyAndNonFinallyTry3() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry3.kt");
}
@TestMetadata("nestedFinallyAndNonFinallyTry4.kt")
public void testNestedFinallyAndNonFinallyTry4() throws Exception {
runTest("compiler/testData/codegen/box/finally/nestedFinallyAndNonFinallyTry4.kt");
}
@TestMetadata("notChainCatch.kt")
public void testNotChainCatch() throws Exception {
runTest("compiler/testData/codegen/box/finally/notChainCatch.kt");