Skip to content

Commit

Permalink
feat: Assert no attributes used on self and ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Nov 20, 2024
1 parent 63f6db8 commit f721860
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
32 changes: 32 additions & 0 deletions sylvia-derive/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub fn process_fields<'s, Generic>(
where
Generic: GetPath + PartialEq,
{
assert_no_self_ctx_attributes(sig);

sig.inputs
.iter()
.skip(2)
Expand All @@ -80,3 +82,33 @@ where
})
.collect()
}

/// Assert Sylvia attributes were not used on `self` and `ctx` parameters.
fn assert_no_self_ctx_attributes(sig: &Signature) {
sig.inputs.iter().take(2).for_each(|arg| match arg {
FnArg::Receiver(item) => {
item.attrs.iter().for_each(|attr| {
if SylviaAttribute::new(attr).is_none() {
return;
}
emit_error!(attr.span(),
"Invalid usage of Sylvia attribute.";
note = "First and second arguments of the method should be `self` and `ctx` respectively.";
note = "Unexpected attribute on `self` parameter."
);
});
}
FnArg::Typed(item) => {
item.attrs.iter().for_each(|attr| {
if SylviaAttribute::new(attr).is_none() {
return;
}
emit_error!(attr.span(),
"Invalid usage of Sylvia attribute.";
note = "First and second arguments of the method should be `self` and `ctx` respectively.";
note = "Unexpected attribute on `ctx` parameter."
);
});
}
});
}
28 changes: 28 additions & 0 deletions sylvia/tests/ui/attributes/payload/invalid_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,32 @@ pub mod used_on_context {
}
}

pub mod used_on_self {
use super::*;

pub struct Contract {}

#[sylvia::contract]
#[sv::features(replies)]
impl Contract {
pub const fn new() -> Self {
Self {}
}

#[sv::msg(instantiate)]
fn instantiate(&self, _ctx: InstantiateCtx) -> StdResult<Response> {
Ok(Response::new())
}

#[sv::msg(reply, reply_on=success)]
fn reply(
#[sv::payload(raw)] &self,
_ctx: ReplyCtx,
payload: Binary,
) -> StdResult<Response> {
Ok(Response::new())
}
}
}

fn main() {}
20 changes: 20 additions & 0 deletions sylvia/tests/ui/attributes/payload/invalid_usage.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
error: Invalid usage of Sylvia attribute.

= note: First and second arguments of the method should be `self` and `ctx` respectively.
= note: Unexpected attribute on `ctx` parameter.

--> tests/ui/attributes/payload/invalid_usage.rs:23:25
|
23 | fn reply(&self, #[sv::payload(raw)] _ctx: ReplyCtx) -> StdResult<Response> {
| ^

error: Missing payload parameter.

= note: Expected at least one payload parameter at the end of parameter list.
Expand All @@ -6,3 +16,13 @@ error: Missing payload parameter.
|
23 | fn reply(&self, #[sv::payload(raw)] _ctx: ReplyCtx) -> StdResult<Response> {
| ^^^^^

error: Invalid usage of Sylvia attribute.

= note: First and second arguments of the method should be `self` and `ctx` respectively.
= note: Unexpected attribute on `self` parameter.

--> tests/ui/attributes/payload/invalid_usage.rs:48:13
|
48 | #[sv::payload(raw)] &self,
| ^

0 comments on commit f721860

Please sign in to comment.