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

Check namespace argument is identifier #931

Merged
merged 3 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 17 additions & 2 deletions crates/lang/ir/src/ir/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
),
})
}
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions crates/lang/macro/tests/compile_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: encountered invalid Rust identifier for namespace argument
--> $DIR/N-01-namespace-invalid-identifier.rs:8:23
|
8 | #[ink(namespace = "::invalid_identifier")]
| ^^^^^^^^^^^^^^^^^^^^^^
20 changes: 20 additions & 0 deletions crates/lang/macro/tests/ui/fail/N-02-namespace-invalid-type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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)]
| ^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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)]
| ^^^^^^^^^