-
Notifications
You must be signed in to change notification settings - Fork 765
Add constexpr to not_null comparison operators #1208
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
+188
−7
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f49811a
Initial plan for issue
Copilot 556fa41
Add test and plan to make not_null comparison functions constexpr
Copilot 17f47f0
Add constexpr to not_null comparison operators
Copilot ef879ea
Fix copyright year in constexpr_notnull_tests.cpp
Copilot d3bb9e2
Fix constexpr tests for better compiler compatibility
Copilot 018e3bc
Remove build artifacts and update .gitignore
Copilot 12bb775
Fix constexpr tests to be compatible with more compilers
Copilot a72a9a3
copilot: Provide more project context for the Copilot coding agent (#…
carsonRadtke 8fb2a99
Initial plan for issue
Copilot 4c3da61
Rebase onto main and verify changes meet project guidelines
Copilot 5da5d89
Update .gitignore to exclude build-cxx* directories
Copilot 9ea182d
Fix newline at end of constexpr_notnull_tests.cpp and update .gitignore
Copilot 0711357
Remove C++14 feature check that is redundant since C++14 is the minim…
Copilot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| # GitHub Copilot Instructions for GSL (Guidelines Support Library) | ||
|
|
||
| ## Project Overview | ||
| This repository contains the Guidelines Support Library (GSL), a Microsoft implementation of types and functions | ||
| suggested for use by the C++ Core Guidelines. It's a header-only C++ library with emphasis on safety, | ||
| correctness, and zero overhead. | ||
|
|
||
| ## Coding Standards | ||
|
|
||
| ### General | ||
| - Follow C++ Core Guidelines wherever possible | ||
| - Use meaningful type, function, and template parameter names | ||
| - Keep functions small and focused with clear preconditions/postconditions | ||
| - Include comments for complex code, but prefer self-documenting code | ||
| - Use the Expects() and Ensures() macros for contract verification | ||
|
|
||
| ### Style Guidelines | ||
| - Use 4 spaces for indentation (not tabs) | ||
| - Maximum line length of 100 characters | ||
| - Follow GSL naming conventions (lowercase with underscores) | ||
| - Keep templates clean and readable with appropriate spacing | ||
| - Use C++14 features since this is the minimum standard supported | ||
|
|
||
| ### Error Handling | ||
| - Use Expects() for preconditions and Ensures() for postconditions | ||
| - Design for fail-fast semantics (std::terminate) on contract violations | ||
| - Template constraints should use static_assert or SFINAE | ||
| - Don't throw exceptions from basic operations | ||
|
|
||
| ### Testing | ||
| - Write thorough unit tests for every component using GTest | ||
| - Test for all edge cases and error conditions | ||
| - Ensure cross-platform compatibility in tests | ||
| - Maintain 100% code coverage for changed code | ||
|
|
||
| ## Project-Specific Conventions | ||
|
|
||
| ### Architecture | ||
| - All public types must be in the gsl namespace | ||
| - Design for zero overhead abstractions when possible | ||
| - Respect the distinction between Owners and Views | ||
| - Maintain backward compatibility with existing GSL code | ||
|
|
||
| ### Version Control | ||
| - Link all PRs to related issues | ||
| - Use clear commit messages explaining what and why | ||
| - Follow the contribution guidelines documented in CONTRIBUTING.md | ||
| - PRs should include appropriate tests with 100% coverage for changed code | ||
|
|
||
| ### Documentation | ||
| - Document all public APIs with clarity on preconditions and postconditions | ||
| - Keep header comments up-to-date | ||
| - Include examples for complex functionality in docs/headers.md | ||
|
|
||
| ## Technology Stack | ||
| - C++14 (minimum) for core implementation | ||
| - CMake build system (3.14+) | ||
| - Google Test for unit testing | ||
| - Support for multiple compilers (MSVC, GCC, Clang) | ||
|
|
||
| ## Security Considerations | ||
| - Bounds checking is a core principle - enforce it consistently | ||
| - Design for safety while minimizing overhead | ||
| - Ensure undefined behavior is explicitly detected where possible | ||
|
|
||
| ## Performance Guidelines | ||
| - Optimize for both safety and performance | ||
| - Constexpr-enable functions wherever possible | ||
| - Avoid hidden allocations | ||
| - Use noexcept appropriately for move operations and other performance-critical functions | ||
|
|
||
| ## Cross-Platform Support | ||
| - Code must work across: | ||
| - Windows (MSVC) | ||
| - Linux (GCC, Clang) | ||
| - macOS (AppleClang) | ||
| - Android and iOS where applicable | ||
|
|
||
| ## Copilot Tasks | ||
| - You can find the CMake artifacts for C++20 in build-cxx20 and C++14 in build-cxx14. | ||
| - Before publishing a PR, verify the following: | ||
| - There are no compiler warnings or errors when building the test suite. | ||
| - The test suite passes on all supported platforms and compilers. | ||
| - The test suite passes for both C++14 and C++20. |
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,22 @@ | ||
| name: "Copilot Setup Steps" | ||
|
|
||
| on: workflow_dispatch | ||
|
|
||
| jobs: | ||
| copilot-setup-steps: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Install Build Dependencies | ||
| run: sudo apt-get update && sudo apt-get install -y clang cmake make | ||
|
|
||
| - name: Configure CMake (C++14) | ||
| run: cmake -B build-cxx14 . -DGSL_CXX_STANDARD=14 -DGSL_TEST=ON -G "Unix Makefiles" | ||
|
|
||
| - name: Configure CMake (C++20) | ||
| run: cmake -B build-cxx20 . -DGSL_CXX_STANDARD=20 -DGSL_TEST=ON -G "Unix Makefiles" | ||
|
|
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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| CMakeFiles | ||
| build | ||
| build*/ | ||
| tests/CMakeFiles | ||
| tests/Debug | ||
| *.opensdf | ||
|
|
||
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,74 @@ | ||
| /////////////////////////////////////////////////////////////////////////////// | ||
| // | ||
| // Copyright (c) 2025 Microsoft Corporation. All rights reserved. | ||
| // | ||
| // This code is licensed under the MIT License (MIT). | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| // THE SOFTWARE. | ||
| // | ||
| /////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| #include <gsl/pointers> // for not_null | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <type_traits> // for declval | ||
|
|
||
| using namespace gsl; | ||
|
|
||
| namespace | ||
| { | ||
| constexpr bool comparison_test(const int* ptr1, const int* ptr2) | ||
| { | ||
| const not_null<const int*> p1(ptr1); | ||
| const not_null<const int*> p1_same(ptr1); | ||
| const not_null<const int*> p2(ptr2); | ||
|
|
||
| // Testing operator== | ||
| const bool eq_result = (p1 == p1_same); // Should be true | ||
| const bool neq_result = (p1 != p2); // Should be true | ||
|
|
||
| // Testing operator<= and operator>= | ||
| const bool le_result = (p1 <= p1_same); // Should be true | ||
| const bool ge_result = (p1 >= p1_same); // Should be true | ||
|
|
||
| // The exact comparison results will depend on pointer ordering, | ||
| // but we can verify that the basic equality checks work as expected | ||
| return eq_result && neq_result && le_result && ge_result; | ||
| } | ||
|
|
||
| constexpr bool workaround_test(const int* ptr1, const int* ptr2) | ||
| { | ||
| const not_null<const int*> p1(ptr1); | ||
| const not_null<const int*> p1_same(ptr1); | ||
| const not_null<const int*> p2(ptr2); | ||
|
|
||
| // Using .get() to compare | ||
| const bool eq_result = (p1.get() == p1_same.get()); // Should be true | ||
| const bool neq_result = (p1.get() != p2.get()); // Should be true | ||
|
|
||
| return eq_result && neq_result; | ||
| } | ||
| } // namespace | ||
|
|
||
| constexpr int test_value1 = 1; | ||
| constexpr int test_value2 = 2; | ||
|
|
||
| static_assert(comparison_test(&test_value1, &test_value2), "not_null comparison operators should be constexpr"); | ||
| static_assert(workaround_test(&test_value1, &test_value2), "not_null .get() comparison workaround should work"); | ||
|
|
||
| TEST(notnull_constexpr_tests, TestNotNullConstexprComparison) | ||
| { | ||
| // This test simply verifies that the constexpr functions compile and run | ||
| // If we got here, it means the constexpr comparison operators are working | ||
| static const int value1 = 1; | ||
| static const int value2 = 2; | ||
| EXPECT_TRUE(comparison_test(&value1, &value2)); | ||
| EXPECT_TRUE(workaround_test(&value1, &value2)); | ||
| } | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a good reason not to check operators < and >?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really. We could certainly add a check for
!(p1 < p1_same)and!(p1 > p1_same)or something along those lines.