-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement comptime support for
array_len
builtin (#5272)
# Description ## Problem\* Resolves <!-- Link to GitHub Issue --> ## Summary\* This PR implements support for the `array_len` builtin so we can query the length of arrays in `comptime` blocks. Ideally we'd tie this closer to the `Intrinsics` struct in `noirc_evaluator` but we can handle this morn cleanly later. ## Additional Context ## Documentation\* Check one: - [x] 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.
- Loading branch information
1 parent
55d8e05
commit c91186a
Showing
4 changed files
with
32 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use noirc_errors::Location; | ||
|
||
use crate::hir::comptime::{errors::IResult, Value}; | ||
|
||
pub(super) fn array_len(arguments: &[(Value, Location)]) -> IResult<Value> { | ||
assert_eq!(arguments.len(), 1, "ICE: `array_len` should only receive a single argument"); | ||
match &arguments[0].0 { | ||
Value::Array(values, _) | Value::Slice(values, _) => Ok(Value::U32(values.len() as u32)), | ||
// Type checking should prevent this branch being taken. | ||
_ => unreachable!("ICE: Cannot query length of types other than arrays or slices"), | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
test_programs/compile_success_empty/comptime_array_len/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "comptime_array_len" | ||
type = "bin" | ||
authors = [""] | ||
|
||
[dependencies] |
6 changes: 6 additions & 0 deletions
6
test_programs/compile_success_empty/comptime_array_len/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
fn main() { | ||
comptime | ||
{ | ||
assert_eq([1, 2, 3].len(), 3); | ||
} | ||
} |