Skip to content

Commit 192a081

Browse files
Nolan O'Brienfacebook-github-bot
authored andcommitted
Revert D79891669: Revert D77051161: [folly][IGiOS] Fix exhaustive switches
Differential Revision: D79891669 Original commit changeset: 7e0c15457246 Original Phabricator Diff: D77051161 fbshipit-source-id: cdf3387a6d053e9e76b59640b9014edc4c7662af
1 parent c7089a4 commit 192a081

File tree

10 files changed

+145
-94
lines changed

10 files changed

+145
-94
lines changed

folly/BUCK

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ non_fbcode_target(
7575
":scope_guard",
7676
":small_vector",
7777
"//xplat/folly/io/async:destructor_check",
78+
"//xplat/folly/lang:switch",
7879
],
7980
)
8081

@@ -1922,6 +1923,7 @@ non_fbcode_target(
19221923
name = "synchronization_baton",
19231924
feature = triage_InfrastructureSupermoduleOptou,
19241925
exported_deps = [
1926+
"//xplat/folly/lang:switch",
19251927
"//xplat/folly/synchronization:baton",
19261928
],
19271929
)
@@ -2112,6 +2114,7 @@ non_fbcode_target(
21122114
name = "synchronization_saturating_semaphore",
21132115
feature = triage_InfrastructureSupermoduleOptou,
21142116
exported_deps = [
2117+
"//xplat/folly/lang:switch",
21152118
"//xplat/folly/synchronization:saturating_semaphore",
21162119
],
21172120
)
@@ -5696,6 +5699,7 @@ fbcode_target(
56965699
":scope_guard",
56975700
":small_vector",
56985701
"//folly/io/async:destructor_check",
5702+
"//folly/lang:switch",
56995703
],
57005704
exported_external_deps = [
57015705
"glog",

folly/Expected.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ struct ExpectedStorage<Value, Error, StorageType::eUnion>
512512
this->error().~Error();
513513
break;
514514
case Which::eEmpty:
515+
default:
515516
break;
516517
}
517518
this->which_ = Which::eEmpty;

folly/FBString.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,9 @@ class fbstring_core {
344344
return ml_.data_;
345345
case Category::isLarge:
346346
return mutableDataLarge();
347+
default:
348+
folly::assume_unreachable();
347349
}
348-
folly::assume_unreachable();
349350
}
350351

351352
const Char* c_str() const {

folly/ObserverContainer.h

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <folly/Optional.h>
2626
#include <folly/ScopeGuard.h>
2727
#include <folly/io/async/DestructorCheck.h>
28+
#include <folly/lang/Switch.h>
2829
#include <folly/small_vector.h>
2930

3031
/**
@@ -166,21 +167,25 @@ class ObserverContainerStore : public ObserverContainerStoreBase<Observer> {
166167
if (iterating_) {
167168
CHECK(maybeCurrentIterationPolicy_.has_value());
168169
const auto& policy = maybeCurrentIterationPolicy_.value();
169-
switch (policy) {
170-
case InvokeWhileIteratingPolicy::InvokeAdded:
171-
case InvokeWhileIteratingPolicy::DoNotInvokeAdded:
172-
break;
173-
case InvokeWhileIteratingPolicy::CheckNoChange:
174-
folly::terminate_with<std::runtime_error>(
175-
"Cannot add observers while iterating "
176-
"per current iteration policy (CheckNoChange)");
177-
break;
178-
case InvokeWhileIteratingPolicy::CheckNoAdded:
179-
folly::terminate_with<std::runtime_error>(
180-
"Cannot add observers while iterating "
181-
"per current iteration policy (CheckNoAdded)");
182-
break;
183-
}
170+
FOLLY_EXHAUSTIVE_SWITCH({
171+
switch (policy) {
172+
case InvokeWhileIteratingPolicy::InvokeAdded:
173+
case InvokeWhileIteratingPolicy::DoNotInvokeAdded:
174+
break;
175+
case InvokeWhileIteratingPolicy::CheckNoChange:
176+
folly::terminate_with<std::runtime_error>(
177+
"Cannot add observers while iterating "
178+
"per current iteration policy (CheckNoChange)");
179+
break;
180+
case InvokeWhileIteratingPolicy::CheckNoAdded:
181+
folly::terminate_with<std::runtime_error>(
182+
"Cannot add observers while iterating "
183+
"per current iteration policy (CheckNoAdded)");
184+
break;
185+
default:
186+
folly::assume_unreachable();
187+
}
188+
});
184189
}
185190
observers_.insert(observers_.end(), observer);
186191
return true;
@@ -203,18 +208,22 @@ class ObserverContainerStore : public ObserverContainerStoreBase<Observer> {
203208
if (iterating_) {
204209
CHECK(maybeCurrentIterationPolicy_.has_value());
205210
const auto& policy = maybeCurrentIterationPolicy_.value();
206-
switch (policy) {
207-
case InvokeWhileIteratingPolicy::InvokeAdded:
208-
case InvokeWhileIteratingPolicy::DoNotInvokeAdded:
209-
break;
210-
case InvokeWhileIteratingPolicy::CheckNoChange:
211-
folly::terminate_with<std::runtime_error>(
212-
"Cannot remove observers while iterating "
213-
"per current iteration policy (CheckNoChange)");
214-
break;
215-
case InvokeWhileIteratingPolicy::CheckNoAdded:
216-
break;
217-
}
211+
FOLLY_EXHAUSTIVE_SWITCH({
212+
switch (policy) {
213+
case InvokeWhileIteratingPolicy::InvokeAdded:
214+
case InvokeWhileIteratingPolicy::DoNotInvokeAdded:
215+
break;
216+
case InvokeWhileIteratingPolicy::CheckNoChange:
217+
folly::terminate_with<std::runtime_error>(
218+
"Cannot remove observers while iterating "
219+
"per current iteration policy (CheckNoChange)");
220+
break;
221+
case InvokeWhileIteratingPolicy::CheckNoAdded:
222+
break;
223+
default:
224+
folly::assume_unreachable();
225+
}
226+
});
218227

219228
*it = nullptr;
220229
removalDuringIteration_ = true;

folly/futures/detail/BUCK

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ non_fbcode_target(
3232
"//xplat/folly/io/async:request_context",
3333
"//xplat/folly/lang:assume",
3434
"//xplat/folly/lang:exception",
35+
"//xplat/folly/lang:switch",
3536
],
3637
)
3738

@@ -69,6 +70,7 @@ fbcode_target(
6970
"//folly/io/async:request_context",
7071
"//folly/lang:assume",
7172
"//folly/lang:exception",
73+
"//folly/lang:switch",
7274
"//folly/synchronization:atomic_util",
7375
],
7476
exported_external_deps = [

folly/futures/detail/Core.h

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <folly/io/async/Request.h>
3535
#include <folly/lang/Assume.h>
3636
#include <folly/lang/Exception.h>
37+
#include <folly/lang/Switch.h>
3738
#include <folly/synchronization/AtomicUtil.h>
3839

3940
namespace folly {
@@ -437,50 +438,55 @@ class CoreBase {
437438
}
438439
handler_type* handler = nullptr;
439440
auto interrupt = interrupt_.load(std::memory_order_acquire);
440-
switch (interrupt & InterruptMask) {
441-
case InterruptInitial: { // store the handler
442-
assert(!interrupt);
443-
handler = new handler_type(static_cast<F&&>(fn));
444-
auto exchanged = folly::atomic_compare_exchange_strong_explicit(
445-
&interrupt_,
446-
&interrupt,
447-
reinterpret_cast<uintptr_t>(handler) | InterruptHasHandler,
448-
std::memory_order_release,
449-
std::memory_order_acquire);
450-
if (exchanged) {
451-
return;
452-
}
453-
// lost the race!
454-
if (interrupt & InterruptHasHandler) {
455-
terminate_with<std::logic_error>("set-interrupt-handler race");
456-
}
457-
assert(interrupt & InterruptHasObject);
458-
[[fallthrough]];
459-
}
460-
case InterruptHasObject: { // invoke over the stored object
461-
auto exchanged = interrupt_.compare_exchange_strong(
462-
interrupt, InterruptTerminal, std::memory_order_relaxed);
463-
if (!exchanged) {
464-
terminate_with<std::logic_error>("set-interrupt-handler race");
441+
FOLLY_EXHAUSTIVE_SWITCH({
442+
switch (interrupt & InterruptMask) {
443+
case InterruptInitial: { // store the handler
444+
assert(!interrupt);
445+
handler = new handler_type(static_cast<F&&>(fn));
446+
auto exchanged = folly::atomic_compare_exchange_strong_explicit(
447+
&interrupt_,
448+
&interrupt,
449+
reinterpret_cast<uintptr_t>(handler) | InterruptHasHandler,
450+
std::memory_order_release,
451+
std::memory_order_acquire);
452+
if (exchanged) {
453+
return;
454+
}
455+
// lost the race!
456+
if (interrupt & InterruptHasHandler) {
457+
terminate_with<std::logic_error>("set-interrupt-handler race");
458+
}
459+
assert(interrupt & InterruptHasObject);
460+
[[fallthrough]];
465461
}
466-
auto pointer = interrupt & ~InterruptMask;
467-
auto object = reinterpret_cast<exception_wrapper*>(pointer);
468-
if (handler) {
469-
handler->handle(*object);
470-
delete handler;
471-
} else {
472-
// mimic constructing and invoking a handler: 1 copy; non-const invoke
473-
auto fn_ = static_cast<F&&>(fn);
474-
fn_(std::as_const(*object));
462+
case InterruptHasObject: { // invoke over the stored object
463+
auto exchanged = interrupt_.compare_exchange_strong(
464+
interrupt, InterruptTerminal, std::memory_order_relaxed);
465+
if (!exchanged) {
466+
terminate_with<std::logic_error>("set-interrupt-handler race");
467+
}
468+
auto pointer = interrupt & ~InterruptMask;
469+
auto object = reinterpret_cast<exception_wrapper*>(pointer);
470+
if (handler) {
471+
handler->handle(*object);
472+
delete handler;
473+
} else {
474+
// mimic constructing and invoking a handler: 1 copy; non-const
475+
// invoke
476+
auto fn_ = static_cast<F&&>(fn);
477+
fn_(std::as_const(*object));
478+
}
479+
delete object;
480+
return;
475481
}
476-
delete object;
477-
return;
482+
case InterruptHasHandler: // fail all calls after the first
483+
terminate_with<std::logic_error>("set-interrupt-handler duplicate");
484+
case InterruptTerminal: // fail all calls after the first
485+
terminate_with<std::logic_error>("set-interrupt-handler after done");
486+
default:
487+
folly::assume_unreachable();
478488
}
479-
case InterruptHasHandler: // fail all calls after the first
480-
terminate_with<std::logic_error>("set-interrupt-handler duplicate");
481-
case InterruptTerminal: // fail all calls after the first
482-
terminate_with<std::logic_error>("set-interrupt-handler after done");
483-
}
489+
}); // FOLLY_EXHAUSTIVE_SWITCH
484490
}
485491

486492
protected:

folly/synchronization/BUCK

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ non_fbcode_target(
469469
"//xplat/folly:portability_asm",
470470
"//xplat/folly/detail:futex",
471471
"//xplat/folly/detail:memory_idler",
472+
"//xplat/folly/lang:switch",
472473
"//xplat/folly/synchronization/detail:spin",
473474
],
474475
)
@@ -609,6 +610,7 @@ fbcode_target(
609610
"//folly/detail:async_trace",
610611
"//folly/detail:futex",
611612
"//folly/detail:memory_idler",
613+
"//folly/lang:switch",
612614
"//folly/portability:asm",
613615
"//folly/synchronization/detail:spin",
614616
],
@@ -880,6 +882,7 @@ fbcode_target(
880882
"//folly:likely",
881883
"//folly/detail:futex",
882884
"//folly/detail:memory_idler",
885+
"//folly/lang:switch",
883886
"//folly/portability:asm",
884887
"//folly/synchronization/detail:spin",
885888
],

folly/synchronization/Baton.h

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <folly/detail/AsyncTrace.h>
3232
#include <folly/detail/Futex.h>
3333
#include <folly/detail/MemoryIdler.h>
34+
#include <folly/lang/Switch.h>
3435
#include <folly/portability/Asm.h>
3536
#include <folly/synchronization/AtomicUtil.h>
3637
#include <folly/synchronization/WaitOptions.h>
@@ -314,26 +315,36 @@ class Baton {
314315
deadline - Clock::now()));
315316
}
316317

317-
switch (detail::spin_pause_until(deadline, opt, [this] {
318-
return ready();
319-
})) {
320-
case detail::spin_result::success:
321-
return true;
322-
case detail::spin_result::timeout:
323-
return false;
324-
case detail::spin_result::advance:
325-
break;
326-
}
327-
328-
if (!MayBlock) {
329-
switch (detail::spin_yield_until(deadline, [this] { return ready(); })) {
318+
FOLLY_EXHAUSTIVE_SWITCH({
319+
switch (detail::spin_pause_until(deadline, opt, [this] {
320+
return ready();
321+
})) {
330322
case detail::spin_result::success:
331323
return true;
332324
case detail::spin_result::timeout:
333325
return false;
334326
case detail::spin_result::advance:
335327
break;
328+
default:
329+
folly::assume_unreachable();
336330
}
331+
});
332+
333+
if (!MayBlock) {
334+
FOLLY_EXHAUSTIVE_SWITCH({
335+
switch (detail::spin_yield_until(deadline, [this] {
336+
return ready();
337+
})) {
338+
case detail::spin_result::success:
339+
return true;
340+
case detail::spin_result::timeout:
341+
return false;
342+
case detail::spin_result::advance:
343+
break;
344+
default:
345+
folly::assume_unreachable();
346+
}
347+
});
337348
}
338349

339350
// Try transitioning from the spinning phase to the blocking phase via a CAS

folly/synchronization/SaturatingSemaphore.h

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <folly/Likely.h>
2424
#include <folly/detail/Futex.h>
2525
#include <folly/detail/MemoryIdler.h>
26+
#include <folly/lang/Switch.h>
2627
#include <folly/portability/Asm.h>
2728
#include <folly/synchronization/AtomicUtil.h>
2829
#include <folly/synchronization/WaitOptions.h>
@@ -275,24 +276,34 @@ template <typename Clock, typename Duration>
275276
FOLLY_NOINLINE bool SaturatingSemaphore<MayBlock, Atom>::tryWaitSlow(
276277
const std::chrono::time_point<Clock, Duration>& deadline,
277278
const WaitOptions& opt) noexcept {
278-
switch (detail::spin_pause_until(deadline, opt, [this] { return ready(); })) {
279-
case detail::spin_result::success:
280-
return true;
281-
case detail::spin_result::timeout:
282-
return false;
283-
case detail::spin_result::advance:
284-
break;
285-
}
286-
287-
if (!MayBlock) {
288-
switch (detail::spin_yield_until(deadline, [this] { return ready(); })) {
279+
FOLLY_EXHAUSTIVE_SWITCH({
280+
switch (detail::spin_pause_until(deadline, opt, [this] {
281+
return ready();
282+
})) {
289283
case detail::spin_result::success:
290284
return true;
291285
case detail::spin_result::timeout:
292286
return false;
293287
case detail::spin_result::advance:
294288
break;
289+
default:
290+
folly::assume_unreachable();
295291
}
292+
});
293+
294+
if (!MayBlock) {
295+
FOLLY_EXHAUSTIVE_SWITCH({
296+
switch (detail::spin_yield_until(deadline, [this] { return ready(); })) {
297+
case detail::spin_result::success:
298+
return true;
299+
case detail::spin_result::timeout:
300+
return false;
301+
case detail::spin_result::advance:
302+
break;
303+
default:
304+
folly::assume_unreachable();
305+
}
306+
});
296307
}
297308

298309
auto before = state_.load(std::memory_order_relaxed);

0 commit comments

Comments
 (0)