Dereferencing array should be a tokenizer or parser error #4862
I've asked about this in the chat but haven't heard anything regarding if it's ever valid to access
Since fish array indices start at
The text was updated successfully, but these errors were encountered:
It used to be:
Thanks for the links, interesting reads. I agree that if fish has no problem with undefined variables then it shouldn't have a problem with out-of-bound array indices. But I think
mqudsi@ZBook /m/c/U/Mahmoud> fish --version fish, version 2.7.1-1016-g04b8b35a mqudsi@ZBook /m/c/U/Mahmoud> set array hello set: Array index out of bounds mqudsi@ZBook /m/c/U/Mahmoud> echo $array mqudsi@ZBook /m/c/U/Mahmoud> set array hello mqudsi@ZBook /m/c/U/Mahmoud> echo $array hello mqudsi@ZBook /m/c/U/Mahmoud>
I guess the array-out-of-bounds code still lives on here ;)
Sure. But it's not just used literally, you can also use variables or command substitutions as index -
If you make it error out, people will have to add 0-checks everywhere.
Obviously, if the default empty value isn't correct for your code, then the error will show an actual bug.
Yeah, that should be consistent.
If we do add an error for 0, then it should either be "Invalid index value" (like it already is for
So when you say "add an error" do you mean "write to stdout and continue" or do you mean "write to stdout and abort"?
I am suggesting the former (with a special case for literal array that would be a tokenizer error rather than a parser error, complete with the "display error message then bring up the old contents of the line so the user can correct their mistake when at the tty").
Sooo.... we've had some more experiences with signalling errors, and I feel like we should have some more discussion on that in general, and if possible come up with a workable policy.
In the past few cycles, we've done a few changes to tighten errors, and we've done some to loosen them.
We've tightened errors related to
We've loosened errors related to array indices -
This is somewhere between the two.
If anything, stderr.
Though that kind of shows the difference between the
Couple that with something like
echo $somevar[(math "someexpression")]
Do you really want an unsilencable error here just because "someexpression" happened to print 0? That way you just get guard expressions, like
set -l var set -l result (math "someexpression" 2>/dev/null) test "$result" -ne 0 2>/dev/null and set var $somevar[$result] echo $var
Which is just full of footguns - the quoting, stderr silencing, ...
This could be simplified if
That would obviously work -
I think you're missing the more important point here, in that if you are referencing array index 0, your code already has a bug and won't work, except it will do so silently, making your job so much harder.
There's a difference between the recently discussed:
set -l foo bar set -el foo set -el foo
Where the second
But if the user is using the result of a math operation to index an array, then the user relies on that value. In particular, the user believes
I can't think of a case where someone is (even indirectly) referencing
The control flow in expand.cpp is a bit more complicated than it seemed at first blush. Ref #4862.
The control flow in expand.cpp is a bit more complicated than it seemed at first blush. Ref fish-shell#4862.