Skip to content

Commit 2d477d2

Browse files
committed
Sema: Don't create redundant type variables for multiple references to the same closure parameter type
1 parent c4dca65 commit 2d477d2

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -922,12 +922,17 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
922922
// constraint.
923923
if (auto *param = dyn_cast<ParamDecl>(varDecl)) {
924924
if (param->isLet() && valueType->is<TypeVariableType>()) {
925-
Type paramType = valueType;
926-
valueType = createTypeVariable(getConstraintLocator(locator),
925+
auto found = OpenedParameterTypes.find(param);
926+
if (found != OpenedParameterTypes.end())
927+
return { found->second, found->second };
928+
929+
auto typeVar = createTypeVariable(getConstraintLocator(locator),
927930
TVO_CanBindToLValue |
928931
TVO_CanBindToInOut);
929-
addConstraint(ConstraintKind::BindParam, paramType, valueType,
932+
addConstraint(ConstraintKind::BindParam, valueType, typeVar,
930933
getConstraintLocator(locator));
934+
OpenedParameterTypes.insert(std::make_pair(param, typeVar));
935+
return { typeVar, typeVar };
931936
}
932937
}
933938

lib/Sema/ConstraintSystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,10 @@ class ConstraintSystem {
959959
/// the types on the expression nodes.
960960
llvm::DenseMap<const Expr *, TypeBase *> ExprTypes;
961961

962+
/// Maps closure parameters to type variables.
963+
llvm::DenseMap<const ParamDecl *, TypeVariableType *>
964+
OpenedParameterTypes;
965+
962966
/// There can only be a single contextual type on the root of the expression
963967
/// being checked. If specified, this holds its type along with the base
964968
/// expression, and the purpose of it.

0 commit comments

Comments
 (0)