-
Notifications
You must be signed in to change notification settings - Fork 1.6k
<limits>: fix traps for integers
#5816
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
Merged
StephanTLavavej
merged 45 commits into
microsoft:main
from
AlexGuteniev:tricks-and-traps
Mar 4, 2026
Merged
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
2b4fb31
Integer coverage
AlexGuteniev 9833a4a
Product code fix
AlexGuteniev b17606a
Skip numeric.limits.members/traps.pass.cpp for Clang.
AlexGuteniev 97e6faa
Skippy skip
AlexGuteniev 586b09f
Merge remote-tracking branch 'upstream/main' into tricks-and-traps
AlexGuteniev e627022
it is always the trap
AlexGuteniev ad6a32f
but Clang
AlexGuteniev 816d158
concise override
AlexGuteniev 6996605
Spell that more naturally
AlexGuteniev 3347a99
Now we know
AlexGuteniev a49bce3
comprehensive compile-time coverage
AlexGuteniev e1a996e
nicer test format
AlexGuteniev 257f831
No longer SKIPPED actually
AlexGuteniev 4ede9b1
No more death, no more execution!
AlexGuteniev 72bd6f4
spelling
AlexGuteniev 614a323
spelling
AlexGuteniev c9c5ab7
ARM64EC fix
AlexGuteniev 1c3179e
back 2 skipped
AlexGuteniev 6e2f0e9
Merge branch 'main' into tricks-and-traps
AlexGuteniev b0fa9a6
mention LWG-554
AlexGuteniev db1c4c7
promoted integers don't trap
AlexGuteniev 1354e26
missing wchar_t
AlexGuteniev 870d8ee
missing short
AlexGuteniev a10866e
Avoid variable and comments
AlexGuteniev b60b033
unnecessary transitivity
AlexGuteniev a9ae3eb
even fewer comments
AlexGuteniev 2c6a27b
Merge branch 'main' into tricks-and-traps
AlexGuteniev 167deb4
Don't trap on promoted uint32_t
AlexGuteniev 6780f4c
elaborate
AlexGuteniev a6936c2
backwards
AlexGuteniev 44ef94f
don't binary negate bool
AlexGuteniev 0a872b0
Merge branch 'microsoft:main' into tricks-and-traps
AlexGuteniev f26b5aa
Merge branch 'microsoft:main' into tricks-and-traps
AlexGuteniev 240bd24
Merge branch 'main' into tricks-and-traps
AlexGuteniev c22ec61
Merge branch 'main' into tricks-and-traps
AlexGuteniev 2ec9e13
fix formatting
AlexGuteniev ef9aa41
Merge branch 'microsoft:main' into tricks-and-traps
AlexGuteniev 3b0a357
Merge branch 'main' into tricks-and-traps
AlexGuteniev 6c55a8a
Merge branch 'main' into tricks-and-traps
StephanTLavavej a69fe74
Comment: Arm64 => ARM64
StephanTLavavej 7794d8e
Add parens around logical AND.
StephanTLavavej 4a25854
Drop extra space.
StephanTLavavej d6e9620
Include `<type_traits>` for `is_same_v`.
StephanTLavavej 70f74d3
Wrap the traps
StephanTLavavej d7955b0
"MSVC inserts check" => "MSVC inserts a check"
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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_matrix.lst |
56 changes: 56 additions & 0 deletions
56
tests/std/tests/GH_005816_numeric_limits_traps/test.compile.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <limits> | ||
| #include <type_traits> | ||
|
|
||
| template <class T> | ||
| constexpr bool traps_ = std::numeric_limits<T>::traps; | ||
|
|
||
| #if defined(_M_IX86) || (defined(_M_X64) && !defined(_M_ARM64EC)) | ||
| static_assert(traps_<int>, | ||
| "The #ED hardware exception always happens for zero division and for division overflow INT_MIN/-1. " | ||
| "These are translated to STATUS_INTEGER_DIVIDE_BY_ZERO and STATUS_INTEGER_OVERFLOW SEH exceptions"); | ||
| #elif defined(_M_ARM64) || defined(_M_ARM64EC) || defined(_M_HYBRID_X86_ARM64) | ||
| #ifdef __clang__ | ||
| static_assert(!traps_<int>, "The hardware does not trap. Clang compiles code as is, so there's no trap"); | ||
| #else // ^^^ defined(__clang__) / !defined(__clang__) vvv | ||
| static_assert(traps_<int>, "The hardware does not trap. MSVC inserts a check for zero to trap zero division. " | ||
| "It does not insert checks for INT_MIN/-1 division overflow though"); | ||
| #endif // ^^^ !defined(__clang__) ^^^ | ||
| #else // ^^^ defined(_M_ARM64) || defined(_M_ARM64EC) || defined(_M_HYBRID_X86_ARM64) ^^^ | ||
| #error Unsupported hardware | ||
| #endif | ||
|
|
||
| template <class T> | ||
| constexpr bool non_promoted_and_traps_if_int_does = std::is_same_v<decltype(~T{}), T> && traps_<T> == traps_<int>; | ||
|
|
||
| static_assert(non_promoted_and_traps_if_int_does<unsigned int> // | ||
| && non_promoted_and_traps_if_int_does<long> // | ||
| && non_promoted_and_traps_if_int_does<unsigned long> // | ||
| && non_promoted_and_traps_if_int_does<long long> // | ||
| && non_promoted_and_traps_if_int_does<unsigned long long>, | ||
| "all non-promoted integers should trap or not trap equally"); | ||
|
|
||
| template <class T> | ||
| constexpr bool promoted_and_does_not_trap = !std::is_same_v<decltype(~T{}), T> && !traps_<T>; | ||
|
|
||
| static_assert(!traps_<bool>, "bool does not trap for a moot reason; see LWG-554 resolution"); | ||
|
|
||
| static_assert(promoted_and_does_not_trap<char> // | ||
| && promoted_and_does_not_trap<signed char> // | ||
| && promoted_and_does_not_trap<unsigned char> // | ||
| && promoted_and_does_not_trap<short> // | ||
| && promoted_and_does_not_trap<unsigned short> // | ||
| && promoted_and_does_not_trap<wchar_t> // | ||
| && promoted_and_does_not_trap<char16_t> // | ||
| && promoted_and_does_not_trap<char32_t>, | ||
| "promoted integers do not trap for a moot reason; see LWG-554 resolution"); | ||
|
|
||
| #ifdef __cpp_char8_t | ||
| static_assert( | ||
| promoted_and_does_not_trap<char8_t>, "promoted integers do not trap for a moot reason; see LWG-554 resolution"); | ||
| #endif | ||
|
|
||
| static_assert(!traps_<float> && !traps_<double> && !traps_<long double>, | ||
| "floats don't trap because even if '/fp:except' is passed, it should be enabled at runtime"); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.