Code review comment: This won't be the correct error when the constructed record is also open, right? E.g., the example you raised  ```ballerina type Student record {|     string name;     int age;     int...; |};  type Employee record {     string name;     int age; };  public function main() {     Employee emp = {name: "A", age: 1};     Student _ = {...emp}; } ```

Code before review:
private BType checkMappingField(RecordLiteralNode.RecordField field, BType mappingType, AnalyzerData data) {
BType fieldType = symTable.semanticError;
boolean keyValueField = field.isKeyValueField();
boolean spreadOpField = field.getKind() == NodeKind.RECORD_LITERAL_SPREAD_OP;
boolean readOnlyConstructorField = false;
String fieldName = null;
Location pos = null;
BLangExpression valueExpr = null;
if (keyValueField) {
valueExpr = ((BLangRecordKeyValueField) field).valueExpr;
} else if (!spreadOpField) {
valueExpr = (BLangRecordVarNameField) field;
}
boolean isOptional = false;
switch (mappingType.tag) {
case TypeTags.RECORD:
if (keyValueField) {
BLangRecordKeyValueField keyValField = (BLangRecordKeyValueField) field;
BLangRecordKey key = keyValField.key;
TypeSymbolPair typeSymbolPair = checkRecordLiteralKeyExpr(key.expr, key.computedKey,
(BRecordType) mappingType, data);
BVarSymbol fieldSymbol = typeSymbolPair.fieldSymbol;
if (fieldSymbol != null && Symbols.isOptional(fieldSymbol)) {
isOptional = true;
}
fieldType = typeSymbolPair.determinedType;
key.fieldSymbol = fieldSymbol;
readOnlyConstructorField = keyValField.readonly;
pos = key.expr.pos;
fieldName = getKeyValueFieldName(keyValField);
} else if (spreadOpField) {
BLangExpression spreadExpr = ((BLangRecordLiteral.BLangRecordSpreadOperatorField) field).expr;
checkExpr(spreadExpr, data);
BRecordType mappingRecordType = (BRecordType) mappingType;
BType spreadExprType = Types.getImpliedType(spreadExpr.getBType());
if (spreadExprType.tag == TypeTags.MAP) {
return types.checkType(spreadExpr.pos, ((BMapType) spreadExprType).constraint,
getAllFieldType(mappingRecordType),
DiagnosticErrorCode.INCOMPATIBLE_TYPES);
}
if (spreadExprType.tag != TypeTags.RECORD) {
dlog.error(spreadExpr.pos, DiagnosticErrorCode.INCOMPATIBLE_TYPES_SPREAD_OP,
spreadExprType);
return symTable.semanticError;
}
BRecordType spreadRecordType = (BRecordType) spreadExprType;
boolean errored = false;
for (BField bField : spreadRecordType.fields.values()) {
BType specFieldType = bField.type;
if (types.isNeverTypeOrStructureTypeWithARequiredNeverMember(specFieldType)) {
continue;
}
BSymbol fieldSymbol = symResolver.resolveStructField(spreadExpr.pos, data.env, bField.name,
mappingType.tsymbol);
BType expectedFieldType = checkRecordLiteralKeyByName(spreadExpr.pos, fieldSymbol, bField.name,
mappingRecordType);
if (expectedFieldType != symTable.semanticError &&
!types.isAssignable(specFieldType, expectedFieldType)) {
dlog.error(spreadExpr.pos, DiagnosticErrorCode.INCOMPATIBLE_TYPES_FIELD,
expectedFieldType, bField.name, specFieldType);
if (!errored) {
errored = true;
}
}
}
if (!spreadRecordType.sealed && (mappingRecordType.sealed ||
!types.isAssignable(spreadRecordType.restFieldType, mappingRecordType.restFieldType))) {
dlog.error(spreadExpr.pos,
DiagnosticErrorCode.INVALID_SPREAD_OP_TO_CREATE_CLOSED_RECORD_FROM_OPEN_RECORD,
spreadExprType);
errored = true;
}
return errored ? symTable.semanticError : symTable.noType;
} else {
BLangRecordVarNameField varNameField = (BLangRecordVarNameField) field;
TypeSymbolPair typeSymbolPair = checkRecordLiteralKeyExpr(varNameField, false,
(BRecordType) mappingType, data);
BVarSymbol fieldSymbol = typeSymbolPair.fieldSymbol;
if (fieldSymbol != null && Symbols.isOptional(fieldSymbol)) {
isOptional = true;
}
fieldType = typeSymbolPair.determinedType;
readOnlyConstructorField = varNameField.readonly;
pos = varNameField.pos;
fieldName = getVarNameFieldName(varNameField);
}
break;
case TypeTags.MAP:
if (spreadOpField) {
BLangExpression spreadExp = ((BLangRecordLiteral.BLangRecordSpreadOperatorField) field).expr;
BType spreadOpType = checkExpr(spreadExp, data);
BType spreadOpMemberType = checkSpreadFieldWithMapType(spreadOpType);
if (spreadOpMemberType.tag == symTable.semanticError.tag) {
dlog.error(spreadExp.pos, DiagnosticErrorCode.INCOMPATIBLE_TYPES_SPREAD_OP,
spreadOpType);
return symTable.semanticError;
}
return types.checkType(spreadExp.pos, spreadOpMemberType, ((BMapType) mappingType).constraint,
DiagnosticErrorCode.INCOMPATIBLE_TYPES);
}
boolean validMapKey;
if (keyValueField) {
BLangRecordKeyValueField keyValField = (BLangRecordKeyValueField) field;
BLangRecordKey key = keyValField.key;
validMapKey = checkValidJsonOrMapLiteralKeyExpr(key.expr, key.computedKey, data);
readOnlyConstructorField = keyValField.readonly;
pos = key.pos;
fieldName = getKeyValueFieldName(keyValField);
} else {
BLangRecordVarNameField varNameField = (BLangRecordVarNameField) field;
validMapKey = checkValidJsonOrMapLiteralKeyExpr(varNameField, false, data);
readOnlyConstructorField = varNameField.readonly;
pos = varNameField.pos;
fieldName = getVarNameFieldName(varNameField);
}
fieldType = validMapKey ? ((BMapType) mappingType).constraint : symTable.semanticError;
break;
}
if (readOnlyConstructorField) {
if (types.isSelectivelyImmutableType(fieldType, data.env.enclPkg.packageID)) {
fieldType =
ImmutableTypeCloner.getImmutableIntersectionType(pos, types, fieldType, data.env, symTable,
anonymousModelHelper, names, new HashSet<>());
} else if (!types.isInherentlyImmutableType(fieldType)) {
dlog.error(pos, DiagnosticErrorCode.INVALID_READONLY_MAPPING_FIELD, fieldName, fieldType);
fieldType = symTable.semanticError;
}
}
if (spreadOpField) {
valueExpr = ((BLangRecordLiteral.BLangRecordSpreadOperatorField) field).expr;
}
BLangExpression exprToCheck = valueExpr;
if (data.commonAnalyzerData.nonErrorLoggingCheck) {
exprToCheck = nodeCloner.cloneNode(valueExpr);
} else {
((BLangNode) field).setBType(fieldType);
}
return checkExpr(exprToCheck, data.env,
isOptional ? types.addNilForNillableAccessType(fieldType) : fieldType, data);
}

Line under review: errored = true;

Code after addressing the review:
private BType checkMappingField(RecordLiteralNode.RecordField field, BType mappingType, AnalyzerData data) {
BType fieldType = symTable.semanticError;
boolean keyValueField = field.isKeyValueField();
boolean spreadOpField = field.getKind() == NodeKind.RECORD_LITERAL_SPREAD_OP;
boolean readOnlyConstructorField = false;
String fieldName = null;
Location pos = null;
BLangExpression valueExpr = null;
if (keyValueField) {
valueExpr = ((BLangRecordKeyValueField) field).valueExpr;
} else if (!spreadOpField) {
valueExpr = (BLangRecordVarNameField) field;
}
boolean isOptional = false;
switch (mappingType.tag) {
case TypeTags.RECORD:
if (keyValueField) {
BLangRecordKeyValueField keyValField = (BLangRecordKeyValueField) field;
BLangRecordKey key = keyValField.key;
TypeSymbolPair typeSymbolPair = checkRecordLiteralKeyExpr(key.expr, key.computedKey,
(BRecordType) mappingType, data);
BVarSymbol fieldSymbol = typeSymbolPair.fieldSymbol;
if (fieldSymbol != null && Symbols.isOptional(fieldSymbol)) {
isOptional = true;
}
fieldType = typeSymbolPair.determinedType;
key.fieldSymbol = fieldSymbol;
readOnlyConstructorField = keyValField.readonly;
pos = key.expr.pos;
fieldName = getKeyValueFieldName(keyValField);
} else if (spreadOpField) {
BLangExpression spreadExpr = ((BLangRecordLiteral.BLangRecordSpreadOperatorField) field).expr;
checkExpr(spreadExpr, data);
BRecordType mappingRecordType = (BRecordType) mappingType;
BType spreadExprType = Types.getImpliedType(spreadExpr.getBType());
if (spreadExprType.tag == TypeTags.MAP) {
return types.checkType(spreadExpr.pos, ((BMapType) spreadExprType).constraint,
getAllFieldType(mappingRecordType),
DiagnosticErrorCode.INCOMPATIBLE_TYPES);
}
if (spreadExprType.tag != TypeTags.RECORD) {
dlog.error(spreadExpr.pos, DiagnosticErrorCode.INCOMPATIBLE_TYPES_SPREAD_OP,
spreadExprType);
return symTable.semanticError;
}
BRecordType spreadRecordType = (BRecordType) spreadExprType;
boolean errored = false;
for (BField bField : spreadRecordType.fields.values()) {
BType specFieldType = bField.type;
if (types.isNeverTypeOrStructureTypeWithARequiredNeverMember(specFieldType)) {
continue;
}
BSymbol fieldSymbol = symResolver.resolveStructField(spreadExpr.pos, data.env, bField.name,
mappingType.tsymbol);
BType expectedFieldType = checkRecordLiteralKeyByName(spreadExpr.pos, fieldSymbol, bField.name,
mappingRecordType);
if (expectedFieldType != symTable.semanticError &&
!types.isAssignable(specFieldType, expectedFieldType)) {
dlog.error(spreadExpr.pos, DiagnosticErrorCode.INCOMPATIBLE_TYPES_FIELD,
expectedFieldType, bField.name, specFieldType);
if (!errored) {
errored = true;
}
}
}
if (!spreadRecordType.sealed) {
if (mappingRecordType.sealed) {
dlog.error(spreadExpr.pos,
DiagnosticErrorCode.INVALID_SPREAD_FIELD_TO_CREATE_CLOSED_RECORD_FROM_OPEN_RECORD,
spreadRecordType);
errored = true;
} else if (!types.isAssignable(spreadRecordType.restFieldType,
mappingRecordType.restFieldType)) {
dlog.error(spreadExpr.pos,
DiagnosticErrorCode.INVALID_SPREAD_FIELD_REST_FIELD_MISMATCH,
spreadRecordType, spreadRecordType.restFieldType,
mappingRecordType.restFieldType);
errored = true;
}
}
return errored ? symTable.semanticError : symTable.noType;
} else {
BLangRecordVarNameField varNameField = (BLangRecordVarNameField) field;
TypeSymbolPair typeSymbolPair = checkRecordLiteralKeyExpr(varNameField, false,
(BRecordType) mappingType, data);
BVarSymbol fieldSymbol = typeSymbolPair.fieldSymbol;
if (fieldSymbol != null && Symbols.isOptional(fieldSymbol)) {
isOptional = true;
}
fieldType = typeSymbolPair.determinedType;
readOnlyConstructorField = varNameField.readonly;
pos = varNameField.pos;
fieldName = getVarNameFieldName(varNameField);
}
break;
case TypeTags.MAP:
if (spreadOpField) {
BLangExpression spreadExp = ((BLangRecordLiteral.BLangRecordSpreadOperatorField) field).expr;
BType spreadOpType = checkExpr(spreadExp, data);
BType spreadOpMemberType = checkSpreadFieldWithMapType(spreadOpType);
if (spreadOpMemberType.tag == symTable.semanticError.tag) {
dlog.error(spreadExp.pos, DiagnosticErrorCode.INCOMPATIBLE_TYPES_SPREAD_OP,
spreadOpType);
return symTable.semanticError;
}
return types.checkType(spreadExp.pos, spreadOpMemberType, ((BMapType) mappingType).constraint,
DiagnosticErrorCode.INCOMPATIBLE_TYPES);
}
boolean validMapKey;
if (keyValueField) {
BLangRecordKeyValueField keyValField = (BLangRecordKeyValueField) field;
BLangRecordKey key = keyValField.key;
validMapKey = checkValidJsonOrMapLiteralKeyExpr(key.expr, key.computedKey, data);
readOnlyConstructorField = keyValField.readonly;
pos = key.pos;
fieldName = getKeyValueFieldName(keyValField);
} else {
BLangRecordVarNameField varNameField = (BLangRecordVarNameField) field;
validMapKey = checkValidJsonOrMapLiteralKeyExpr(varNameField, false, data);
readOnlyConstructorField = varNameField.readonly;
pos = varNameField.pos;
fieldName = getVarNameFieldName(varNameField);
}
fieldType = validMapKey ? ((BMapType) mappingType).constraint : symTable.semanticError;
break;
}
if (readOnlyConstructorField) {
if (types.isSelectivelyImmutableType(fieldType, data.env.enclPkg.packageID)) {
fieldType =
ImmutableTypeCloner.getImmutableIntersectionType(pos, types, fieldType, data.env, symTable,
anonymousModelHelper, names, new HashSet<>());
} else if (!types.isInherentlyImmutableType(fieldType)) {
dlog.error(pos, DiagnosticErrorCode.INVALID_READONLY_MAPPING_FIELD, fieldName, fieldType);
fieldType = symTable.semanticError;
}
}
if (spreadOpField) {
valueExpr = ((BLangRecordLiteral.BLangRecordSpreadOperatorField) field).expr;
}
BLangExpression exprToCheck = valueExpr;
if (data.commonAnalyzerData.nonErrorLoggingCheck) {
exprToCheck = nodeCloner.cloneNode(valueExpr);
} else {
((BLangNode) field).setBType(fieldType);
}
return checkExpr(exprToCheck, data.env,
isOptional ? types.addNilForNillableAccessType(fieldType) : fieldType, data);
}