Resolve local function parameters with function inner scope

This allows to use type parameters and value paramters in default value expressions

 #KT-7984 Fixed
 #KT-7985 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2017-04-14 16:10:52 +03:00
parent e86d52b681
commit b17b3f4c63
17 changed files with 121 additions and 4 deletions
@@ -101,18 +101,18 @@ internal class FunctionsTypingVisitor(facade: ExpressionTypingInternals) : Expre
// Necessary for local functions
ForceResolveUtil.forceResolveAllContents(functionDescriptor.annotations)
val functionInnerScope = FunctionDescriptorUtil.getFunctionInnerScope(context.scope, functionDescriptor, context.trace, components.overloadChecker)
if (!function.hasDeclaredReturnType() && !function.hasBlockBody()) {
ForceResolveUtil.forceResolveAllContents(functionDescriptor.returnType)
}
else {
val functionInnerScope = FunctionDescriptorUtil.getFunctionInnerScope(context.scope, functionDescriptor, context.trace, components.overloadChecker)
components.expressionTypingServices.checkFunctionReturnType(
functionInnerScope, function, functionDescriptor, context.dataFlowInfo, null, context.trace
)
}
components.valueParameterResolver.resolveValueParameters(
function.valueParameters, functionDescriptor.valueParameters, context.scope, context.dataFlowInfo, context.trace
function.valueParameters, functionDescriptor.valueParameters, functionInnerScope, context.dataFlowInfo, context.trace
)
components.modifiersChecker.withTrace(context.trace).checkModifiersForLocalDeclaration(function, functionDescriptor)
@@ -0,0 +1,11 @@
fun foo(): String {
fun bar(x: String, y: String = x): String {
return y
}
return bar("OK")
}
fun box(): String {
return foo()
}
@@ -0,0 +1,6 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun foo() {
fun bar(x: String, y: String = x) {}
fun baz(x: Int = <!UNINITIALIZED_PARAMETER!>y<!>, y: Int) {}
}
@@ -0,0 +1,3 @@
package
public fun foo(): kotlin.Unit
@@ -0,0 +1,7 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun foo() {
fun <T> bar(x: List<T> = listOf<T>()) {}
}
fun <T> listOf(): List<T> = TODO()
@@ -0,0 +1,4 @@
package
public fun foo(): kotlin.Unit
public fun </*0*/ T> listOf(): kotlin.collections.List<T>
@@ -10398,6 +10398,21 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
}
}
@TestMetadata("compiler/testData/codegen/box/localFunctions")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class LocalFunctions extends AbstractIrBlackBoxCodegenTest {
public void testAllFilesPresentInLocalFunctions() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/localFunctions"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
}
@TestMetadata("parameterAsDefaultValue.kt")
public void testParameterAsDefaultValue() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/localFunctions/parameterAsDefaultValue.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/codegen/box/mangling")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -17164,6 +17164,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("parameterAsDefaultValueInLocalFunction.kt")
public void testParameterAsDefaultValueInLocalFunction() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/resolve/parameterAsDefaultValueInLocalFunction.kt");
doTest(fileName);
}
@TestMetadata("resolveAnnotatedLambdaArgument.kt")
public void testResolveAnnotatedLambdaArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/resolve/resolveAnnotatedLambdaArgument.kt");
@@ -17218,6 +17224,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("typeParameterInDefaultValueInLocalFunction.kt")
public void testTypeParameterInDefaultValueInLocalFunction() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/resolve/typeParameterInDefaultValueInLocalFunction.kt");
doTest(fileName);
}
@TestMetadata("wrongNumberOfTypeArguments.kt")
public void testWrongNumberOfTypeArguments() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/resolve/wrongNumberOfTypeArguments.kt");
@@ -10398,6 +10398,21 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
}
}
@TestMetadata("compiler/testData/codegen/box/localFunctions")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class LocalFunctions extends AbstractBlackBoxCodegenTest {
public void testAllFilesPresentInLocalFunctions() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/localFunctions"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
}
@TestMetadata("parameterAsDefaultValue.kt")
public void testParameterAsDefaultValue() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/localFunctions/parameterAsDefaultValue.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/codegen/box/mangling")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -10398,6 +10398,21 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
}
}
@TestMetadata("compiler/testData/codegen/box/localFunctions")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class LocalFunctions extends AbstractLightAnalysisModeTest {
public void testAllFilesPresentInLocalFunctions() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/localFunctions"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
}
@TestMetadata("parameterAsDefaultValue.kt")
public void testParameterAsDefaultValue() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/localFunctions/parameterAsDefaultValue.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/codegen/box/mangling")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -1,4 +1,5 @@
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter a: kotlin.Int defined in bar.foo
fun bar(n: Int) {
fun foo(a: Int, b: Int = <selection>a + n</selection>) = a + b - n - 1
}
@@ -0,0 +1,7 @@
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter a: kotlin.Int defined in bar.foo
fun bar(n: Int) {
fun i(a: Int) = a + n
fun foo(a: Int, b: Int = i(a)) = a + b - n - 1
}
@@ -1 +0,0 @@
Cannot generate function with erroneous return type
@@ -1,4 +1,5 @@
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter a: kotlin.Int defined in bar.foo
fun bar(n: Int) {
fun foo(a: Int, b: Int = { <selection>a + n</selection> }.invoke()) = a + b - n - 1
}
@@ -0,0 +1,7 @@
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter a: kotlin.Int defined in bar.foo
fun bar(n: Int) {
fun i(a: Int) = a + n
fun foo(a: Int, b: Int = { i(a) }.invoke()) = a + b - n - 1
}
@@ -1 +0,0 @@
Cannot generate function with erroneous return type
@@ -11941,6 +11941,21 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
}
}
@TestMetadata("compiler/testData/codegen/box/localFunctions")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class LocalFunctions extends AbstractJsCodegenBoxTest {
public void testAllFilesPresentInLocalFunctions() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/localFunctions"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
}
@TestMetadata("parameterAsDefaultValue.kt")
public void testParameterAsDefaultValue() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/localFunctions/parameterAsDefaultValue.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/codegen/box/mangling")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)