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

feat: Emit error if contract macro is above entry_points #408

Merged
merged 3 commits into from
Jul 31, 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
39 changes: 39 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,45 @@ jobs:
path: |
examples/contracts/**/schema/*.json

# Errors differ between Rust versions. We test only the latest output.
test_ui:
name: Test UI
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- build: macOS
os: macOS-latest
- build: Windows
os: windows-latest
defaults:
run:
shell: bash
working-directory: ./
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Cache
uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: wasm32-unknown-unknown
profile: minimal
override: true
- name: Test project
run: cargo test --locked -- --ignored

coverage:
name: Code coverage
runs-on: ubuntu-latest
Expand Down
56 changes: 34 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 40 additions & 4 deletions sylvia-derive/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use proc_macro2::TokenStream;
use proc_macro2::{Span, TokenStream};
use proc_macro_error::{emit_error, emit_warning};
use quote::quote;
use syn::{GenericParam, Ident, ItemImpl, ItemTrait, TraitItem};
Expand All @@ -7,13 +7,13 @@
use crate::executor::{ContractExecutor, InterfaceExecutor};
use crate::interfaces::Interfaces;
use crate::message::{
ContractApi, ContractEnumMessage, EnumMessage, GlueMessage, InterfaceApi, MsgVariants,
StructMessage,
ContractApi, ContractEnumMessage, EntryPoints, EnumMessage, GlueMessage, InterfaceApi,
MsgVariants, StructMessage,
};
use crate::multitest::{ContractMtHelpers, TraitMtHelpers};
use crate::parser::attributes::msg::MsgType;
use crate::parser::{
assert_new_method_defined, ContractErrorAttr, Custom, OverrideEntryPoint,
assert_new_method_defined, ContractErrorAttr, Custom, EntryPointArgs, OverrideEntryPoint,
ParsedSylviaAttributes,
};
use crate::querier::{ContractQuerier, InterfaceQuerier};
Expand Down Expand Up @@ -339,3 +339,39 @@
ContractMtHelpers::new(item, generic_params, custom, override_entry_points.clone()).emit()
}
}

pub struct EntryPointInput<'a> {
item: &'a ItemImpl,
args: EntryPointArgs,
}

impl<'a> EntryPointInput<'a> {
pub fn new(item: &'a ItemImpl, args: EntryPointArgs, attr_span: Span) -> Self {
let instantiate =
MsgVariants::<GenericParam>::new(item.as_variants(), MsgType::Instantiate, &[], &None);

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

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/input.rs#L349-L351

Added lines #L349 - L351 were not covered by tests

if args.generics.len() != item.generics.params.len() {
emit_error!(
attr_span,

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

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/input.rs#L353-L355

Added lines #L353 - L355 were not covered by tests
"Missing concrete types.";
note = "For every generic type in the contract, a concrete type must be provided in `#[entry_points(generics<T1, T2, ...>)]`.";

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

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/input.rs#L357

Added line #L357 was not covered by tests
);
}

if instantiate.get_only_variant().is_none() {
emit_error!(
attr_span, "Missing instantiation message.";
note = "`sylvia::entry_points` requires exactly one method marked with `#[sv::msg(instantiation)]` attribute.";
note = "Make sure you implemented the `entry_points` macro above the `contract` macro."

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

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/input.rs#L361-L365

Added lines #L361 - L365 were not covered by tests
);
}

Self { item, args }
}

pub fn process(&self) -> TokenStream {
let Self { item, args } = self;

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

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/input.rs#L372-L373

Added lines #L372 - L373 were not covered by tests

EntryPoints::new(item, args).emit()

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

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/input.rs#L375

Added line #L375 was not covered by tests
}
}
14 changes: 6 additions & 8 deletions sylvia-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
//!
//! Please refer to the [Sylvia-book](https://cosmwasm.github.io/sylvia-book/index.html) on how to use these macros.

use input::{ImplInput, TraitInput};
use crate::parser::EntryPointArgs;
use input::{EntryPointInput, ImplInput, TraitInput};
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro_error::proc_macro_error;
use quote::quote;
use strip_input::StripInput;
use syn::fold::Fold;
use syn::spanned::Spanned;
use syn::{parse2, parse_quote, ItemImpl, ItemTrait, Path};

mod associated_types;
Expand All @@ -26,11 +29,6 @@
mod utils;
mod variant_descs;

use strip_input::StripInput;

use crate::message::EntryPoints;
use crate::parser::EntryPointArgs;

#[cfg(not(test))]
pub(crate) fn crate_module() -> Path {
use proc_macro_crate::{crate_name, FoundCrate};
Expand Down Expand Up @@ -759,8 +757,8 @@
fn entry_points_impl(attr: TokenStream2, item: TokenStream2) -> TokenStream2 {
fn inner(attr: TokenStream2, item: TokenStream2) -> syn::Result<TokenStream2> {
let input: ItemImpl = parse2(item)?;
let attrs = EntryPointArgs::new(&attr, &input)?;
let expanded = EntryPoints::new(&input, attrs).emit();
let args = EntryPointArgs::new(&attr)?;
let expanded = EntryPointInput::new(&input, args, attr.span()).process();

Check warning on line 761 in sylvia-derive/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/lib.rs#L760-L761

Added lines #L760 - L761 were not covered by tests

Ok(quote! {
#input
Expand Down
4 changes: 2 additions & 2 deletions sylvia-derive/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,11 +1264,11 @@
override_entry_points: Vec<OverrideEntryPoint>,
generics: Vec<&'a GenericParam>,
where_clause: &'a Option<WhereClause>,
attrs: EntryPointArgs,
attrs: &'a EntryPointArgs,
}

impl<'a> EntryPoints<'a> {
pub fn new(source: &'a ItemImpl, attrs: EntryPointArgs) -> Self {
pub fn new(source: &'a ItemImpl, attrs: &'a EntryPointArgs) -> Self {

Check warning on line 1271 in sylvia-derive/src/message.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/message.rs#L1271

Added line #L1271 was not covered by tests
let name = StripGenerics.fold_type(*source.self_ty.clone());
let parsed_attrs = ParsedSylviaAttributes::new(source.attrs.iter());
let override_entry_points = parsed_attrs.override_entry_point_attrs;
Expand Down
12 changes: 2 additions & 10 deletions sylvia-derive/src/parser/entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use syn::parse::{Error, Nothing, Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::{parse2, GenericArgument, ItemImpl, Path, Result, Token};
use syn::{parse2, GenericArgument, Path, Result, Token};

use super::extract_generics_from_path;

Expand All @@ -15,20 +15,12 @@
}

impl EntryPointArgs {
pub fn new(attr: &TokenStream2, source: &ItemImpl) -> Result<Self> {
pub fn new(attr: &TokenStream2) -> Result<Self> {

Check warning on line 18 in sylvia-derive/src/parser/entry_point.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/entry_point.rs#L18

Added line #L18 was not covered by tests
let args: Self = parse2(attr.clone()).map_err(|err| {
emit_error!(attr, err);
err
})?;

if args.generics.len() != source.generics.params.len() {
emit_error!(
attr.span(),
"Missing concrete types.";
note = "For every generic type in the contract, a concrete type must be provided in `#[entry_points(generics<T1, T2, ...>)]`.";
);
}

Ok(args)
}
}
Expand Down
2 changes: 0 additions & 2 deletions sylvia/src/into_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ impl<C> IntoMsg<C> for SubMsg<Empty> {
let msg = match self.msg {
CosmosMsg::Wasm(wasm) => CosmosMsg::Wasm(wasm),
CosmosMsg::Bank(bank) => CosmosMsg::Bank(bank),
#[cfg(feature = "staking")]
CosmosMsg::Staking(staking) => CosmosMsg::Staking(staking),
#[cfg(feature = "staking")]
CosmosMsg::Distribution(distribution) => CosmosMsg::Distribution(distribution),
CosmosMsg::Custom(_) => Err(StdError::generic_err(
"Custom Empty message should not be sent",
Expand Down
1 change: 1 addition & 0 deletions sylvia/tests/ui.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[test]
#[ignore]
fn ui() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/**/*.rs");
Expand Down
19 changes: 19 additions & 0 deletions sylvia/tests/ui/macros/entry_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,23 @@ pub mod missing_generics {
}
}

pub mod wrong_order {
use super::*;

pub struct Contract;

#[contract]
#[entry_points]
impl Contract {
pub fn new() -> Self {
Self
}

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

fn main() {}
Loading
Loading