diff --git a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java index e7da6a2ca2e..5cc7afca015 100644 --- a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java +++ b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java @@ -259,6 +259,11 @@ public class LazyBodyIsNotTouchedTilContractsPhaseTestGenerated extends Abstract runTest("compiler/fir/analysis-tests/testData/resolve/labelAndReceiverForInfix.kt"); } + @TestMetadata("labeledReturnFromNotLabeledUnnamedFunction.kt") + public void testLabeledReturnFromNotLabeledUnnamedFunction() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/labeledReturnFromNotLabeledUnnamedFunction.kt"); + } + @TestMetadata("lambdaArgInScopeFunction.kt") public void testLambdaArgInScopeFunction() throws Exception { runTest("compiler/fir/analysis-tests/testData/resolve/lambdaArgInScopeFunction.kt"); diff --git a/compiler/fir/analysis-tests/testData/resolve/functionTypes.kt b/compiler/fir/analysis-tests/testData/resolve/functionTypes.kt index d147e89102c..c129c99f8b2 100644 --- a/compiler/fir/analysis-tests/testData/resolve/functionTypes.kt +++ b/compiler/fir/analysis-tests/testData/resolve/functionTypes.kt @@ -1,4 +1,4 @@ -fun simpleRun(f: (T) -> Unit): Unit = f(return) +fun simpleRun(f: (T) -> Unit): Unit = f(return) fun List.simpleMap(f: (T) -> R): R { diff --git a/compiler/fir/analysis-tests/testData/resolve/labeledReturnFromNotLabeledUnnamedFunction.fir.txt b/compiler/fir/analysis-tests/testData/resolve/labeledReturnFromNotLabeledUnnamedFunction.fir.txt new file mode 100644 index 00000000000..86ec4e3a2a4 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/labeledReturnFromNotLabeledUnnamedFunction.fir.txt @@ -0,0 +1,10 @@ +FILE: labeledReturnFromNotLabeledUnnamedFunction.kt + public final fun notInline(block: R|(kotlin/Boolean) -> kotlin/Unit|): R|kotlin/String| { + ^notInline String() + } + public final fun test(): R|kotlin/String| { + ^test R|/notInline|(fun (b: R|kotlin/Boolean|): R|kotlin/Unit| { + ^@notInline Unit + } + ) + } diff --git a/compiler/fir/analysis-tests/testData/resolve/labeledReturnFromNotLabeledUnnamedFunction.kt b/compiler/fir/analysis-tests/testData/resolve/labeledReturnFromNotLabeledUnnamedFunction.kt new file mode 100644 index 00000000000..f0f18b3861a --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/labeledReturnFromNotLabeledUnnamedFunction.kt @@ -0,0 +1,9 @@ +fun notInline(block: (Boolean) -> Unit): String { + return "" +} + +fun test(): String { + return notInline(fun(b: Boolean) { + return@notInline + }) +} diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java index e26b4bbbe6e..87183b4460f 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java @@ -302,6 +302,12 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest { runTest("compiler/fir/analysis-tests/testData/resolve/labelAndReceiverForInfix.kt"); } + @Test + @TestMetadata("labeledReturnFromNotLabeledUnnamedFunction.kt") + public void testLabeledReturnFromNotLabeledUnnamedFunction() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/labeledReturnFromNotLabeledUnnamedFunction.kt"); + } + @Test @TestMetadata("lambdaArgInScopeFunction.kt") public void testLambdaArgInScopeFunction() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java index 2c46833fd02..9f40c1aacd1 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java @@ -305,6 +305,12 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolve/labelAndReceiverForInfix.kt"); } + @Test + @TestMetadata("labeledReturnFromNotLabeledUnnamedFunction.kt") + public void testLabeledReturnFromNotLabeledUnnamedFunction() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/labeledReturnFromNotLabeledUnnamedFunction.kt"); + } + @Test @TestMetadata("lambdaArgInScopeFunction.kt") public void testLambdaArgInScopeFunction() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index cc613311df9..51f26dfd35f 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -34413,6 +34413,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/inline"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @Test + @TestMetadata("inlineConstructorOfArray.kt") + public void testInlineConstructorOfArray() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/inline/inlineConstructorOfArray.kt"); + } + @Test @TestMetadata("inlineOnlySuppressesNothingToInline.kt") public void testInlineOnlySuppressesNothingToInline() throws Exception { diff --git a/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/DiagnosticData.kt b/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/DiagnosticData.kt index 189e0543e48..0ab4cd5db9b 100644 --- a/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/DiagnosticData.kt +++ b/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/DiagnosticData.kt @@ -54,7 +54,7 @@ enum class PositioningStrategy(private val strategy: String? = null) { NAME_OF_NAMED_ARGUMENT, VALUE_ARGUMENTS, SUPERTYPES_LIST, - RETURN_KEYWORD, + RETURN_WITH_LABEL, ; diff --git a/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt b/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt index 24da511ad95..c0aa8a85d6b 100644 --- a/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt +++ b/compiler/fir/checkers/checkers-component-generator/src/org/jetbrains/kotlin/fir/checkers/generator/diagnostics/FirDiagnosticsList.kt @@ -59,7 +59,6 @@ object DIAGNOSTICS_LIST : DiagnosticList() { val BREAK_OR_CONTINUE_OUTSIDE_A_LOOP by error() val NOT_A_LOOP_LABEL by error() val VARIABLE_EXPECTED by error() - val RETURN_NOT_ALLOWED by error() val DELEGATION_IN_INTERFACE by error() val NESTED_CLASS_NOT_ALLOWED by error(PositioningStrategy.DECLARATION_NAME) { parameter("declaration") @@ -596,6 +595,11 @@ object DIAGNOSTICS_LIST : DiagnosticList() { val VARIABLE_NEVER_READ by warning(PositioningStrategy.DECLARATION_NAME) val USELESS_CALL_ON_NOT_NULL by warning(PositioningStrategy.SELECTOR_BY_QUALIFIED) } + + val RETURNS by object : DiagnosticGroup("Returns") { + val RETURN_NOT_ALLOWED by error(PositioningStrategy.RETURN_WITH_LABEL) + val RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY by error(PositioningStrategy.RETURN_WITH_LABEL) + } } private val exposedVisibilityDiagnosticInit: DiagnosticBuilder.() -> Unit = { diff --git a/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt b/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt index 6bf8ea98baa..07b090d150e 100644 --- a/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt +++ b/compiler/fir/checkers/gen/org/jetbrains/kotlin/fir/analysis/diagnostics/FirErrors.kt @@ -46,6 +46,7 @@ import org.jetbrains.kotlin.psi.KtPrimaryConstructor import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtPropertyAccessor import org.jetbrains.kotlin.psi.KtPropertyDelegate +import org.jetbrains.kotlin.psi.KtReturnExpression import org.jetbrains.kotlin.psi.KtSimpleNameExpression import org.jetbrains.kotlin.psi.KtTypeAlias import org.jetbrains.kotlin.psi.KtTypeParameter @@ -76,7 +77,6 @@ object FirErrors { val BREAK_OR_CONTINUE_OUTSIDE_A_LOOP by error0() val NOT_A_LOOP_LABEL by error0() val VARIABLE_EXPECTED by error0() - val RETURN_NOT_ALLOWED by error0() val DELEGATION_IN_INTERFACE by error0() val NESTED_CLASS_NOT_ALLOWED by error1(SourceElementPositioningStrategies.DECLARATION_NAME) @@ -359,4 +359,8 @@ object FirErrors { val VARIABLE_NEVER_READ by warning0(SourceElementPositioningStrategies.DECLARATION_NAME) val USELESS_CALL_ON_NOT_NULL by warning0(SourceElementPositioningStrategies.SELECTOR_BY_QUALIFIED) + // Returns + val RETURN_NOT_ALLOWED by error0(SourceElementPositioningStrategies.RETURN_WITH_LABEL) + val RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY by error0(SourceElementPositioningStrategies.RETURN_WITH_LABEL) + } diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirReturnAllowedChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirReturnAllowedChecker.kt new file mode 100644 index 00000000000..fcb19009525 --- /dev/null +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirReturnAllowedChecker.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * 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.fir.analysis.checkers.expression + +import org.jetbrains.kotlin.fir.FirFakeSourceElementKind +import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext +import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter +import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors +import org.jetbrains.kotlin.fir.analysis.diagnostics.reportOn +import org.jetbrains.kotlin.fir.declarations.* +import org.jetbrains.kotlin.fir.expressions.FirReturnExpression +import org.jetbrains.kotlin.fir.expressions.impl.FirSingleExpressionBlock +import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol + +object FirReturnAllowedChecker : FirReturnExpressionChecker() { + override fun check(expression: FirReturnExpression, context: CheckerContext, reporter: DiagnosticReporter) { + val source = expression.source + if (source?.kind == FirFakeSourceElementKind.ImplicitReturn) return + + val targetSymbol = expression.target.labeledElement.symbol + + if (!isReturnAllowed(targetSymbol, context)) { + reporter.reportOn(source, FirErrors.RETURN_NOT_ALLOWED, context) + } + + val containingDeclaration = context.containingDeclarations.last() + if (containingDeclaration is FirFunction<*> && containingDeclaration.body is FirSingleExpressionBlock) { + reporter.reportOn(source, FirErrors.RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY, context) + } + } + + private fun isReturnAllowed(targetSymbol: FirFunctionSymbol<*>, context: CheckerContext): Boolean { + for (containingDeclaration in context.containingDeclarations.asReversed()) { + when (containingDeclaration) { + // return from member of local class or anonymous object + is FirClass<*> -> return false + is FirFunction<*> -> { + when { + containingDeclaration.symbol == targetSymbol -> return true + containingDeclaration is FirAnonymousFunction -> { + if (!containingDeclaration.inlineStatus.returnAllowed) return false + } + else -> return false + } + } + is FirProperty -> if (!containingDeclaration.isLocal) return false + is FirValueParameter -> return true + } + } + return true + } +} diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/FirDefaultErrorMessages.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/FirDefaultErrorMessages.kt index 3618fe194d3..8754a6331b6 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/FirDefaultErrorMessages.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/FirDefaultErrorMessages.kt @@ -184,6 +184,7 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.REDUNDANT_VISIBIL import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.REIFIED_TYPE_IN_CATCH_CLAUSE import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.REPEATED_MODIFIER import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.RESERVED_MEMBER_INSIDE_INLINE_CLASS +import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.RETURN_NOT_ALLOWED import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.RETURN_TYPE_MISMATCH_ON_OVERRIDE import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.SEALED_CLASS_CONSTRUCTOR_CALL @@ -274,7 +275,6 @@ class FirDefaultErrorMessages : DefaultErrorMessages.Extension { map.put(BREAK_OR_CONTINUE_OUTSIDE_A_LOOP, "'break' and 'continue' are only allowed inside a loop") map.put(NOT_A_LOOP_LABEL, "The label does not denote a loop") // * map.put(VARIABLE_EXPECTED, "Variable expected") - map.put(RETURN_NOT_ALLOWED, "'return' is not allowed here") map.put(DELEGATION_IN_INTERFACE, "Interfaces cannot use delegation") map.put(NESTED_CLASS_NOT_ALLOWED, "{0} is not allowed here", TO_STRING) @@ -742,6 +742,10 @@ class FirDefaultErrorMessages : DefaultErrorMessages.Extension { // Type alias map.put(TOPLEVEL_TYPEALIASES_ONLY, "Nested and local type aliases are not supported") + // Returns + map.put(RETURN_NOT_ALLOWED, "'return' is not allowed here") + map.put(RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY, "Returns are not allowed for functions with expression body. Use block body in '{...}'") + // Extended checkers group map.put(REDUNDANT_VISIBILITY_MODIFIER, "Redundant visibility modifier") map.put(REDUNDANT_MODALITY_MODIFIER, "Redundant modality modifier") diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/LightTreePositioningStrategies.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/LightTreePositioningStrategies.kt index cd7ef79245d..6f554beab6f 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/LightTreePositioningStrategies.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/LightTreePositioningStrategies.kt @@ -497,13 +497,17 @@ object LightTreePositioningStrategies { } } - val RETURN_KEYWORD = object : LightTreePositioningStrategy() { + val RETURN_WITH_LABEL = object : LightTreePositioningStrategy() { override fun mark( node: LighterASTNode, startOffset: Int, endOffset: Int, tree: FlyweightCapableTreeStructure ): List { + val labeledExpression = tree.findChildByType(node, KtNodeTypes.LABEL_QUALIFIER) + if (labeledExpression != null) { + return markRange(node, labeledExpression, startOffset, endOffset, tree, node) + } return markElement(tree.returnKeyword(node) ?: node, startOffset, endOffset, tree) } } diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/SourceElementPositioningStrategies.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/SourceElementPositioningStrategies.kt index 8aa5b4273cc..fbb8dda6919 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/SourceElementPositioningStrategies.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/SourceElementPositioningStrategies.kt @@ -168,8 +168,8 @@ object SourceElementPositioningStrategies { PositioningStrategies.ARRAY_ACCESS ) - val RETURN_KEYWORD = SourceElementPositioningStrategy( - LightTreePositioningStrategies.RETURN_KEYWORD, - PositioningStrategies.RETURN_KEYWORD + val RETURN_WITH_LABEL = SourceElementPositioningStrategy( + LightTreePositioningStrategies.RETURN_WITH_LABEL, + PositioningStrategies.RETURN_WITH_LABEL ) } diff --git a/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/checkers/CommonExpressionCheckers.kt b/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/checkers/CommonExpressionCheckers.kt index f225692fe90..07907307d77 100644 --- a/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/checkers/CommonExpressionCheckers.kt +++ b/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/checkers/CommonExpressionCheckers.kt @@ -42,4 +42,8 @@ object CommonExpressionCheckers : ExpressionCheckers() { override val whenExpressionCheckers: Set = setOf( FirExhaustiveWhenChecker ) + + override val returnExpressionCheckers: Set = setOf( + FirReturnAllowedChecker + ) } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt index 30138e7a5b4..59a0eeff0d2 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt @@ -421,12 +421,6 @@ object PositioningStrategies { } } - val RETURN_KEYWORD: PositioningStrategy = object : PositioningStrategy() { - override fun mark(element: KtReturnExpression): List { - return markElement(element.returnKeyword) - } - } - private class ModifierSetBasedPositioningStrategy(private val modifierSet: TokenSet) : PositioningStrategy() { override fun mark(element: KtModifierListOwner): List { val modifierList = element.modifierList diff --git a/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt b/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt index 8a956ae6273..ed8c4e2930c 100644 --- a/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt +++ b/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt @@ -8,12 +8,12 @@ fun unitEmpty() : Unit {} fun unitEmptyReturn() : Unit {return} fun unitIntReturn() : Unit {return 1} fun unitUnitReturn() : Unit {return Unit} -fun test1() : Any = {return} +fun test1() : Any = {return} fun test2() : Any = a@ {return@a 1} fun test3() : Any { return } -fun test4(): ()-> Unit = { return@test4 } +fun test4(): ()-> Unit = { return@test4 } fun test5(): Any = l@{ return@l } -fun test6(): Any = {return 1} +fun test6(): Any = {return 1} fun bbb() { return 1 @@ -137,7 +137,7 @@ fun blockNoReturnIfUnitInOneBranch(): Int { fun nonBlockReturnIfEmptyIf(): Int = if (1 < 2) {} else {} fun nonBlockNoReturnIfUnitInOneBranch(): Int = if (1 < 2) {} else 2 -val a = return 1 +val a = return 1 class A() { } diff --git a/compiler/testData/diagnostics/tests/Return.fir.kt b/compiler/testData/diagnostics/tests/Return.fir.kt index 747f383243e..dc27987692e 100644 --- a/compiler/testData/diagnostics/tests/Return.fir.kt +++ b/compiler/testData/diagnostics/tests/Return.fir.kt @@ -6,12 +6,12 @@ class A { if (1 < 2) return@inner else - return@outer + return@outer } if (1 < 2) - return@A + return@A else if (2 < 3) - return@inner + return@inner return@outer } } diff --git a/compiler/testData/diagnostics/tests/ReturnInFunctionWithoutBody.fir.kt b/compiler/testData/diagnostics/tests/ReturnInFunctionWithoutBody.fir.kt index d2a51a83105..6e38c369152 100644 --- a/compiler/testData/diagnostics/tests/ReturnInFunctionWithoutBody.fir.kt +++ b/compiler/testData/diagnostics/tests/ReturnInFunctionWithoutBody.fir.kt @@ -1,3 +1,3 @@ -fun foo1(): () -> String = return { "some long expression "} -fun foo2(): () -> String = return@label { "some long expression "} -fun foo3(): () -> String = return@ { "some long expression "} \ No newline at end of file +fun foo1(): () -> String = return { "some long expression "} +fun foo2(): () -> String = return@label { "some long expression "} +fun foo3(): () -> String = return@ { "some long expression "} diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/kt10823.fir.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/kt10823.fir.kt index 14ffce99161..62ad22dcff4 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/kt10823.fir.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/kt10823.fir.kt @@ -1,6 +1,6 @@ fun find2(): Any? { fun visit(element: Any) { - return@find2 element + return@find2 element } return null } @@ -10,7 +10,7 @@ fun find2(): Any? { fun find(): Any? { object : Any() { fun visit(element: Any) { - return@find element + return@find element } } return null @@ -18,7 +18,7 @@ fun find(): Any? { fun find4(): Any? { inline fun visit(element: Any) { - return@find4 element + return@find4 element } return null } @@ -26,7 +26,7 @@ fun find4(): Any? { fun find3(): Any? { object : Any() { inline fun visit(element: Any) { - return@find3 element + return@find3 element } } return null diff --git a/compiler/testData/diagnostics/tests/controlStructures/kt799.fir.kt b/compiler/testData/diagnostics/tests/controlStructures/kt799.fir.kt index 014d78806cc..9035778dd09 100644 --- a/compiler/testData/diagnostics/tests/controlStructures/kt799.fir.kt +++ b/compiler/testData/diagnostics/tests/controlStructures/kt799.fir.kt @@ -12,11 +12,11 @@ fun test() { doSmth(if (true) 3 else return, 1) } -val a : Nothing = return 1 +val a : Nothing = return 1 -val b = return 1 +val b = return 1 -val c = doSmth(if (true) 3 else return) +val c = doSmth(if (true) 3 else return) fun f(mi: Int = if (true) 0 else return) {} diff --git a/compiler/testData/diagnostics/tests/controlStructures/localReturnInsidePropertyAccessor.fir.kt b/compiler/testData/diagnostics/tests/controlStructures/localReturnInsidePropertyAccessor.fir.kt index 310a467d293..5e7e19ac22c 100644 --- a/compiler/testData/diagnostics/tests/controlStructures/localReturnInsidePropertyAccessor.fir.kt +++ b/compiler/testData/diagnostics/tests/controlStructures/localReturnInsidePropertyAccessor.fir.kt @@ -10,11 +10,11 @@ fun f() = object : ClassData { fun g() = object : ClassData { init { if (true) { - return 0 + return 0 } } fun some(): Int { return 6 } -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/controlStructures/notAFunctionLabel_after.fir.kt b/compiler/testData/diagnostics/tests/controlStructures/notAFunctionLabel_after.fir.kt index fd20b9f612b..6b7fd939a6b 100644 --- a/compiler/testData/diagnostics/tests/controlStructures/notAFunctionLabel_after.fir.kt +++ b/compiler/testData/diagnostics/tests/controlStructures/notAFunctionLabel_after.fir.kt @@ -31,7 +31,7 @@ fun testAnnotatedLambdaLabel() = fun testLambdaMultipleLabels1() = lambda1@ lambda2@ { - return@lambda1 + return@lambda1 } fun testLambdaMultipleLabels2() = @@ -46,7 +46,7 @@ fun testAnonymousFunctionLabel() = fun testLoopLabelInReturn(xs: List) { L@ for (x in xs) { - if (x > 0) return@L + if (x > 0) return@L } } @@ -59,4 +59,4 @@ fun testHighOrderFunctionCallLabelInReturn() { L@ run { return@L } -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/controlStructures/notAFunctionLabel_before.fir.kt b/compiler/testData/diagnostics/tests/controlStructures/notAFunctionLabel_before.fir.kt index 1a09741d73d..bd3a1979a04 100644 --- a/compiler/testData/diagnostics/tests/controlStructures/notAFunctionLabel_before.fir.kt +++ b/compiler/testData/diagnostics/tests/controlStructures/notAFunctionLabel_before.fir.kt @@ -31,7 +31,7 @@ fun testAnnotatedLambdaLabel() = fun testLambdaMultipleLabels1() = lambda1@ lambda2@ { - return@lambda1 + return@lambda1 } fun testLambdaMultipleLabels2() = @@ -46,7 +46,7 @@ fun testAnonymousFunctionLabel() = fun testLoopLabelInReturn(xs: List) { L@ for (x in xs) { - if (x > 0) return@L + if (x > 0) return@L } } @@ -59,4 +59,4 @@ fun testHighOrderFunctionCallLabelInReturn() { L@ run { return@L } -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/declarationChecks/ScalaLikeNamedFun.fir.kt b/compiler/testData/diagnostics/tests/declarationChecks/ScalaLikeNamedFun.fir.kt index 1277796ace5..7c93a338394 100644 --- a/compiler/testData/diagnostics/tests/declarationChecks/ScalaLikeNamedFun.fir.kt +++ b/compiler/testData/diagnostics/tests/declarationChecks/ScalaLikeNamedFun.fir.kt @@ -1,34 +1,34 @@ // !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE // KT-5068 Add special error for scala-like syntax 'fun foo(): Int = { 1 }' -fun test1(): Int = { return 1 } +fun test1(): Int = { return 1 } fun test2(): Int = { 1 } -val test3: () -> Int = fun (): Int = { return 1 } +val test3: () -> Int = fun (): Int = { return 1 } val test4: () -> Int = fun (): Int = { 1 } fun test5(): Int { return { 1 } } fun test6(): Int = fun (): Int = 1 fun outer() { - fun test1(): Int = { return 1 } + fun test1(): Int = { return 1 } fun test2(): Int = { 1 } - val test3: () -> Int = fun (): Int = { return 1 } + val test3: () -> Int = fun (): Int = { return 1 } val test4: () -> Int = fun (): Int = { 1 } fun test5(): Int { return { 1 } } fun test6(): Int = fun (): Int = 1 } class Outer { - fun test1(): Int = { return 1 } + fun test1(): Int = { return 1 } fun test2(): Int = { 1 } - val test3: () -> Int = fun (): Int = { return 1 } + val test3: () -> Int = fun (): Int = { return 1 } val test4: () -> Int = fun (): Int = { 1 } fun test5(): Int { return { 1 } } fun test6(): Int = fun (): Int = 1 class Nested { - fun test1(): Int = { return 1 } + fun test1(): Int = { return 1 } fun test2(): Int = { 1 } - val test3: () -> Int = fun (): Int = { return 1 } + val test3: () -> Int = fun (): Int = { return 1 } val test4: () -> Int = fun (): Int = { 1 } fun test5(): Int { return { 1 } } fun test6(): Int = fun (): Int = 1 diff --git a/compiler/testData/diagnostics/tests/functionAsExpression/AutoLabels.fir.kt b/compiler/testData/diagnostics/tests/functionAsExpression/AutoLabels.fir.kt index 1b2f22647d0..967679cfa31 100644 --- a/compiler/testData/diagnostics/tests/functionAsExpression/AutoLabels.fir.kt +++ b/compiler/testData/diagnostics/tests/functionAsExpression/AutoLabels.fir.kt @@ -7,5 +7,5 @@ fun autolabel(l: List) = l.map (fun (i: Int): Int { }) fun unresolvedMapLabel(l: List) = l.map (l@ fun(i: Int): Int { - return@map 4 + return@map 4 }) diff --git a/compiler/testData/diagnostics/tests/functionAsExpression/ForbiddenNonLocalReturn.fir.kt b/compiler/testData/diagnostics/tests/functionAsExpression/ForbiddenNonLocalReturn.fir.kt deleted file mode 100644 index f7936c4f4e3..00000000000 --- a/compiler/testData/diagnostics/tests/functionAsExpression/ForbiddenNonLocalReturn.fir.kt +++ /dev/null @@ -1,22 +0,0 @@ -// !DIAGNOSTICS: -UNUSED_VARIABLE - -fun test() { - fun bar() { - val bas = fun() { - return@bar - } - } - - val bar = fun() { - return@test - } -} - -fun foo() { - val bal = bag@ fun () { - val bar = fun() { - return@bag - } - return@bag - } -} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/functionAsExpression/ForbiddenNonLocalReturn.kt b/compiler/testData/diagnostics/tests/functionAsExpression/ForbiddenNonLocalReturn.kt index 274d3e57aa1..bbc3a489d07 100644 --- a/compiler/testData/diagnostics/tests/functionAsExpression/ForbiddenNonLocalReturn.kt +++ b/compiler/testData/diagnostics/tests/functionAsExpression/ForbiddenNonLocalReturn.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_VARIABLE fun test() { diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/AutoLabelsNonLocal.fir.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/AutoLabelsNonLocal.fir.kt deleted file mode 100644 index ddf260e5d7b..00000000000 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/AutoLabelsNonLocal.fir.kt +++ /dev/null @@ -1,11 +0,0 @@ -fun f() { - foo { - bar { - return@foo 1 - } - return@foo 1 - } -} - -fun foo(a: Any) {} -fun bar(a: Any) {} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/AutoLabelsNonLocal.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/AutoLabelsNonLocal.kt index 10c61dc4e87..b4ab24ddbf4 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/AutoLabelsNonLocal.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/AutoLabelsNonLocal.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL fun f() { foo { bar { diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/ForbiddenNonLocalReturnNoType.fir.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/ForbiddenNonLocalReturnNoType.fir.kt deleted file mode 100644 index 921de7f2663..00000000000 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/ForbiddenNonLocalReturnNoType.fir.kt +++ /dev/null @@ -1,19 +0,0 @@ -// !WITH_NEW_INFERENCE -fun test() { - run {return} - run {} -} - -fun test2() { - run {return@test2} - run {} -} - -fun test3() { - fun test4() { - run {return@test3} - run {} - } -} - -fun run(f: () -> T): T { return f() } diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/ForbiddenNonLocalReturnNoType.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/ForbiddenNonLocalReturnNoType.kt index 0ad96640de8..a02e3ec1010 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/ForbiddenNonLocalReturnNoType.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/ForbiddenNonLocalReturnNoType.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !WITH_NEW_INFERENCE fun test() { run {return} diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalAndNonLocalReturnInLambda.fir.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalAndNonLocalReturnInLambda.fir.kt deleted file mode 100644 index dc0204cf421..00000000000 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalAndNonLocalReturnInLambda.fir.kt +++ /dev/null @@ -1,11 +0,0 @@ -// !CHECK_TYPE - -fun test2(a: Int) { - val x = run f@{ - if (a > 0) return - return@f 1 - } - checkSubtype(x) -} - -fun run(f: () -> T): T { return f() } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalAndNonLocalReturnInLambda.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalAndNonLocalReturnInLambda.kt index a1534478ae8..6fdec72c883 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalAndNonLocalReturnInLambda.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalAndNonLocalReturnInLambda.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !CHECK_TYPE fun test2(a: Int) { diff --git a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/labeledReturn.fir.kt b/compiler/testData/diagnostics/tests/inline/nonLocalReturns/labeledReturn.fir.kt deleted file mode 100644 index 4b290879ed1..00000000000 --- a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/labeledReturn.fir.kt +++ /dev/null @@ -1,31 +0,0 @@ -fun inlineCall(): String { - inlineFun { - if (true) { - return@inlineCall "" - } - 1 - } - - return "x" -} - -inline fun inlineFun(s: ()->Int) { - s() -} - - -fun noInlineCall(): String { - noInline { - if (true) { - return@noInlineCall "" - } - 1 - } - - return "x" -} - - -fun noInline(s: ()->Int) { - s() -} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/labeledReturn.kt b/compiler/testData/diagnostics/tests/inline/nonLocalReturns/labeledReturn.kt index c55fd323b4d..a615cc7926c 100644 --- a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/labeledReturn.kt +++ b/compiler/testData/diagnostics/tests/inline/nonLocalReturns/labeledReturn.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL fun inlineCall(): String { inlineFun { if (true) { diff --git a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/noInlineAnnotation.fir.kt b/compiler/testData/diagnostics/tests/inline/nonLocalReturns/noInlineAnnotation.fir.kt deleted file mode 100644 index f6a5b2ec051..00000000000 --- a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/noInlineAnnotation.fir.kt +++ /dev/null @@ -1,10 +0,0 @@ -// !DIAGNOSTICS: -NOTHING_TO_INLINE -fun main() { - test { - return - } -} - -inline fun test(noinline lambda: () -> Unit) { - lambda() -} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/noInlineAnnotation.kt b/compiler/testData/diagnostics/tests/inline/nonLocalReturns/noInlineAnnotation.kt index 7f79214b51f..84330704eac 100644 --- a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/noInlineAnnotation.kt +++ b/compiler/testData/diagnostics/tests/inline/nonLocalReturns/noInlineAnnotation.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !DIAGNOSTICS: -NOTHING_TO_INLINE fun main() { test { diff --git a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/onlyLocalReturnLambda.fir.kt b/compiler/testData/diagnostics/tests/inline/nonLocalReturns/onlyLocalReturnLambda.fir.kt deleted file mode 100644 index c4419fdb152..00000000000 --- a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/onlyLocalReturnLambda.fir.kt +++ /dev/null @@ -1,43 +0,0 @@ -// !DIAGNOSTICS: -UNUSED_EXPRESSION -UNUSED_PARAMETER -UNUSED_VARIABLE -NOTHING_TO_INLINE - -fun fun1(p: () -> R) { - inlineFun { - p() - } -} - -fun fun1ValueArgument(p: () -> R) { - inlineFun ({ - p() - }) -} - -fun fun3(p: () -> R) { - inlineFun { - return; - } -} - -fun fun3ValueArgument(p: () -> R) { - inlineFun ({ - return; - }) -} - - -fun fun4(p: () -> R) { - inlineFun lambda@ { - return@lambda p(); - } -} - -fun fun4ValueArgument(p: () -> R) { - inlineFun (lambda@ { - return@lambda p(); - }) -} - - -inline fun inlineFun(crossinline p: () -> R) { - p() -} diff --git a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/onlyLocalReturnLambda.kt b/compiler/testData/diagnostics/tests/inline/nonLocalReturns/onlyLocalReturnLambda.kt index 6930354fd0e..26a87ebd164 100644 --- a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/onlyLocalReturnLambda.kt +++ b/compiler/testData/diagnostics/tests/inline/nonLocalReturns/onlyLocalReturnLambda.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_EXPRESSION -UNUSED_PARAMETER -UNUSED_VARIABLE -NOTHING_TO_INLINE fun fun1(p: () -> R) { diff --git a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/onlyLocalReturnLambdaBinaryExpr.fir.kt b/compiler/testData/diagnostics/tests/inline/nonLocalReturns/onlyLocalReturnLambdaBinaryExpr.fir.kt deleted file mode 100644 index cf5ca794851..00000000000 --- a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/onlyLocalReturnLambdaBinaryExpr.fir.kt +++ /dev/null @@ -1,25 +0,0 @@ -// !DIAGNOSTICS: -UNUSED_EXPRESSION -UNUSED_PARAMETER -UNUSED_VARIABLE -NOTHING_TO_INLINE - -class Z { - inline infix fun inlineFun(crossinline p: () -> R) { - p() - } -} - -fun fun1(p: () -> R) { - Z() inlineFun { - p() - } -} - -fun fun3(p: () -> R) { - Z() inlineFun { - return; - } -} - -fun fun4(p: () -> R) { - Z() inlineFun lambda@ { - return@lambda p(); - } -} diff --git a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/onlyLocalReturnLambdaBinaryExpr.kt b/compiler/testData/diagnostics/tests/inline/nonLocalReturns/onlyLocalReturnLambdaBinaryExpr.kt index 91a244fe12e..e9e2f5317c7 100644 --- a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/onlyLocalReturnLambdaBinaryExpr.kt +++ b/compiler/testData/diagnostics/tests/inline/nonLocalReturns/onlyLocalReturnLambdaBinaryExpr.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_EXPRESSION -UNUSED_PARAMETER -UNUSED_VARIABLE -NOTHING_TO_INLINE class Z { diff --git a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/propertyAccessorsAndConstructor.fir.kt b/compiler/testData/diagnostics/tests/inline/nonLocalReturns/propertyAccessorsAndConstructor.fir.kt deleted file mode 100644 index d5449715989..00000000000 --- a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/propertyAccessorsAndConstructor.fir.kt +++ /dev/null @@ -1,24 +0,0 @@ -inline fun doCall(p: () -> R) { - p() -} - -inline fun doCallInt(p: () -> R): R { - return p() -} - -class A { - var result: Int = doCallInt { return this }; - - var field: Int - get() { - doCall { return 1 } - return 2 - } - set(v: Int) { - doCall { - result = v / 2 - return - } - result = v - } -} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/propertyAccessorsAndConstructor.kt b/compiler/testData/diagnostics/tests/inline/nonLocalReturns/propertyAccessorsAndConstructor.kt index 0e91c0c2736..f216ffaf47f 100644 --- a/compiler/testData/diagnostics/tests/inline/nonLocalReturns/propertyAccessorsAndConstructor.kt +++ b/compiler/testData/diagnostics/tests/inline/nonLocalReturns/propertyAccessorsAndConstructor.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL inline fun doCall(p: () -> R) { p() } diff --git a/compiler/testData/diagnostics/tests/labels/kt361.fir.kt b/compiler/testData/diagnostics/tests/labels/kt361.fir.kt deleted file mode 100644 index 18f66608df9..00000000000 --- a/compiler/testData/diagnostics/tests/labels/kt361.fir.kt +++ /dev/null @@ -1,11 +0,0 @@ -fun nonlocals(b : Boolean) { - a@{ - fun foo() { - if (b) { - return@a 1 // The label must be resolved, but an error should be reported for a non-local return - } - } - - return@a 5 - } -} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/labels/kt361.kt b/compiler/testData/diagnostics/tests/labels/kt361.kt index f6025937086..aeacad2a9b4 100644 --- a/compiler/testData/diagnostics/tests/labels/kt361.kt +++ b/compiler/testData/diagnostics/tests/labels/kt361.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL fun nonlocals(b : Boolean) { a@{ fun foo() { diff --git a/compiler/testData/diagnostics/tests/regressions/kt411.fir.kt b/compiler/testData/diagnostics/tests/regressions/kt411.fir.kt index e5b9998793d..d7a776f7cb5 100644 --- a/compiler/testData/diagnostics/tests/regressions/kt411.fir.kt +++ b/compiler/testData/diagnostics/tests/regressions/kt411.fir.kt @@ -23,7 +23,7 @@ fun t2() : String { if (true) { return@l 1 } - return "s" + return "s" } return "s" } @@ -32,10 +32,10 @@ fun t3() : String { invoker( l@{ if (true) { - return@t3 "1" + return@t3 "1" } else { - return 2 + return 2 } return@l 0 } @@ -47,7 +47,7 @@ fun t3() : String { ) invoker( { - return "0" + return "0" } ) return "2" @@ -66,4 +66,4 @@ fun t4() : Int { } return 12 -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/secondaryConstructors/return.fir.kt b/compiler/testData/diagnostics/tests/secondaryConstructors/return.fir.kt index 44f330699d5..86490c6c3da 100644 --- a/compiler/testData/diagnostics/tests/secondaryConstructors/return.fir.kt +++ b/compiler/testData/diagnostics/tests/secondaryConstructors/return.fir.kt @@ -1,8 +1,8 @@ // !WITH_NEW_INFERENCE class A { init { - return - return 1 + return + return 1 } constructor() { if (1 == 1) { diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendFunctionType/inline.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendFunctionType/inline.fir.kt index 6acb6e04c2d..5c48382d95d 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendFunctionType/inline.fir.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendFunctionType/inline.fir.kt @@ -16,15 +16,15 @@ suspend fun baz() { } foo2 { - return@baz + return@baz } foo3 { - return@baz + return@baz } foo4 { - return@baz + return@baz } bar1 { @@ -32,14 +32,14 @@ suspend fun baz() { } bar2 { - return@baz + return@baz } bar3 { - return@baz + return@baz } bar4 { - return@baz + return@baz } } diff --git a/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/returnFromInlineLambda.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/returnFromInlineLambda.fir.kt deleted file mode 100644 index af86c7df384..00000000000 --- a/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/returnFromInlineLambda.fir.kt +++ /dev/null @@ -1,51 +0,0 @@ -// !LANGUAGE: +NewInference +OverloadResolutionByLambdaReturnType -// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION -EXPERIMENTAL_API_USAGE -EXPERIMENTAL_UNSIGNED_LITERALS -// ISSUE: KT-11265 - -// FILE: OverloadResolutionByLambdaReturnType.kt - -package kotlin - -annotation class OverloadResolutionByLambdaReturnType - -// FILE: main.kt - -import kotlin.OverloadResolutionByLambdaReturnType - -inline fun Array.myFlatMap(transform: (T) -> Iterable): List { - TODO() -} - -@OverloadResolutionByLambdaReturnType -@JvmName("seqFlatMap") -inline fun Array.myFlatMap(transform: (T) -> Sequence): List { - TODO() -} - -fun String.toList(): List = null!! - -fun test_1(a: Array, b: Boolean) { - a.myFlatMap { it.toList().ifEmpty { return } } - a.myFlatMap { - if (b) return - it.toList() - } -} - -fun Array.noInlineFlatMap(transform: (T) -> Iterable): List { - TODO() -} - -@OverloadResolutionByLambdaReturnType -@JvmName("noInlineSeqFlatMap") -fun Array.noInlineFlatMap(transform: (T) -> Sequence): List { - TODO() -} - -fun test_2(a: Array, b: Boolean) { - a.noInlineFlatMap { it.toList().ifEmpty { return } } - a.noInlineFlatMap { - if (b) return - it.toList() - } -} diff --git a/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/returnFromInlineLambda.kt b/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/returnFromInlineLambda.kt index b53de27da0b..028410467b6 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/returnFromInlineLambda.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/returnFromInlineLambda.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !LANGUAGE: +NewInference +OverloadResolutionByLambdaReturnType // !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION -EXPERIMENTAL_API_USAGE -EXPERIMENTAL_UNSIGNED_LITERALS // ISSUE: KT-11265 diff --git a/compiler/testData/diagnostics/testsWithStdLib/inline/inlineConstructorOfArray.kt b/compiler/testData/diagnostics/testsWithStdLib/inline/inlineConstructorOfArray.kt new file mode 100644 index 00000000000..23355500f0c --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/inline/inlineConstructorOfArray.kt @@ -0,0 +1,20 @@ +// FIR_IDENTICAL +fun testArray(b: Boolean) { + Array(5) { i -> + if (b) return + i + } + throw AssertionError() +} + +fun testMyArray(b: Boolean) { + MyArray(5) { i -> + if (b) return + i + } + throw AssertionError() +} + +class MyArray { + constructor(size: Int, init: (Int) -> T) +} diff --git a/compiler/testData/diagnostics/testsWithStdLib/inline/inlineConstructorOfArray.txt b/compiler/testData/diagnostics/testsWithStdLib/inline/inlineConstructorOfArray.txt new file mode 100644 index 00000000000..06c0d8ffbb1 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/inline/inlineConstructorOfArray.txt @@ -0,0 +1,11 @@ +package + +public fun testArray(/*0*/ b: kotlin.Boolean): kotlin.Unit +public fun testMyArray(/*0*/ b: kotlin.Boolean): kotlin.Unit + +public final class MyArray { + public constructor MyArray(/*0*/ size: kotlin.Int, /*1*/ init: (kotlin.Int) -> T) + 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 +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 9562245d726..b346fa6b536 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -34509,6 +34509,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/inline"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @Test + @TestMetadata("inlineConstructorOfArray.kt") + public void testInlineConstructorOfArray() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/inline/inlineConstructorOfArray.kt"); + } + @Test @TestMetadata("inlineOnlySuppressesNothingToInline.kt") public void testInlineOnlySuppressesNothingToInline() throws Exception { diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/p-6/pos/5.2.fir.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/p-6/pos/5.2.fir.kt index 843af7b2a83..41f47302837 100644 --- a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/p-6/pos/5.2.fir.kt +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/p-6/pos/5.2.fir.kt @@ -225,8 +225,8 @@ fun case_23(value_1: Nothing) { // TESTCASE NUMBER: 24 fun case_24(value_1: Nothing?) = when (value_1) { - throw Exception(), return "" -> "" - null, return return return "", throw throw throw Exception() -> "" + throw Exception(), return "" -> "" + null, return return return "", throw throw throw Exception() -> "" else -> "" } @@ -237,8 +237,8 @@ fun case_24(value_1: Nothing?) = when (value_1) { */ fun case_25(value_1: Boolean) = when (value_1) { true -> {} - throw Exception(), return -> {} - false, return return return, throw throw throw Exception() -> {} + throw Exception(), return -> {} + false, return return return, throw throw throw Exception() -> {} } /* diff --git a/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDataClassConverters.kt b/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDataClassConverters.kt index a6f7d784f25..8fdac77f772 100644 --- a/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDataClassConverters.kt +++ b/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDataClassConverters.kt @@ -111,12 +111,6 @@ internal val KT_DIAGNOSTIC_CONVERTER = KtDiagnosticConverterBuilder.buildConvert token, ) } - add(FirErrors.RETURN_NOT_ALLOWED) { firDiagnostic -> - ReturnNotAllowedImpl( - firDiagnostic as FirPsiDiagnostic<*>, - token, - ) - } add(FirErrors.DELEGATION_IN_INTERFACE) { firDiagnostic -> DelegationInInterfaceImpl( firDiagnostic as FirPsiDiagnostic<*>, @@ -1633,4 +1627,16 @@ internal val KT_DIAGNOSTIC_CONVERTER = KtDiagnosticConverterBuilder.buildConvert token, ) } + add(FirErrors.RETURN_NOT_ALLOWED) { firDiagnostic -> + ReturnNotAllowedImpl( + firDiagnostic as FirPsiDiagnostic<*>, + token, + ) + } + add(FirErrors.RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY) { firDiagnostic -> + ReturnInFunctionWithExpressionBodyImpl( + firDiagnostic as FirPsiDiagnostic<*>, + token, + ) + } } diff --git a/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnostics.kt b/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnostics.kt index ef7b20280aa..14a1e7595ca 100644 --- a/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnostics.kt +++ b/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnostics.kt @@ -40,6 +40,7 @@ import org.jetbrains.kotlin.psi.KtPrimaryConstructor import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtPropertyAccessor import org.jetbrains.kotlin.psi.KtPropertyDelegate +import org.jetbrains.kotlin.psi.KtReturnExpression import org.jetbrains.kotlin.psi.KtSimpleNameExpression import org.jetbrains.kotlin.psi.KtTypeAlias import org.jetbrains.kotlin.psi.KtTypeParameter @@ -97,10 +98,6 @@ sealed class KtFirDiagnostic : KtDiagnosticWithPsi { override val diagnosticClass get() = VariableExpected::class } - abstract class ReturnNotAllowed : KtFirDiagnostic() { - override val diagnosticClass get() = ReturnNotAllowed::class - } - abstract class DelegationInInterface : KtFirDiagnostic() { override val diagnosticClass get() = DelegationInInterface::class } @@ -1149,4 +1146,12 @@ sealed class KtFirDiagnostic : KtDiagnosticWithPsi { override val diagnosticClass get() = UselessCallOnNotNull::class } + abstract class ReturnNotAllowed : KtFirDiagnostic() { + override val diagnosticClass get() = ReturnNotAllowed::class + } + + abstract class ReturnInFunctionWithExpressionBody : KtFirDiagnostic() { + override val diagnosticClass get() = ReturnInFunctionWithExpressionBody::class + } + } diff --git a/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnosticsImpl.kt b/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnosticsImpl.kt index 89080b1f917..6cddec2c21e 100644 --- a/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnosticsImpl.kt +++ b/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/diagnostics/KtFirDiagnosticsImpl.kt @@ -42,6 +42,7 @@ import org.jetbrains.kotlin.psi.KtPrimaryConstructor import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtPropertyAccessor import org.jetbrains.kotlin.psi.KtPropertyDelegate +import org.jetbrains.kotlin.psi.KtReturnExpression import org.jetbrains.kotlin.psi.KtSimpleNameExpression import org.jetbrains.kotlin.psi.KtTypeAlias import org.jetbrains.kotlin.psi.KtTypeParameter @@ -128,13 +129,6 @@ internal class VariableExpectedImpl( override val firDiagnostic: FirPsiDiagnostic<*> by weakRef(firDiagnostic) } -internal class ReturnNotAllowedImpl( - firDiagnostic: FirPsiDiagnostic<*>, - override val token: ValidityToken, -) : KtFirDiagnostic.ReturnNotAllowed(), KtAbstractFirDiagnostic { - override val firDiagnostic: FirPsiDiagnostic<*> by weakRef(firDiagnostic) -} - internal class DelegationInInterfaceImpl( firDiagnostic: FirPsiDiagnostic<*>, override val token: ValidityToken, @@ -1864,3 +1858,17 @@ internal class UselessCallOnNotNullImpl( override val firDiagnostic: FirPsiDiagnostic<*> by weakRef(firDiagnostic) } +internal class ReturnNotAllowedImpl( + firDiagnostic: FirPsiDiagnostic<*>, + override val token: ValidityToken, +) : KtFirDiagnostic.ReturnNotAllowed(), KtAbstractFirDiagnostic { + override val firDiagnostic: FirPsiDiagnostic<*> by weakRef(firDiagnostic) +} + +internal class ReturnInFunctionWithExpressionBodyImpl( + firDiagnostic: FirPsiDiagnostic<*>, + override val token: ValidityToken, +) : KtFirDiagnostic.ReturnInFunctionWithExpressionBody(), KtAbstractFirDiagnostic { + override val firDiagnostic: FirPsiDiagnostic<*> by weakRef(firDiagnostic) +} + diff --git a/idea/testData/checker/FunctionReturnTypes.fir.kt b/idea/testData/checker/FunctionReturnTypes.fir.kt index 7e6ecc8c2f2..41ffd65f4af 100644 --- a/idea/testData/checker/FunctionReturnTypes.fir.kt +++ b/idea/testData/checker/FunctionReturnTypes.fir.kt @@ -8,7 +8,7 @@ fun unitEmpty() : Unit {} fun unitEmptyReturn() : Unit {return} fun unitIntReturn() : Unit {return 1} fun unitUnitReturn() : Unit {return Unit} -fun test1() : Any = { return } +fun test1() : Any = { return } fun test2() : Any = a@ {return@a 1} fun test3() : Any { return } @@ -133,7 +133,7 @@ fun blockNoReturnIfUnitInOneBranch(): Int { fun nonBlockReturnIfEmptyIf(): Int = if (1 < 2) {} else {} fun nonBlockNoReturnIfUnitInOneBranch(): Int = if (1 < 2) {} else 2 -val a = return 1 +val a = return 1 class A() { }