Skip to content

Introduce data-flow analysis#200

Merged
withered-magic merged 9 commits intomainfrom
codeflow
Jul 12, 2024
Merged

Introduce data-flow analysis#200
withered-magic merged 9 commits intomainfrom
codeflow

Conversation

@withered-magic
Copy link
Owner

@withered-magic withered-magic commented Apr 15, 2024

Currently, the type inference mechanism is very simple in that in infers types for a variable based only on the last seen assignment to the variable. This has shortcomings for scenarios like the following, where the type may differ based on the branch taken.

x = ""

if y == 0:
    x = True

x # inferred as `Literal[True]`, should be `Literal[""] | True`

To make the type checker more accurate in these situations, this PR introduces a primitive form of data-flow analysis heavily based on Pyright and, by extension, the TypeScript compiler. This includes constructing a code-flow graph as an additional lowering step and using it to determine types as part of the type checker. While under active development, this functionality will be gated behind the --experimental_use_code_flow_analysis flag.

Tasks:

  • Support constructing a code-flow graph from the current HIR
    • if statements
    • for loops
    • List/dict comprehensions
    • Assignments for params (will do in follow-up)
  • Use code-flow graph as part of type inference
    • if statements
    • for loops
  • Caching mechanism data-flow analysis results
    • Effective type results (will do in follow-up)
    • Type of reference at specific flow nodes
  • Add --experimental_use_code_flow_analysis flag

@withered-magic withered-magic marked this pull request as draft April 15, 2024 08:50
@withered-magic withered-magic changed the title Introduce code-flow analysis Introduce data-flow analysis Apr 15, 2024
@withered-magic withered-magic force-pushed the codeflow branch 2 times, most recently from 705f89e to d944c46 Compare April 22, 2024 03:59
@withered-magic withered-magic force-pushed the codeflow branch 3 times, most recently from c643895 to aca46d5 Compare May 13, 2024 01:44
@zakora31

This comment was marked as spam.

wip

more wip

wip

wip

more wip

cont

cleanup

fix tests

fix reassignments to functions, load items, etc.

wip

wip comps

cont

support list/dict comps

add if stmt tests

handle for stmt
@withered-magic withered-magic marked this pull request as ready for review July 12, 2024 10:48
@withered-magic withered-magic merged commit 6ca62eb into main Jul 12, 2024
@withered-magic withered-magic deleted the codeflow branch July 12, 2024 10:51
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.

2 participants