Skip to content

Commit 2558060

Browse files
committed
[CSGen] Restore var/let declaration type favoring
1 parent 16cbc00 commit 2558060

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

lib/Sema/CSGen.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,29 +1354,31 @@ namespace {
13541354

13551355
auto locator = CS.getConstraintLocator(E);
13561356

1357-
if (auto *param = dyn_cast<ParamDecl>(E->getDecl())) {
1357+
// If this is a 'var' or 'let' declaration with already
1358+
// resolved type, let's favor it.
1359+
if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1360+
Type type;
1361+
if (VD->hasInterfaceType()) {
1362+
type = VD->getInterfaceType();
1363+
if (type->hasTypeParameter())
1364+
type = VD->getDeclContext()->mapTypeIntoContext(type);
1365+
CS.setFavoredType(E, type.getPointer());
1366+
}
1367+
13581368
// This can only happen when failure diangostics is trying
13591369
// to type-check expressions inside of a single-statement
13601370
// closure which refer to anonymous parameters, in this case
13611371
// let's either use type as written or allocate a fresh type
13621372
// variable, just like we do for closure type.
1363-
if (!CS.hasType(param)) {
1364-
Type paramType;
1365-
if (param->hasInterfaceType()) {
1366-
paramType = param->getInterfaceType();
1367-
if (paramType->hasTypeParameter())
1368-
paramType =
1369-
param->getDeclContext()->mapTypeIntoContext(paramType);
1370-
1371-
if (paramType->hasUnboundGenericType())
1372-
paramType = CS.openUnboundGenericType(paramType, locator);
1373-
1374-
CS.setFavoredType(E, paramType.getPointer());
1375-
} else {
1376-
paramType = CS.createTypeVariable(locator, TVO_CanBindToLValue);
1373+
if (auto *PD = dyn_cast<ParamDecl>(VD)) {
1374+
if (!CS.hasType(PD)) {
1375+
if (type && type->hasUnboundGenericType())
1376+
type = CS.openUnboundGenericType(type, locator);
1377+
1378+
CS.setType(
1379+
PD, type ? type
1380+
: CS.createTypeVariable(locator, TVO_CanBindToLValue));
13771381
}
1378-
1379-
CS.setType(param, paramType);
13801382
}
13811383
}
13821384

0 commit comments

Comments
 (0)