Closed krzema12 closed 3 years ago
Example failing code with UnboundLocalError
error:
13998 def box():
13999 init_properties_python_kt()
14000 return 'OK'
14001
14002 pass
14003 def init_properties_python_kt():
14004 if not (properties_initialized_python_kt):
14005 properties_initialized_python_kt = True
14006 xi = 0
14007 xin = 0
14008 xinn = None
14009 xl = Long(0, 0)
14010 xln = Long(0, 0)
14011 xlnn = None
14012 xb = visitConst_other_Byte
14013 xbn = visitConst_other_Byte
14014 xbnn = None
14015 xf = kotlin_Float(0)
14016 xfn = kotlin_Float(0)
14017 xfnn = None
14018 xd = 0.0
14019 xdn = 0.0
14020 xdnn = None
14021 xs = visitConst_other_Short
14022 xsn = visitConst_other_Short
14023 xsnn = None
14024
NameError: name 'kotlin_UInt' is not defined
NameError: name 'log' is not defined
NameError: name 'Companion_instance' is not defined
NameError: name 'Companion_instance' is not defined
NameError: name 'Unit_instance' is not defined
To solve Unit_instance
, we have to define it globally and then use global Unit_instance
in Unit_getInstance
. Then, support visitExpression_other__inToPyStatementTransformer_org_jetbrains_kotlin_ir_expressions_impl_IrConstructorCallImpl
.
Next problem: there's a clash in companion object names:
> less python/experiments/out_ir.py | grep Companion_instance
Companion_instance = None
global Companion_instance
if Companion_instance == None:
return Companion_instance
Companion_instance = self
Companion_instance = None
global Companion_instance
if Companion_instance == None:
return Companion_instance
Companion_instance = self
Companion_instance = None
global Companion_instance
if Companion_instance == None:
return Companion_instance
Companion_instance = self
Companion_instance = None
global Companion_instance
if Companion_instance == None:
return Companion_instance
In JS, some numbers are added to ensure isolation:
> less python/experiments/out-ir.js | grep Companion_instance
Companion_instance = this;
var Companion_instance;
if (Companion_instance == null)
return Companion_instance;
Companion_instance_0 = this;
var Companion_instance_0;
if (Companion_instance_0 == null)
return Companion_instance_0;
Companion_instance_1 = this;
var Companion_instance_1;
if (Companion_instance_1 == null)
return Companion_instance_1;
Companion_instance_2 = this;
var Companion_instance_2;
if (Companion_instance_2 == null)
return Companion_instance_2;
Companion_instance_3 = this;
var Companion_instance_3;
if (Companion_instance_3 == null)
return Companion_instance_3;
The logic responsible for it is in compiler/ir/backend.py/src/org/jetbrains/kotlin/ir/backend/py/utils/NameTables.kt
and it's executed, but it doesn't end up in the output. TODO: find why.
NameTables.merge(...)
is called and produces unique identifiersval fieldName = context.getNameForField(expression.symbol.owner)
. In Python backend, we don't use the context at allConclusion: we have to use the context.getNameFor...
in every transformer method to get the names right.
TODO: once #27 and #28 is merged, ensure that all tests that passed so far thanks to this PR also pass in the other mentioned PRs, and then close this PR without merging.
python-backend
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$ControlStructures$ReturnsNothing > testIfElse FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$ControlStructures$ReturnsNothing > testPropertyGetter FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$LazyCodegen$Optimizations > testNegateObjectComp FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$LazyCodegen$Optimizations > testNegateObjectComp2 FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$Multiplatform$DefaultArguments > testBothInExpectAndActual2 FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$PrimitiveTypes$EqualityWithObject$Generated > testBoxedEqPrimitiveBoolean FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$PrimitiveTypes$EqualityWithObject$Generated > testBoxedEqPrimitiveInt FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$PrimitiveTypes$EqualityWithObject$Generated > testPrimitiveEqBoxedBoolean FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$PrimitiveTypes$EqualityWithObject$Generated > testPrimitiveEqBoxedInt FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$Properties > testFieldSimple FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$Properties > testTwoAnnotatedExtensionPropertiesWithoutBackingFields FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$Regressions > testArrayLengthNPE FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$Regressions > testHashCodeNPE FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$Regressions > testInternalTopLevelOtherPackage FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$Regressions > testKt1528 FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$TopLevelPrivate > testPrivateInInlineNested FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$When$WhenSubjectVariable > testEqualityWithSubjectVariable FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenBoxTestGenerated$When$WhenSubjectVariable > testRangeCheckOnSubjectVariable FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenInlineTestGenerated$Private > testPrivateInline FAILED
org.jetbrains.kotlin.python.test.ir.semantics.IrPythonCodegenInlineTestGenerated$SyntheticAccessors > testPackagePrivateMembers FAILED
Regressions:
Constants > testFoldingBinaryOpsUnsignedConst
Python error:DelegatedProperty$ProvideDelegate > testEvaluationOrder
Python error:Package > testBoxPrimitiveTypeInClinit
Python error:Package > testNullablePrimitiveNoFieldInitializer
Python error:Regressions > testLambdaPostponeConstruction
Python error:Unit > testKt3634
Python error: