Code review comment: Lets add some comments to explain what we are doing here.  Also, why do we omit, if `hasInvalidFieldIndex` is true?

Code before review:
public void visit(BLangRecordLiteral recordLiteral) {
BType actualType = symTable.errType;
int expTypeTag = expType.tag;
BType originalExpType = expType;
if (expTypeTag == TypeTags.NONE || expTypeTag == TypeTags.ANY) {
expType = symTable.mapType;
}
if (expTypeTag == TypeTags.ANY
|| (expTypeTag == TypeTags.MAP && recordLiteral.keyValuePairs.isEmpty())
|| expTypeTag == TypeTags.OBJECT) {
dlog.error(recordLiteral.pos, DiagnosticCode.INVALID_RECORD_LITERAL, originalExpType);
resultType = symTable.errType;
return;
}
List<BType> matchedTypeList = getRecordCompatibleType(expType, recordLiteral);
boolean hasInvalidFieldIndex = false;
if (matchedTypeList.isEmpty()) {
dlog.error(recordLiteral.pos, DiagnosticCode.INVALID_LITERAL_FOR_TYPE, expType);
} else if (matchedTypeList.size() > 1) {
dlog.error(recordLiteral.pos, DiagnosticCode.AMBIGUOUS_TYPES, expType);
} else {
for (BLangRecordKeyValue keyValuePair : recordLiteral.keyValuePairs) {
BType type = checkRecLiteralKeyValue(keyValuePair, matchedTypeList.get(0));
hasInvalidFieldIndex = type.tag == TypeTags.ERROR;
}
actualType = matchedTypeList.get(0);
}
resultType = types.checkType(recordLiteral, actualType, expType);
if (recordLiteral.type.tag == TypeTags.RECORD && !hasInvalidFieldIndex) {
BRecordType recordType = (BRecordType) recordLiteral.type;
int maskOptional = Flags.asMask(EnumSet.of(Flag.OPTIONAL));
int maskDefaultable = Flags.asMask(EnumSet.of(Flag.DEFAULTABLE));
recordType.fields.forEach(field -> {
boolean hasField = recordLiteral.keyValuePairs.stream().anyMatch(
keyVal -> field.name.value.equals(((BLangSimpleVarRef) keyVal.key.expr).variableName.value));
if (!hasField && !Symbols.isFlagOn(field.symbol.flags, maskOptional) &&
(!types.defaultValueExists(recordLiteral.pos, field.type) &&
!Symbols.isFlagOn(field.symbol.flags, maskDefaultable))) {
dlog.error(recordLiteral.pos, DiagnosticCode.MISSING_REQUIRED_RECORD_FIELD, field.name);
}
});
}
}

Line under review: if (recordLiteral.type.tag == TypeTags.RECORD && !hasInvalidFieldIndex) {

Code after addressing the review:
public void visit(BLangRecordLiteral recordLiteral) {
BType actualType = symTable.errType;
int expTypeTag = expType.tag;
BType originalExpType = expType;
if (expTypeTag == TypeTags.NONE || expTypeTag == TypeTags.ANY) {
expType = symTable.mapType;
}
if (expTypeTag == TypeTags.ANY || expTypeTag == TypeTags.OBJECT) {
dlog.error(recordLiteral.pos, DiagnosticCode.INVALID_RECORD_LITERAL, originalExpType);
resultType = symTable.errType;
return;
}
List<BType> matchedTypeList = getRecordCompatibleType(expType, recordLiteral);
if (matchedTypeList.isEmpty()) {
dlog.error(recordLiteral.pos, DiagnosticCode.INVALID_LITERAL_FOR_TYPE, expType);
} else if (matchedTypeList.size() > 1) {
dlog.error(recordLiteral.pos, DiagnosticCode.AMBIGUOUS_TYPES, expType);
} else {
recordLiteral.keyValuePairs
.forEach(keyValuePair -> checkRecLiteralKeyValue(keyValuePair, matchedTypeList.get(0)));
actualType = matchedTypeList.get(0);
}
resultType = types.checkType(recordLiteral, actualType, expType);
if (recordLiteral.type.tag == TypeTags.RECORD) {
checkMissingRequiredFields((BRecordType) recordLiteral.type, recordLiteral.keyValuePairs,
recordLiteral.pos);
}
}