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 19, 2024
1 parent a7af42b commit d9ee004
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
29 changes: 29 additions & 0 deletions sylvia-derive/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,35 @@ pub fn process_fields<'s, Generic>(
where
Generic: GetPath + PartialEq,
{
// Assert no accidental usage of Sylvia attributes on `self` and `ctx` parameters.
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;

Check warning on line 75 in sylvia-derive/src/parser/mod.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/mod.rs#L74-L75

Added lines #L74 - L75 were not covered by tests
}
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."

Check warning on line 80 in sylvia-derive/src/parser/mod.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/mod.rs#L77-L80

Added lines #L77 - L80 were not covered by tests
);
});
}

FnArg::Typed(item) => {
item.attrs.iter().for_each(|attr| {
if SylviaAttribute::new(attr).is_none() {
return;

Check warning on line 88 in sylvia-derive/src/parser/mod.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/mod.rs#L87-L88

Added lines #L87 - L88 were not covered by tests
}
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."

Check warning on line 93 in sylvia-derive/src/parser/mod.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/mod.rs#L90-L93

Added lines #L90 - L93 were not covered by tests
);
});
}
});

sig.inputs
.iter()
.skip(2)
Expand Down
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 d9ee004

Please sign in to comment.