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

Adds Hash trait and Hasher struct to std lib. #4701

Merged
merged 13 commits into from
Aug 21, 2023
Merged

Adds Hash trait and Hasher struct to std lib. #4701

merged 13 commits into from
Aug 21, 2023

Conversation

esdrubal
Copy link
Contributor

@esdrubal esdrubal commented Jun 22, 2023

Description

Implements Hash trait for builtin types and for some std lib types.

This is required to have a proper hashing that does not change in case the internal memory representation of type changes.

Current hashing functions are hashing the types in memory directly, as this representation might change in future versions of the VM and compiler we want to have a more reliable way of getting hashes that won't change in future versions of the compiler and std lib.

Replaces use of sha256 function with Hasher in storage_map and storage_vec.

Adds intrinsic __check_str_type.

__check_str_type returns an error at compile time in case the provided generic type is not a str.

This is used to guarantee that Hasher write_str is not called with non str types.

Closes #3835.

Checklist

  • I have linked to any relevant issues.
  • 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).
  • 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.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@esdrubal esdrubal added the lib: std Standard library label Jun 22, 2023
@esdrubal esdrubal self-assigned this Jun 22, 2023
@IGI-111 IGI-111 added this to the October 2023 milestone Jul 6, 2023
esdrubal added a commit that referenced this pull request Jul 7, 2023
We were not calling replace_decl for variable declarations.
This was making some trait method calls to not be replaced.

Unblocks #4701
Fixes #4773
IGI-111 pushed a commit that referenced this pull request Jul 10, 2023
## Description

We were not calling replace_decl for variable declarations.
This was making some trait method calls to not be replaced.

Unblocks #4701
Fixes #4773

## 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).
- [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.
@esdrubal esdrubal force-pushed the esdrubal/hash branch 3 times, most recently from b5eb0b6 to 542c84a Compare July 10, 2023 14:22
@esdrubal esdrubal added the breaking May cause existing user code to break. Requires a minor or major release. label Jul 10, 2023
@esdrubal esdrubal requested a review from a team July 10, 2023 14:44
@esdrubal esdrubal marked this pull request as ready for review July 10, 2023 14:44
esdrubal added a commit that referenced this pull request Jul 17, 2023
The issue this commit solves is the method not found error thrown when
 when we try to use generic traits.

 This issue was fixed by adding support to the `unify_check` so left
 types can match right types `UnknownGeneric`. We also check that the left
 type implemnts the trait constraints of the `UnknownGeneric` before return true.

 Fixes #4806
 Unblocks #4701
esdrubal added a commit that referenced this pull request Jul 17, 2023
The issue this commit solves is the method not found error thrown when
 when we try to use generic traits.

 This issue was fixed by adding support to the `unify_check` so left
 types can match right types `UnknownGeneric`. We also check that the left
 type implemnts the trait constraints of the `UnknownGeneric` before return true.

 Fixes #4806
 Unblocks #4701
esdrubal added a commit that referenced this pull request Jul 19, 2023
The issue this commit solves is the method not found error thrown when
we try to use generic traits.

This issue was fixed by calling `insert_trait_implementation_for_type` while resolving
tuples and arrays, we also call `insert_trait_implementation_for_type` as
a fail safe when a method is not found.

Fixes #4806
Unblocks #4701
esdrubal added a commit that referenced this pull request Jul 19, 2023
The issue this commit solves is the method not found error thrown when
we try to use generic traits.

This issue was fixed by calling `insert_trait_implementation_for_type` while resolving
tuples and arrays, we also call `insert_trait_implementation_for_type` as
a fail safe when a method is not found.

Fixes #4806
Unblocks #4701
esdrubal added a commit that referenced this pull request Jul 20, 2023
## Description

The issue this commit solves is the method not found error thrown when
we try to use generic traits.

This issue was fixed by calling `insert_trait_implementation_for_type`
while resolving
tuples and arrays, we also call `insert_trait_implementation_for_type`
as
a fail-safe when a method is not found.

 Fixes #4806
 Unblocks #4701

## 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).
- [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: Anton Trunov <anton.a.trunov@gmail.com>
@anton-trunov
Copy link
Contributor

I'm wondering if this issue #3835 is related. Should it be closed after this PR is merged?

@IGI-111
Copy link
Contributor

IGI-111 commented Jul 26, 2023

It should, yeah.

@esdrubal esdrubal force-pushed the esdrubal/hash branch 3 times, most recently from 75f5566 to 1bd50e1 Compare August 4, 2023 10:12
Implements Hash trait for builtin types and for some std lib types.

This is required to have a proper hashing that does not change in
case the internal memory representation of type changes.

Current hashing functions are hashing the types in memory directly, as this
representation might change in future versions of the VM and compiler we
want to have a more reliable way of getting hashes that won't change in
future versions of the compiler and std lib.

Replaces use of sha256 function with Hasher in storage_map and storage_vec.

Adds intrinsic __check_str_type.

__check_str_type returns an error at compile time in case the provided
generic type is not a str.

This is used to guarantee that `Hasher` `write_str` is not called with
non str types.
@esdrubal
Copy link
Contributor Author

The PR is finally green, all review requests were addressed. Ready for review again.

@esdrubal esdrubal requested a review from a team August 17, 2023 08:44
@IGI-111
Copy link
Contributor

IGI-111 commented Aug 17, 2023

Small nit but LGTM

@IGI-111 IGI-111 requested a review from a team August 17, 2023 18:18
@esdrubal esdrubal merged commit 4f76001 into master Aug 21, 2023
@esdrubal esdrubal deleted the esdrubal/hash branch August 21, 2023 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking May cause existing user code to break. Requires a minor or major release. lib: std Standard library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce Sha256 and Keccak256 traits to the standard library
6 participants