c3c icon indicating copy to clipboard operation
c3c copied to clipboard

No boundary error when indexing a slice starting at its own length

Open NotsoanoNimus opened this issue 5 months ago • 6 comments

Assigning from a slice starting at its current length returns an empty slice and does not cause a boundary error:

fn void main()
{
    char[] c = "abcdefgh1111111111111111111111111111111111111111";
    io::printf("%s\n", c.len);
    char[] data = c[48..];
    io::printf("%s, %s\n", data, data=={});
}
Launching ./slice_test
48
[], true
Program completed with exit code 0.

NotsoanoNimus avatar Jul 11 '25 14:07 NotsoanoNimus

Should it cause an error?

lerno avatar Aug 25 '25 10:08 lerno

I was of the opinion that it should not be an error, and that the 0-length slice is fine. I will attach our last conversation about this.

Image Image

NotsoanoNimus avatar Aug 25 '25 12:08 NotsoanoNimus

I remember, I was investigating this and found that it was more or less deliberate and it's desirable to fix corner cases (and this possibly means that other .. should work as well). So maybe check that other variants work as well.

lerno avatar Aug 26 '25 12:08 lerno

For example c[0..-1] should probably work.

lerno avatar Aug 26 '25 12:08 lerno

I've relaxed the rules so that all of these are now allowed by default.

lerno avatar Oct 09 '25 22:10 lerno

Please test it.

lerno avatar Oct 10 '25 10:10 lerno