Skip to content

Commit 6553dce

Browse files
authored
Allow CEI analysis with const generics (#7392)
## Description Continuation of #6860. ## 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. - [ ] 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.
1 parent d524df9 commit 6553dce

File tree

4 files changed

+9
-5
lines changed

4 files changed

+9
-5
lines changed

sway-core/src/language/ty/expression/expression.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,9 @@ impl MaterializeConstGenerics for TyExpression {
597597
exp.materialize_const_generics(engines, handler, name, value)
598598
}
599599
TyExpressionVariant::Continue | TyExpressionVariant::Break => Ok(()),
600+
TyExpressionVariant::AbiCast { address, .. } => {
601+
address.materialize_const_generics(engines, handler, name, value)
602+
}
600603
_ => Err(handler.emit_err(
601604
sway_error::error::CompileError::ConstGenericNotSupportedHere {
602605
span: self.span.clone(),

sway-core/src/semantic_analysis/cei_pattern_analysis.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,11 @@ fn analyze_expression(
220220
use crate::ty::TyExpressionVariant::*;
221221
let decl_engine = engines.de();
222222
match &expr.expression {
223-
ConstGenericExpression { .. } => {
224-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
225-
}
226223
// base cases: no warnings can be emitted
227224
Literal(_)
228225
| ConstantExpression { .. }
229226
| ConfigurableExpression { .. }
227+
| ConstGenericExpression { .. }
230228
| VariableExpression { .. }
231229
| FunctionParameter
232230
| StorageAccess(_)

test/src/e2e_vm_tests/test_programs/should_pass/static_analysis/cei_pattern_violation_in_standalone_function/src/main.sw

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ abi TestAbi {
88
}
99

1010
#[storage(write)]
11-
fn standalone_function() {
11+
fn standalone_function<const N: u64>() {
1212
let other_contract = abi(TestAbi, 0x3dba0a4455b598b7655a7fb430883d96c9527ef275b49739e7b0ad12f8280eae);
1313

14+
let _ = __dbg(N);
15+
1416
// interaction
1517
other_contract.deposit();
1618
// effect -- therefore violation of CEI where effect should go before interaction
@@ -21,6 +23,6 @@ fn standalone_function() {
2123
impl TestAbi for Contract {
2224
#[storage(write)]
2325
fn deposit() {
24-
standalone_function();
26+
standalone_function::<5>();
2527
}
2628
}

test/src/e2e_vm_tests/test_programs/should_pass/static_analysis/cei_pattern_violation_in_standalone_function/test.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ category = "compile"
22

33
# check: $()Storage write after external contract interaction in function or method "standalone_function". Consider making all storage writes before calling another contract
44
expected_warnings = 2
5+
experimental = { const_generics = true }

0 commit comments

Comments
 (0)