Unverified Commit 41b0a1c0 authored by Robin Freyler's avatar Robin Freyler Committed by GitHub
Browse files

Check namespace argument is identifier (#931)

* check that argument in #[ink(namespace = "argument")] is a Rust identifier

* add UI tests for #[ink(namespace = "..")] ink! property

# Conflicts:
#	crates/lang/macro/tests/compile_tests.rs
#	crates/lang/macro/tests/ui/fail/N-01-namespace-invalid-identifier.rs
#	crates/lang/macro/tests/ui/fail/N-01-namespace-invalid-identifier.stderr
#	crates/lang/macro/tests/ui/fail/N-02-namespace-invalid-type.rs
#	crates/lang/macro/tests/ui/fail/N-02-namespace-invalid-type.stderr
#	crates/lang/macro/tests/ui/fail/N-03-namespace-missing-argument.rs
#	crates/lang/macro/tests/ui/fail/N-03-namespace-missing-argument.stderr

* fix error message for non-identifier namespace argument
parent 09ec050e
Pipeline #158319 passed with stages
in 25 minutes and 56 seconds
......@@ -766,11 +766,16 @@ impl TryFrom<syn::NestedMeta> for AttributeFrag {
}
if name_value.path.is_ident("namespace") {
if let syn::Lit::Str(lit_str) = &name_value.lit {
let bytes = lit_str.value().into_bytes();
let argument = lit_str.value();
syn::parse_str::<syn::Ident>(&argument)
.map_err(|_error| format_err!(
lit_str,
"encountered invalid Rust identifier for namespace argument",
))?;
return Ok(AttributeFrag {
ast: meta,
arg: AttributeArg::Namespace(
Namespace::from(bytes),
Namespace::from(argument.into_bytes()),
),
})
}
......@@ -1090,6 +1095,16 @@ mod tests {
);
}
#[test]
fn namespace_invalid_identifier() {
assert_attribute_try_from(
syn::parse_quote! {
#[ink(namespace = "::invalid_identifier")]
},
Err("encountered invalid Rust identifier for namespace argument"),
);
}
#[test]
fn namespace_invalid_type() {
assert_attribute_try_from(
......
......@@ -60,5 +60,9 @@ fn compile_tests() {
t.compile_fail("tests/ui/fail/S-05-storage-as-event.rs");
t.compile_fail("tests/ui/fail/S-06-event-as-storage.rs");
t.compile_fail("tests/ui/fail/N-01-namespace-invalid-identifier.rs");
t.compile_fail("tests/ui/fail/N-02-namespace-invalid-type.rs");
t.compile_fail("tests/ui/fail/N-03-namespace-missing-argument.rs");
t.pass("tests/ui/chain_extension/E-01-simple.rs");
}
use ink_lang as ink;
#[ink::contract]
mod invalid_namespace_identifier {
#[ink(storage)]
pub struct MyStorage {}
#[ink(namespace = "::invalid_identifier")]
impl MyStorage {
#[ink(constructor)]
pub fn constructor() -> Self {
Self {}
}
#[ink(message)]
pub fn message(&self) {}
}
}
fn main() {}
error: encountered invalid Rust identifier for namespace argument
--> $DIR/N-01-namespace-invalid-identifier.rs:8:23
|
8 | #[ink(namespace = "::invalid_identifier")]
| ^^^^^^^^^^^^^^^^^^^^^^
use ink_lang as ink;
#[ink::contract]
mod invalid_namespace_identifier {
#[ink(storage)]
pub struct MyStorage {}
#[ink(namespace = true)]
impl MyStorage {
#[ink(constructor)]
pub fn constructor() -> Self {
Self {}
}
#[ink(message)]
pub fn message(&self) {}
}
}
fn main() {}
error: expecteded string type for `namespace` argument, e.g. #[ink(namespace = "hello")]
--> $DIR/N-02-namespace-invalid-type.rs:8:11
|
8 | #[ink(namespace = true)]
| ^^^^^^^^^^^^^^^^
use ink_lang as ink;
#[ink::contract]
mod invalid_namespace_identifier {
#[ink(storage)]
pub struct MyStorage {}
#[ink(namespace)]
impl MyStorage {
#[ink(constructor)]
pub fn constructor() -> Self {
Self {}
}
#[ink(message)]
pub fn message(&self) {}
}
}
fn main() {}
error: encountered #[ink(namespace)] that is missing its string parameter. Did you mean #[ink(namespace = name: str)] ?
--> $DIR/N-03-namespace-missing-argument.rs:8:11
|
8 | #[ink(namespace)]
| ^^^^^^^^^
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