KT-37861 'this' is uninitialized in constructor default parameters

This commit is contained in:
Dmitry Petrov
2020-03-30 14:12:09 +03:00
parent 078cf02c8a
commit cec64a2ec7
10 changed files with 102 additions and 0 deletions
@@ -1207,6 +1207,10 @@ public class FunctionCodegen {
@NotNull MemberCodegen<?> parentCodegen,
@NotNull Method defaultMethod
) {
if (methodContext instanceof ConstructorContext) {
((ConstructorContext) methodContext).setThisInitialized(false);
}
GenerationState state = parentCodegen.state;
JvmMethodSignature signature = state.getTypeMapper().mapSignatureWithGeneric(functionDescriptor, methodContext.getContextKind());
@@ -854,6 +854,19 @@ class CodegenAnnotatingVisitor extends KtVisitorVoid {
checkSamCall(call);
}
@Override
public void visitParameter(@NotNull KtParameter parameter) {
PsiElement parent = parameter.getParent(); // KtParameterList
if (parent != null && parent.getParent() instanceof KtConstructor<?>) {
KtExpression defaultValue = parameter.getDefaultValue();
if (defaultValue != null) {
withinUninitializedClass(defaultValue, () -> defaultValue.accept(this));
}
} else {
super.visitParameter(parameter);
}
}
private void withinUninitializedClass(@NotNull KtElement element, @NotNull Runnable operation) {
ClassDescriptor currentClass = peekFromStack(classStack);
assert currentClass != null : element.getClass().getSimpleName() + " should be inside a class: " + element.getText();