Skip to content

Commit

Permalink
Check namespace argument is identifier (#931)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Robbepop authored Sep 21, 2021
1 parent 09ec050 commit 41b0a1c
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 2 deletions.
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)]
| ^^^^^^^^^^^^^^^^
20 changes: 20 additions & 0 deletions crates/lang/macro/tests/ui/fail/N-03-namespace-missing-argument.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)]
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)]
| ^^^^^^^^^

0 comments on commit 41b0a1c

Please sign in to comment.