Skip to content

Commit

Permalink
[lang] Improve invalid selector error (#561)
Browse files Browse the repository at this point in the history
* [lang] Improve invalid selector message

* [lang] Improve span

* [lang] Return syn::Error instead of panicking

* Make clippy happy

* [lang] Reduce code dup with non_hex_err

* [lang] Rename fn

* [lang] Fix err functions

* [lang] Add comments

* [lang] Remove unnecessary lifetime
  • Loading branch information
cmichi authored Nov 4, 2020
1 parent c85cb78 commit 0eca603
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
44 changes: 25 additions & 19 deletions crates/lang/ir/src/ir/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,19 @@ impl InkAttribute {
}
}

/// Returns an error to notify about non-hex digits at a position.
fn err_non_hex(meta: &syn::Meta, pos: usize) -> syn::Error {
format_err_spanned!(meta, "encountered non-hex digit at position {}", pos)
}

/// Returns an error to notify about an invalid ink! selector.
fn invalid_selector_err_regex(meta: &syn::Meta) -> syn::Error {
format_err_spanned!(
meta,
"invalid selector - a selector must consist of four bytes in hex (e.g. `selector = \"0xCAFEBABE\"`)"
)
}

impl TryFrom<syn::NestedMeta> for AttributeArg {
type Error = syn::Error;

Expand All @@ -577,27 +590,20 @@ impl TryFrom<syn::NestedMeta> for AttributeArg {
if let syn::Lit::Str(lit_str) = &name_value.lit {
let regex = Regex::new(
r"0x([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})"
).map_err(|_| {
format_err_spanned!(
meta,
"invalid selector bytes"
)
})?;
).map_err(|_| invalid_selector_err_regex(&meta))?;
let str = lit_str.value();
let cap = regex.captures(&str).unwrap();
let cap = regex
.captures(&str)
.ok_or_else(|| invalid_selector_err_regex(&meta))?;
let selector_bytes = [
u8::from_str_radix(&cap[1], 16).expect(
"encountered non-hex digit at position 0",
),
u8::from_str_radix(&cap[2], 16).expect(
"encountered non-hex digit at position 1",
),
u8::from_str_radix(&cap[3], 16).expect(
"encountered non-hex digit at position 2",
),
u8::from_str_radix(&cap[4], 16).expect(
"encountered non-hex digit at position 3",
),
u8::from_str_radix(&cap[1], 16)
.map_err(|_| err_non_hex(&meta, 0))?,
u8::from_str_radix(&cap[2], 16)
.map_err(|_| err_non_hex(&meta, 1))?,
u8::from_str_radix(&cap[3], 16)
.map_err(|_| err_non_hex(&meta, 2))?,
u8::from_str_radix(&cap[4], 16)
.map_err(|_| err_non_hex(&meta, 3))?,
];
return Ok(AttributeArg {
ast: meta,
Expand Down
1 change: 1 addition & 0 deletions crates/lang/macro/tests/compile_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fn compile_tests() {
t.compile_fail("tests/ui/fail/M-02-message-missing-self-arg.rs");
t.compile_fail("tests/ui/fail/M-03-message-returns-self.rs");
t.compile_fail("tests/ui/fail/M-04-message-returns-non-codec.rs");
t.compile_fail("tests/ui/fail/M-05-message-invalid-selector.rs");
t.compile_fail("tests/ui/fail/M-10-method-unknown-ink-marker.rs");

t.compile_fail("tests/ui/fail/S-01-missing-storage-struct.rs");
Expand Down
19 changes: 19 additions & 0 deletions crates/lang/macro/tests/ui/fail/M-05-message-invalid-selector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use ink_lang as ink;

#[ink::contract]
mod message_invalid_selector {
#[ink(storage)]
pub struct MessageInvalidSelector {}

impl MessageInvalidSelector {
#[ink(constructor)]
pub fn constructor() -> Self {
Self {}
}

#[ink(message, selector = "0x00")]
pub fn invalid_selector(&self) { }
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: invalid selector - a selector must consist of four bytes in hex (e.g. `selector = "0xCAFEBABE"`)
--> $DIR/M-05-message-invalid-selector.rs:14:24
|
14 | #[ink(message, selector = "0x00")]
| ^^^^^^^^^^^^^^^^^

0 comments on commit 0eca603

Please sign in to comment.