From ef7c08c4c2e638ff91241c273e10ab60a5b8767c Mon Sep 17 00:00:00 2001 From: Tomasz Kulik Date: Wed, 3 Jul 2024 16:56:44 +0200 Subject: [PATCH] chore: Add trybuild check for two instantiate methods; Remove unused code --- sylvia-derive/src/input.rs | 28 +++++++++++++ .../src/parser/attributes/messages.rs | 3 -- .../tests/ui/missing_method/msgs_misused.rs | 41 +++++++++++++++++++ .../ui/missing_method/msgs_misused.stderr | 26 ++++++++++++ 4 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 sylvia/tests/ui/missing_method/msgs_misused.rs create mode 100644 sylvia/tests/ui/missing_method/msgs_misused.stderr diff --git a/sylvia-derive/src/input.rs b/sylvia-derive/src/input.rs index 46bf8057..148ae7c5 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 cd09181d..138f9e05 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 { impl Parse for ContractMessageAttr { fn parse(input: ParseStream) -> Result { 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::().is_ok() { 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 00000000..11572ceb --- /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; + + #[sv::msg(instantiate)] + fn instantiate(&self, ctx: InstantiateCtx) -> StdResult; + + #[sv::msg(migrate)] + fn migrate(&self, ctx: MigrateCtx) -> StdResult; + } +} + +#[sylvia::contract] +impl Contract { + pub const fn new() -> Self { + Contract + } + + #[sv::msg(instantiate)] + pub fn instantiate(&self, ctx: InstantiateCtx) -> StdResult { + Ok(Response::new()) + } + + #[sv::msg(instantiate)] + pub fn instantiate2(&self, ctx: InstantiateCtx) -> StdResult { + 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 00000000..6d360062 --- /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; + | ^^^^^^^^^^^ + +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; + | ^^^^^^^ + +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)] + | ^