diff --git a/crates/nu-parser/src/errors.rs b/crates/nu-parser/src/errors.rs index 444873d1a..3c92ca829 100644 --- a/crates/nu-parser/src/errors.rs +++ b/crates/nu-parser/src/errors.rs @@ -73,6 +73,10 @@ pub enum ParseError { #[diagnostic(code(nu::parser::variable_not_found), url(docsrs))] VariableNotFound(#[label = "variable not found"] Span), + #[error("Variable name not supported.")] + #[diagnostic(code(nu::parser::variable_not_valid), url(docsrs))] + VariableNotValid(#[label = "variable name can't contain spaces or quotes"] Span), + #[error("Module not found.")] #[diagnostic(code(nu::parser::module_not_found), url(docsrs))] ModuleNotFound(#[label = "module not found"] Span), diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 2f8f94597..e4eb9f489 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -1782,6 +1782,13 @@ pub fn parse_var_with_opt_type( ) -> (Expression, Option) { let bytes = working_set.get_span_contents(spans[*spans_idx]).to_vec(); + if bytes.contains(&b' ') || bytes.contains(&b'"') || bytes.contains(&b'\'') { + return ( + garbage(spans[*spans_idx]), + Some(ParseError::VariableNotValid(spans[*spans_idx])), + ); + } + if bytes.ends_with(b":") { // We end with colon, so the next span should be the type if *spans_idx + 1 < spans.len() { diff --git a/src/tests.rs b/src/tests.rs index 13c1f8a55..e0e2d1508 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -759,3 +759,8 @@ fn custom_switch4() -> TestResult { "bar", ) } + +#[test] +fn bad_var_name() -> TestResult { + fail_test(r#"let $"foo bar" = 4"#, "can't contain") +}