Repository navigation
Description
Tests: saturating ops coverage (§4)
Summary
Saturating arithmetic has Sema and LLVM codegen support, but the requirements campaign still needs focused tests for the §4.2.3 contract. Add behavior and compile-error coverage for explicit saturating operators, min/max clamping for signed and unsigned integers, and float rejection.
Spec
docs/with-specification.md:869-921 defines overflow behavior and saturating arithmetic. +|, -|, and *| clamp to numeric min/max on overflow, apply only to integer types, and reject floats. The same section covers overflow modes such as @[overflow(saturate)].
Current state
src/SemaCheck.w:4056andsrc/SemaCheck.w:4212-4217recognize saturating arithmetic operators and reject floating-point operands with "saturating arithmetic is not defined for floating-point types".src/CodegenDispatch.w:1665-1668selects LLVM signed/unsigned saturating intrinsics for add/sub;src/CodegenDispatch.w:1898-1904routes explicit saturating ops and saturating overflow mode through that helper.src/CodegenDispatch.w:2861handles saturating integer negation under saturating overflow mode.src/CCodegen.w:2752-2754currently fails loudly for C backend saturating integer arithmetic, so new tests should be clear about the backend they exercise or expose that as an implementation follow-up.- Existing
test/behavior/behav_overflow_explicit_modes.w:14-15has one constu8saturating-add smoke case; it is not enough for the requirement IDs in this issue.
Implementation plan
- Add behavior tests for explicit
+|,-|, and*|across unsigned and signed integer widths. - Add min-bound and max-bound cases: unsigned underflow to
0, unsigned overflow to max, signed positive overflow to max, signed negative overflow to min. - Add tests for
@[overflow(saturate)]if the attribute is already implemented for checked operators. - Add compile-error tests for saturating float operations.
- If the normal test runner exercises the C backend for these tests, either implement C backend saturating arithmetic or mark the backend gap as a separate loud issue; do not hide it with a fallback.
Test plan
test/behavior/behav_saturating_ops.w:u8:250 +| 20 == 255,3 -| 9 == 0,20 *| 20 == 255.i8:120 +| 20 == 127,-120 -| 20 == -128, positive and negative multiply clamps.- one wider type (
i32oru32) to avoid only sub-byte coverage.
- Compile-error tests for
1.0 +| 2.0,1.0 -| 2.0, and1.0 *| 2.0. - Attribute-mode test if
@[overflow(saturate)]is accepted by the parser/Sema. - Run
with build,with build :fixpoint, andwith build :test.
Acceptance criteria
- Explicit saturating add/sub/mul are tested for signed and unsigned integers.
- Tests cover min and max clamping.
- Floating-point saturating operators are compile errors.
- Saturating overflow mode is covered or explicitly split if not implemented.
- Any C backend incompatibility is surfaced as a real implementation issue, not skipped silently.
Dependencies
No test dependency. Potential implementation follow-up if C backend support is required by the selected test path.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 4.2.3.11, 4.2.3.12, 4.2.3.13, 4.2.3.9.
Activity
ehartford commentedon Jul 3, 2026
Closing — saturating +|/-|/*| pinned by behav_saturating_ops (u8/i8/u32 clamps at bounds) + f64 rejection (err_saturating_float), ec03406. §4.
test: coverage-sweep batch 2 (#483 #484 #522 #493 #494 #514 #515 #532)