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

chore: Add trybuild check for two instantiate methods #392

Merged
merged 1 commit into from
Jul 9, 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
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 @@
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.";

Check warning on line 139 in sylvia-derive/src/input.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/input.rs#L137-L139

Added lines #L137 - L139 were not covered by tests
);
}

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.";

Check warning on line 153 in sylvia-derive/src/input.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/input.rs#L151-L153

Added lines #L151 - L153 were not covered by tests
);
}

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)]
| ^
Loading