Skip to content
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

Sort blocks by line number of the first opcode #1786

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

copybara-service[bot]
Copy link

Sort blocks by line number of the first opcode

Blocks are sorted by number of predecessors. Previously the index of the first opcode, i.e. the order in the bytecode, served as a tie breaker.

In Python 3.12 the added test case fails with:

dummy_input_file:10:3: error: in <module>: None [assert-type]
  Expected: Optional[str]
    Actual: None

  assert_type(err, str|None)
  ~~~~~~~~~~~~~~~~~~~~~~~~~~

Python 3.12 emits the bytecode for the except block last for some reason.

Because of the while loop, every block is a predecessor of every other block, and the predecessor-based sorting fails. It falls back to the tie breaker and pytype executes the except block last, i.e. after assert_type. By the time pytype executes assert_type it don't know about the possible str type.

This change uses the line number of the first opcode as a tie breaker first and then falls back to the index.

Blocks are sorted by number of predecessors. Previously the index of the first opcode, i.e. the order in the bytecode, served as a tie breaker.

In Python 3.12 the added test case fails with:

    dummy_input_file:10:3: error: in <module>: None [assert-type]
      Expected: Optional[str]
        Actual: None

      assert_type(err, str|None)
      ~~~~~~~~~~~~~~~~~~~~~~~~~~

Python 3.12 emits the bytecode for the `except` block last for some reason.

Because of the while loop, every block is a predecessor of every other block, and the predecessor-based sorting fails. It falls back to the tie breaker and pytype executes the `except` block last, i.e. after `assert_type`. By the time pytype executes `assert_type` it don't know about the possible `str` type.

This change uses the line number of the first opcode as a tie breaker first and then falls back to the index.

PiperOrigin-RevId: 679166584
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.

1 participant