From 67ea0bd5e71a8f5a2a9776bf83936296589ce7dc Mon Sep 17 00:00:00 2001 From: bugarela Date: Tue, 9 Apr 2024 10:37:32 -0300 Subject: [PATCH 1/2] Only translate functions with `DepsMut` as actions While translations with `Deps` are pure helper defs --- src/translate.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/translate.rs b/src/translate.rs index 040612c..a40b6c9 100644 --- a/src/translate.rs +++ b/src/translate.rs @@ -44,7 +44,7 @@ impl Translatable for Ident { ("i32", "int"), ("Decimal", "int"), ("Timestamp", "int"), - ("DepsMut", "ContractState"), + ("DepsMut", "ContractStateMut"), // Not an actual translation, but a placeholder ("Deps", "ContractState"), ("Ok", "Ok"), ("deps", "state"), @@ -432,7 +432,7 @@ impl Translatable for rustc_hir::FnRetTy<'_> { impl Translatable for Function<'_> { fn translate(&self, ctx: &mut Context) -> String { - // If one of the params is of type Deps or DepsMut, and the return type is "Result", this is a state transformer, + // If one of the params is of type DepsMut, and the return type is "Result", this is a state transformer, // and therefore should take the state as an argument and return it let mut has_state = false; @@ -441,10 +441,13 @@ impl Translatable for Function<'_> { .map(|(input, param)| { let translated_param = param.translate(ctx); let translated_type = input.translate(ctx); - if translated_type == "ContractState" { + if translated_type == "ContractStateMut" { has_state = true; return "state: ContractState".to_string(); } + if translated_type == "ContractState" { + return "state: ContractState".to_string(); + } format!("{}: {}", translated_param, translated_type) }) .collect_vec() From ef1a9ef33f70a01344f470513238149b26660ad0 Mon Sep 17 00:00:00 2001 From: bugarela Date: Tue, 9 Apr 2024 10:53:55 -0300 Subject: [PATCH 2/2] Rename variables and attributes to match new criteria --- src/boilerplate.rs | 4 ++-- src/lib.rs | 2 +- src/translate.rs | 16 ++++++++-------- src/types.rs | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/boilerplate.rs b/src/boilerplate.rs index 87adf29..32a436e 100644 --- a/src/boilerplate.rs +++ b/src/boilerplate.rs @@ -154,7 +154,7 @@ pub fn post_items(ctx: &Context) -> String { .join(",\n"); let special_actions = ["execute", "instantiate", "reply"]; - let reply = if !ctx.stateful_ops.contains(&"reply".to_string()) { + let reply = if !ctx.ops_with_mutability.contains(&"reply".to_string()) { // Generate default reply to be given for the message handler " @@ -166,7 +166,7 @@ pub fn post_items(ctx: &Context) -> String { }; let actions = ctx - .stateful_ops + .ops_with_mutability .iter() .filter(|op| !special_actions.contains(&op.as_str())) .map(|op| format!("{op}_action")) diff --git a/src/lib.rs b/src/lib.rs index 1e90949..bc347ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -145,7 +145,7 @@ fn traslate_items(tcx: TyCtxt, crate_name: &str, items: Vec<&rustc_hir::Item>) { }, )]), structs: HashMap::new(), - stateful_ops: vec![], + ops_with_mutability: vec![], contract_state: vec![], // scoped record_fields: vec![], diff --git a/src/translate.rs b/src/translate.rs index a40b6c9..19012e2 100644 --- a/src/translate.rs +++ b/src/translate.rs @@ -434,7 +434,7 @@ impl Translatable for Function<'_> { fn translate(&self, ctx: &mut Context) -> String { // If one of the params is of type DepsMut, and the return type is "Result", this is a state transformer, // and therefore should take the state as an argument and return it - let mut has_state = false; + let mut has_mutability = false; let param_tuples = zip(self.decl.inputs, self.body.params); let input = param_tuples @@ -442,7 +442,7 @@ impl Translatable for Function<'_> { let translated_param = param.translate(ctx); let translated_type = input.translate(ctx); if translated_type == "ContractStateMut" { - has_state = true; + has_mutability = true; return "state: ContractState".to_string(); } if translated_type == "ContractState" { @@ -460,8 +460,8 @@ impl Translatable for Function<'_> { return "".to_string(); } - if has_state { - ctx.stateful_ops.push(ctx.current_item_name.clone()); + if has_mutability { + ctx.ops_with_mutability.push(ctx.current_item_name.clone()); format!("({input}): ({output}, ContractState)") } else { @@ -519,12 +519,12 @@ impl Translatable for rustc_hir::Item<'_> { return "".to_string(); } - let has_state = ctx.stateful_ops.contains(&name.to_string()); + let has_mutability = ctx.ops_with_mutability.contains(&name.to_string()); let body_value = body.value.translate(ctx); - if !has_state || name == "execute" { - // Direct translation for non-stateful functions (i.e. - // helpers) Also for execute, since it's a special case - it + if !has_mutability || name == "execute" { + // Direct translation for functions with no mutability (i.e. + // helpers). Also for execute, since it's a special case - it // has a match statement to call other actions. Excute is // always called from `execute_message` from the boilerplate // part diff --git a/src/types.rs b/src/types.rs index 214c6a7..8536500 100644 --- a/src/types.rs +++ b/src/types.rs @@ -33,7 +33,7 @@ pub struct Context<'tcx> { pub message_type_for_action: HashMap, pub constructors: HashMap, pub structs: HashMap>, - pub stateful_ops: Vec, + pub ops_with_mutability: Vec, pub tcx: TyCtxt<'tcx>, pub contract_state: Vec<(String, String)>, // scoped