From 1981927c8730919d663b5570be42dc7e6ae05d56 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sat, 6 Jul 2024 21:06:14 -0700 Subject: [PATCH 1/2] Fix function argument count mismatch error message --- src/function.rs | 10 +++++----- src/parser.rs | 12 ++++++------ tests/functions.rs | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/function.rs b/src/function.rs index dbce9349e1..08c1f7cbde 100644 --- a/src/function.rs +++ b/src/function.rs @@ -110,13 +110,13 @@ pub(crate) fn get(name: &str) -> Option { impl Function { pub(crate) fn argc(&self) -> Range { match *self { - Nullary(_) => 0..0, - Unary(_) => 1..1, - UnaryOpt(_) => 1..2, + Nullary(_) => 0..1, + Unary(_) => 1..2, + UnaryOpt(_) => 1..3, UnaryPlus(_) => 1..usize::MAX, - Binary(_) => 2..2, + Binary(_) => 2..3, BinaryPlus(_) => 2..usize::MAX, - Ternary(_) => 3..3, + Ternary(_) => 3..4, } } } diff --git a/src/parser.rs b/src/parser.rs index b1d088e9ec..01f2acfb26 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2568,7 +2568,7 @@ mod tests { kind: FunctionArgumentCountMismatch { function: "arch", found: 1, - expected: 0..0, + expected: 0..1, }, } @@ -2582,7 +2582,7 @@ mod tests { kind: FunctionArgumentCountMismatch { function: "env_var", found: 0, - expected: 1..1, + expected: 1..2, }, } @@ -2596,7 +2596,7 @@ mod tests { kind: FunctionArgumentCountMismatch { function: "env", found: 3, - expected: 1..2, + expected: 1..3, }, } @@ -2610,7 +2610,7 @@ mod tests { kind: FunctionArgumentCountMismatch { function: "env", found: 0, - expected: 1..2, + expected: 1..3, }, } @@ -2624,7 +2624,7 @@ mod tests { kind: FunctionArgumentCountMismatch { function: "env_var_or_default", found: 1, - expected: 2..2, + expected: 2..3, }, } @@ -2652,7 +2652,7 @@ mod tests { kind: FunctionArgumentCountMismatch { function: "replace", found: 1, - expected: 3..3, + expected: 3..4, }, } } diff --git a/tests/functions.rs b/tests/functions.rs index 68f8640bce..600668f80f 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -1050,3 +1050,21 @@ fn is_dependency() { .stdout("beta false\ngamma true\n") .run(); } + +#[test] +fn unary_argument_count_mismatfch_error_message() { + Test::new() + .justfile("x := datetime()") + .args(["--evaluate"]) + .stderr( + " + error: Function `datetime` called with 0 arguments but takes 1 + ——▶ justfile:1:6 + │ + 1 │ x := datetime() + │ ^^^^^^^^ + ", + ) + .status(EXIT_FAILURE) + .run(); +} From 51e70db03400aad483c59bb73c8bcd49a77547b5 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sat, 6 Jul 2024 21:17:25 -0700 Subject: [PATCH 2/2] Use exclusive range --- src/compile_error_kind.rs | 2 +- src/function.rs | 16 ++++++++-------- src/parser.rs | 14 +++++++------- src/range_ext.rs | 24 +++++++++++------------- tests/functions.rs | 2 +- 5 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/compile_error_kind.rs b/src/compile_error_kind.rs index 49b5624d0d..36a984379e 100644 --- a/src/compile_error_kind.rs +++ b/src/compile_error_kind.rs @@ -66,7 +66,7 @@ pub(crate) enum CompileErrorKind<'src> { FunctionArgumentCountMismatch { function: &'src str, found: usize, - expected: Range, + expected: RangeInclusive, }, Include, InconsistentLeadingWhitespace { diff --git a/src/function.rs b/src/function.rs index 08c1f7cbde..f741b5e411 100644 --- a/src/function.rs +++ b/src/function.rs @@ -108,15 +108,15 @@ pub(crate) fn get(name: &str) -> Option { } impl Function { - pub(crate) fn argc(&self) -> Range { + pub(crate) fn argc(&self) -> RangeInclusive { match *self { - Nullary(_) => 0..1, - Unary(_) => 1..2, - UnaryOpt(_) => 1..3, - UnaryPlus(_) => 1..usize::MAX, - Binary(_) => 2..3, - BinaryPlus(_) => 2..usize::MAX, - Ternary(_) => 3..4, + Nullary(_) => 0..=0, + Unary(_) => 1..=1, + UnaryOpt(_) => 1..=2, + UnaryPlus(_) => 1..=usize::MAX, + Binary(_) => 2..=2, + BinaryPlus(_) => 2..=usize::MAX, + Ternary(_) => 3..=3, } } } diff --git a/src/parser.rs b/src/parser.rs index 01f2acfb26..90389a29f0 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2568,7 +2568,7 @@ mod tests { kind: FunctionArgumentCountMismatch { function: "arch", found: 1, - expected: 0..1, + expected: 0..=0, }, } @@ -2582,7 +2582,7 @@ mod tests { kind: FunctionArgumentCountMismatch { function: "env_var", found: 0, - expected: 1..2, + expected: 1..=1, }, } @@ -2596,7 +2596,7 @@ mod tests { kind: FunctionArgumentCountMismatch { function: "env", found: 3, - expected: 1..3, + expected: 1..=2, }, } @@ -2610,7 +2610,7 @@ mod tests { kind: FunctionArgumentCountMismatch { function: "env", found: 0, - expected: 1..3, + expected: 1..=2, }, } @@ -2624,7 +2624,7 @@ mod tests { kind: FunctionArgumentCountMismatch { function: "env_var_or_default", found: 1, - expected: 2..3, + expected: 2..=2, }, } @@ -2638,7 +2638,7 @@ mod tests { kind: FunctionArgumentCountMismatch { function: "join", found: 1, - expected: 2..usize::MAX, + expected: 2..=usize::MAX, }, } @@ -2652,7 +2652,7 @@ mod tests { kind: FunctionArgumentCountMismatch { function: "replace", found: 1, - expected: 3..4, + expected: 3..=3, }, } } diff --git a/src/range_ext.rs b/src/range_ext.rs index bf4995e881..4773d22bcf 100644 --- a/src/range_ext.rs +++ b/src/range_ext.rs @@ -10,16 +10,14 @@ pub(crate) trait RangeExt { pub(crate) struct DisplayRange(T); -impl Display for DisplayRange<&Range> { +impl Display for DisplayRange<&RangeInclusive> { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - if self.0.start == self.0.end { - write!(f, "0")?; - } else if self.0.start == self.0.end - 1 { - write!(f, "{}", self.0.start)?; - } else if self.0.end == usize::MAX { - write!(f, "{} or more", self.0.start)?; + if self.0.start() == self.0.end() { + write!(f, "{}", self.0.start())?; + } else if *self.0.end() == usize::MAX { + write!(f, "{} or more", self.0.start())?; } else { - write!(f, "{} to {}", self.0.start, self.0.end - 1)?; + write!(f, "{} to {}", self.0.start(), self.0.end())?; } Ok(()) } @@ -76,10 +74,10 @@ mod tests { assert!(!(1..1).contains(&1)); assert!((1..1).is_empty()); assert!((5..5).is_empty()); - assert_eq!((1..1).display().to_string(), "0"); - assert_eq!((1..2).display().to_string(), "1"); - assert_eq!((5..6).display().to_string(), "5"); - assert_eq!((5..10).display().to_string(), "5 to 9"); - assert_eq!((1..usize::MAX).display().to_string(), "1 or more"); + assert_eq!((0..=0).display().to_string(), "0"); + assert_eq!((1..=1).display().to_string(), "1"); + assert_eq!((5..=5).display().to_string(), "5"); + assert_eq!((5..=9).display().to_string(), "5 to 9"); + assert_eq!((1..=usize::MAX).display().to_string(), "1 or more"); } } diff --git a/tests/functions.rs b/tests/functions.rs index 600668f80f..8fc81aca2b 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -1052,7 +1052,7 @@ fn is_dependency() { } #[test] -fn unary_argument_count_mismatfch_error_message() { +fn unary_argument_count_mismamatch_error_message() { Test::new() .justfile("x := datetime()") .args(["--evaluate"])