Skip to content

Commit

Permalink
chore: Add trybuild check for two instantiate methods (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
kulikthebird committed Jul 9, 2024
1 parent 0cec90b commit 3ab2a01
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
28 changes: 28 additions & 0 deletions sylvia-derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 0 additions & 3 deletions sylvia-derive/src/parser/attributes/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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() {
Expand Down
41 changes: 41 additions & 0 deletions sylvia/tests/ui/missing_method/msgs_misused.rs
Original file line number Diff line number Diff line change
@@ -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() {}
26 changes: 26 additions & 0 deletions sylvia/tests/ui/missing_method/msgs_misused.stderr
Original file line number Diff line number Diff line change
@@ -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)]
| ^

0 comments on commit 3ab2a01

Please sign in to comment.