Skip to content
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

Failed to find trait implementation #7075

Open
sirasistant opened this issue Jan 15, 2025 · 2 comments
Open

Failed to find trait implementation #7075

sirasistant opened this issue Jan 15, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@sirasistant
Copy link
Contributor

sirasistant commented Jan 15, 2025

Aim

Implement an automatically derivable serialize trait.
I have outputted what the derive is generating, and it's not finding a trait and I don't know why:

pub trait Deserialize<let N: u32> {
    fn deserialize(serialized: [Field; N]) -> Self;
}
impl Deserialize<1> for Field {
    fn deserialize(serialized: [Field; 1]) -> Self {
        serialized[0]
    }
}
impl Deserialize<1> for u32 {
    fn deserialize(serialized: [Field; 1]) -> Self {
        serialized[0] as u32
    }
}
impl<T, let K: u32> Deserialize<K + 1> for Option<T>
where
    T: Deserialize<K>,
{
    fn deserialize(serialized: [Field; K + 1]) -> Self {
        let is_some = serialized[0] == 1;
        if is_some {
            let mut deserialized = [0; K];
            for i in 0..K {
                deserialized[i] = serialized[i + 1];
            }
            Self::some(T::deserialize(deserialized))
        } else {
            Self::none()
        }
    }
}
impl<T, let N: u32, let K: u32> Deserialize<K * N> for [T; N]
where
    T: Deserialize<K>,
{
    fn deserialize(serialized: [Field; K * N]) -> Self {
        let mut result = [std::mem::zeroed(); N];
        let mut offset = 0;
        for i in 0..N {
            let mut deserialized = [0; K];
            for j in 0..K {
                deserialized[j] = serialized[offset + j];
            }
            result[i] = T::deserialize(deserialized);
            offset += K;
        }
        result
    }
}
#[derive(Eq)]
struct Bar {
    a: [Field; 1],
    b: Option<Field>,
}

// Autogenerated implementation
impl<let a_serialized_length: u32, let b_serialized_length: u32> Deserialize<a_serialized_length + b_serialized_length> for Bar
where
    [Field; 1]: Deserialize<a_serialized_length>,
    Option<Field>: Deserialize<b_serialized_length>,
{
    fn deserialize(serialized: [Field; a_serialized_length + b_serialized_length]) -> Self {
        let mut offset = 0;
        let mut a_serialized = [0; a_serialized_length];
        for i in 0..a_serialized_length {
            a_serialized[i] = serialized[offset + i];
        }
        offset += a_serialized_length;
        let a = Deserialize::deserialize(a_serialized);
        let mut b_serialized = [0; b_serialized_length];
        for i in 0..b_serialized_length {
            b_serialized[i] = serialized[offset + i];
        }
        offset += b_serialized_length;
        let b = Deserialize::deserialize(b_serialized);
        Self { a, b }
    }
}


fn main() {
    let bar_deserialized = Bar::deserialize([0; 3]);
}

Expected Behavior

I think the above should compile

Bug

error: No matching impl found for `[Field; 1]: Deserialize<(3 - _)>`
    ┌─ src/main.nr:288:28
    │
288 │     let bar_deserialized = Bar::deserialize([0; 3]);
    │                            ---------------- No impl for `[Field; 1]: Deserialize<(3 - _)>`
    │

To Reproduce

Workaround

None

Workaround Description

No response

Additional Context

No response

Project Impact

None

Blocker Context

No response

Nargo Version

No response

NoirJS Version

No response

Proving Backend Tooling & Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@sirasistant sirasistant added the bug Something isn't working label Jan 15, 2025
@asterite
Copy link
Collaborator

It's interesting that if main is this:

    let bar_deserialized: Bar = Deserialize::deserialize([0; 3]);

then it still doesn't compile, but if you try to compile it multiple times the compiler will give one of two different errors (so there's a bit of non-determinism here, which is very strange!)

@TomAFrench
Copy link
Member

there's a bit of non-determinism here

arggggggghhhhhhhhhh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants