Open Bug 1670897 Opened 5 years ago Updated 3 years ago

After moving to C++20, support propagating errors from cleanup functions with QM_TRY

Categories

(Core :: Storage: Quota Manager, task)

task

Tracking

()

People

(Reporter: sg, Unassigned)

References

Details

To allow using a failable cleanup handler with QM_TRY, e.g.:

          nsDependentSubstring subdirNameBase;
          IDB_TRY(
              OkIf(GetFilenameBase(subdirName, kFileManagerDirectoryNameSuffix,
                                   subdirNameBase)),
              Ok{},
              ([&directory, &subdirName](const NotOk) -> Result<Ok, nsresult> {
                // If there is an unexpected directory in the idb directory,
                // trying to delete at first instead of breaking the whole
                // initialization.
                IDB_TRY(DeleteFilesNoQuota(directory, subdirName),
                        Err(NS_ERROR_UNEXPECTED));

                return Ok{};
              }));

the definition of QM_TRY_CUSTOM_RET_VAL_WITH_CLEANUP could be changed to:

#define QM_TRY_CUSTOM_RET_VAL_WITH_CLEANUP(ns, tryResult, expr, customRetVal, \
                                           cleanup)                           \
  auto tryResult = ::mozilla::ToResult(expr);                                 \
  static_assert(std::is_empty_v<typename decltype(tryResult)::ok_type>);      \
  if (MOZ_UNLIKELY(tryResult.isErr())) {                                      \
    auto tryTempError = tryResult.unwrapErr();                                \
    ns::QM_HANDLE_ERROR(expr);                                                \
    if constexpr (std::is_same_v<void, decltype(cleanup(tryTempError))>) {    \
      cleanup(tryTempError);                                                  \
    } else {                                                                  \
      QM_TRY_PROPAGATE_ERR(ns, MOZ_UNIQUE_VAR(tryResult),                     \
                           cleanup(tryTempError));                            \
    }                                                                         \
    return customRetVal;                                                      \
  }

This requires the C++20 feature of allowing the use of lambda expressions in unevaluated contexts.

Maybe there is a C++17 solution for this, but I didn't find one yet.

Actually, this is not possible: if constexpr can only be used in a template, so this part would somehow need to move inside a template.

Summary: After moving to C++20, supporting propagating errors from cleanup functions with QM_TRY → After moving to C++20, support propagating errors from cleanup functions with QM_TRY
Depends on: C++20
You need to log in before you can comment on or make changes to this bug.