Give unique names to fields for captured local functions
When a local function is captured, corresponding field accesses are later transformed by the inliner. It doesn't have enough information to restore the original semantics completely, so it has to rely on field names. Local functions can be overloaded or can have names matching local variable names, in both cases we generated fields with the same name for captured values. Now, we use the same '$<local-class-number>' suffix for field names for local functions as it is present in the corresponding local class name. This allows to distinguish captured local functions from captured local variables and between different overloads of a function with the same name. #KT-19827 Fixed #KT-18639 Fixed
This commit is contained in:
@@ -106,14 +106,21 @@ public interface LocalLookup {
|
||||
BindingContext bindingContext = state.getBindingContext();
|
||||
Type localType = asmTypeForAnonymousClass(bindingContext, vd);
|
||||
|
||||
MutableClosure localFunClosure = bindingContext.get(CLOSURE, bindingContext.get(CLASS_FOR_CALLABLE, vd));
|
||||
ClassDescriptor callableClass = bindingContext.get(CLASS_FOR_CALLABLE, vd);
|
||||
assert callableClass != null : "No CLASS_FOR_CALLABLE:" + vd;
|
||||
|
||||
MutableClosure localFunClosure = bindingContext.get(CLOSURE, callableClass);
|
||||
if (localFunClosure != null && JvmCodegenUtil.isConst(localFunClosure)) {
|
||||
// This is an optimization: we can obtain an instance of a const closure simply by GETSTATIC ...$instance
|
||||
// (instead of passing this instance to the constructor and storing as a field)
|
||||
return StackValue.field(localType, localType, JvmAbi.INSTANCE_FIELD, true, StackValue.LOCAL_0, vd);
|
||||
}
|
||||
|
||||
String fieldName = "$" + vd.getName();
|
||||
String localFunClassName = callableClass.getName().asString();
|
||||
int localClassIndexStart = localFunClassName.lastIndexOf('$');
|
||||
String localFunSuffix = localClassIndexStart >= 0 ? localFunClassName.substring(localClassIndexStart) : "";
|
||||
|
||||
String fieldName = "$" + vd.getName() + localFunSuffix;
|
||||
StackValue.StackValueWithSimpleReceiver innerValue = StackValue.field(localType, classType, fieldName, false,
|
||||
StackValue.LOCAL_0, vd);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user