diff --git a/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinExpressionParsing.java b/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinExpressionParsing.java index ffd17253398..fa536ccdfb8 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinExpressionParsing.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinExpressionParsing.java @@ -1484,7 +1484,8 @@ public class KotlinExpressionParsing extends AbstractKotlinParsing { } thenBranch.done(THEN); - if (at(ELSE_KEYWORD)) { + // lookahead for arrow is needed to prevent capturing of whenEntry like "else -> " + if (at(ELSE_KEYWORD) && lookahead(1) != ARROW) { advance(); // ELSE_KEYWORD PsiBuilder.Marker elseBranch = mark(); diff --git a/compiler/testData/psi/When.kt b/compiler/testData/psi/When.kt index 268723bf3ed..4d709cfb8fc 100644 --- a/compiler/testData/psi/When.kt +++ b/compiler/testData/psi/When.kt @@ -67,3 +67,10 @@ fun whenWithoutCondition(i : Int) { else -> 2 } } + +fun ifDoesntCaptureElse(x : Int) { + when (x) { + 2 -> if(1) 3 + else -> 6 + } +} \ No newline at end of file diff --git a/compiler/testData/psi/When.txt b/compiler/testData/psi/When.txt index a9128723eb5..c3f79f49ebe 100644 --- a/compiler/testData/psi/When.txt +++ b/compiler/testData/psi/When.txt @@ -755,3 +755,64 @@ JetFile: When.kt PsiElement(RBRACE)('}') PsiWhiteSpace('\n') PsiElement(RBRACE)('}') + PsiWhiteSpace('\n\n') + FUN + PsiElement(fun)('fun') + PsiWhiteSpace(' ') + PsiElement(IDENTIFIER)('ifDoesntCaptureElse') + VALUE_PARAMETER_LIST + PsiElement(LPAR)('(') + VALUE_PARAMETER + PsiElement(IDENTIFIER)('x') + PsiWhiteSpace(' ') + PsiElement(COLON)(':') + PsiWhiteSpace(' ') + TYPE_REFERENCE + USER_TYPE + REFERENCE_EXPRESSION + PsiElement(IDENTIFIER)('Int') + PsiElement(RPAR)(')') + PsiWhiteSpace(' ') + BLOCK + PsiElement(LBRACE)('{') + PsiWhiteSpace('\n ') + WHEN + PsiElement(when)('when') + PsiWhiteSpace(' ') + PsiElement(LPAR)('(') + REFERENCE_EXPRESSION + PsiElement(IDENTIFIER)('x') + PsiElement(RPAR)(')') + PsiWhiteSpace(' ') + PsiElement(LBRACE)('{') + PsiWhiteSpace('\n ') + WHEN_ENTRY + WHEN_CONDITION_WITH_EXPRESSION + INTEGER_CONSTANT + PsiElement(INTEGER_LITERAL)('2') + PsiWhiteSpace(' ') + PsiElement(ARROW)('->') + PsiWhiteSpace(' ') + IF + PsiElement(if)('if') + PsiElement(LPAR)('(') + CONDITION + INTEGER_CONSTANT + PsiElement(INTEGER_LITERAL)('1') + PsiElement(RPAR)(')') + PsiWhiteSpace(' ') + THEN + INTEGER_CONSTANT + PsiElement(INTEGER_LITERAL)('3') + PsiWhiteSpace('\n ') + WHEN_ENTRY + PsiElement(else)('else') + PsiWhiteSpace(' ') + PsiElement(ARROW)('->') + PsiWhiteSpace(' ') + INTEGER_CONSTANT + PsiElement(INTEGER_LITERAL)('6') + PsiWhiteSpace('\n ') + PsiElement(RBRACE)('}') + PsiWhiteSpace('\n') + PsiElement(RBRACE)('}') \ No newline at end of file