Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P0627R6: Function to mark unreachable code #2526

Merged
merged 13 commits into from
Feb 12, 2022
8 changes: 8 additions & 0 deletions stl/inc/utility
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,14 @@ 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 */ {
#ifdef __clang__
__builtin_unreachable();
#else // ^^^ __clang__ ^^^ // vvv not __clang__ vvv
__assume(false);
AlexGuteniev marked this conversation as resolved.
Show resolved Hide resolved
#endif // ^^^ not __clang__ ^^^
}
#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 Function To Mark Unreachable Code
AlexGuteniev marked this conversation as resolved.
Show resolved Hide resolved
// P0798R8 Monadic Operations For optional
// P0943R6 Supporting C Atomics In C++
// P1048R1 is_scoped_enum
Expand Down Expand Up @@ -1418,6 +1419,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

#define __cpp_lib_experimental_erase_if 201411L
Expand Down
1 change: 1 addition & 0 deletions tests/std/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,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
29 changes: 29 additions & 0 deletions tests/std/tests/P0627R6_unreachable/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <cassert>
#include <utility>

constexpr int test_impl(int arg) {
AlexGuteniev marked this conversation as resolved.
Show resolved Hide resolved
switch (arg) {
case 1:
return 'x';

case 2:
return 'y';

default:
std::unreachable();
}
}

constexpr bool test() {
assert(test_impl(1) == 'x');
assert(test_impl(2) == 'y');
return true;
AlexGuteniev marked this conversation as resolved.
Show resolved Hide resolved
}

int main() {
test();
static_assert(test());
}
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