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

TryInto for converting Vec to array of fixed size has the wrong error type #1359

Open
maximebuyse opened this issue Mar 12, 2025 · 1 comment
Labels
f* F* backend libcore

Comments

@maximebuyse
Copy link
Contributor

fn main() {
    let x = vec![1];
    let y : Result<[u8; 32], _> = x.try_into();
}

Open this code snippet in the playground

The previous example gets rejected by F*:

* Error 19 at Playground.fst(36,4-40,7):
  - Subtyping check failed
  - Expected type
      Core.Result.t_Result (Rust_primitives.Arrays.t_Array Rust_primitives.Integers.u8
            (Rust_primitives.Integers.mk_usize 32))
        (Rust_primitives.Arrays.t_Slice Rust_primitives.Integers.u8)
    got type
      Core.Result.t_Result (Rust_primitives.Arrays.t_Array Rust_primitives.Integers.u8
            (Rust_primitives.Integers.mk_usize 32))
        (Core.Convert.impl_6 Rust_primitives.Integers.u8
            (Rust_primitives.Integers.mk_usize 32))
          .f_Error
  - The SMT solver could not prove the query. Use --query_stats for more
    details.
  - See also /home/.cargo/git/checkouts/hax-310c6deb65d920f9/f7ecc0c/proof-libs/fstar/core/Alloc.Vec.fst(4,32-4,41)
Verified module: Playground
1 error was reported (see above)
make[1]: *** [Makefile:79: Playground.fst.checked] Error 1
make: *** [Makefile:12: all] Error 2

This is because when trying to convert for example &[u8] to [u8, 32] , the error type is TryFromSliceError, but when converting Vec<u8> to [u8, 32], the error type is Vec<u8>.

I am afraid we cannot solve this because Vecs and slices have the same encoding in our F* core lib so this means we cannot provide a different error type for the two type class implementations of TryInto because both types are the same so the type class implementation must be the same as well.

@maximebuyse maximebuyse added f* F* backend libcore labels Mar 12, 2025
@maximebuyse
Copy link
Contributor Author

A workaround for this can be to write (&x[..]).try_into()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
f* F* backend libcore
Projects
None yet
Development

No branches or pull requests

1 participant