Code review comment: This fails in https://github.com/ballerina-platform/ballerina-lang/blob/1b14109d11a9bf13e55f343b8517ff05abfd0d90/langlib/lang.query/src/main/ballerina/types.bal#L821  in `lang.query` lang-lib

Code under review:
public void visit(BLangRecordLiteral recordLiteral, AnalyzerData data) {
        List<RecordLiteralNode.RecordField> fields = recordLiteral.fields;
        BType recLiteralType = recordLiteral.getBType();

        for (RecordLiteralNode.RecordField field : fields) {
            if (field.isKeyValueField()) {
                analyzeExpr(((BLangRecordKeyValueField) field).valueExpr, data);

                BVarSymbol fieldSymbol = ((BLangRecordKeyValueField) field).key.fieldSymbol;
                if (fieldSymbol != null) {
                    reportIfDeprecatedUsage(fieldSymbol, fieldSymbol.toString(),
                            recordLiteral, ((BLangRecordKeyValueField) field).pos);
                }
            } else if (field.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
                BLangRecordLiteral.BLangRecordVarNameField recField
                        = (BLangRecordLiteral.BLangRecordVarNameField) field;
                analyzeExpr(recField, data);

                if (recLiteralType.tsymbol != null && recLiteralType.tsymbol.type != null
                        && recLiteralType.tsymbol.type.getKind() == TypeKind.RECORD) {

                    BRecordType recordType = (BRecordType) recLiteralType.tsymbol.type;
                    BField matchingField = recordType.getFields().get(recField.symbol.getName().getValue());

                    if (matchingField != null && matchingField.symbol != null) {
                        reportIfDeprecatedUsage(matchingField.symbol, matchingField.name.getValue(),
                                recordLiteral, ((BLangRecordLiteral.BLangRecordVarNameField) field).pos);
                    }
                }
            } else {
                BLangRecordLiteral.BLangRecordSpreadOperatorField spreadField
                        = (BLangRecordLiteral.BLangRecordSpreadOperatorField) field;
                analyzeExpr(spreadField.expr, data);

                BType spreadFieldType = Types.getReferredType(spreadField.expr.getBType());
                BType contextType = Types.getReferredType(recLiteralType);

                if (spreadFieldType != null && spreadFieldType.getKind() == TypeKind.RECORD
                        && contextType.getKind() == TypeKind.RECORD) {
                    for (BField fieldEntry: ((BRecordType) spreadFieldType).getFields().values()) {
                        BRecordType recordType = (BRecordType) contextType;
                        BField matchingField = recordType.getFields().get(fieldEntry.getName().getValue());

                        if (matchingField != null && matchingField.symbol != null) {
                            reportIfDeprecatedUsage(matchingField.symbol, matchingField.name.getValue(),
                                    recordLiteral, spreadField.expr.pos);
                        }
                    }
                }
            }
        }

        Set<Object> names = new HashSet<>();
        Set<Object> neverTypedKeys = new HashSet<>();
        BType type = Types.getReferredType(recLiteralType);
        boolean isRecord = type.tag == TypeTags.RECORD;
        boolean isOpenRecord = isRecord && !((BRecordType) type).sealed;

        
        
        boolean isInferredRecordForMapCET = isRecord && recordLiteral.expectedType != null &&
                recordLiteral.expectedType.tag == TypeTags.MAP;

        BLangRecordLiteral.BLangRecordSpreadOperatorField inclusiveTypeSpreadField = null;
        for (RecordLiteralNode.RecordField field : fields) {

            BLangExpression keyExpr;

            if (field.getKind() == NodeKind.RECORD_LITERAL_SPREAD_OP) {
                BLangRecordLiteral.BLangRecordSpreadOperatorField spreadOpField =
                        (BLangRecordLiteral.BLangRecordSpreadOperatorField) field;
                BLangExpression spreadOpExpr = spreadOpField.expr;

                analyzeExpr(spreadOpExpr, data);

                BType spreadOpExprType = Types.getReferredType(spreadOpExpr.getBType());
                int spreadFieldTypeTag = spreadOpExprType.tag;
                if (spreadFieldTypeTag == TypeTags.MAP) {
                    if (inclusiveTypeSpreadField != null) {
                        this.dlog.error(spreadOpExpr.pos, DiagnosticErrorCode.MULTIPLE_INCLUSIVE_TYPES);
                        continue;
                    }
                    inclusiveTypeSpreadField = spreadOpField;

                    if (fields.size() > 1) {
                        if (names.size() > 0) {
                            this.dlog.error(spreadOpExpr.pos,
                                            DiagnosticErrorCode.SPREAD_FIELD_MAY_DULPICATE_ALREADY_SPECIFIED_KEYS,
                                            spreadOpExpr);
                        }
                        
                        continue;
                    }
                }

                if (spreadFieldTypeTag != TypeTags.RECORD) {
                    continue;
                }

                BRecordType spreadExprRecordType = (BRecordType) spreadOpExprType;
                boolean isSpreadExprRecordTypeSealed = spreadExprRecordType.sealed;
                if (!isSpreadExprRecordTypeSealed) {
                    
                    if (inclusiveTypeSpreadField != null) {
                        this.dlog.error(spreadOpExpr.pos, DiagnosticErrorCode.MULTIPLE_INCLUSIVE_TYPES);
                    } else {
                        inclusiveTypeSpreadField = spreadOpField;
                    }
                }

                LinkedHashMap<String, BField> fieldsInRecordType = getUnescapedFieldList(spreadExprRecordType.fields);

                for (Object fieldName : names) {
                    if (!fieldsInRecordType.containsKey(fieldName) && !isSpreadExprRecordTypeSealed) {
                        this.dlog.error(spreadOpExpr.pos,
                                DiagnosticErrorCode.SPREAD_FIELD_MAY_DULPICATE_ALREADY_SPECIFIED_KEYS,
                                spreadOpExpr);
                        break;
                    }
                }

                for (String fieldName : fieldsInRecordType.keySet()) {
                    BField bField = fieldsInRecordType.get(fieldName);
                    if (names.contains(fieldName)) {
                        if (bField.type.tag != TypeTags.NEVER) {
                            this.dlog.error(spreadOpExpr.pos,
                                    DiagnosticErrorCode.DUPLICATE_KEY_IN_RECORD_LITERAL_SPREAD_OP,
                                    type.getKind().typeName(), fieldName, spreadOpField);
                        }
                        continue;
                    }

                    if (bField.type.tag == TypeTags.NEVER) {
                        neverTypedKeys.add(fieldName);
                        continue;
                    }

                    if (!neverTypedKeys.remove(fieldName) &&
                            inclusiveTypeSpreadField != null && isSpreadExprRecordTypeSealed) {
                        this.dlog.error(spreadOpExpr.pos,
                                DiagnosticErrorCode.POSSIBLE_DUPLICATE_OF_FIELD_SPECIFIED_VIA_SPREAD_OP,
                                Types.getReferredType(recordLiteral.expectedType).getKind().typeName(),
                                bField.symbol, spreadOpField);
                    }
                    names.add(fieldName);
                }

            } else {
                if (field.isKeyValueField()) {
                    BLangRecordLiteral.BLangRecordKey key = ((BLangRecordKeyValueField) field).key;
                    keyExpr = key.expr;
                    if (key.computedKey) {
                        analyzeExpr(keyExpr, data);
                        continue;
                    }
                } else {
                    keyExpr = (BLangRecordLiteral.BLangRecordVarNameField) field;
                }

                if (keyExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
                    String name = ((BLangSimpleVarRef) keyExpr).variableName.value;
                    String unescapedName = Utils.unescapeJava(name);
                    if (names.contains(unescapedName)) {
                        this.dlog.error(keyExpr.pos, DiagnosticErrorCode.DUPLICATE_KEY_IN_MAPPING_CONSTRUCTOR,
                                        Types.getReferredType(recordLiteral.expectedType).getKind().typeName(),
                                        unescapedName);
                    } else if (inclusiveTypeSpreadField != null && !neverTypedKeys.contains(unescapedName)) {
                        this.dlog.error(keyExpr.pos,
                                        DiagnosticErrorCode.POSSIBLE_DUPLICATE_OF_FIELD_SPECIFIED_VIA_SPREAD_OP,
                                unescapedName, inclusiveTypeSpreadField);
                    }

                    if (!isInferredRecordForMapCET && isOpenRecord && !((BRecordType) type).fields.containsKey(name)) {
                        dlog.error(keyExpr.pos, DiagnosticErrorCode.INVALID_RECORD_LITERAL_IDENTIFIER_KEY,
                                unescapedName);
                    }

                    names.add(unescapedName);
                } else if (keyExpr.getKind() == NodeKind.LITERAL || keyExpr.getKind() == NodeKind.NUMERIC_LITERAL) {
                    Object name = ((BLangLiteral) keyExpr).value;
                    if (names.contains(name)) {
                        this.dlog.error(keyExpr.pos, DiagnosticErrorCode.DUPLICATE_KEY_IN_MAPPING_CONSTRUCTOR,
                                Types.getReferredType(recordLiteral.parent.getBType())
                                        .getKind().typeName(), name);
                    } else if (inclusiveTypeSpreadField != null && !neverTypedKeys.contains(name)) {
                        this.dlog.error(keyExpr.pos,
                                        DiagnosticErrorCode.POSSIBLE_DUPLICATE_OF_FIELD_SPECIFIED_VIA_SPREAD_OP,
                                        name, inclusiveTypeSpreadField);
                    }
                    names.add(name);
                }
            }
        }

        if (isInferredRecordForMapCET) {
            recordLiteral.expectedType = type;
        }
    }

Line commented on: if (fieldSymbol != null) {

Revised code:
public void visit(BLangRecordLiteral recordLiteral, AnalyzerData data) {
        BType referredType = Types.getReferredType(recordLiteral.getBType());
        boolean isRecord = referredType.tag == TypeTags.RECORD;
        List<RecordLiteralNode.RecordField> recordLiteralFields = recordLiteral.fields;

        LinkedHashMap<String, BField> recordFields = null;
        if (isRecord) {
            recordFields = ((BRecordType) referredType).getFields();
        }

        for (RecordLiteralNode.RecordField field : recordLiteralFields) {
            if (field.isKeyValueField()) {
                analyzeExpr(((BLangRecordKeyValueField) field).valueExpr, data);
                reportIfDeprecatedUsage(((BLangRecordKeyValueField) field).key.fieldSymbol, recordLiteral,
                        ((BLangRecordKeyValueField) field).pos);
            } else if (field.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
                BLangRecordLiteral.BLangRecordVarNameField recField
                        = (BLangRecordLiteral.BLangRecordVarNameField) field;
                analyzeExpr(recField, data);

                if (isRecord) {
                    BField matchingField = recordFields.get(recField.symbol.getName().getValue());
                    if (matchingField != null) {
                        reportIfDeprecatedUsage(matchingField.symbol, recordLiteral, recField.pos);
                    }
                }
            } else {
                BLangRecordLiteral.BLangRecordSpreadOperatorField spreadField
                        = (BLangRecordLiteral.BLangRecordSpreadOperatorField) field;
                analyzeExpr(spreadField.expr, data);

                BType spreadFieldType = Types.getReferredType(spreadField.expr.getBType());
                if (isRecord && spreadFieldType != null && spreadFieldType.tag == TypeTags.RECORD) {
                    for (BField fieldEntry: ((BRecordType) spreadFieldType).getFields().values()) {
                        BField matchingField = recordFields.get(fieldEntry.getName().getValue());
                        if (matchingField != null) {
                            reportIfDeprecatedUsage(matchingField.symbol, recordLiteral, spreadField.expr.pos);
                        }
                    }
                }
            }
        }

        Set<Object> names = new HashSet<>();
        Set<Object> neverTypedKeys = new HashSet<>();
        boolean isOpenRecord = isRecord && !((BRecordType) referredType).sealed;

        
        
        boolean isInferredRecordForMapCET = isRecord && recordLiteral.expectedType != null &&
                recordLiteral.expectedType.tag == TypeTags.MAP;

        BLangRecordLiteral.BLangRecordSpreadOperatorField inclusiveTypeSpreadField = null;
        for (RecordLiteralNode.RecordField field : recordLiteralFields) {

            BLangExpression keyExpr;

            if (field.getKind() == NodeKind.RECORD_LITERAL_SPREAD_OP) {
                BLangRecordLiteral.BLangRecordSpreadOperatorField spreadOpField =
                        (BLangRecordLiteral.BLangRecordSpreadOperatorField) field;
                BLangExpression spreadOpExpr = spreadOpField.expr;

                analyzeExpr(spreadOpExpr, data);

                BType spreadOpExprType = Types.getReferredType(spreadOpExpr.getBType());
                int spreadFieldTypeTag = spreadOpExprType.tag;
                if (spreadFieldTypeTag == TypeTags.MAP) {
                    if (inclusiveTypeSpreadField != null) {
                        this.dlog.error(spreadOpExpr.pos, DiagnosticErrorCode.MULTIPLE_INCLUSIVE_TYPES);
                        continue;
                    }
                    inclusiveTypeSpreadField = spreadOpField;

                    if (recordLiteralFields.size() > 1) {
                        if (names.size() > 0) {
                            this.dlog.error(spreadOpExpr.pos,
                                            DiagnosticErrorCode.SPREAD_FIELD_MAY_DULPICATE_ALREADY_SPECIFIED_KEYS,
                                            spreadOpExpr);
                        }
                        
                        continue;
                    }
                }

                if (spreadFieldTypeTag != TypeTags.RECORD) {
                    continue;
                }

                BRecordType spreadExprRecordType = (BRecordType) spreadOpExprType;
                boolean isSpreadExprRecordTypeSealed = spreadExprRecordType.sealed;
                if (!isSpreadExprRecordTypeSealed) {
                    
                    if (inclusiveTypeSpreadField != null) {
                        this.dlog.error(spreadOpExpr.pos, DiagnosticErrorCode.MULTIPLE_INCLUSIVE_TYPES);
                    } else {
                        inclusiveTypeSpreadField = spreadOpField;
                    }
                }

                LinkedHashMap<String, BField> fieldsInRecordType = getUnescapedFieldList(spreadExprRecordType.fields);

                for (Object fieldName : names) {
                    if (!fieldsInRecordType.containsKey(fieldName) && !isSpreadExprRecordTypeSealed) {
                        this.dlog.error(spreadOpExpr.pos,
                                DiagnosticErrorCode.SPREAD_FIELD_MAY_DULPICATE_ALREADY_SPECIFIED_KEYS,
                                spreadOpExpr);
                        break;
                    }
                }

                for (String fieldName : fieldsInRecordType.keySet()) {
                    BField bField = fieldsInRecordType.get(fieldName);
                    if (names.contains(fieldName)) {
                        if (bField.type.tag != TypeTags.NEVER) {
                            this.dlog.error(spreadOpExpr.pos,
                                    DiagnosticErrorCode.DUPLICATE_KEY_IN_RECORD_LITERAL_SPREAD_OP,
                                    referredType.getKind().typeName(), fieldName, spreadOpField);
                        }
                        continue;
                    }

                    if (bField.type.tag == TypeTags.NEVER) {
                        neverTypedKeys.add(fieldName);
                        continue;
                    }

                    if (!neverTypedKeys.remove(fieldName) &&
                            inclusiveTypeSpreadField != null && isSpreadExprRecordTypeSealed) {
                        this.dlog.error(spreadOpExpr.pos,
                                DiagnosticErrorCode.POSSIBLE_DUPLICATE_OF_FIELD_SPECIFIED_VIA_SPREAD_OP,
                                Types.getReferredType(recordLiteral.expectedType).getKind().typeName(),
                                bField.symbol, spreadOpField);
                    }
                    names.add(fieldName);
                }

            } else {
                if (field.isKeyValueField()) {
                    BLangRecordLiteral.BLangRecordKey key = ((BLangRecordKeyValueField) field).key;
                    keyExpr = key.expr;
                    if (key.computedKey) {
                        analyzeExpr(keyExpr, data);
                        continue;
                    }
                } else {
                    keyExpr = (BLangRecordLiteral.BLangRecordVarNameField) field;
                }

                if (keyExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
                    String name = ((BLangSimpleVarRef) keyExpr).variableName.value;
                    String unescapedName = Utils.unescapeJava(name);
                    if (names.contains(unescapedName)) {
                        this.dlog.error(keyExpr.pos, DiagnosticErrorCode.DUPLICATE_KEY_IN_MAPPING_CONSTRUCTOR,
                                        Types.getReferredType(recordLiteral.expectedType).getKind().typeName(),
                                        unescapedName);
                    } else if (inclusiveTypeSpreadField != null && !neverTypedKeys.contains(unescapedName)) {
                        this.dlog.error(keyExpr.pos,
                                        DiagnosticErrorCode.POSSIBLE_DUPLICATE_OF_FIELD_SPECIFIED_VIA_SPREAD_OP,
                                unescapedName, inclusiveTypeSpreadField);
                    }

                    if (!isInferredRecordForMapCET && isOpenRecord
                            && !((BRecordType) referredType).fields.containsKey(name)) {
                        dlog.error(keyExpr.pos, DiagnosticErrorCode.INVALID_RECORD_LITERAL_IDENTIFIER_KEY,
                                unescapedName);
                    }

                    names.add(unescapedName);
                } else if (keyExpr.getKind() == NodeKind.LITERAL || keyExpr.getKind() == NodeKind.NUMERIC_LITERAL) {
                    Object name = ((BLangLiteral) keyExpr).value;
                    if (names.contains(name)) {
                        this.dlog.error(keyExpr.pos, DiagnosticErrorCode.DUPLICATE_KEY_IN_MAPPING_CONSTRUCTOR,
                                Types.getReferredType(recordLiteral.parent.getBType())
                                        .getKind().typeName(), name);
                    } else if (inclusiveTypeSpreadField != null && !neverTypedKeys.contains(name)) {
                        this.dlog.error(keyExpr.pos,
                                        DiagnosticErrorCode.POSSIBLE_DUPLICATE_OF_FIELD_SPECIFIED_VIA_SPREAD_OP,
                                        name, inclusiveTypeSpreadField);
                    }
                    names.add(name);
                }
            }
        }

        if (isInferredRecordForMapCET) {
            recordLiteral.expectedType = referredType;
        }
    }