|
| 1 | +.. |
| 2 | + Copyright (c) 2025 The STE||AR-Group |
| 3 | + Copyright (c) 2025 Alexandros Papadakis |
| 4 | +
|
| 5 | + SPDX-License-Identifier: BSL-1.0 |
| 6 | + Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 7 | + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 8 | + |
| 9 | +.. _modules_contracts: |
| 10 | + |
| 11 | +========= |
| 12 | +contracts |
| 13 | +========= |
| 14 | + |
| 15 | +The contracts module provides C++ contracts support for HPX with intelligent |
| 16 | +fallback to assertions when native contracts are not available. This module |
| 17 | +implements a forward-compatible API that works across different C++ standards |
| 18 | +and compiler capabilities. |
| 19 | + |
| 20 | +The module provides three primary macros: :c:macro:`HPX_PRE`, |
| 21 | +:c:macro:`HPX_POST`, and :c:macro:`HPX_CONTRACT_ASSERT`. These macros |
| 22 | +automatically adapt their behavior based on compiler capabilities: |
| 23 | + |
| 24 | +* When C++26 native contracts are available (``__cpp_contracts`` defined), |
| 25 | + they map to standard contract syntax |
| 26 | +* When contracts are not available but ``HPX_WITH_CONTRACTS=ON``, preconditions |
| 27 | + and postconditions become no-ops while contract assertions fall back to |
| 28 | + :c:macro:`HPX_ASSERT` for compatibility |
| 29 | +* When ``HPX_WITH_CONTRACTS=OFF``, preconditions and postconditions become |
| 30 | + no-ops while contract assertions remain available as enhanced assertions |
| 31 | + |
| 32 | +Configuration |
| 33 | +============= |
| 34 | + |
| 35 | +Enable contracts in CMake:: |
| 36 | + |
| 37 | + cmake -DHPX_WITH_CONTRACTS=ON -DCMAKE_CXX_STANDARD=26 |
| 38 | + |
| 39 | +Enable contract-enhanced assertions (optional):: |
| 40 | + |
| 41 | + cmake -DHPX_WITH_CONTRACTS=ON -DHPX_WITH_ASSERTS_AS_CONTRACT_ASSERTS=ON |
| 42 | + |
| 43 | +Contract assertions work even when contracts are disabled:: |
| 44 | + |
| 45 | + cmake -DHPX_WITH_CONTRACTS=OFF # HPX_CONTRACT_ASSERT still maps to HPX_ASSERT |
| 46 | + |
| 47 | +Advanced Features |
| 48 | +================= |
| 49 | + |
| 50 | +Contract-Enhanced Assertions |
| 51 | +---------------------------- |
| 52 | + |
| 53 | +When ``HPX_WITH_ASSERTS_AS_CONTRACT_ASSERTS=ON`` is enabled, regular |
| 54 | +:c:macro:`HPX_ASSERT` calls are automatically upgraded to use contract |
| 55 | +assertions in C++26 mode:: |
| 56 | + |
| 57 | + void example_function(int value) |
| 58 | + { |
| 59 | + HPX_ASSERT(value > 0); // Becomes contract_assert(value > 0) in C++26 mode |
| 60 | + // ... rest of function |
| 61 | + } |
| 62 | + |
| 63 | +.. warning:: |
| 64 | + Enhanced assertions only provide benefits when native C++26 contracts are |
| 65 | + supported by the compiler. Without native contract support, |
| 66 | + ``HPX_ASSERT`` → ``HPX_CONTRACT_ASSERT`` → ``HPX_ASSERT`` (no enhancement). |
| 67 | + CMake will issue a warning if you enable this option without native contract support. |
| 68 | + |
| 69 | +This provides enhanced contract semantics throughout your existing codebase |
| 70 | +without requiring changes to assertion code. The transformation occurs in the |
| 71 | +contracts module (``contracts.hpp``) where the ``HPX_ASSERT`` macro is |
| 72 | +overridden to use ``HPX_CONTRACT_ASSERT`` when ``HPX_WITH_ASSERTS_AS_CONTRACT_ASSERTS=ON``: |
| 73 | + |
| 74 | +* ``HPX_WITH_CONTRACTS=ON`` - Contracts module is enabled |
| 75 | +* ``HPX_WITH_ASSERTS_AS_CONTRACT_ASSERTS=ON`` - Assertion enhancement is enabled |
| 76 | +* ``HPX_HAVE_NATIVE_CONTRACTS=ON`` - C++26 native contracts are available (from config test) |
| 77 | + |
| 78 | +The implementation works by redefining ``HPX_ASSERT`` in ``contracts.hpp`` to |
| 79 | +use ``HPX_CONTRACT_ASSERT``, which automatically adapts to the current contract |
| 80 | +mode (native C++26 contracts or assertion fallback). This ensures all existing |
| 81 | +``HPX_ASSERT`` calls throughout the HPX codebase automatically gain contract |
| 82 | +semantics when available. |
| 83 | + |
| 84 | +Usage Examples |
| 85 | +============== |
| 86 | + |
| 87 | +Preconditions and postconditions using declaration syntax (C++26):: |
| 88 | + |
| 89 | + int divide(int a, int b) HPX_PRE(b != 0) |
| 90 | + { |
| 91 | + return a / b; |
| 92 | + } |
| 93 | + |
| 94 | + int factorial(int n) HPX_PRE(n >= 0) HPX_POST(r; r > 0) |
| 95 | + { |
| 96 | + return n <= 1 ? 1 : n * factorial(n - 1); |
| 97 | + } |
| 98 | + |
| 99 | +Contract assertions (available in all modes):: |
| 100 | + |
| 101 | + void process_array(std::vector<int>& arr, size_t index) |
| 102 | + { |
| 103 | + HPX_CONTRACT_ASSERT(index < arr.size()); |
| 104 | + arr[index] *= 2; |
| 105 | + } |
| 106 | + |
| 107 | +Design Philosophy |
| 108 | +================= |
| 109 | + |
| 110 | +**HPX_CONTRACT_ASSERT**: Enhanced assertion mechanism |
| 111 | + Available even when ``HPX_WITH_CONTRACTS=OFF`` because it provides value |
| 112 | + as an enhanced assertion. Maps to :c:macro:`HPX_ASSERT` in all configurations. |
| 113 | + |
| 114 | +**HPX_PRE/HPX_POST**: True contract syntax |
| 115 | + Represent language-level contract semantics. When contracts are enabled but |
| 116 | + native C++26 contracts are not available, these become no-ops to maintain |
| 117 | + forward compatibility. When ``HPX_WITH_CONTRACTS=OFF``, they are also no-ops. |
| 118 | + This prepares for C++26 migration where they will be attached to function |
| 119 | + declarations rather than used in function bodies. |
| 120 | + |
| 121 | +Migration Strategy |
| 122 | +================== |
| 123 | + |
| 124 | +The module is designed for smooth migration to C++26 native contracts: |
| 125 | + |
| 126 | +Current (transition mode):: |
| 127 | + |
| 128 | + int func(int x) |
| 129 | + { |
| 130 | + HPX_PRE(x > 0); // No-op in fallback mode, active in native mode |
| 131 | + return x; |
| 132 | + } |
| 133 | + |
| 134 | +Target (C++26 native):: |
| 135 | + |
| 136 | + int func(int x) HPX_PRE(x > 0) |
| 137 | + { |
| 138 | + return x; |
| 139 | + } |
| 140 | + |
| 141 | +Note: In fallback mode, ``HPX_PRE`` and ``HPX_POST`` become no-ops to maintain |
| 142 | +forward compatibility and avoid performance overhead. Use ``HPX_CONTRACT_ASSERT`` |
| 143 | +when you need runtime validation in all modes. |
| 144 | + |
| 145 | +Testing |
| 146 | +======= |
| 147 | + |
| 148 | +The module includes comprehensive testing with automatic compiler capability |
| 149 | +detection. Tests are organized into three categories: |
| 150 | + |
| 151 | +* **Declaration tests**: Test C++26 native contract syntax when ``__cpp_contracts`` is available |
| 152 | +* **Fallback tests**: Test assertion fallback behavior when contracts are not natively supported |
| 153 | +* **Disabled tests**: Test no-op behavior when contracts are disabled |
| 154 | + |
| 155 | +The test suite automatically detects compiler capabilities at configure time |
| 156 | +and builds only the appropriate tests for the current configuration. |
| 157 | + |
| 158 | +See the :ref:`API reference <modules_contracts_api>` of the module for more details. |
0 commit comments