Commit 704df442 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

simplify catch clauses constructing exception_wrapper

Summary:
Now that `exception_wrapper` can access the exception object directly from the `std::exception_ptr`, it no longer needs to be passed the exception object separately.

This eliminates duplicative `catch (exception const&)` clauses which primarily construct `exception_wrapper` instances, reducing build artifact size.

Reviewed By: akrieger

Differential Revision: D27888763

fbshipit-source-id: 5f60a07083b3c8d818f0eafd5b17afcfca239ab8
parent fdc8edc2
......@@ -65,8 +65,8 @@ class File {
static Expected<File, exception_wrapper> makeFile(Args&&... args) noexcept {
try {
return File(std::forward<Args>(args)...);
} catch (const std::system_error& se) {
return makeUnexpected(exception_wrapper(std::current_exception(), se));
} catch (const std::system_error&) {
return makeUnexpected(exception_wrapper(std::current_exception()));
}
}
......
......@@ -255,8 +255,6 @@ makeTryWithNoUnwrap(F&& f) {
using ResultType = invoke_result_t<F>;
try {
return Try<ResultType>(f());
} catch (std::exception& e) {
return Try<ResultType>(exception_wrapper(std::current_exception(), e));
} catch (...) {
return Try<ResultType>(exception_wrapper(std::current_exception()));
}
......@@ -269,8 +267,6 @@ typename std::
try {
f();
return Try<void>();
} catch (std::exception& e) {
return Try<void>(exception_wrapper(std::current_exception(), e));
} catch (...) {
return Try<void>(exception_wrapper(std::current_exception()));
}
......@@ -290,8 +286,6 @@ typename std::enable_if<isTry<invoke_result_t<F>>::value, invoke_result_t<F>>::
using ResultType = invoke_result_t<F>;
try {
return f();
} catch (std::exception& e) {
return ResultType(exception_wrapper(std::current_exception(), e));
} catch (...) {
return ResultType(exception_wrapper(std::current_exception()));
}
......@@ -301,9 +295,6 @@ template <typename T, typename... Args>
T* tryEmplace(Try<T>& t, Args&&... args) noexcept {
try {
return std::addressof(t.emplace(static_cast<Args&&>(args)...));
} catch (const std::exception& ex) {
t.emplaceException(std::current_exception(), ex);
return nullptr;
} catch (...) {
t.emplaceException(std::current_exception());
return nullptr;
......@@ -321,9 +312,6 @@ T* tryEmplaceWith(Try<T>& t, Func&& func) noexcept {
"Unable to initialise a value of type T with the result of 'func'");
try {
return std::addressof(t.emplace(static_cast<Func&&>(func)()));
} catch (const std::exception& ex) {
t.emplaceException(std::current_exception(), ex);
return nullptr;
} catch (...) {
t.emplaceException(std::current_exception());
return nullptr;
......@@ -339,9 +327,6 @@ bool tryEmplaceWith(Try<void>& t, Func&& func) noexcept {
static_cast<Func&&>(func)();
t.emplace();
return true;
} catch (const std::exception& ex) {
t.emplaceException(std::current_exception(), ex);
return false;
} catch (...) {
t.emplaceException(std::current_exception());
return false;
......@@ -379,8 +364,6 @@ template <typename T>
void tryAssign(Try<T>& t, Try<T>&& other) noexcept {
try {
t = std::move(other);
} catch (const std::exception& ex) {
t.emplaceException(std::current_exception(), ex);
} catch (...) {
t.emplaceException(std::current_exception());
}
......
......@@ -832,8 +832,6 @@ exception_wrapper SchemaValidator::try_validate(
if (auto se = validate(vc, value)) {
return make_exception_wrapper<SchemaError>(*se);
}
} catch (const std::exception& e) {
return exception_wrapper(std::current_exception(), e);
} catch (...) {
return exception_wrapper(std::current_exception());
}
......
......@@ -207,8 +207,7 @@ class AsyncGeneratorPromise {
if (state_ == State::EXCEPTION_WRAPPER) {
return std::move(exceptionWrapper_.get());
} else {
return exception_wrapper::from_exception_ptr(
std::move(exceptionPtr_.get()));
return exception_wrapper(std::move(exceptionPtr_.get()));
}
}
......
......@@ -98,7 +98,7 @@ class BlockingWaitPromise final : public BlockingWaitPromiseBase {
void unhandled_exception() noexcept {
result_->emplaceException(
folly::exception_wrapper::from_exception_ptr(std::current_exception()));
folly::exception_wrapper{std::current_exception()});
}
template <
......@@ -126,7 +126,7 @@ class BlockingWaitPromise<T&> final : public BlockingWaitPromiseBase {
void unhandled_exception() noexcept {
result_->emplaceException(
folly::exception_wrapper::from_exception_ptr(std::current_exception()));
folly::exception_wrapper{std::current_exception()});
}
auto yield_value(T&& value) noexcept {
......@@ -171,8 +171,7 @@ class BlockingWaitPromise<void> final : public BlockingWaitPromiseBase {
void return_void() noexcept {}
void unhandled_exception() noexcept {
result_->emplaceException(
exception_wrapper::from_exception_ptr(std::current_exception()));
result_->emplaceException(exception_wrapper{std::current_exception()});
}
void setTry(folly::Try<void>* result) noexcept { result_ = result; }
......
......@@ -59,11 +59,6 @@ BarrierTask makeCollectAllTryTask(
co_withCancellation(
cancelToken, static_cast<SemiAwaitable&&>(awaitable))));
}
// This causes clang internal error on Windows.
#if !(defined(_WIN32) && defined(__clang__))
} catch (const std::exception& ex) {
result.emplaceException(std::current_exception(), ex);
#endif
} catch (...) {
result.emplaceException(std::current_exception());
}
......@@ -156,12 +151,6 @@ auto collectAllImpl(
co_withCancellation(
cancelToken, static_cast<decltype(awaitable)>(awaitable))));
}
} catch (const std::exception& ex) {
anyFailures.store(true, std::memory_order_relaxed);
if (!cancelSource.requestCancellation()) {
// This was the first failure, remember it's error.
firstException = exception_wrapper{std::current_exception(), ex};
}
} catch (...) {
anyFailures.store(true, std::memory_order_relaxed);
if (!cancelSource.requestCancellation()) {
......@@ -339,11 +328,6 @@ auto collectAllRange(InputRange awaitables)
tryResults[index].emplace(co_await co_viaIfAsync(
executor.get_alias(),
co_withCancellation(cancelToken, std::move(semiAwaitable))));
} catch (const std::exception& ex) {
anyFailures.store(true, std::memory_order_relaxed);
if (!cancelSource.requestCancellation()) {
firstException = exception_wrapper{std::current_exception(), ex};
}
} catch (...) {
anyFailures.store(true, std::memory_order_relaxed);
if (!cancelSource.requestCancellation()) {
......@@ -430,11 +414,6 @@ auto collectAllRange(InputRange awaitables) -> folly::coro::Task<void> {
co_await co_viaIfAsync(
executor.get_alias(),
co_withCancellation(cancelToken, std::move(semiAwaitable)));
} catch (const std::exception& ex) {
anyFailures.store(true, std::memory_order_relaxed);
if (!cancelSource.requestCancellation()) {
firstException = exception_wrapper{std::current_exception(), ex};
}
} catch (...) {
anyFailures.store(true, std::memory_order_relaxed);
if (!cancelSource.requestCancellation()) {
......@@ -510,12 +489,6 @@ auto collectAllTryRange(InputRange awaitables)
executor.get_alias(),
co_withCancellation(cancelToken, std::move(semiAwaitable))));
}
// This causes "Instruction does not dominate all uses!" internal compiler
// error on Windows with Clang.
#if !(defined(_WIN32) && defined(__clang__))
} catch (const std::exception& ex) {
result.emplaceException(std::current_exception(), ex);
#endif
} catch (...) {
result.emplaceException(std::current_exception());
}
......@@ -608,9 +581,6 @@ auto collectAllWindowed(InputRange awaitables, std::size_t maxConcurrency)
try {
awaitable.emplace(*iter);
++iter;
} catch (const std::exception& ex) {
iterationException = exception_wrapper{std::current_exception(), ex};
cancelSource.requestCancellation();
} catch (...) {
iterationException = exception_wrapper{std::current_exception()};
cancelSource.requestCancellation();
......@@ -626,8 +596,6 @@ auto collectAllWindowed(InputRange awaitables, std::size_t maxConcurrency)
co_await co_viaIfAsync(
executor.get_alias(),
co_withCancellation(cancelToken, std::move(*awaitable)));
} catch (const std::exception& ex) {
trySetFirstException(exception_wrapper{std::current_exception(), ex});
} catch (...) {
trySetFirstException(exception_wrapper{std::current_exception()});
}
......@@ -669,11 +637,6 @@ auto collectAllWindowed(InputRange awaitables, std::size_t maxConcurrency)
lock = co_await mutex.co_scoped_lock();
}
} catch (const std::exception& ex) {
// Only a fatal error if we failed to create any worker tasks.
if (workerTasks.empty()) {
iterationException = exception_wrapper{std::current_exception(), ex};
}
} catch (...) {
if (workerTasks.empty()) {
iterationException = exception_wrapper{std::current_exception()};
......@@ -748,9 +711,6 @@ auto collectAllWindowed(InputRange awaitables, std::size_t maxConcurrency)
tryResults.emplace_back();
awaitable.emplace(*iter);
++iter;
} catch (const std::exception& ex) {
iterationException = exception_wrapper{std::current_exception(), ex};
cancelSource.requestCancellation();
} catch (...) {
iterationException = exception_wrapper{std::current_exception()};
cancelSource.requestCancellation();
......@@ -771,8 +731,6 @@ auto collectAllWindowed(InputRange awaitables, std::size_t maxConcurrency)
executor.get_alias(),
co_withCancellation(
cancelToken, static_cast<awaitable_t&&>(*awaitable))));
} catch (const std::exception& ex) {
trySetFirstException(exception_wrapper{std::current_exception(), ex});
} catch (...) {
trySetFirstException(exception_wrapper{std::current_exception()});
}
......@@ -782,8 +740,6 @@ auto collectAllWindowed(InputRange awaitables, std::size_t maxConcurrency)
try {
tryResults[thisIndex] = std::move(tryResult);
} catch (const std::exception& ex) {
trySetFirstException(exception_wrapper{std::current_exception(), ex});
} catch (...) {
trySetFirstException(exception_wrapper{std::current_exception()});
}
......@@ -824,14 +780,10 @@ auto collectAllWindowed(InputRange awaitables, std::size_t maxConcurrency)
lock = co_await mutex.co_scoped_lock();
}
} catch (const std::exception& ex) {
} catch (...) {
// Only a fatal error if we failed to create any worker tasks.
if (workerTasks.empty()) {
// No need to synchronise here. There are no concurrent tasks running.
iterationException = exception_wrapper{std::current_exception(), ex};
}
} catch (...) {
if (workerTasks.empty()) {
iterationException = exception_wrapper{std::current_exception()};
}
}
......@@ -899,8 +851,6 @@ auto collectAllTryWindowed(InputRange awaitables, std::size_t maxConcurrency)
results.emplace_back();
awaitable.emplace(*iter);
++iter;
} catch (const std::exception& ex) {
iterationException = exception_wrapper{std::current_exception(), ex};
} catch (...) {
iterationException = exception_wrapper{std::current_exception()};
}
......@@ -926,8 +876,6 @@ auto collectAllTryWindowed(InputRange awaitables, std::size_t maxConcurrency)
executor.get_alias(),
co_withCancellation(cancelToken, std::move(*awaitable))));
}
} catch (const std::exception& ex) {
result.emplaceException(std::current_exception(), ex);
} catch (...) {
result.emplaceException(std::current_exception());
}
......@@ -937,8 +885,6 @@ auto collectAllTryWindowed(InputRange awaitables, std::size_t maxConcurrency)
try {
results[thisIndex] = std::move(result);
} catch (const std::exception& ex) {
results[thisIndex].emplaceException(std::current_exception(), ex);
} catch (...) {
results[thisIndex].emplaceException(std::current_exception());
}
......@@ -976,14 +922,10 @@ auto collectAllTryWindowed(InputRange awaitables, std::size_t maxConcurrency)
lock = co_await mutex.co_scoped_lock();
}
} catch (const std::exception& ex) {
} catch (...) {
// Failure to create a worker is an error if we failed
// to create _any_ workers. As long as we created one then
// the algorithm should still be able to make forward progress.
if (workerTasks.empty()) {
iterationException = exception_wrapper{std::current_exception(), ex};
}
} catch (...) {
if (workerTasks.empty()) {
iterationException = exception_wrapper{std::current_exception()};
}
......
......@@ -29,8 +29,6 @@ AsyncGenerator<CallbackRecord<Reference>, CallbackRecord<Value>> materialize(
while (auto item = co_await source.next()) {
co_yield EventType{callback_record_value, *std::move(item)};
}
} catch (const std::exception& e) {
ex = folly::exception_wrapper{std::current_exception(), e};
} catch (...) {
ex = folly::exception_wrapper{std::current_exception()};
}
......
......@@ -94,8 +94,6 @@ AsyncGenerator<Reference, Value> merge(
break;
}
}
} catch (const std::exception& e) {
ex = exception_wrapper{std::current_exception(), e};
} catch (...) {
ex = exception_wrapper{std::current_exception()};
}
......@@ -125,8 +123,6 @@ AsyncGenerator<Reference, Value> merge(
}
makeWorkerTask(state, *std::move(item)).start(&barrier, asyncFrame);
}
} catch (const std::exception& e) {
ex = exception_wrapper{std::current_exception(), e};
} catch (...) {
ex = exception_wrapper{std::current_exception()};
}
......
......@@ -66,8 +66,6 @@ auto retryWhen(Func func, RetryDelayFunc retryDelay)
assert(result.hasException());
error = std::move(result.exception());
}
} catch (const std::exception& e) {
error = exception_wrapper(std::current_exception(), e);
} catch (...) {
error = exception_wrapper(std::current_exception());
}
......
......@@ -154,8 +154,7 @@ class TaskPromise : public TaskPromiseBase {
Task<T> get_return_object() noexcept;
void unhandled_exception() noexcept {
result_.emplaceException(
exception_wrapper::from_exception_ptr(std::current_exception()));
result_.emplaceException(exception_wrapper{std::current_exception()});
}
template <typename U = T>
......@@ -211,8 +210,7 @@ class TaskPromise<void> : public TaskPromiseBase {
Task<void> get_return_object() noexcept;
void unhandled_exception() noexcept {
result_.emplaceException(
exception_wrapper::from_exception_ptr(std::current_exception()));
result_.emplaceException(exception_wrapper{std::current_exception()});
}
void return_void() noexcept { result_.emplace(); }
......@@ -360,11 +358,6 @@ class FOLLY_NODISCARD TaskWithExecutor {
detail::InlineTaskDetached startImpl(TaskWithExecutor task, F cb) {
try {
cb(co_await folly::coro::co_awaitTry(std::move(task)));
// This causes clang internal error on Windows.
#if !(defined(_WIN32) && defined(__clang__))
} catch (const std::exception& e) {
cb(Try<StorageType>(exception_wrapper(std::current_exception(), e)));
#endif
} catch (...) {
cb(Try<StorageType>(exception_wrapper(std::current_exception())));
}
......@@ -374,11 +367,6 @@ class FOLLY_NODISCARD TaskWithExecutor {
detail::InlineTaskDetached startInlineImpl(TaskWithExecutor task, F cb) {
try {
cb(co_await InlineTryAwaitable{std::exchange(task.coro_, {})});
// This causes clang internal error on Windows.
#if !(defined(_WIN32) && defined(__clang__))
} catch (const std::exception& e) {
cb(Try<StorageType>(exception_wrapper(std::current_exception(), e)));
#endif
} catch (...) {
cb(Try<StorageType>(exception_wrapper(std::current_exception())));
}
......
......@@ -82,8 +82,6 @@ Task<typename semi_await_try_result_t<SemiAwaitable>::element_type> timeout(
}
co_return std::move(resultTry).value();
} catch (const std::exception& ex) {
error = exception_wrapper{std::current_exception(), ex};
} catch (...) {
error = exception_wrapper{std::current_exception()};
}
......
......@@ -116,7 +116,7 @@ class InlineTaskPromise : public InlineTaskPromiseBase {
void unhandled_exception() noexcept {
result_.emplaceException(
folly::exception_wrapper::from_exception_ptr(std::current_exception()));
folly::exception_wrapper{std::current_exception()});
}
T result() { return std::move(result_).value(); }
......@@ -143,7 +143,7 @@ class InlineTaskPromise<void> : public InlineTaskPromiseBase {
void unhandled_exception() noexcept {
result_.emplaceException(
folly::exception_wrapper::from_exception_ptr(std::current_exception()));
folly::exception_wrapper{std::current_exception()});
}
void result() { return result_.value(); }
......
......@@ -122,11 +122,6 @@ class BatchDispatcher {
for (size_t i = 0; i < promises.size(); i++) {
promises[i].setValue(std::move(results[i]));
}
} catch (const std::exception& ex) {
for (size_t i = 0; i < promises.size(); i++) {
promises[i].setException(
exception_wrapper(std::current_exception(), ex));
}
} catch (...) {
for (size_t i = 0; i < promises.size(); i++) {
promises[i].setException(exception_wrapper(std::current_exception()));
......
......@@ -587,9 +587,6 @@ makeSemiFutureWith(F&& func) {
using InnerType = typename isFutureOrSemiFuture<invoke_result_t<F>>::Inner;
try {
return static_cast<F&&>(func)();
} catch (std::exception& e) {
return makeSemiFuture<InnerType>(
exception_wrapper(std::current_exception(), e));
} catch (...) {
return makeSemiFuture<InnerType>(
exception_wrapper(std::current_exception()));
......@@ -1239,9 +1236,6 @@ typename std::
using InnerType = typename isFuture<invoke_result_t<F>>::Inner;
try {
return static_cast<F&&>(func)();
} catch (std::exception& e) {
return makeFuture<InnerType>(
exception_wrapper(std::current_exception(), e));
} catch (...) {
return makeFuture<InnerType>(exception_wrapper(std::current_exception()));
}
......@@ -1915,8 +1909,6 @@ SemiFuture<T> unorderedReduceSemiFuture(It first, It last, T initial, F func) {
ctx->func_(
std::move(v.value()),
mt.template get<IsTry::value, Arg&&>()));
} catch (std::exception& e) {
ew = exception_wrapper{std::current_exception(), e};
} catch (...) {
ew = exception_wrapper{std::current_exception()};
}
......
......@@ -239,7 +239,7 @@ class Promise {
/// Promise<MyValue> p = ...
/// ...
/// auto const ep = std::exception_ptr();
/// auto const ew = exception_wrapper::from_exception_ptr(ep);
/// auto const ew = exception_wrapper{ep};
/// p.setException(ew);
///
/// Functionally equivalent to `setTry(Try<T>(std::move(ew)))`
......
......@@ -532,8 +532,6 @@ void CoreBase::doCallback(
RequestContextScopeGuard rctx(std::move(core->context_));
core->callback_(*core, std::move(ka), nullptr);
});
} catch (const std::exception& e) {
ew = exception_wrapper(std::current_exception(), e);
} catch (...) {
ew = exception_wrapper(std::current_exception());
}
......
......@@ -165,11 +165,7 @@ TEST(FutureSplitter, splitFutureFailure) {
p.getSemiFuture().via(&InlineExecutor::instance()));
auto f1 = sp.getFuture();
EXPECT_FALSE(f1.isReady());
try {
throw std::runtime_error("Oops");
} catch (std::exception& e) {
p.setException(exception_wrapper(std::current_exception(), e));
}
p.setException(exception_wrapper{std::runtime_error("Oops")});
EXPECT_TRUE(f1.isReady());
EXPECT_TRUE(f1.hasException());
auto f2 = sp.getFuture();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment