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

Trait is not implemented for array #6619

Open
Lukasz2891 opened this issue Oct 7, 2024 · 1 comment
Open

Trait is not implemented for array #6619

Lukasz2891 opened this issue Oct 7, 2024 · 1 comment
Labels
bug Something isn't working compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen

Comments

@Lukasz2891
Copy link

Lukasz2891 commented Oct 7, 2024

Related Component

compiler

Problem

library;

pub trait ArrWrap<T> {
    fn _len(self) -> u64;
    fn _get(self, i: u64) -> T;
}

impl<T> ArrWrap<T> for [T; 1] {
    fn _len(self) -> u64 {
        1
    }

    fn _get(self, i: u64) -> T {
        self[i]
    }
}

pub trait ToVec {
    type V;

    fn to_vec(self) -> Vec<Self::V>;
}

impl<T, U> ToVec for T
where
    T: ArrWrap<U>,
{
    type V = U;

    fn to_vec(self) -> Vec<U> {
        let mut result = Vec::new();
        let mut i = 0;
        while (i < self._len()) {
            result.push(self._get(i));
            i += 1;
        }

        result
    }
}
contract;

use to_vec::*;

configurable {
    TEST: [b256; 1] = ["0x0000000000000000000000008BB8F32Df04c8b654987DAaeD53D6B6091e3B774"],
}
fn xxx() {
    let _: Vec<b256> = TEST.to_vec();
}

causes

fn xxx() {
    let _: Vec<b256> = TEST.to_vec();
                           ^^^^^^ Trait "ArrWrap<U>" is not implemented for type "[b256; 1]".
}

Steps

Use the code as above

Possible Solution(s)

Remove generic parameters ;)

Notes

No response

Installed components

[components]
fuel-core = "0.37.1"
forc = "0.65.2"
@Lukasz2891 Lukasz2891 added bug Something isn't working triage This issue was opened with a template and needs to be triaged by code owners. labels Oct 7, 2024
@IGI-111 IGI-111 added the compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen label Oct 8, 2024
@IGI-111
Copy link
Contributor

IGI-111 commented Oct 8, 2024

Here's as minimal an example as I can make it:

script;

pub trait Foo<T> {
    fn foo(a: T);
}

impl<T> Foo<T> for [T; 1] {
    fn foo(a: T) {}
}

pub trait Bar {
    fn bar(self);
}

impl<T> Bar for T
where
    T: Foo<T>,
{
    fn bar(self) {}
}

fn main() {
    let a: [b256; 1] = [0x0000000000000000000000008BB8F32Df04c8b654987DAaeD53D6B6091e3B774];
    a.bar();
}

The fact that Foo<T> is generic seems to be required to trigger the error. Even with the trait constraint a non generic Foo compiles fine.

@IGI-111 IGI-111 removed the triage This issue was opened with a template and needs to be triaged by code owners. label Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen
Projects
None yet
Development

No branches or pull requests

2 participants