swiftlang/swift

[SR-10506] Formalize getType closure pattern

Open

#52,906 opened on Apr 18, 2019

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Swift (69,989 stars) (10,719 forks)batch import
compilergood first issuetype checker

Description

Previous ID SR-10506
Radar None
Original Reporter @beccadax
Type Task
Votes 0
Component/s Compiler
Labels Task, StarterBug, TypeChecker
Assignee None
Priority Medium

md5: dd815550dade98eb89026bfbc9fc1d2f

Issue Description:

Various methods in the compiler from swiftAST up take one or more closure parameters which are used to override how the types of AST nodes (mainly expressions) are accessed. In practice, these are mainly used to make a function look up types in a ConstraintSystem during type checking instead of using the values in the nodes themselves. A non-exhaustive list of examples:

  • AbstractClosureExpr::getResultType() and CallExpr::create() take a "getType" closure.

  • TypeExpr::getInstanceType() takes two closures for "hasType" and "getType".

  • LiteralExpr::shallowClone() takes two closures for "getType" and "setType".

  • Expr::dump() takes three closures for "getType", "getTypeOfTypeLoc", and "getTypeOfKeyPathComponent". These are plumbed through to various internal types in ASTDumper.cpp.

Rather than continuing to pass around ad-hoc closures, we should formalize this interface into a class that we can pass around. For example, we might declare an "ExprTypeMap" class in AST which declares all of the hasType()/getType()/setType() methods from ConstraintSystem and simply calls through to the equivalent methods on the AST nodes; then we can have ConstraintSystem inherit from this class and override these methods.

Contributor guide