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

Finish implementing support for array shapes in type checker #1382

TysonAndre opened this issue Jan 22, 2018 · 0 comments

Finish implementing support for array shapes in type checker #1382

TysonAndre opened this issue Jan 22, 2018 · 0 comments


Copy link

@TysonAndre TysonAndre commented Jan 22, 2018

related to #1343

class ArrayShapeType exists for array{field:T}, but I still need to do the following:

  • Stop immediately casting it to a GenericArrayType (T[])

  • When parsing union types from AST nodes, generate ArrayShapeType instead of GenericArrayType based on a heuristic. (heuristic TBD)

    E.g. if one of the key values can't be determined, then convert to GenericArrayType.

  • Add rule to check if an ArrayShapeType can cast to another ArrayShapeType, add unit tests

  • Add a rule to check if a GenericArrayType can cast to an ArrayShapeType, and vice versa

  • When normalizing union types, make sure there is at most one ArrayShapeType.
    (Haven't created a specification of what to do when there are both GenericArrayType and ArrayShapeType. One possibility is to use the GenericArrayType when keys don't match)

  • Merge two ArrayShapeType into a single ArrayShapeType when there are two branches

  • Figure out and implement a heuristic for normalizing types when recursively analyzing function calls for when quick_mode is off (one heuristic is to always cast to GenericArrayType when recursing to analyze).

  • In an assignment, If the key value is known, AssignmentVisitor should update GenericArrayType with the new key value

  • (Optional) support unset (Doesn't catch that many bugs)

  • ArrayReturnType plugin will need to be changed to properly handle array shapes.

  • UnionTypeVisitor will need to check the values of keys during array accesses. If a GenericArrayType is part of the union type, and contains that key, then the corresponding field type from the shape should be returned.

Follow-up tasks:

  • Indicate fields are optional in a shape. Modify the type checker and merging logic. Warn about fetching an optional field.

Other followup tasks:

  • Add more unit tests
  • Improve scalar casting rules. E.g. array{a:int} should not be able to cast to array{a:int, other:int}.
    A followup PR may add optional fields to array shapes
  • Test this on large projects for crashes and unexpected results
  • Reduce false positives in loops (Another PR provided ways to suppress issues for an entire file (phan-file-suppress) or to set a variable's type inline (phan-var in top level string literal))
  • Look into inferring [] as array{} in future PRs. This was disabled due to high false positives in loops
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant