From f43d25a48b31c320ef8bfbf80885ef1667a45065 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 6 Sep 2024 16:27:12 -0300 Subject: [PATCH 1/3] feat: add a `panic` method to the stdlib --- noir_stdlib/src/lib.nr | 1 + noir_stdlib/src/meta/op.nr | 6 ++++-- noir_stdlib/src/panic.nr | 4 ++++ noir_stdlib/src/prelude.nr | 1 + 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 noir_stdlib/src/panic.nr diff --git a/noir_stdlib/src/lib.nr b/noir_stdlib/src/lib.nr index 81c408c01bc..714079d306a 100644 --- a/noir_stdlib/src/lib.nr +++ b/noir_stdlib/src/lib.nr @@ -28,6 +28,7 @@ mod runtime; mod meta; mod append; mod mem; +mod panic; // Oracle calls are required to be wrapped in an unconstrained function // Thus, the only argument to the `println` oracle is expected to always be an ident diff --git a/noir_stdlib/src/meta/op.nr b/noir_stdlib/src/meta/op.nr index f3060a1648b..b3433cc9582 100644 --- a/noir_stdlib/src/meta/op.nr +++ b/noir_stdlib/src/meta/op.nr @@ -39,7 +39,8 @@ impl UnaryOp { } else if self.is_dereference() { quote { * } } else { - crate::mem::zeroed() + let op = self; + panic(f"Unexpected unary operator in UnaryOp::quoted: {op}") } } } @@ -181,7 +182,8 @@ impl BinaryOp { } else if self.is_modulo() { quote { % } } else { - crate::mem::zeroed() + let op = self; + panic(f"Unexpected binary operator in BinaryOp::quoted: {op}") } } } diff --git a/noir_stdlib/src/panic.nr b/noir_stdlib/src/panic.nr new file mode 100644 index 00000000000..4f765bd8717 --- /dev/null +++ b/noir_stdlib/src/panic.nr @@ -0,0 +1,4 @@ +pub fn panic(message: fmtstr) -> U { + assert(false, message); + std::mem::zeroed() +} diff --git a/noir_stdlib/src/prelude.nr b/noir_stdlib/src/prelude.nr index b14f38bdf55..b6e54eaae60 100644 --- a/noir_stdlib/src/prelude.nr +++ b/noir_stdlib/src/prelude.nr @@ -7,3 +7,4 @@ pub use crate::cmp::{Eq, Ord}; pub use crate::default::Default; pub use crate::convert::{From, Into}; pub use crate::meta::{derive, derive_via}; +pub use crate::panic::panic; From 7302454a6ce6ad9230c149435c2ce6b585ecf8bf Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 6 Sep 2024 16:36:52 -0300 Subject: [PATCH 2/3] Fully qualify panic --- noir_stdlib/src/meta/op.nr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/noir_stdlib/src/meta/op.nr b/noir_stdlib/src/meta/op.nr index b3433cc9582..57c20a37546 100644 --- a/noir_stdlib/src/meta/op.nr +++ b/noir_stdlib/src/meta/op.nr @@ -40,7 +40,7 @@ impl UnaryOp { quote { * } } else { let op = self; - panic(f"Unexpected unary operator in UnaryOp::quoted: {op}") + std::panic::panic(f"Unexpected unary operator in UnaryOp::quoted: {op}") } } } @@ -183,7 +183,7 @@ impl BinaryOp { quote { % } } else { let op = self; - panic(f"Unexpected binary operator in BinaryOp::quoted: {op}") + std::panic::panic(f"Unexpected binary operator in BinaryOp::quoted: {op}") } } } From 63ae319f11edeebaca2b35b06acfa913ba49c04b Mon Sep 17 00:00:00 2001 From: jfecher Date: Fri, 6 Sep 2024 15:17:25 -0500 Subject: [PATCH 3/3] Apply suggestions from code review --- noir_stdlib/src/meta/op.nr | 4 ++-- noir_stdlib/src/panic.nr | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/noir_stdlib/src/meta/op.nr b/noir_stdlib/src/meta/op.nr index 57c20a37546..1917a04da59 100644 --- a/noir_stdlib/src/meta/op.nr +++ b/noir_stdlib/src/meta/op.nr @@ -40,7 +40,7 @@ impl UnaryOp { quote { * } } else { let op = self; - std::panic::panic(f"Unexpected unary operator in UnaryOp::quoted: {op}") + crate::panic::panic(f"Unexpected unary operator in UnaryOp::quoted: {op}") } } } @@ -183,7 +183,7 @@ impl BinaryOp { quote { % } } else { let op = self; - std::panic::panic(f"Unexpected binary operator in BinaryOp::quoted: {op}") + crate::panic::panic(f"Unexpected binary operator in BinaryOp::quoted: {op}") } } } diff --git a/noir_stdlib/src/panic.nr b/noir_stdlib/src/panic.nr index 4f765bd8717..833c1f3dcfc 100644 --- a/noir_stdlib/src/panic.nr +++ b/noir_stdlib/src/panic.nr @@ -1,4 +1,4 @@ pub fn panic(message: fmtstr) -> U { assert(false, message); - std::mem::zeroed() + crate::mem::zeroed() }