Skip to content

Commit

Permalink
Merge pull request #17 from informalsystems/gabriela/fix-action-criteria
Browse files Browse the repository at this point in the history
Fix criteria for translating a function to an action
  • Loading branch information
bugarela authored Apr 10, 2024
2 parents 3015c74 + ef1a9ef commit 70f7b2e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/boilerplate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"
Expand All @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
Expand Down
23 changes: 13 additions & 10 deletions src/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -432,17 +432,20 @@ 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;
let mut has_mutability = false;

let param_tuples = zip(self.decl.inputs, self.body.params);
let input = param_tuples
.map(|(input, param)| {
let translated_param = param.translate(ctx);
let translated_type = input.translate(ctx);
if translated_type == "ContractStateMut" {
has_mutability = true;
return "state: ContractState".to_string();
}
if translated_type == "ContractState" {
has_state = true;
return "state: ContractState".to_string();
}
format!("{}: {}", translated_param, translated_type)
Expand All @@ -457,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 {
Expand Down Expand Up @@ -516,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
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct Context<'tcx> {
pub message_type_for_action: HashMap<String, String>,
pub constructors: HashMap<String, Constructor>,
pub structs: HashMap<String, Vec<Field>>,
pub stateful_ops: Vec<String>,
pub ops_with_mutability: Vec<String>,
pub tcx: TyCtxt<'tcx>,
pub contract_state: Vec<(String, String)>,
// scoped
Expand Down

0 comments on commit 70f7b2e

Please sign in to comment.