1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-26 00:10:27 +00:00

error if '& is followed by 2+ items in destructure

The current destructure pattern ends when '& is encountered.
This commit adds an error if it is followed by more than
a symbol to bind the array to.

Although its not critical since the extra items can be ignored,
they're a sign of some kind of mistake so its best to complain.
This commit is contained in:
Ian Shehadeh 2022-01-20 08:52:37 -05:00
parent 62608bec03
commit 4118d581af

View File

@ -161,6 +161,20 @@ static int destructure(JanetCompiler *c,
return 1;
}
if (i + 2 < len) {
int32_t num_extra = len - i - 1;
Janet* extra = janet_tuple_begin(num_extra);
janet_tuple_flag(extra) |= JANET_TUPLE_FLAG_BRACKETCTOR;
for (int32_t j = 0; j < num_extra; ++j) {
extra[j] = values[j + i + 1];
}
janetc_error(c, janet_formatc("expected a single symbol follow '& in destructuring pattern, found %q", janet_wrap_tuple(janet_tuple_end(extra))));
return 1;
}
if (!janet_checktype(values[i + 1], JANET_SYMBOL)) {
janetc_error(c, janet_formatc("expected symbol following '& in destructuring pattern, found %q", values[i + 1]));
return 1;