sway/forc-plugins/forc-migrate
Igor Rončević e303b0f1ab
Add migration for new_hashing (#7287)
## Description

This PR:
- implements "Review existing usages of `StorageMap`, `sha256`, and
`keccak256`" migration step for `new_hashing` experimental feature, as
described in the Breaking Changes chapter of the tracking issue #7256.
- extends the migration infrastructure by providing an optional help
message related to an occurrance in code. Instead of returning only
`Span`, migration steps now return an `Occurrance` that combine the
`Span` with an optional help message.
- extends the migration infrastructure to visit all the
`TyExpressionVariant`s. For some of the expressions, the typed elements
are still not provided. In such cases the migration step for
`new_hashing` still detects an occurrence, but the help message lacks
the detailed type information.
- extends the migration infrastructure to visit all the declarations
relevant for the `new_hashing` migration step. E.g.; `trait`, `abi`, and
`storage` decls.

Additionally, the PR implements `AsRawSlice` for `str`.

Here is an excerpt of an output of the migration step run on a test
project:

```console
info: [new_hashing] Review existing usages of `StorageMap`, `sha256`, and `keccak256`
   --> /src/test.sw:62:39
    |
...
 12 |     let _ = sha256("affected");
    |                    ---------- info: This has type "str".
...
 24 |         let user_metadata_key_id = sha256((user_id, key));
    |                                           -------------- info: This has type "(b256, String)", that recursively contains "Bytes".
    |
 71 |     let _ = sha256_alias(StructCRec::new());
    |                          ----------------- info: This has type "StructCRec", that recursively contains "Vec<u8>".
    |
   ::: /src/main.sw:52:5
    |
...
 13 |     let _ = sha256(x);
    |                    - info: This has generic type "T". Review all the concrete types used with it.
    |
 39 |     directly_affected_str_array: StorageMap<str[3], u64> = StorageMap {},
    |     --------------------------- info: Review this storage field, because of "str[3]" in "StorageMap" key.
    |
 43 |     nested_affected_storage_maps: StorageMap<StructA, StorageMap<raw_slice, StorageMap<EnumARec, StorageVec<StorageMap<str[3], u64>>>>> = StorageMap {},
    |     ---------------------------- info: Review this storage field, because of "[u8; 0]", "slice", "[u8; 0]", and "str[3]" in "StorageMap" keys.
    |
 52 |     metadata: StorageMetadata = StorageMetadata {},
    |     -------- info: Review this storage field, because of potential custom storage type "StorageMetadata".
 56 | 
 72 |         storage.metadata.insert(sha256(handle_name), key, value);
    |                                        ----------- info: This has type "String", that contains "Bytes".
    |
    |
    = help: New hashing changes the hashes of instances of the following types:
    = help:   - string slices (`str`)
    = help:   - string arrays (`str[N]`)
    = help:   - arrays (`[T; N]`)
    = help:   - raw slices (`raw_slice`)
    = help:   - vectors (`std::vec::Vec<T>`)
    = help:   - bytes (`std::bytes::Bytes`)
    = help:  
    = help: To decide if opting-in to new hashing is backward-compatible and safe or not,
    = help: review if those types are directly used, or are contained in types:
    = help:   - used as keys in `StorageMap`s,
    = help:   - used in custom storage types,
    = help:   - hashed using `sha256` or `keccak256` functions.
    = help:  
    = help: ╔═════════════════════════════════════════════════════════════════════════════════════╗
    = help: ║ The above occurrences must not be seen as comprehensive, but rather as a guideline. ║
    = help: ║ Carefully review all the storage access and hashing patterns in your code.          ║
    = help: ║ E.g., using precomputed hashes, having custom `Hash` implementations, and similar.  ║
    = help: ╚═════════════════════════════════════════════════════════════════════════════════════╝
    = help:  
    = help: For a detailed migration guide see: https://github.com/FuelLabs/sway/issues/7256
```


## 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)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] 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.
2025-07-18 04:40:36 +00:00
..
src Add migration for new_hashing (#7287) 2025-07-18 04:40:36 +00:00
Cargo.toml Bump fuel deps (#7228) 2025-06-23 13:05:53 +04:00