Code review comment: Do we need a new field btw? Haven't we come across the requirement to identify if we are in a match statement before?

Code under review:
private boolean validateErrorVariable(BLangErrorVariable errorVariable) {
        BErrorType errorType;
        switch (errorVariable.type.tag) {
            case TypeTags.UNION:
                BUnionType unionType = ((BUnionType) errorVariable.type);
                List<BErrorType> possibleTypes = unionType.getMemberTypes().stream()
                        .filter(type -> TypeTags.ERROR == type.tag)
                        .map(BErrorType.class::cast)
                        .collect(Collectors.toList());
                if (possibleTypes.isEmpty()) {
                    dlog.error(errorVariable.pos, DiagnosticCode.INVALID_ERROR_BINDING_PATTERN, errorVariable.type);
                    return false;
                }
                if (possibleTypes.size() > 1) {
                    LinkedHashSet<BType> detailType = new LinkedHashSet<>();
                    for (BErrorType possibleErrType : possibleTypes) {
                        detailType.add(possibleErrType.detailType);
                    }
                    BType errorDetailType = detailType.size() > 1
                                    ? BUnionType.create(null, detailType)
                                    : detailType.iterator().next();
                    errorType = new BErrorType(null, symTable.stringType,
                            errorDetailType);
                } else {
                    errorType = possibleTypes.get(0);
                }
                break;
            case TypeTags.ERROR:
                errorType = (BErrorType) errorVariable.type;
                break;
            default:
                dlog.error(errorVariable.pos, DiagnosticCode.INVALID_ERROR_BINDING_PATTERN, errorVariable.type);
                return false;
        }
        errorVariable.type = errorType;
        boolean isReasonIgnored = false;
        BLangSimpleVariable reasonVariable = errorVariable.reason;
        if (Names.IGNORE == names.fromIdNode(reasonVariable.name)) {
            reasonVariable.type = symTable.noType;
            isReasonIgnored = true;
        } else {
            errorVariable.reason.type = errorType.reasonType;
            errorVariable.reason.accept(this);
        }

        if (errorVariable.detail == null || (errorVariable.detail.isEmpty()
                && !isRestDetailBindingAvailable(errorVariable))) {
            if (isReasonIgnored) {
                dlog.error(errorVariable.pos, DiagnosticCode.NO_NEW_VARIABLES_VAR_ASSIGNMENT);
                return false;
            }
            return true;
        }

        if (errorType.detailType.getKind() == TypeKind.RECORD) {
            BRecordType recordType = (BRecordType) errorType.detailType;
            Map<String, BField> fieldMap = recordType.fields.stream()
                    .collect(Collectors.toMap(f -> f.name.value, f -> f));

            for (BLangErrorVariable.BLangErrorDetailEntry errorDetailEntry : errorVariable.detail) {
                String entryName = errorDetailEntry.key.getValue();
                BField entryField = fieldMap.get(entryName);

                BLangVariable boundVar = errorDetailEntry.valueBindingPattern;
                if (entryField != null) {
                    if ((entryField.symbol.flags & Flags.OPTIONAL) == Flags.OPTIONAL) {
                        boundVar.type = BUnionType.create(null, entryField.type, symTable.nilType);
                    } else {
                        boundVar.type = entryField.type;
                    }
                } else {
                    if (recordType.sealed) {
                        dlog.error(errorVariable.pos, DiagnosticCode.INVALID_ERROR_BINDING_PATTERN, errorVariable.type);
                        boundVar.type = symTable.semanticError;
                        return false;
                    } else {
                        boundVar.type = BUnionType.create(null, recordType.restFieldType, symTable.nilType);
                    }
                }

                boolean isIgnoredVar = boundVar.getKind() == NodeKind.VARIABLE
                        && ((BLangSimpleVariable) boundVar).name.value.equals(Names.IGNORE.value);
                if (!isIgnoredVar) {
                    boundVar.accept(this);
                }
            }

            if (isRestDetailBindingAvailable(errorVariable)) {
                BTypeSymbol typeSymbol = createTypeSymbol(SymTag.TYPE);
                BMapType restType = new BMapType(TypeTags.MAP, recordType.restFieldType, typeSymbol);
                typeSymbol.type = restType;
                errorVariable.restDetail.type = restType;
                errorVariable.restDetail.accept(this);
            }

            if (isReasonSpecified(errorVariable)
                    && !errorVariable.reasonVarPrefixAvailable
                    && errorVariable.reasonMatchConst == null
                    && errorVariable.isInMatchStmt) {

                BSymbol reasonConst = symResolver.lookupSymbol(
                        this.env.enclEnv, names.fromString(errorVariable.reason.name.value), SymTag.CONSTANT);
                if (reasonConst == symTable.notFoundSymbol) {
                    dlog.error(errorVariable.reason.pos, DiagnosticCode.INVALID_ERROR_REASON_BINDING_PATTERN,
                            errorVariable.reason.name);
                } else {
                    dlog.error(errorVariable.reason.pos, DiagnosticCode.UNSUPPORTED_ERROR_REASON_CONST_MATCH);
                }
                return false;
            }
            return true;

        } else if (errorType.detailType.getKind() == TypeKind.UNION) {
            BErrorTypeSymbol errorTypeSymbol = new BErrorTypeSymbol(SymTag.ERROR, Flags.PUBLIC, Names.ERROR,
                    env.enclPkg.packageID, symTable.errorType, env.scope.owner);
            
            errorVariable.type = new BErrorType(errorTypeSymbol, symTable.stringType, symTable.detailType);
            return validateErrorVariable(errorVariable);
        }

        if (isRestDetailBindingAvailable(errorVariable)) {
            
            errorVariable.restDetail.type = symTable.detailType;
            errorVariable.restDetail.accept(this);
        }
        return true;
    }

Line commented on: && errorVariable.isInMatchStmt) {

Revised code:
private boolean validateErrorVariable(BLangErrorVariable errorVariable) {
        BErrorType errorType;
        switch (errorVariable.type.tag) {
            case TypeTags.UNION:
                BUnionType unionType = ((BUnionType) errorVariable.type);
                List<BErrorType> possibleTypes = unionType.getMemberTypes().stream()
                        .filter(type -> TypeTags.ERROR == type.tag)
                        .map(BErrorType.class::cast)
                        .collect(Collectors.toList());
                if (possibleTypes.isEmpty()) {
                    dlog.error(errorVariable.pos, DiagnosticCode.INVALID_ERROR_BINDING_PATTERN, errorVariable.type);
                    return false;
                }
                if (possibleTypes.size() > 1) {
                    LinkedHashSet<BType> detailType = new LinkedHashSet<>();
                    for (BErrorType possibleErrType : possibleTypes) {
                        detailType.add(possibleErrType.detailType);
                    }
                    BType errorDetailType = detailType.size() > 1
                                    ? BUnionType.create(null, detailType)
                                    : detailType.iterator().next();
                    errorType = new BErrorType(null, symTable.stringType,
                            errorDetailType);
                } else {
                    errorType = possibleTypes.get(0);
                }
                break;
            case TypeTags.ERROR:
                errorType = (BErrorType) errorVariable.type;
                break;
            default:
                dlog.error(errorVariable.pos, DiagnosticCode.INVALID_ERROR_BINDING_PATTERN, errorVariable.type);
                return false;
        }
        errorVariable.type = errorType;
        boolean isReasonIgnored = false;
        BLangSimpleVariable reasonVariable = errorVariable.reason;
        if (Names.IGNORE == names.fromIdNode(reasonVariable.name)) {
            reasonVariable.type = symTable.noType;
            isReasonIgnored = true;
        } else {
            errorVariable.reason.type = errorType.reasonType;
            errorVariable.reason.accept(this);
        }

        if (errorVariable.detail == null || (errorVariable.detail.isEmpty()
                && !isRestDetailBindingAvailable(errorVariable))) {
            if (isReasonIgnored) {
                dlog.error(errorVariable.pos, DiagnosticCode.NO_NEW_VARIABLES_VAR_ASSIGNMENT);
                return false;
            }
            return true;
        }

        if (errorType.detailType.getKind() == TypeKind.RECORD) {
            return validateErrorVariable(errorVariable, errorType);
        } else if (errorType.detailType.getKind() == TypeKind.UNION) {
            BErrorTypeSymbol errorTypeSymbol = new BErrorTypeSymbol(SymTag.ERROR, Flags.PUBLIC, Names.ERROR,
                    env.enclPkg.packageID, symTable.errorType, env.scope.owner);
            
            errorVariable.type = new BErrorType(errorTypeSymbol, symTable.stringType, symTable.detailType);
            return validateErrorVariable(errorVariable);
        }

        if (isRestDetailBindingAvailable(errorVariable)) {
            
            errorVariable.restDetail.type = symTable.detailType;
            errorVariable.restDetail.accept(this);
        }
        return true;
    }