Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix criteria for translating a function to an action #17

Merged
merged 2 commits into from
Apr 10, 2024
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
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
Loading