diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 313725245d..79c4cc73b9 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -2323,6 +2323,11 @@ pub fn parse_unit_value<'res>( let lhs = strip_underscores(value[..lhs_len].as_bytes()); let lhs_span = Span::new(span.start, span.start + lhs_len); let unit_span = Span::new(span.start + lhs_len, span.end); + if lhs.ends_with('$') { + // If `parse_unit_value` has higher precedence over `parse_range`, + // a variable with the name of a unit could otherwise not be used as the end of a range. + return None; + } let (decimal_part, number_part) = modf(match lhs.parse::() { Ok(it) => it, diff --git a/crates/nu-parser/tests/test_parser.rs b/crates/nu-parser/tests/test_parser.rs index 6c305c2971..8446146b98 100644 --- a/crates/nu-parser/tests/test_parser.rs +++ b/crates/nu-parser/tests/test_parser.rs @@ -1191,6 +1191,16 @@ mod range { assert!(!working_set.parse_errors.is_empty()); } + + #[test] + fn vars_not_read_as_units() { + let engine_state = EngineState::new(); + let mut working_set = StateWorkingSet::new(&engine_state); + + let _ = parse(&mut working_set, None, b"0..<$day", true); + + assert!(working_set.parse_errors.is_empty()); + } } #[cfg(test)]