-
Notifications
You must be signed in to change notification settings - Fork 170
Negative variable indexing do not work for lists #1533
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
Comments
I am not fully sure how we could approach this . Indexing for strings are pretty similar
But here negative indexing through a variable works because of the
So is this the correct way (implementing something similar to what has been done for strings) to go about this issue ? |
We discussed this here: #1099 (comment) |
Even case two - index value at runtime time isn't completely correct on my master
@anutosh491 Can you check. If this is valid you can include this in your future pr ! |
This looks like a good thread to figure this issue out . I'll go through it ! |
We discussed this in #1099. The final design decision was made in #1099 (review). I guess if we are allowing constant negative indices then we should also allow variable negative indices. Here's how I would re-take the decision,
For now allowing only constant negative indices creates confusion and is non-intuitive. cc: @certik |
I don't know if we should have a different semantics for My reasoning for tuples was that we can't index tuples at runtime anyway, because the type of the result depends on the index, so this is a fundamentally compile time operation, whether positive or negative. And it is done quickly, no runtime overhead. For lists on the other hand, I think we should not allow negative indexing, since it cannot be done at runtime quickly. |
Yeah I do agree with the points raised here.
Would it make sense to comment out, how the constant negative indexing case is being solved and raise a |
Assuming we are fine that indexing into tuples is only allowed for compile time indices (and forbidden for runtime indices), then why would it create confusion to take this concept further and allow compile time indices (but not runtime indices) for more data structures than just tuples? |
That makes sense. However runtime indexing (both positive and negative) for tuples is theoretically not possible (as discussed in #1534 (comment)). So tuples allow only compile time indices irrespective of whether the index is positive or negative. The source of confusion in lists is that we are allowing runtime indices for positive values but not for negative values. On top of that compile time indices of any sign are allowed in lists. For compile time case one can use both positive and negative indexing in lists. So that will intuitively make one think that we can also do the same with runtime indices but then we face the out of bounds error. Even if we are okay with current status in main, but I think users should receive a detailed error message when they use runtime negative indices in lists, Something like, |
What works
What doesn't work
So basically what's happening is , it follows the expected code flow and through the code block responsible for handling negative indexing . But here the
val
comes out to be a null pointer when indexing is done through a variable .lpython/src/lpython/semantics/python_ast_to_asr.cpp
Lines 3109 to 3114 in 8f62ac4
The positive index remains unaffected though (it skips this block ) and still works out well through the code which follows
The text was updated successfully, but these errors were encountered: