From bc156841af0eb1a17de26f9961bacb91baa47628 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Wo=C5=BAniak?= Date: Wed, 3 Apr 2024 14:59:42 +0200 Subject: [PATCH] chore: Improve `no instantiation` error message --- Cargo.lock | 112 +++++++++++++++++- sylvia-derive/src/message.rs | 5 +- sylvia/Cargo.toml | 1 + sylvia/tests/ui.rs | 5 + .../tests/ui/missing_method/instantiation.rs | 10 ++ .../ui/missing_method/instantiation.stderr | 8 ++ 6 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 sylvia/tests/ui.rs create mode 100644 sylvia/tests/ui/missing_method/instantiation.rs create mode 100644 sylvia/tests/ui/missing_method/instantiation.stderr diff --git a/Cargo.lock b/Cargo.lock index 5fa5a9e2..525f778f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -414,6 +414,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "group" version = "0.13.0" @@ -571,7 +577,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" dependencies = [ "once_cell", - "toml_edit", + "toml_edit 0.19.15", ] [[package]] @@ -768,6 +774,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" +dependencies = [ + "serde", +] + [[package]] name = "sha2" version = "0.9.9" @@ -842,6 +857,7 @@ dependencies = [ "serde-json-wasm", "sylvia-derive", "thiserror", + "trybuild", ] [[package]] @@ -899,6 +915,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.58" @@ -919,11 +944,26 @@ dependencies = [ "syn 2.0.55", ] +[[package]] +name = "toml" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit 0.22.9", +] + [[package]] name = "toml_datetime" version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +dependencies = [ + "serde", +] [[package]] name = "toml_edit" @@ -933,7 +973,35 @@ checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" dependencies = [ "indexmap", "toml_datetime", - "winnow", + "winnow 0.5.40", +] + +[[package]] +name = "toml_edit" +version = "0.22.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e40bb779c5187258fd7aad0eb68cb8706a0a81fa712fbea808ab43c4b8374c4" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow 0.6.5", +] + +[[package]] +name = "trybuild" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ad7eb6319ebadebca3dacf1f85a93bc54b73dd81b9036795f73de7ddfe27d5a" +dependencies = [ + "glob", + "once_cell", + "serde", + "serde_derive", + "serde_json", + "termcolor", + "toml", ] [[package]] @@ -981,6 +1049,37 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "winnow" version = "0.5.40" @@ -990,6 +1089,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "winnow" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dffa400e67ed5a4dd237983829e66475f0a4a26938c4b04c21baede6262215b8" +dependencies = [ + "memchr", +] + [[package]] name = "zeroize" version = "1.7.0" diff --git a/sylvia-derive/src/message.rs b/sylvia-derive/src/message.rs index 3be5f42a..73fa3e24 100644 --- a/sylvia-derive/src/message.rs +++ b/sylvia-derive/src/message.rs @@ -94,7 +94,10 @@ impl<'a> StructMessage<'a> { method } else { if ty == MsgType::Instantiate { - emit_error!(source.span(), "No instantiation message"); + emit_error!( + source.span(), "Missing instantiation message."; + note = source.span() => "`sylvia::contract` requires exactly one method marked with `#[sv::msg(instantiation)]` attribute." + ); } return None; }; diff --git a/sylvia/Cargo.toml b/sylvia/Cargo.toml index 4e09ba00..030e0905 100644 --- a/sylvia/Cargo.toml +++ b/sylvia/Cargo.toml @@ -46,6 +46,7 @@ cw-multi-test = { workspace = true } cw-storage-plus = { workspace = true } cw-utils = { workspace = true } thiserror = { workspace = true } +trybuild = "1.0.91" [package.metadata.docs.rs] all-features = true diff --git a/sylvia/tests/ui.rs b/sylvia/tests/ui.rs new file mode 100644 index 00000000..bd72ecdd --- /dev/null +++ b/sylvia/tests/ui.rs @@ -0,0 +1,5 @@ +#[test] +fn ui() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/ui/**/*.rs"); +} diff --git a/sylvia/tests/ui/missing_method/instantiation.rs b/sylvia/tests/ui/missing_method/instantiation.rs new file mode 100644 index 00000000..12aff85e --- /dev/null +++ b/sylvia/tests/ui/missing_method/instantiation.rs @@ -0,0 +1,10 @@ +pub struct Contract {} + +#[sylvia::contract] +impl Contract { + pub const fn new() -> Self { + Self {} + } +} + +fn main() {} diff --git a/sylvia/tests/ui/missing_method/instantiation.stderr b/sylvia/tests/ui/missing_method/instantiation.stderr new file mode 100644 index 00000000..77352b93 --- /dev/null +++ b/sylvia/tests/ui/missing_method/instantiation.stderr @@ -0,0 +1,8 @@ +error: Missing instantiation message. + + = note: `sylvia::contract` requires exactly one method marked with `#[sv::msg(instantiation)]` attribute. + + --> tests/ui/missing_method/instantiation.rs:4:1 + | +4 | impl Contract { + | ^^^^