Recapture shared variables when calling local class constructor
When a local function or class A creates an instance of a local class B capturing an outer variable 'x', it should use ref for 'x', but not the value of 'x'.
This commit is contained in:
@@ -1086,11 +1086,15 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
||||
}
|
||||
|
||||
for (Map.Entry<DeclarationDescriptor, EnclosedValueDescriptor> entry : closure.getCaptureVariables().entrySet()) {
|
||||
Type sharedVarType = typeMapper.getSharedVarType(entry.getKey());
|
||||
DeclarationDescriptor declarationDescriptor = entry.getKey();
|
||||
EnclosedValueDescriptor valueDescriptor = entry.getValue();
|
||||
|
||||
Type sharedVarType = typeMapper.getSharedVarType(declarationDescriptor);
|
||||
boolean asSharedVar = sharedVarType != null;
|
||||
if (sharedVarType == null) {
|
||||
sharedVarType = typeMapper.mapType((VariableDescriptor) entry.getKey());
|
||||
sharedVarType = typeMapper.mapType((VariableDescriptor) declarationDescriptor);
|
||||
}
|
||||
StackValue capturedVar = lookupOuterValue(entry.getValue());
|
||||
StackValue capturedVar = lookupOuterValue(valueDescriptor, asSharedVar);
|
||||
callGenerator.putCapturedValueOnStack(capturedVar, sharedVarType, paramIndex++);
|
||||
}
|
||||
|
||||
@@ -1144,11 +1148,16 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public StackValue lookupOuterValue(EnclosedValueDescriptor d) {
|
||||
private StackValue lookupOuterValue(EnclosedValueDescriptor d, boolean asSharedVar) {
|
||||
DeclarationDescriptor descriptor = d.getDescriptor();
|
||||
for (LocalLookup.LocalLookupCase aCase : LocalLookup.LocalLookupCase.values()) {
|
||||
if (aCase.isCase(descriptor)) {
|
||||
return aCase.outerValue(d, this);
|
||||
StackValue outerValue = aCase.outerValue(d, this);
|
||||
if (asSharedVar && outerValue instanceof StackValue.FieldForSharedVar) {
|
||||
StackValue.FieldForSharedVar fieldForSharedVar = (StackValue.FieldForSharedVar) outerValue;
|
||||
return fieldForSharedVar.receiver;
|
||||
}
|
||||
return outerValue;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("Can't get outer value in " + this + " for " + d);
|
||||
|
||||
Reference in New Issue
Block a user