Skip to content

chore: Allow count of elements in repeated-element array syntax to be any static Field #2386

Closed
ghost wants to merge 7 commits intomasterfrom
unknown repository
Closed

chore: Allow count of elements in repeated-element array syntax to be any static Field #2386
ghost wants to merge 7 commits intomasterfrom
unknown repository

Conversation

@ghost
Copy link

@ghost ghost commented Aug 21, 2023

Description

WIP

Problem

Resolves #445

Summary

WIP

PR Checklist

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@ghost
Copy link
Author

ghost commented Aug 21, 2023

@jfecher, сan you check this and give me a hint?

@jfecher
Copy link
Contributor

jfecher commented Aug 21, 2023

@f01dab1e, #445 is only for array expressions like let foo = [1; length];, so we shouldn't need any type changes to this PR. I was thinking this may be quite difficult to implement originally but now that we have slices I think you can leverage those to desugar the above syntax into something like:

let foo = {
    let mut result = [];
    for _ in 0 .. length {
        result = result.push_back(1);
    }
    result.as_array()
};

We could even break this up and add this function to the std library:

// Free function in std::array
fn repeated<T>(element: T, length: Field) -> [T] {
    let mut result = [];
    for _ in 0 .. length {
        result = result.push_back(1);
    }
    result
}

// New method in std::slice
impl<T> [T] {
    fn as_array<N>(self) -> [T; N] {
        assert(self.len() == N);
        ... now somehow return an array without using [elem; len] syntax ...
    }
}

Hmm we may need a builtin for converting a slice to an array since I don't see how you can write as_array otherwise.

@ghost
Copy link
Author

ghost commented Aug 23, 2023

@jfecher, how to handle a case like this?

fn main(x : Field) {
    let x = [x];
    let n = x.as_array();
}
The application panicked (crashed).
Message:  Non-numeric type variable used in expression expecting a value
Location: crates/noirc_frontend/src/monomorphization/mod.rs:626
use dep::std;

fn main() {
    let slice = std::array::repeated(0, 5);
    let xs: [Field; 5] = slice.as_array();
    let x = xs[0];
    std::println(x);
}
error: Could not determine loop bound at compile-time
   ┌─ std/array.nr:89:17
   │
89 │     for _ in 0..length {
   │                 ------
   │
   = Call stack:
     1. std/array.nr:89:17

Error: Aborting due to 1 previous error

@jfecher
Copy link
Contributor

jfecher commented Aug 24, 2023

@f01dab1e I'm not sure. I'd think N should default to 0 if it cannot be inferred but of the top of my head I'm not sure why your errors are happening. The for loop bounds error are usually either because the length is actually not known at compile-time, or SSA optimizations are not optimizing it out by that point.

@ghost ghost mentioned this pull request Aug 24, 2023
2 tasks
@ghost
Copy link
Author

ghost commented Aug 24, 2023

@jfecher, closed by mistake, can you open it?

@jfecher jfecher reopened this Aug 24, 2023
@kevaundray
Copy link
Contributor

What is the status of this PR and can the issue and PR title be updated given we don't have comptime?

@ghost
Copy link
Author

ghost commented Dec 11, 2023

This was blocked by the fact that we had no way to indicate the type (size cannot be inferred from the context), now it's blocked by the same thing, but there's an open pull request after which we can resume work, but we encounter another problem, that the method with desugaring will break the formatter.

@ghost ghost changed the title chore: Allow count of elements in repeated-element array syntax to be any comptime Field chore: Allow count of elements in repeated-element array syntax to be any static Field Dec 12, 2023
@TomAFrench TomAFrench closed this Apr 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow count of elements in repeated-element array syntax to be any comptime Field

3 participants