Skip to content

Commit fe7aa8a

Browse files
committed
Add more tests for vector variable expansion
Update the error message to make it more clear. Also add compile_fail tests to ensure invalid syntax are rejected. Note rust-analyzer may still report false errors, as a workaround we can disable the errors by adding below config: `"rust-analyzer.diagnostics.disabled": ["macro-error"]`
1 parent 74d1a95 commit fe7aa8a

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

macros/src/lexer.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,10 @@ impl Lexer {
418418
self.extend_last_arg(quote!(#var.as_os_str()));
419419
} else {
420420
if !self.last_arg_str.is_empty() {
421-
abort!(span, "vector variable can only be used alone");
421+
abort!(
422+
span,
423+
"vector variable cannot have content immediately before it"
424+
);
422425
}
423426
self.args.push(ParseArg::ArgVec(quote!(#var)));
424427
}

macros/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@
5656
//! run_fun!(echo "${msg 0}");
5757
//! ```
5858
//!
59+
//! ### Invalid vector variable syntax
60+
//!
61+
//! Vector variables cannot have text immediately before them:
62+
//! ```compile_fail
63+
//! # use cmd_lib::*;
64+
//! let opts = ["-a", "-f"];
65+
//! run_cmd!(ls xxx$[opts]);
66+
//! ```
67+
//!
68+
//! ```compile_fail
69+
//! # use cmd_lib::*;
70+
//! let msg = "hello";
71+
//! let opts = ["-a", "-f"];
72+
//! run_cmd!(ls $msg$[opts]);
73+
//! ```
74+
//!
75+
//! ```compile_fail
76+
//! # use cmd_lib::*;
77+
//! let opts = ["-a", "-f"];
78+
//! run_cmd!(echo$[opts]);
79+
//! ```
80+
//!
5981
//! ### Invalid redirect syntax
6082
//!
6183
//! Invalid redirect operator spacing:

tests/test_macros.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,9 @@ fn test_empty_arg() {
372372
fn test_env_var_with_equal_sign() {
373373
assert!(run_cmd!(A="-c B=c" echo).is_ok());
374374
}
375+
376+
#[test]
377+
fn test_vector_variable() {
378+
let opts = ["-n", "hello"];
379+
assert!(run_cmd!(echo $[opts]).is_ok())
380+
}

0 commit comments

Comments
 (0)