Skip to content

Commit

Permalink
Check for storage types in impl method params (FuelLabs#4119)
Browse files Browse the repository at this point in the history
## Description
Fixes FuelLabs#4040 

## 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).
- [ ] 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.

---------

Co-authored-by: Anton Trunov <[email protected]>
  • Loading branch information
vaivaswatha and anton-trunov authored Feb 17, 2023
1 parent ebe7afe commit 7f95449
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
15 changes: 15 additions & 0 deletions sway-core/src/semantic_analysis/storage_only_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,21 @@ fn decl_validate(engines: Engines<'_>, decl: &ty::TyDeclaration) -> CompileResul
warnings,
errors
);
for param in method.parameters {
if !param.is_self() {
check!(
check_type(
engines,
param.type_argument.type_id,
param.type_argument.span.clone(),
false
),
continue,
warnings,
errors
);
}
}
check!(
check_type(
engines,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,25 @@ fn insert(mapping: StorageMap<u64, u64>) {
#[storage(read)]
fn return_storage_vec_standalone_fn() -> StorageVec<u64> {
storage.v
}
}

pub struct MyStruct { }

impl MyStruct {
#[storage(read, write)]
pub fn takes_storage_struct_in_impl(self, my_struct: StorageVec<u64>) {
my_struct.push(5);
}
}

pub trait MyTrait {
#[storage(read, write)]
fn takes_storage_struct_in_trait_impl(self, my_struct: StorageVec<u64>);
}

impl MyTrait for MyStruct {
#[storage(read, write)]
fn takes_storage_struct_in_trait_impl(self, my_struct: StorageVec<u64>) {
my_struct.push(5);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ category = "fail"
# check: $()fn return_storage_vec_standalone_fn() -> StorageVec<u64> {
# nextln: $()Type StorageVec<u64> can only be declared directly as a storage field

# check: $()error
# check: $()pub fn takes_storage_struct_in_impl(self, my_struct: StorageVec<u64>) {
# nextln: $()Type StorageVec<u64> can only be declared directly as a storage field

# check: $()error
# check: $()fn takes_storage_struct_in_trait_impl(self, my_struct: StorageVec<u64>) {
# nextln: $()Type StorageVec<u64> can only be declared directly as a storage field

# check: $()error
# check: $()bad_type: StorageVec<Vec<bool>> = StorageVec {},
# nextln: $()The type "StorageVec<Vec<bool>>" is not allowed in storage.

0 comments on commit 7f95449

Please sign in to comment.