Code review comment: This was done to fix NPE in BirGen in match statement due to scope entries and init function misssing when creating record type.

Code before review:
private BType getStructuredBindingPatternType(BLangVariable bindingPatternVariable) {
if (NodeKind.TUPLE_VARIABLE == bindingPatternVariable.getKind()) {
BLangTupleVariable tupleVariable = (BLangTupleVariable) bindingPatternVariable;
List<BType> memberTypes = new ArrayList<>();
for (int i = 0; i < tupleVariable.memberVariables.size(); i++) {
memberTypes.add(getStructuredBindingPatternType(tupleVariable.memberVariables.get(i)));
}
return new BTupleType(memberTypes);
}
if (NodeKind.RECORD_VARIABLE == bindingPatternVariable.getKind()) {
BLangRecordVariable recordVariable = (BLangRecordVariable) bindingPatternVariable;
BRecordTypeSymbol recordSymbol =
Symbols.createRecordSymbol(0, names.fromString("$anonRecordType$" + recordCount++),
env.enclPkg.symbol.pkgID, null, env.scope.owner);
recordSymbol.scope = new Scope(recordSymbol);
List<BField> fields = new ArrayList<>();
List<BLangSimpleVariable> typeDefFields = new ArrayList<>();
for (int i = 0; i < recordVariable.variableList.size(); i++) {
String fieldName = recordVariable.variableList.get(i).key.value;
BType fieldType = getStructuredBindingPatternType(
recordVariable.variableList.get(i).valueBindingPattern);
BVarSymbol fieldSymbol = new BVarSymbol(Flags.REQUIRED, names.fromString(fieldName),
env.enclPkg.symbol.pkgID, fieldType, recordSymbol);
fields.add(new BField(names.fromString(fieldName), bindingPatternVariable.pos, fieldSymbol));
typeDefFields.add(ASTBuilderUtil.createVariable(null, fieldName, fieldType, null, fieldSymbol));
recordSymbol.scope.define(fieldSymbol.name, fieldSymbol);
}
BRecordType recordVarType = new BRecordType(recordSymbol);
recordVarType.fields = fields;
if (recordVariable.isClosed) {
recordVarType.sealed = true;
} else {
recordVarType.restFieldType = recordVariable.restParam != null ?
((BMapType) ((BLangSimpleVariable) recordVariable.restParam).type).constraint :
symTable.anydataType;
}
BLangRecordTypeNode recordTypeNode = createRecordTypeNode(typeDefFields, recordVarType);
recordTypeNode.pos = bindingPatternVariable.pos;
recordSymbol.type = recordVarType;
recordVarType.tsymbol = recordSymbol;
recordTypeNode.symbol = recordSymbol;
recordTypeNode.initFunction = createInitFunctionForStructureType(recordTypeNode, env,
Names.INIT_FUNCTION_SUFFIX);
recordSymbol.scope.define(recordSymbol.initializerFunc.symbol.name, recordSymbol.initializerFunc.symbol);
createTypeDefinition(recordVarType, recordSymbol, recordTypeNode);
return recordVarType;
}
if (NodeKind.ERROR_VARIABLE == bindingPatternVariable.getKind()) {
BLangErrorVariable errorVariable = (BLangErrorVariable) bindingPatternVariable;
BErrorTypeSymbol errorTypeSymbol = new BErrorTypeSymbol(SymTag.ERROR, Flags.PUBLIC,
names.fromString("$anonErrorType$" + errorCount++),
env.enclPkg.symbol.pkgID,
null, null);
BErrorType errorType = new BErrorType(errorTypeSymbol, symTable.stringType,
errorVariable.detail == null ||
errorVariable.detail.type == symTable.noType ?
symTable.pureTypeConstrainedMap :
getStructuredBindingPatternType(errorVariable.detail));
errorTypeSymbol.type = errorType;
createTypeDefinition(errorType, errorTypeSymbol, createErrorTypeNode(errorType));
return errorType;
}
return bindingPatternVariable.type;
}

Line under review: BLangRecordTypeNode recordTypeNode = createRecordTypeNode(typeDefFields, recordVarType);

Code after addressing the review:
private BType getStructuredBindingPatternType(BLangVariable bindingPatternVariable) {
if (NodeKind.TUPLE_VARIABLE == bindingPatternVariable.getKind()) {
BLangTupleVariable tupleVariable = (BLangTupleVariable) bindingPatternVariable;
List<BType> memberTypes = new ArrayList<>();
for (int i = 0; i < tupleVariable.memberVariables.size(); i++) {
memberTypes.add(getStructuredBindingPatternType(tupleVariable.memberVariables.get(i)));
}
return new BTupleType(memberTypes);
}
if (NodeKind.RECORD_VARIABLE == bindingPatternVariable.getKind()) {
BLangRecordVariable recordVariable = (BLangRecordVariable) bindingPatternVariable;
BRecordTypeSymbol recordSymbol =
Symbols.createRecordSymbol(0, names.fromString("$anonRecordType$" + recordCount++),
env.enclPkg.symbol.pkgID, null, env.scope.owner);
recordSymbol.scope = new Scope(recordSymbol);
List<BField> fields = new ArrayList<>();
List<BLangSimpleVariable> typeDefFields = new ArrayList<>();
for (int i = 0; i < recordVariable.variableList.size(); i++) {
String fieldName = recordVariable.variableList.get(i).key.value;
BType fieldType = getStructuredBindingPatternType(
recordVariable.variableList.get(i).valueBindingPattern);
BVarSymbol fieldSymbol = new BVarSymbol(Flags.REQUIRED, names.fromString(fieldName),
env.enclPkg.symbol.pkgID, fieldType, recordSymbol);
fields.add(new BField(names.fromString(fieldName), bindingPatternVariable.pos, fieldSymbol));
typeDefFields.add(ASTBuilderUtil.createVariable(null, fieldName, fieldType, null, fieldSymbol));
recordSymbol.scope.define(fieldSymbol.name, fieldSymbol);
}
BRecordType recordVarType = new BRecordType(recordSymbol);
recordVarType.fields = fields;
if (recordVariable.isClosed) {
recordVarType.sealed = true;
} else {
recordVarType.restFieldType = recordVariable.restParam != null ?
((BMapType) ((BLangSimpleVariable) recordVariable.restParam).type).constraint :
symTable.anydataType;
}
BLangRecordTypeNode recordTypeNode = createRecordTypeNode(typeDefFields, recordVarType);
recordTypeNode.pos = bindingPatternVariable.pos;
recordSymbol.type = recordVarType;
recordVarType.tsymbol = recordSymbol;
recordTypeNode.symbol = recordSymbol;
recordTypeNode.initFunction = createInitFunctionForStructureType(recordTypeNode, env,
Names.INIT_FUNCTION_SUFFIX);
recordSymbol.scope.define(recordSymbol.initializerFunc.symbol.name, recordSymbol.initializerFunc.symbol);
createTypeDefinition(recordVarType, recordSymbol, recordTypeNode);
return recordVarType;
}
if (NodeKind.ERROR_VARIABLE == bindingPatternVariable.getKind()) {
BLangErrorVariable errorVariable = (BLangErrorVariable) bindingPatternVariable;
BErrorTypeSymbol errorTypeSymbol = new BErrorTypeSymbol(SymTag.ERROR, Flags.PUBLIC,
names.fromString("$anonErrorType$" + errorCount++),
env.enclPkg.symbol.pkgID,
null, null);
BErrorType errorType = new BErrorType(errorTypeSymbol, symTable.stringType,
errorVariable.detail == null ||
errorVariable.detail.type == symTable.noType ?
symTable.pureTypeConstrainedMap :
getStructuredBindingPatternType(errorVariable.detail));
errorTypeSymbol.type = errorType;
createTypeDefinition(errorType, errorTypeSymbol, createErrorTypeNode(errorType));
return errorType;
}
return bindingPatternVariable.type;
}