| 1 | <!-- Copyright 2025 The Fuchsia Authors |
| 2 | |
| 3 | Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 |
| 4 | <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT |
| 5 | license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. |
| 6 | This file may not be copied, modified, or distributed except according to |
| 7 | those terms. --> |
| 8 | |
| 9 | # Validating Changes |
| 10 | |
| 11 | This document covers the procedures and requirements for validating changes to |
| 12 | the project, including linting, testing, and pre-submission checks. |
| 13 | |
| 14 | ## Linting |
| 15 | |
| 16 | Clippy should **always** be run on the `nightly` toolchain. |
| 17 | |
| 18 | ```bash |
| 19 | ./cargo.sh +nightly clippy |
| 20 | ./cargo.sh +nightly clippy --tests |
| 21 | ``` |
| 22 | |
| 23 | ### Strict Linting |
| 24 | |
| 25 | - We deny warnings in CI. Even warnings not explicitly listed in `lib.rs` will |
| 26 | cause CI to fail. |
| 27 | - **Why:** We maintain a zero-warning policy so that new warnings (which often |
| 28 | indicate bugs) are immediately obvious and not obscured by existing ones. |
| 29 | - Do not introduce new warnings. |
| 30 | - Respect the strict `deny` list in `src/lib.rs`. |
| 31 | |
| 32 | ## Validating Changes |
| 33 | |
| 34 | Ensure the library builds on all supported toolchains and that Clippy passes. |
| 35 | |
| 36 | ```bash |
| 37 | ./cargo.sh +msrv check --tests --features __internal_use_only_features_that_work_on_stable |
| 38 | ./cargo.sh +stable check --tests --features __internal_use_only_features_that_work_on_stable |
| 39 | ./cargo.sh +nightly check --tests --all-features |
| 40 | ./cargo.sh +nightly clippy --tests --all-features --workspace |
| 41 | ``` |
| 42 | |
| 43 | **Note:** Tests are rarely toolchain-sensitive. Running tests on `nightly` is |
| 44 | usually sufficient. |
| 45 | |
| 46 | ## Testing Strategy |
| 47 | |
| 48 | - **Unit Tests:** Place unit tests in a `mod tests` module within the source |
| 49 | file they test. |
| 50 | - **UI/Compile-Fail Tests:** |
| 51 | - **`zerocopy`:** Place in `tests/ui-*` (top-level). The top-level `tests` |
| 52 | directory contains *only* UI tests. |
| 53 | - **`zerocopy-derive`:** Place in `zerocopy-derive/tests/ui-*`. |
| 54 | - **Derive Integration Tests:** Place integration tests for derive macros in |
| 55 | `zerocopy-derive/tests`. |
| 56 | - **Derive Output Tests:** Place unit tests that verify the *generated code* |
| 57 | (token streams) in `zerocopy-derive/src/output_tests.rs`. |
| 58 | - **Formal Verification (Kani):** Place Kani proofs in a `mod proofs` module |
| 59 | within the source file they test. |
| 60 | - **Purpose:** Use the |
| 61 | [Kani Rust Verifier](https://model-checking.github.io/kani/) to prove the |
| 62 | soundness of `unsafe` code or code relied upon by `unsafe` blocks. Unlike |
| 63 | testing, which checks specific inputs, Kani proves properties for *all* |
| 64 | possible inputs. |
| 65 | - **How to Write Proofs:** |
| 66 | - **Harnesses:** Mark proof functions with `#[kani::proof]`. |
| 67 | - **Inputs:** Use `kani::any()` to generate arbitrary inputs. |
| 68 | - **Assumptions:** Use `kani::assume(condition)` to constrain inputs to |
| 69 | valid states (e.g., `align.is_power_of_two()`). |
| 70 | - **Assertions:** Use `assert!(condition)` to verify the properties you |
| 71 | want to prove. |
| 72 | - **CI:** Kani runs in CI using the `model-checking/kani-github-action` with |
| 73 | specific feature flags to ensure compatibility. |
| 74 | |
| 75 | <!-- FIXME: Describe how to ensure that a Kani proof is "total" (esp wrt function inputs). --> |
| 76 | |
| 77 | ## Feature Gates |
| 78 | |
| 79 | When editing code gated by a feature, compile **with and without** that feature. |
| 80 | |
| 81 | ```bash |
| 82 | ./cargo.sh +stable check --tests |
| 83 | ./cargo.sh +stable check --tests --feature foo |
| 84 | ``` |