KEMBAR78
[mypyc] Fixing index variable in for-loop with builtins.enumerate. by advait-dixit · Pull Request #18202 · python/mypy · GitHub
Skip to content

Conversation

advait-dixit
Copy link
Contributor

Fixes mypyc/mypyc#1046

This change fixes two problems:

  1. The index variable was getting instantiated even while enumerating an empty iterable.
  2. After exiting the for-loop, the value of the index variable is off by 1 (see issue linked above).

This change fixes both problems by assigning the temporary register to the index variable at the beginning of the for-loop body. Before this change, this assignment was happening before the for-loop and at the end of the for-loop body.

@advait-dixit advait-dixit marked this pull request as ready for review November 27, 2024 21:56
Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, that's a bad correctness bug!

@hauntsaninja hauntsaninja merged commit d39eacc into python:master Nov 27, 2024
13 checks passed
@advait-dixit advait-dixit deleted the fix_enumerate_loop branch November 27, 2024 23:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Off by one error using enumerate

2 participants