New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dereferencing array[0] should be a tokenizer or parser error #4862
Comments
It used to be:
This was changed as a result of #4151 (resolving #826), so I think it's fair to say that it is deliberate. Whether |
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[0] hello
set: Array index out of bounds
mqudsi@ZBook /m/c/U/Mahmoud> echo $array[0]
mqudsi@ZBook /m/c/U/Mahmoud> set array[1] hello
mqudsi@ZBook /m/c/U/Mahmoud> echo $array[1]
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[0] 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 |
Mostly resolves fish-shell#4862, though there remains the lingering question of whether or not to emit a warning to /dev/tty or stderr when a non-literal-zero index evaluates to zero.
The control flow in expand.cpp is a bit more complicated than it seemed at first blush. Ref #4862.
It would be cute if, when interactive, |
Mostly resolves fish-shell#4862, though there remains the lingering question of whether or not to emit a warning to /dev/tty or stderr when a non-literal-zero index evaluates to zero.
The control flow in expand.cpp is a bit more complicated than it seemed at first blush. Ref fish-shell#4862.
I've asked about this in the chat but haven't heard anything regarding if it's ever valid to access
array[0]
. Presuming the answer is no:Since fish array indices start at
1
(unlike many other programming/scripting languages), I'm probably not the only one that forgets and starts at0
from time to time. If[0]
has no special meaning, it should be a hard error (instead of returning an empty result) so that it is clear there is something wrong with the script.The text was updated successfully, but these errors were encountered: