Contents
Rust integrates ownership and data race checks into normal compilation. Ada has built-in range constraints and runtime checks, while SPARK uses separate analysis and proof to provide stronger guarantees.
Key Takeaways#
- Ada lets you constrain numeric ranges with subtype declarations. Rust typically uses private fields and validated constructors.
- Integer overflow behaves differently across Cargo’s default profiles, so production arithmetic needs an explicit policy.
- Safe Rust rejects memory misuse and data races that normal Ada compilation accepts.
- SPARK can check ownership and prove specified properties, but compiling Ada code alone does not perform these checks.
Build Constraints into Types#
A percentage should stay between 0 and 100. Ada expresses this constraint directly with subtype Percent is Integer range 0 .. 100;. Assigning an out-of-range value triggers a range check, typically raising Constraint_Error. A subtype is treated as the same type as its base type, so use a derived type if you want mixing up units to cause a type error.
Rust typically expresses the same constraint with a newtype such as Percent(u8) and a constructor returning Option or Result. Keeping the field private within the module forces external callers to use that constructor. Every public operation must preserve the invariant.
In Go Komura’s November 1, 2026 comparison ↗, assigning 255 to Ada’s range-constrained percentage raised Constraint_Error, while passing 120 to the Rust constructor returned None. Decide whether invalid input should produce an exception or an explicit return value, and design the interface accordingly.
Make the Overflow Policy Explicit#
The reported Ada experiment used a modular type declared with mod 256, so adding 1 to 255 produced 0. Adding 1 to Integer'Last with checks enabled raised Constraint_Error. Modular arithmetic and checked signed integer arithmetic thus showed clearly different behavior.
The Rust experiment revealed a deployment concern. With Cargo’s default development profile, adding 1 to a runtime u8 value of 255 caused a panic and exit code 101. With the default release profile, the result was 0 and the program exited successfully. Setting overflow-checks = true under [profile.release] caused release builds to panic as well.
For critical arithmetic, specify the behavior directly. In Rust, checked_add returned None, wrapping_add returned 0, and saturating_add returned 255. In Ada, choose modular types deliberately and review the compiler’s check settings. Suppressing required checks can turn an execution that should fail a check into erroneous execution. A result observed once is no guarantee.
Check Who Owns the Memory#
Rust rejected both reported examples: using a String after moving it, and retaining a reference after its referent left scope. These checks occur during normal compilation. Ada also rejected a pointer under its accessibility rules when it could outlive the local object it referenced.
A difference emerged with explicit deallocation. The Ada example copied pointer X to Y, then called Unchecked_Deallocation on X. This set X to null, but left Y pointing to freed storage. Normal GNAT compilation accepted the program, but dereferencing Y resulted in erroneous execution. The printed value has no reliable meaning.
SPARK’s ownership policy addresses this kind of aliasing. In the reported test, GNAT compiled the code, but GNATprove rejected access through X after ownership had moved to Y. If your safety assurance relies on SPARK, passing its analysis must be a build requirement.
Protect Shared Data Updates#
When four Rust threads tried to update one counter without synchronization, compilation failed because the mutable borrows overlapped. Protecting the counter with a Mutex allowed compilation, and four threads incrementing it 100,000 times each produced 400,000.
Normal Ada compilation accepted code containing both an equivalent unprotected shared counter and a protected object. The source reports that the unprotected counter’s value varied between runs, while the protected counter reached 400,000. However, unsynchronized access causes erroneous execution, so these observations cannot establish guarantees for the whole program. Separately, GNATprove reported a possible data race in the SPARK example.
Preventing data races does not prove concurrent code correct. Deadlocks and incorrect operation ordering still require design and verification. In particular, when reading shared state and then updating it, check whether the entire operation needs synchronization.
Prove What Your Requirements Demand#
SPARK enables comparisons beyond compiler rejection. In the reported ownership example, removing deallocation produced a possible memory leak diagnostic; restoring it allowed the corresponding check to be proved. Safe Rust permits memory leaks, so memory safety alone does not guarantee bounded resource use.
A proof is limited to the analyzed code, supported features, contracts, and assumptions. A finding that there are no runtime errors does not prove that all functional requirements are met. Record exactly what was checked and keep unresolved diagnostics visible.
The source reports testing with GNAT 13.3.0, GNATprove FSF 15.0, and rustc/Cargo 1.97.0 on Ubuntu x86_64. These are the test versions listed in the source, not independently verified current recommendations.
Next Step#
Run one boundary-value test with your production build settings. Add 1 to the largest integer value your application accepts, and confirm that the result matches your documented overflow policy.
Source: https://comcomponent.com/blog/ada-vs-rust-safety/
Contents
