JS: fix scope of function generated for primary constructor. See KT-15678

This commit is contained in:
Alexey Andreev
2017-01-16 12:22:52 +03:00
parent daac24b62e
commit a927770935
5 changed files with 35 additions and 6 deletions
@@ -53,9 +53,6 @@ class NamingContext(
fun getTemporaryName(candidate: String): JsName = scope.declareTemporaryName(candidate)
fun getFreshName(candidate: JsName) = if (candidate.isTemporary) getTemporaryName(candidate.ident) else getFreshName(candidate.ident)
fun newVar(name: JsName, value: JsExpression? = null) {
val vars = JsAstUtils.newVar(name, value)
vars.synthetic = true
@@ -32,7 +32,7 @@ fun aliasArgumentsIfNeeded(
for ((arg, param) in arguments.zip(parameters)) {
val paramName = param.name
val replacement = context.getFreshName(paramName).apply {
val replacement = context.getTemporaryName(paramName.ident).apply {
staticRef = arg
context.newVar(this, arg.deepCopy())
}.makeRef()
@@ -43,7 +43,7 @@ fun aliasArgumentsIfNeeded(
val defaultParams = parameters.subList(arguments.size, parameters.size)
for (defaultParam in defaultParams) {
val paramName = defaultParam.name
val freshName = context.getFreshName(paramName)
val freshName = context.getTemporaryName(paramName.ident)
context.newVar(freshName)
context.replaceName(paramName, freshName.makeRef())
@@ -5630,6 +5630,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
doTest(fileName);
}
@TestMetadata("constructorLocalVar.kt")
public void testConstructorLocalVar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/nameClashes/constructorLocalVar.kt");
doTest(fileName);
}
@TestMetadata("differenceInCapitalization.kt")
public void testDifferenceInCapitalization() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/nameClashes/differenceInCapitalization.kt");
@@ -75,7 +75,8 @@ class ClassTranslator private constructor(
val scope = context().getScopeForDescriptor(descriptor)
val context = context().newDeclaration(descriptor)
val constructorFunction = context.createRootScopedFunction(descriptor)
val constructorFunction = descriptor.unsubstitutedPrimaryConstructor?.let { context.getFunctionObject(it) } ?:
context.createRootScopedFunction(descriptor)
constructorFunction.name = context.getInnerNameForDescriptor(descriptor)
context.addDeclarationStatement(constructorFunction.makeStmt())
val enumInitFunction = if (descriptor.kind == ClassKind.ENUM_CLASS) createEnumInitFunction() else null
@@ -0,0 +1,25 @@
var log = ""
inline fun f(x: Int): Int {
val result = x * 2
log += "f($x)"
return result
}
fun bar() = 10
class Test {
val value: Int
init {
val x = 3
val y = f(bar())
value = x + y
}
}
fun box(): String {
val test = Test()
if (test.value != 23) return "fail: ${test.value}"
return "OK"
}