Skip to content

Commit 241e7b4

Browse files
authored
Fix function argument count mismatch error message (#2231)
1 parent 0c9b159 commit 241e7b4

File tree

5 files changed

+45
-29
lines changed

5 files changed

+45
-29
lines changed

src/compile_error_kind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub(crate) enum CompileErrorKind<'src> {
6666
FunctionArgumentCountMismatch {
6767
function: &'src str,
6868
found: usize,
69-
expected: Range<usize>,
69+
expected: RangeInclusive<usize>,
7070
},
7171
Include,
7272
InconsistentLeadingWhitespace {

src/function.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ pub(crate) fn get(name: &str) -> Option<Function> {
108108
}
109109

110110
impl Function {
111-
pub(crate) fn argc(&self) -> Range<usize> {
111+
pub(crate) fn argc(&self) -> RangeInclusive<usize> {
112112
match *self {
113-
Nullary(_) => 0..0,
114-
Unary(_) => 1..1,
115-
UnaryOpt(_) => 1..2,
116-
UnaryPlus(_) => 1..usize::MAX,
117-
Binary(_) => 2..2,
118-
BinaryPlus(_) => 2..usize::MAX,
119-
Ternary(_) => 3..3,
113+
Nullary(_) => 0..=0,
114+
Unary(_) => 1..=1,
115+
UnaryOpt(_) => 1..=2,
116+
UnaryPlus(_) => 1..=usize::MAX,
117+
Binary(_) => 2..=2,
118+
BinaryPlus(_) => 2..=usize::MAX,
119+
Ternary(_) => 3..=3,
120120
}
121121
}
122122
}

src/parser.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,7 +2568,7 @@ mod tests {
25682568
kind: FunctionArgumentCountMismatch {
25692569
function: "arch",
25702570
found: 1,
2571-
expected: 0..0,
2571+
expected: 0..=0,
25722572
},
25732573
}
25742574

@@ -2582,7 +2582,7 @@ mod tests {
25822582
kind: FunctionArgumentCountMismatch {
25832583
function: "env_var",
25842584
found: 0,
2585-
expected: 1..1,
2585+
expected: 1..=1,
25862586
},
25872587
}
25882588

@@ -2596,7 +2596,7 @@ mod tests {
25962596
kind: FunctionArgumentCountMismatch {
25972597
function: "env",
25982598
found: 3,
2599-
expected: 1..2,
2599+
expected: 1..=2,
26002600
},
26012601
}
26022602

@@ -2610,7 +2610,7 @@ mod tests {
26102610
kind: FunctionArgumentCountMismatch {
26112611
function: "env",
26122612
found: 0,
2613-
expected: 1..2,
2613+
expected: 1..=2,
26142614
},
26152615
}
26162616

@@ -2624,7 +2624,7 @@ mod tests {
26242624
kind: FunctionArgumentCountMismatch {
26252625
function: "env_var_or_default",
26262626
found: 1,
2627-
expected: 2..2,
2627+
expected: 2..=2,
26282628
},
26292629
}
26302630

@@ -2638,7 +2638,7 @@ mod tests {
26382638
kind: FunctionArgumentCountMismatch {
26392639
function: "join",
26402640
found: 1,
2641-
expected: 2..usize::MAX,
2641+
expected: 2..=usize::MAX,
26422642
},
26432643
}
26442644

@@ -2652,7 +2652,7 @@ mod tests {
26522652
kind: FunctionArgumentCountMismatch {
26532653
function: "replace",
26542654
found: 1,
2655-
expected: 3..3,
2655+
expected: 3..=3,
26562656
},
26572657
}
26582658
}

src/range_ext.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@ pub(crate) trait RangeExt<T> {
1010

1111
pub(crate) struct DisplayRange<T>(T);
1212

13-
impl Display for DisplayRange<&Range<usize>> {
13+
impl Display for DisplayRange<&RangeInclusive<usize>> {
1414
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
15-
if self.0.start == self.0.end {
16-
write!(f, "0")?;
17-
} else if self.0.start == self.0.end - 1 {
18-
write!(f, "{}", self.0.start)?;
19-
} else if self.0.end == usize::MAX {
20-
write!(f, "{} or more", self.0.start)?;
15+
if self.0.start() == self.0.end() {
16+
write!(f, "{}", self.0.start())?;
17+
} else if *self.0.end() == usize::MAX {
18+
write!(f, "{} or more", self.0.start())?;
2119
} else {
22-
write!(f, "{} to {}", self.0.start, self.0.end - 1)?;
20+
write!(f, "{} to {}", self.0.start(), self.0.end())?;
2321
}
2422
Ok(())
2523
}
@@ -76,10 +74,10 @@ mod tests {
7674
assert!(!(1..1).contains(&1));
7775
assert!((1..1).is_empty());
7876
assert!((5..5).is_empty());
79-
assert_eq!((1..1).display().to_string(), "0");
80-
assert_eq!((1..2).display().to_string(), "1");
81-
assert_eq!((5..6).display().to_string(), "5");
82-
assert_eq!((5..10).display().to_string(), "5 to 9");
83-
assert_eq!((1..usize::MAX).display().to_string(), "1 or more");
77+
assert_eq!((0..=0).display().to_string(), "0");
78+
assert_eq!((1..=1).display().to_string(), "1");
79+
assert_eq!((5..=5).display().to_string(), "5");
80+
assert_eq!((5..=9).display().to_string(), "5 to 9");
81+
assert_eq!((1..=usize::MAX).display().to_string(), "1 or more");
8482
}
8583
}

tests/functions.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,3 +1050,21 @@ fn is_dependency() {
10501050
.stdout("beta false\ngamma true\n")
10511051
.run();
10521052
}
1053+
1054+
#[test]
1055+
fn unary_argument_count_mismamatch_error_message() {
1056+
Test::new()
1057+
.justfile("x := datetime()")
1058+
.args(["--evaluate"])
1059+
.stderr(
1060+
"
1061+
error: Function `datetime` called with 0 arguments but takes 1
1062+
——▶ justfile:1:6
1063+
1064+
1 │ x := datetime()
1065+
│ ^^^^^^^^
1066+
",
1067+
)
1068+
.status(EXIT_FAILURE)
1069+
.run();
1070+
}

0 commit comments

Comments
 (0)