-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic traits that return generic arrays don't bind array lengths #4088
Comments
This issue can also cause the following error (a lot clearer) in the monomorphization step:
|
Another possibly related issue: use dep::std::option::Option;
trait MySerialize<N> {
fn serialize(self) -> [Field; N];
}
trait MyDeserialize<N> {
fn deserialize(fields: [Field; N]) -> Self;
}
impl MySerialize<1> for Field {
fn serialize(self) -> [Field; 1] {
[self]
}
}
impl MyDeserialize<1> for Field {
fn deserialize(fields: [Field; 1]) -> Self {
fields[0]
}
}
#[oracle(storageRead)]
fn storage_read_oracle<N>(_storage_slot: Field, _number_of_elements: Field) -> [Field; N] {}
unconstrained fn storage_read_oracle_wrapper<N>(_storage_slot: Field) -> [Field; N] {
storage_read_oracle(_storage_slot, N)
}
pub fn storage_read<N>(storage_slot: Field) -> [Field; N] {
storage_read_oracle_wrapper(storage_slot)
}
struct PublicState<T> {
storage_slot: Field,
}
impl<T> PublicState<T> {
pub fn new(
storage_slot: Field,
) -> Self {
assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1.");
PublicState {
storage_slot,
}
}
pub fn read<T_SERIALIZED_LEN>(self) -> T where T: MyDeserialize<T_SERIALIZED_LEN> {
let fields: [Field; T_SERIALIZED_LEN] = storage_read(self.storage_slot);
T::deserialize(fields)
}
}
fn main(value: Field) {
let ps: PublicState<Field> = PublicState::new(27);
assert(ps.read() == value);
} with
The workaround consists in passing an unused sample array with the length: use dep::aztec::context::{Context};
use dep::std::option::Option;
trait MySerialize<N> {
fn serialize(self) -> [Field; N];
}
trait MyDeserialize<N> {
fn deserialize(fields: [Field; N]) -> Self;
// Such a hackkity hack!!!!
fn empty() -> [Field; N];
}
impl MySerialize<1> for Field {
fn serialize(self) -> [Field; 1] {
[self]
}
}
impl MyDeserialize<1> for Field {
fn deserialize(fields: [Field; 1]) -> Self {
fields[0]
}
fn empty() -> [Field; 1] {
dep::std::unsafe::zeroed()
}
}
#[oracle(storageRead)]
fn storage_read_oracle<N>(_storage_slot: Field, _number_of_elements: Field) -> [Field; N] {}
unconstrained fn storage_read_oracle_wrapper<N>(_storage_slot: Field, _sample: [Field; N]) -> [Field; N] {
storage_read_oracle(_storage_slot, N)
}
pub fn storage_read<N>(storage_slot: Field, _sample: [Field; N]) -> [Field; N] {
storage_read_oracle_wrapper(storage_slot, _sample)
}
struct PublicState<T> {
storage_slot: Field,
}
impl<T> PublicState<T> {
pub fn new(
storage_slot: Field,
) -> Self {
assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1.");
PublicState {
storage_slot,
}
}
pub fn read<T_SERIALIZED_LEN>(self) -> T where T: MyDeserialize<T_SERIALIZED_LEN> {
let fields: [Field; T_SERIALIZED_LEN] = storage_read(self.storage_slot, T::empty());
T::deserialize(fields)
}
}
fn main(value: Field) {
let ps: PublicState<Field> = PublicState::new(27);
assert(ps.read() == value);
} The ugly part of this workaround is that it requires to modify the serialize trait, which would affect users ): |
Another workaround is to call the trait method directly instead of using static method syntax: trait Serialize<N> {
fn serialize(self) -> [Field; N];
}
struct ValueNote {
value: Field,
}
impl Serialize<1> for ValueNote {
fn serialize(self) -> [Field; 1] {
[self.value]
}
}
fn check<N>(serialized_note: [Field; N]) {
assert(serialized_note[0] == 0);
}
fn oopsie<Note, N>(note: Note) where Note: Serialize<N> {
let serialized_note = note.serialize(); //ok
check(serialized_note)
}
fn main(mut note: ValueNote) {
oopsie(note);
} |
@sirasistant I think your second error is a bit different. Do you mind filing a separate issue? |
Opened #4124 |
…ing identifiers (#4121) # Description ## Problem\* Resolves #4088 ## Summary\* We were calling `instantiate` on identifiers before applying any trait constraints. So if we have a constraint like `Foo<Field>` (referring to the trait `trait Foo<T> { ... }`, and an identifier `foo : forall T. fn(T)`, we need to apply the `T = Field` constraint before instantiating `foo` to replace `T` with a fresh type variable. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** 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: Maxim Vezenov <[email protected]>
Aim
Doing code like this:
Expected Behavior
It should compile!
Bug
Panics with
Workaround: typing annotating the return value:
To Reproduce
Installation Method
None
Nargo Version
No response
Additional Context
No response
Would you like to submit a PR for this Issue?
No
Support Needs
No response
The text was updated successfully, but these errors were encountered: