Skip to content

Commit 27f5835

Browse files
authored
Merge pull request #2376 from CosmWasm/co/add-migrate-error
Add compiler error for incorrect `entry_point` macro usage
2 parents f93e5aa + dcc7b16 commit 27f5835

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

packages/derive/src/lib.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,15 @@ fn expand_bindings(crate_path: &syn::Path, mut function: syn::ItemFn) -> TokenSt
130130
let fn_name = &function.sig.ident;
131131
let wasm_export = format_ident!("__wasm_export_{fn_name}");
132132

133-
// Migrate entry point can take 2 or 3 arguments
133+
// Prevent contract dev from using the wrong identifier for the do_migrate_with_info function
134+
if fn_name == "migrate_with_info" {
135+
return syn::Error::new_spanned(
136+
&function.sig.ident,
137+
r#"To use the new migrate function signature, you should provide a "migrate" entry point with 4 arguments, not "migrate_with_info""#,
138+
).into_compile_error();
139+
}
140+
141+
// Migrate entry point can take 2 or 3 arguments (not counting deps)
134142
let do_call = if fn_name == "migrate" && args == 3 {
135143
format_ident!("do_migrate_with_info")
136144
} else {
@@ -263,6 +271,26 @@ mod test {
263271
};
264272

265273
assert_eq!(actual.to_string(), expected.to_string());
274+
275+
// this should cause a compiler error
276+
let code = quote! {
277+
#[entry_point]
278+
pub fn migrate_with_info(
279+
deps: DepsMut,
280+
env: Env,
281+
msg: MigrateMsg,
282+
migrate_info: MigrateInfo,
283+
) -> Result<Response, ()> {
284+
// Logic here
285+
}
286+
};
287+
288+
let actual = entry_point_impl(TokenStream::new(), code);
289+
let expected = quote! {
290+
::core::compile_error! { "To use the new migrate function signature, you should provide a \"migrate\" entry point with 4 arguments, not \"migrate_with_info\"" }
291+
};
292+
293+
assert_eq!(actual.to_string(), expected.to_string());
266294
}
267295

268296
#[test]

0 commit comments

Comments
 (0)