Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions sway-core/src/decl_engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub struct DeclEngine {
abi_parsed_decl_id_map: RwLock<HashMap<DeclId<TyAbiDecl>, ParsedDeclId<AbiDeclaration>>>,
constant_parsed_decl_id_map:
RwLock<HashMap<DeclId<TyConstantDecl>, ParsedDeclId<ConstantDeclaration>>>,
const_generic_parsed_decl_id_map:
RwLock<HashMap<DeclId<TyConstGenericDecl>, ParsedDeclId<ConstGenericDeclaration>>>,
configurable_parsed_decl_id_map:
RwLock<HashMap<DeclId<TyConfigurableDecl>, ParsedDeclId<ConfigurableDeclaration>>>,
const_generics_parsed_decl_id_map:
Expand Down Expand Up @@ -104,6 +106,9 @@ impl Clone for DeclEngine {
constant_parsed_decl_id_map: RwLock::new(
self.constant_parsed_decl_id_map.read().clone(),
),
const_generic_parsed_decl_id_map: RwLock::new(
self.const_generic_parsed_decl_id_map.read().clone(),
),
configurable_parsed_decl_id_map: RwLock::new(
self.configurable_parsed_decl_id_map.read().clone(),
),
Expand Down Expand Up @@ -314,6 +319,7 @@ decl_engine_parsed_decl_id!(struct_parsed_decl_id_map, ty::TyStructDecl);
decl_engine_parsed_decl_id!(storage_parsed_decl_id_map, ty::TyStorageDecl);
decl_engine_parsed_decl_id!(abi_parsed_decl_id_map, ty::TyAbiDecl);
decl_engine_parsed_decl_id!(constant_parsed_decl_id_map, ty::TyConstantDecl);
decl_engine_parsed_decl_id!(const_generic_parsed_decl_id_map, ty::TyConstGenericDecl);
decl_engine_parsed_decl_id!(configurable_parsed_decl_id_map, ty::TyConfigurableDecl);
decl_engine_parsed_decl_id!(enum_parsed_decl_id_map, ty::TyEnumDecl);
decl_engine_parsed_decl_id!(type_alias_parsed_decl_id_map, ty::TyTypeAliasDecl);
Expand Down Expand Up @@ -378,6 +384,11 @@ decl_engine_parsed_decl!(
ty::TyConstantDecl,
Declaration::ConstantDeclaration
);
decl_engine_parsed_decl!(
const_generic_parsed_decl_id_map,
ty::TyConstGenericDecl,
Declaration::ConstGenericDeclaration
);
decl_engine_parsed_decl!(
configurable_parsed_decl_id_map,
ty::TyConfigurableDecl,
Expand Down
4 changes: 1 addition & 3 deletions sway-core/src/language/parsed/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ impl Declaration {
TypeAliasDeclaration(decl_id) => pe.get_type_alias(decl_id).span(),
TraitTypeDeclaration(decl_id) => pe.get_trait_type(decl_id).span(),
TraitFnDeclaration(decl_id) => pe.get_trait_fn(decl_id).span(),
ConstGenericDeclaration(_) => {
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
}
ConstGenericDeclaration(decl_id) => pe.get_const_generic(decl_id).span(),
}
}

Expand Down
6 changes: 6 additions & 0 deletions sway-core/src/language/parsed/declaration/const_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ pub struct ConstGenericDeclaration {
pub ty: TypeId,
pub span: Span,
}

impl ConstGenericDeclaration {
pub fn span(&self) -> Span {
self.span.clone()
}
}
6 changes: 2 additions & 4 deletions sway-core/src/language/ty/declaration/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,7 @@ impl TyDecl {
TyDecl::VariableDecl(_decl) => None,
TyDecl::ConstantDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id),
TyDecl::ConfigurableDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id),
TyDecl::ConstGenericDecl(_) => {
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
}
TyDecl::ConstGenericDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id),
TyDecl::TraitTypeDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id),
TyDecl::FunctionDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id),
TyDecl::TraitDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id),
Expand All @@ -582,9 +580,9 @@ impl TyDecl {
TyDecl::ImplSelfOrTrait(decl) => decl_engine.get_parsed_decl(&decl.decl_id),
TyDecl::AbiDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id),
TyDecl::GenericTypeForFunctionScope(_data) => None,
TyDecl::ErrorRecovery(_, _) => None,
TyDecl::StorageDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id),
TyDecl::TypeAliasDecl(decl) => decl_engine.get_parsed_decl(&decl.decl_id),
TyDecl::ErrorRecovery(_, _) => None,
}
}

Expand Down
7 changes: 4 additions & 3 deletions sway-core/src/semantic_analysis/coins_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ pub fn possibly_nonzero_u64_expression(ctx: &TypeCheckContext, expr: &ty::TyExpr
Some(expr) => possibly_nonzero_u64_expression(ctx, expr),
None => false,
},
ConstGenericExpression { .. } => {
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
}
ConstGenericExpression { decl, .. } => match decl.value.as_ref() {
Some(expr) => possibly_nonzero_u64_expression(ctx, expr),
None => true,
},
VariableExpression { name, .. } => {
match ctx.resolve_symbol(&Handler::default(), name).ok() {
Some(ty_decl) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cmds = [
"forc build --path {root} --experimental const_generics"
]
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ storage {
balances: StorageMap<Identity, u64> = StorageMap {},
}

fn call_using_const_generics_as_coins<const N: u64>(external_contract_id: ContractId) {
let caller = abi(OtherContract, external_contract_id.into());
let _ = caller.non_payable_method { coins: N }();
}

impl MyContract for Contract {
#[storage(read, write)]
fn withdraw(external_contract_id: ContractId) {
Expand All @@ -25,6 +30,9 @@ impl MyContract for Contract {
let caller = abi(OtherContract, external_contract_id.into());
caller.non_payable_method { coins: bal }();

// External call using const generics
call_using_const_generics_as_coins::<0>(external_contract_id);

// Storage update _after_ external call
storage.balances.insert(sender, 0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
source: test/src/snapshot/mod.rs
---
> forc build --path test/src/e2e_vm_tests/test_programs/should_fail/non_payable_non_script_call_with_non_zero_coins --experimental const_generics
exit status: 1
output:
Building test/src/e2e_vm_tests/test_programs/should_fail/non_payable_non_script_call_with_non_zero_coins
Compiling library std (sway-lib-std)
Compiling contract non_payable_non_script_call_with_non_zero_coins (test/src/e2e_vm_tests/test_programs/should_fail/non_payable_non_script_call_with_non_zero_coins)
warning
--> test/src/e2e_vm_tests/test_programs/should_fail/non_payable_non_script_call_with_non_zero_coins/src/main.sw:25:13
|
23 | fn withdraw(external_contract_id: ContractId) {
24 | let sender = msg_sender().unwrap();
25 | let bal = storage.balances.get(sender);
| --- This declaration is never used.
26 |
27 | assert(bal > 0);
|
____

warning
--> test/src/e2e_vm_tests/test_programs/should_fail/non_payable_non_script_call_with_non_zero_coins/src/main.sw:30:13
|
28 |
29 | // External call
30 | let caller = abi(OtherContract, external_contract_id.into());
| ------ This declaration is never used.
31 | caller.non_payable_method { coins: bal }();
32 |
|
____

error
--> test/src/e2e_vm_tests/test_programs/should_fail/non_payable_non_script_call_with_non_zero_coins/src/main.sw:18:13
|
16 | fn call_using_const_generics_as_coins<const N: u64>(external_contract_id: ContractId) {
17 | let caller = abi(OtherContract, external_contract_id.into());
18 | let _ = caller.non_payable_method { coins: N }();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Possibly non-zero amount of coins transferred to non-payable contract method "non_payable_method".
19 | }
20 |
|
____

error: Associated function or method is not found
--> test/src/e2e_vm_tests/test_programs/should_fail/non_payable_non_script_call_with_non_zero_coins/src/main.sw:27:20
|
...
27 | assert(bal > 0);
| ^ "gt(std::storage::storage_key::StorageKey<u64>, {numeric}) -> bool" is not found for type "std::storage::storage_key::StorageKey<u64>".
|
____

error
--> test/src/e2e_vm_tests/test_programs/should_fail/non_payable_non_script_call_with_non_zero_coins/src/main.sw:31:44
|
29 | // External call
30 | let caller = abi(OtherContract, external_contract_id.into());
31 | caller.non_payable_method { coins: bal }();
| ^^^ Mismatched types.
expected: u64
found: std::storage::storage_key::StorageKey<u64>.

32 |
33 | // External call using const generics
|
____

error
--> test/src/e2e_vm_tests/test_programs/should_fail/non_payable_non_script_call_with_non_zero_coins/src/main.sw:31:9
|
29 | // External call
30 | let caller = abi(OtherContract, external_contract_id.into());
31 | caller.non_payable_method { coins: bal }();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Possibly non-zero amount of coins transferred to non-payable contract method "non_payable_method".
32 |
33 | // External call using const generics
|
____

Aborting due to 4 errors.
error: Failed to compile non_payable_non_script_call_with_non_zero_coins

This file was deleted.

Loading