Skip to content

Commit

Permalink
feat: add a panic method to the stdlib (#5966)
Browse files Browse the repository at this point in the history
# Description

## Problem

Resolves #5849

## Summary

Adds a `panic` method exactly as proposed in #5849 , and uses it in a
couple of places where `panic` would have been useful to have.

## Additional Context

Is it okay in `std::panic::panic`? Where should we document this? (maybe
in a new `panic.md` file, or somewhere else?)

## Documentation\*

Check one:
- [ ] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: jfecher <[email protected]>
  • Loading branch information
asterite and jfecher authored Sep 9, 2024
1 parent e1f81da commit b86c2bc
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions noir_stdlib/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions noir_stdlib/src/meta/op.nr
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ impl UnaryOp {
} else if self.is_dereference() {
quote { * }
} else {
crate::mem::zeroed()
let op = self;
crate::panic::panic(f"Unexpected unary operator in UnaryOp::quoted: {op}")
}
}
}
Expand Down Expand Up @@ -181,7 +182,8 @@ impl BinaryOp {
} else if self.is_modulo() {
quote { % }
} else {
crate::mem::zeroed()
let op = self;
crate::panic::panic(f"Unexpected binary operator in BinaryOp::quoted: {op}")
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions noir_stdlib/src/panic.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub fn panic<T, U, let N: u32>(message: fmtstr<N, T>) -> U {
assert(false, message);
crate::mem::zeroed()
}
1 change: 1 addition & 0 deletions noir_stdlib/src/prelude.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit b86c2bc

Please sign in to comment.