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

Implicit Casting of Array Elements #6376

Closed
IGI-111 opened this issue Aug 7, 2024 · 0 comments · Fixed by #6420
Closed

Implicit Casting of Array Elements #6376

IGI-111 opened this issue Aug 7, 2024 · 0 comments · Fixed by #6420
Assignees
Labels
audit-report Related to the audit report bug Something isn't working P: medium

Comments

@IGI-111
Copy link
Contributor

IGI-111 commented Aug 7, 2024

CS-FSSA-010

When it comes to typing the elements of an array, the type system is not able to stick to proper types.
This leads to the following snippets all successfully compiling:

let a = [8, 8, 8u64];
let b: u32 = a[2];
// compiles even though we have given a hint
let a = [1u32, 1u64];
let b:u32 = a[1];
let mut a = [8u16, 8u16, 18446744073709551615];
log(a[2]); // Logs 2**16 - 1

Note, this issue relates to two other issue reported regarding typing of arrays and typing of literals

@IGI-111 IGI-111 added bug Something isn't working P: medium audit-report Related to the audit report labels Aug 7, 2024
@IGI-111 IGI-111 assigned esdrubal and xunilrj and unassigned esdrubal Aug 7, 2024
IGI-111 added a commit that referenced this issue Aug 26, 2024
## Description

This PR fixes: #6376

The issue is that we use the first element of the array as its element
type. This can be problematic as the first element is not "concrete".
For example, when it is `Numeric`.

Now we iterate the array elements, search for the first "good" element,
and use its type as the array type. This means:
1 - if the type is concrete, we use it;
2 - if the type is `Numeric`, we keep searching for something better;

This allows consistent arrays, but where the concrete types are at the
end.

```sway
let a = [1, 2u8]; <- we will choose u8
let a = [None, Some(1), Some(2u8)]; <- we will choose Option<u8>, instead of Option<T>
```

## snapshot tests

This PR also brings an experiment. Now when an "e2e" test has a file
`snapshot.toml` it will run as `cargo insta` snapshot tests. These tests
can be run as normal: `cargo r -p test`, and in two new ways:

```
> cargo t -p test
> cargo insta test
```

Snapshots can be reviewed using normal "cargo insta" workflow.

The secret lies in https://github.com/LukasKalbertodt/libtest-mimic. **I
also want to suggest that all our tests migrate to this model**, where
we could just use "cargo test" normally. Apart from that, I opted to use
the `forc` executable, which means tests will run in parallel. In the
future, if we want to poke compiler internals, we can expose them using
flags, or create a special binary to fit our needs.

`snapshot.toml` is still empty because I have no idea to what put there
at the moment. My initial idea was to configure what ends up into
snapshots (multiple runs, compiler flags, script/contract calls) etc...
But for the moment, empty is enough.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: IGI-111 <igi-111@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
audit-report Related to the audit report bug Something isn't working P: medium
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants