Unverified Commit 0eca603f authored by Michael Müller's avatar Michael Müller Committed by GitHub
Browse files

[lang] Improve invalid selector error (#561)

* [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
parent c85cb782
Pipeline #113190 failed with stages
in 22 minutes and 48 seconds
......@@ -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;
......@@ -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,
......
......@@ -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");
......
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() {}
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")]
| ^^^^^^^^^^^^^^^^^
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment