JS: fix scope of function generated for primary constructor. See KT-15678
This commit is contained in:
@@ -53,9 +53,6 @@ class NamingContext(
|
|||||||
|
|
||||||
fun getTemporaryName(candidate: String): JsName = scope.declareTemporaryName(candidate)
|
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) {
|
fun newVar(name: JsName, value: JsExpression? = null) {
|
||||||
val vars = JsAstUtils.newVar(name, value)
|
val vars = JsAstUtils.newVar(name, value)
|
||||||
vars.synthetic = true
|
vars.synthetic = true
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ fun aliasArgumentsIfNeeded(
|
|||||||
for ((arg, param) in arguments.zip(parameters)) {
|
for ((arg, param) in arguments.zip(parameters)) {
|
||||||
val paramName = param.name
|
val paramName = param.name
|
||||||
|
|
||||||
val replacement = context.getFreshName(paramName).apply {
|
val replacement = context.getTemporaryName(paramName.ident).apply {
|
||||||
staticRef = arg
|
staticRef = arg
|
||||||
context.newVar(this, arg.deepCopy())
|
context.newVar(this, arg.deepCopy())
|
||||||
}.makeRef()
|
}.makeRef()
|
||||||
@@ -43,7 +43,7 @@ fun aliasArgumentsIfNeeded(
|
|||||||
val defaultParams = parameters.subList(arguments.size, parameters.size)
|
val defaultParams = parameters.subList(arguments.size, parameters.size)
|
||||||
for (defaultParam in defaultParams) {
|
for (defaultParam in defaultParams) {
|
||||||
val paramName = defaultParam.name
|
val paramName = defaultParam.name
|
||||||
val freshName = context.getFreshName(paramName)
|
val freshName = context.getTemporaryName(paramName.ident)
|
||||||
context.newVar(freshName)
|
context.newVar(freshName)
|
||||||
|
|
||||||
context.replaceName(paramName, freshName.makeRef())
|
context.replaceName(paramName, freshName.makeRef())
|
||||||
|
|||||||
@@ -5630,6 +5630,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("differenceInCapitalization.kt")
|
||||||
public void testDifferenceInCapitalization() throws Exception {
|
public void testDifferenceInCapitalization() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/nameClashes/differenceInCapitalization.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/nameClashes/differenceInCapitalization.kt");
|
||||||
|
|||||||
+2
-1
@@ -75,7 +75,8 @@ class ClassTranslator private constructor(
|
|||||||
val scope = context().getScopeForDescriptor(descriptor)
|
val scope = context().getScopeForDescriptor(descriptor)
|
||||||
val context = context().newDeclaration(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)
|
constructorFunction.name = context.getInnerNameForDescriptor(descriptor)
|
||||||
context.addDeclarationStatement(constructorFunction.makeStmt())
|
context.addDeclarationStatement(constructorFunction.makeStmt())
|
||||||
val enumInitFunction = if (descriptor.kind == ClassKind.ENUM_CLASS) createEnumInitFunction() else null
|
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"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user