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

[lang] Improve invalid selector error #561

Merged
merged 10 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
46 changes: 32 additions & 14 deletions crates/lang/ir/src/ir/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,24 +580,42 @@ impl TryFrom<syn::NestedMeta> for AttributeArg {
).map_err(|_| {
format_err_spanned!(
meta,
"invalid selector bytes"
"invalid selector - a selector must consist of four bytes in hex (e.g. `selector = \"0xCAFEBABE\"`)"
)
})?;
let str = lit_str.value();
let cap = regex.captures(&str).unwrap();
let cap = regex.captures(&str)
.ok_or(
format_err_spanned!(
meta,
"invalid selector - a selector must consist of four bytes in hex (e.g. `selector = \"0xCAFEBABE\"`)"
)
cmichi marked this conversation as resolved.
Show resolved Hide resolved
)?;
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(|_| {
format_err_spanned!(
meta,
"encountered non-hex digit at position 0",
)
cmichi marked this conversation as resolved.
Show resolved Hide resolved
})?,
u8::from_str_radix(&cap[2], 16).map_err(|_| {
format_err_spanned!(
meta,
"encountered non-hex digit at position 1",
)
})?,
u8::from_str_radix(&cap[3], 16).map_err(|_| {
format_err_spanned!(
meta,
"encountered non-hex digit at position 2",
)
})?,
u8::from_str_radix(&cap[4], 16).map_err(|_| {
format_err_spanned!(
meta,
"encountered non-hex digit at position 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
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")]
| ^^^^^^^^^^^^^^^^^