Repository: CosmWasm/sylvia
Pull request #392 (2024-07-03 15:13:40)

chore: Add trybuild check for two instantiate methods

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/CosmWasm/sylvia/pull/392?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CosmWasm) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 85.57%. Comparing base [(`55e9667`)](https://app.codecov.io/gh/CosmWasm/sylvia/commit/55e96679c61e1fa4b40db2e9efdeb0b473738669?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CosmWasm) to head [(`4d33d1b`)](https://app.codecov.io/gh/CosmWasm/sylvia/commit/4d33d1bece6d5282f762d2a05851589fcb5d5d2e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CosmWasm).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #392      +/-   ##
==========================================
- Coverage   85.83%   85.57%   -0.26%     
==========================================
  Files          40       40              
  Lines        2866     2864       -2     
==========================================
- Hits         2460     2451       -9     
- Misses        406      413       +7     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/CosmWasm/sylvia/pull/392?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CosmWasm).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CosmWasm).


Patch:
diff --git a/sylvia-derive/src/input.rs b/sylvia-derive/src/input.rs
index 46bf805..148ae7c 100644
--- a/sylvia-derive/src/input.rs
+++ b/sylvia-derive/src/input.rs
@@ -126,6 +126,34 @@ impl<'a> TraitInput<'a> {
         let query = self.emit_msg(MsgType::Query);
         let sudo = self.emit_msg(MsgType::Sudo);
 
+        let instantiate = MsgVariants::new(
+            self.item.as_variants(),
+            MsgType::Instantiate,
+            &[] as &[&Ident],
+            &None,
+        );
+
+        if let Some(msg_variant) = instantiate.variants().next() {
+            emit_error!(
+                msg_variant.name().span(), "The message attribute `instantiate` is not supported in interfaces.";
+                note = "Contracts need to implement `instantiate` method within their `impl` block.";
+            );
+        }
+
+        let migrate = MsgVariants::new(
+            self.item.as_variants(),
+            MsgType::Migrate,
+            &[] as &[&Ident],
+            &None,
+        );
+
+        if let Some(msg_variant) = migrate.variants().next() {
+            emit_error!(
+                msg_variant.name().span(), "The message attribute `migrate` is not supported in interfaces";
+                note = "Contracts need to implement `migrate` method within their `impl` block.";
+            );
+        }
+
         quote! {
             #exec
 
diff --git a/sylvia-derive/src/parser/attributes/messages.rs b/sylvia-derive/src/parser/attributes/messages.rs
index cd09181..138f9e0 100644
--- a/sylvia-derive/src/parser/attributes/messages.rs
+++ b/sylvia-derive/src/parser/attributes/messages.rs
@@ -7,7 +7,6 @@ use syn::{parenthesized, Error, Ident, MetaList, Path, Result, Token};
 
 use proc_macro_error::emit_error;
 
-use crate::parser::extract_generics_from_path;
 use crate::strip_generics::StripGenerics;
 
 #[derive(Debug)]
@@ -77,8 +76,6 @@ fn interface_has_custom(content: ParseStream) -> Result<Customs> {
 impl Parse for ContractMessageAttr {
     fn parse(input: ParseStream) -> Result<Self> {
         let module = input.parse()?;
-        // If this is not for backwards compatibility we can remove it
-        let _ = extract_generics_from_path(&module);
         let module = StripGenerics.fold_path(module);
 
         let variant = if input.parse::<Token![as]>().is_ok() {


Test patch:
diff --git a/sylvia/tests/ui/missing_method/msgs_misused.rs b/sylvia/tests/ui/missing_method/msgs_misused.rs
new file mode 100644
index 0000000..11572ce
--- /dev/null
+++ b/sylvia/tests/ui/missing_method/msgs_misused.rs
@@ -0,0 +1,41 @@
+#![allow(unused_imports)]
+use sylvia::cw_std::{Response, StdResult};
+use sylvia::types::InstantiateCtx;
+
+pub struct Contract;
+
+
+mod interface {
+    use sylvia::cw_std::{Response, StdResult, StdError};
+    use sylvia::types::{InstantiateCtx, MigrateCtx};
+
+    #[sylvia::interface]
+    trait Interface {
+        type Error: From<StdError>;
+
+        #[sv::msg(instantiate)]
+        fn instantiate(&self, ctx: InstantiateCtx) -> StdResult<Response>;
+
+        #[sv::msg(migrate)]
+        fn migrate(&self, ctx: MigrateCtx) -> StdResult<Response>;
+    }
+}
+
+#[sylvia::contract]
+impl Contract {
+    pub const fn new() -> Self {
+        Contract
+    }
+
+    #[sv::msg(instantiate)]
+    pub fn instantiate(&self, ctx: InstantiateCtx) -> StdResult<Response> {
+        Ok(Response::new())
+    }
+
+    #[sv::msg(instantiate)]
+    pub fn instantiate2(&self, ctx: InstantiateCtx) -> StdResult<Response> {
+        Ok(Response::new())
+    }
+}
+
+fn main() {}
diff --git a/sylvia/tests/ui/missing_method/msgs_misused.stderr b/sylvia/tests/ui/missing_method/msgs_misused.stderr
new file mode 100644
index 0000000..6d36006
--- /dev/null
+++ b/sylvia/tests/ui/missing_method/msgs_misused.stderr
@@ -0,0 +1,26 @@
+error: The message attribute `instantiate` is not supported in interfaces.
+
+         = note: Contracts need to implement `instantiate` method within their `impl` block.
+
+  --> tests/ui/missing_method/msgs_misused.rs:17:12
+   |
+17 |         fn instantiate(&self, ctx: InstantiateCtx) -> StdResult<Response>;
+   |            ^^^^^^^^^^^
+
+error: The message attribute `migrate` is not supported in interfaces
+
+         = note: Contracts need to implement `migrate` method within their `impl` block.
+
+  --> tests/ui/missing_method/msgs_misused.rs:20:12
+   |
+20 |         fn migrate(&self, ctx: MigrateCtx) -> StdResult<Response>;
+   |            ^^^^^^^
+
+error: More than one instantiation or migration message
+
+         = note: Instantiation/Migration message previously defined here
+
+  --> tests/ui/missing_method/msgs_misused.rs:35:5
+   |
+35 |     #[sv::msg(instantiate)]
+   |     ^