Fix list.pop() for negative arguments#2456
Conversation
|
I just went over post 1 of this GSoC blog. The work done in the project is commendable. I will try to make some contributions if I see any possible improvements. Thanks! |
|
Been a few days since I pushed the code. Does it look good to you ? @certik |
| @@ -4659,10 +4659,26 @@ namespace LCompilers { | |||
|
|
|||
There was a problem hiding this comment.
Can you please update the equivalent C++ code above, to make it clear how the logic works?
There was a problem hiding this comment.
Sure, if we are going ahead with the change I will update the logic as well.
For understanding the logic is below -
if(pos<0){
if(abs(pos)<= end_point){
adjusted_pos = endpoint + pos
}
}
else{
adjusted_pos = pos
}
if(adjusted_pos <0 || adjusted_pos>=end_point) return IndexError
|
For arrays we do not support negative indices (if I am not mistaken), due to runtime overhead that cannot be removed even in Release mode. This PR I think introduces a similar runtime slowdown that cannot be optimized out, correct? If so, the question to discuss is if we want to introduce such a slowdown in exchange to support negative indices, or should we rather disallow negative indices in exchange for faster runtime. |
As far as I remember, negative indexing works and gives correct results with lists -
I came across this issue and felt the need for it because if it is not discarded and returning answers, the output might as well be correct (over incorrect). Stating @czgdp1807 statement from this comment here - 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, |
|
I think compile time negative indexing can be implemented, but runtime cannot without an overhead. So we can implement compile time, but not runtime. |
I get your point. So should we try to give the correct solution in compile time but give the above mentioned error in runtime cases ? |
|
Yes, runtime should give an error in Debug mode (but not Release mode) for the -1 index. |
|
What is the status of this PR? |
|
Please mark this PR ready for review once it is ready. |
|
Hi Thirumalai, unfortunately I do not recall what was wrong in this PR. The latest comments by certik mention -
Can you help me to figure out what exactly I need to do here? The use case here is correct, so let's try to get this merged! |
|
Hi @Shaikh-Ubaid, |
|
I think Ondrej means the following:
Since we would be checking for negative index in debug mode, it would add a computation overhead. We avoid/tackle this computation overhead by not checking for negative index in release build. |
|
Yes, that's exactly what I would do. |
Snippet
On master -
On branch -