From f4b1b2861dea2661d8ad9935bfa8f3b28a5366ea Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 13 Feb 2025 11:48:19 -0300 Subject: [PATCH] fix: update docs about integer overflows --- docs/docs/noir/concepts/data_types/integers.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/docs/noir/concepts/data_types/integers.md b/docs/docs/noir/concepts/data_types/integers.md index 41a823646dd..b8a5d498029 100644 --- a/docs/docs/noir/concepts/data_types/integers.md +++ b/docs/docs/noir/concepts/data_types/integers.md @@ -111,8 +111,9 @@ fn main(x: U128, y: U128) { Computations that exceed the type boundaries will result in overflow errors. This happens with both signed and unsigned integers. For example, attempting to prove: ```rust -fn main(x: u8, y: u8) { +fn main(x: u8, y: u8) -> pub u8 { let z = x + y; + z } ``` @@ -140,10 +141,20 @@ error: Assertion failed: 'attempt to add with overflow' A similar error would happen with signed integers: ```rust -fn main() { +fn main() -> i8 { let x: i8 = -118; let y: i8 = -11; let z = x + y; + z +} +``` + +Note that if a computation ends up being unused the compiler might remove it and it won't end up producing an overflow: + +```rust +fn main() { + // "255 + 1" would overflow, but `z` is unused so no computation happens + let z: u8 = 255 + 1; } ```