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:
Dmitry Petrov
2017-10-13 12:59:16 +03:00
parent 7a156e4407
commit cbef372e69
8 changed files with 127 additions and 5 deletions
@@ -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);