Skip to content

Commit

Permalink
P0627R6: Function to mark unreachable code (#2526)
Browse files Browse the repository at this point in the history
Co-authored-by: Casey Carter <[email protected]>
  • Loading branch information
AlexGuteniev and CaseyCarter authored Feb 12, 2022
1 parent 8096a27 commit b6b9778
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 0 deletions.
7 changes: 7 additions & 0 deletions stl/inc/utility
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,13 @@ template <class _Ty>
_NODISCARD constexpr underlying_type_t<_Ty> to_underlying(_Ty _Value) noexcept {
return static_cast<underlying_type_t<_Ty>>(_Value);
}

[[noreturn]] inline void unreachable() noexcept /* strengthened */ {
_STL_UNREACHABLE;
#ifdef _DEBUG
_CSTD abort(); // likely to be called in debug mode, but can't be relied upon - already entered the UB territory
#endif // _DEBUG
}
#endif // _HAS_CXX23

#if _HAS_TR1_NAMESPACE
Expand Down
2 changes: 2 additions & 0 deletions stl/inc/yvals_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@
// P0288R9 move_only_function
// P0401R6 Providing Size Feedback In The Allocator Interface
// P0448R4 <spanstream>
// P0627R6 unreachable()
// P0798R8 Monadic Operations For optional
// P0943R6 Supporting C Atomics In C++
// P1048R1 is_scoped_enum
Expand Down Expand Up @@ -1382,6 +1383,7 @@
#define __cpp_lib_string_contains 202011L
#define __cpp_lib_string_resize_and_overwrite 202110L
#define __cpp_lib_to_underlying 202102L
#define __cpp_lib_unreachable 202202L
#endif // _HAS_CXX23

// macros with language mode sensitivity
Expand Down
1 change: 1 addition & 0 deletions tests/std/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ tests\P0595R2_is_constant_evaluated
tests\P0607R0_inline_variables
tests\P0608R3_improved_variant_converting_constructor
tests\P0616R0_using_move_in_numeric
tests\P0627R6_unreachable
tests\P0631R8_numbers_math_constants
tests\P0645R10_text_formatting_args
tests\P0645R10_text_formatting_custom_formatting
Expand Down
4 changes: 4 additions & 0 deletions tests/std/tests/P0627R6_unreachable/env.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

RUNALL_INCLUDE ..\usual_latest_matrix.lst
30 changes: 30 additions & 0 deletions tests/std/tests/P0627R6_unreachable/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <cassert>
#include <utility>

constexpr char test_impl(const int arg) {
switch (arg) {
case 1:
return 'a';

case 2:
return 'z';

default:
std::unreachable();
}
}

constexpr bool test() {
assert(test_impl(1) == 'a');
assert(test_impl(2) == 'z');
return true;
}

int main() {
test();
static_assert(test());
static_assert(noexcept(std::unreachable())); // strengthened
}
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,20 @@ STATIC_ASSERT(__cpp_lib_uncaught_exceptions == 201411L);
STATIC_ASSERT(__cpp_lib_unordered_map_try_emplace == 201411L);
#endif

#if _HAS_CXX23
#ifndef __cpp_lib_unreachable
#error __cpp_lib_unreachable is not defined
#elif __cpp_lib_unreachable != 202202L
#error __cpp_lib_unreachable is not 202202L
#else
STATIC_ASSERT(__cpp_lib_unreachable == 202202L);
#endif
#else
#ifdef __cpp_lib_unreachable
#error __cpp_lib_unreachable is defined
#endif
#endif

#if _HAS_CXX20
#ifndef __cpp_lib_unwrap_ref
#error __cpp_lib_unwrap_ref is not defined
Expand Down

0 comments on commit b6b9778

Please sign in to comment.