@@ -1397,6 +1397,38 @@ AnyFunctionType *ConstraintSystem::adjustFunctionTypeForConcurrency(
1397
1397
fnType, decl, dc, numApplies, isMainDispatchQueue, GetClosureType{*this });
1398
1398
}
1399
1399
1400
+ // / For every parameter in \p type that has an error type, replace that
1401
+ // / parameter's type by a placeholder type, where \p value is the declaration
1402
+ // / that declared \p type. This is useful for code completion so we can match
1403
+ // / the types we do know instead of bailing out completely because \p type
1404
+ // / contains an error type.
1405
+ static Type replaceParamErrorTypeByPlaceholder (Type type, ValueDecl *value) {
1406
+ if (!type->is <AnyFunctionType>() || !isa<AbstractFunctionDecl>(value)) {
1407
+ return type;
1408
+ }
1409
+ auto funcType = type->castTo <AnyFunctionType>();
1410
+ auto funcDecl = cast<AbstractFunctionDecl>(value);
1411
+
1412
+ auto declParams = funcDecl->getParameters ();
1413
+ auto typeParams = funcType->getParams ();
1414
+ assert (declParams->size () == typeParams.size ());
1415
+ SmallVector<AnyFunctionType::Param, 4 > newParams;
1416
+ newParams.reserve (declParams->size ());
1417
+ for (auto i : indices (typeParams)) {
1418
+ AnyFunctionType::Param param = typeParams[i];
1419
+ if (param.getPlainType ()->is <ErrorType>()) {
1420
+ auto paramDecl = declParams->get (i);
1421
+ auto placeholder =
1422
+ PlaceholderType::get (paramDecl->getASTContext (), paramDecl);
1423
+ newParams.push_back (param.withType (placeholder));
1424
+ } else {
1425
+ newParams.push_back (param);
1426
+ }
1427
+ }
1428
+ assert (newParams.size () == declParams->size ());
1429
+ return FunctionType::get (newParams, funcType->getResult ());
1430
+ }
1431
+
1400
1432
std::pair<Type, Type>
1401
1433
ConstraintSystem::getTypeOfReference (ValueDecl *value,
1402
1434
FunctionRefKind functionRefKind,
@@ -1458,6 +1490,12 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
1458
1490
openedType->getAs <FunctionType>(),
1459
1491
locator);
1460
1492
1493
+ if (isForCodeCompletion () && openedType->hasError ()) {
1494
+ // In code completion, replace error types by placeholder types so we can
1495
+ // match the types we know instead of bailing out completely.
1496
+ openedType = replaceParamErrorTypeByPlaceholder (openedType, value);
1497
+ }
1498
+
1461
1499
// If we opened up any type variables, record the replacements.
1462
1500
recordOpenedTypes (locator, replacements);
1463
1501
@@ -1950,38 +1988,6 @@ Type constraints::typeEraseOpenedExistentialReference(
1950
1988
});
1951
1989
}
1952
1990
1953
- // / For every parameter in \p type that has an error type, replace that
1954
- // / parameter's type by a placeholder type, where \p value is the declaration
1955
- // / that declared \p type. This is useful for code completion so we can match
1956
- // / the types we do know instead of bailing out completely because \p type
1957
- // / contains an error type.
1958
- static Type replaceParamErrorTypeByPlaceholder (Type type, ValueDecl *value) {
1959
- if (!type->is <AnyFunctionType>() || !isa<AbstractFunctionDecl>(value)) {
1960
- return type;
1961
- }
1962
- auto funcType = type->castTo <AnyFunctionType>();
1963
- auto funcDecl = cast<AbstractFunctionDecl>(value);
1964
-
1965
- auto declParams = funcDecl->getParameters ();
1966
- auto typeParams = funcType->getParams ();
1967
- assert (declParams->size () == typeParams.size ());
1968
- SmallVector<AnyFunctionType::Param, 4 > newParams;
1969
- newParams.reserve (declParams->size ());
1970
- for (auto i : indices (typeParams)) {
1971
- AnyFunctionType::Param param = typeParams[i];
1972
- if (param.getPlainType ()->is <ErrorType>()) {
1973
- auto paramDecl = declParams->get (i);
1974
- auto placeholder =
1975
- PlaceholderType::get (paramDecl->getASTContext (), paramDecl);
1976
- newParams.push_back (param.withType (placeholder));
1977
- } else {
1978
- newParams.push_back (param);
1979
- }
1980
- }
1981
- assert (newParams.size () == declParams->size ());
1982
- return FunctionType::get (newParams, funcType->getResult ());
1983
- }
1984
-
1985
1991
std::pair<Type, Type>
1986
1992
ConstraintSystem::getTypeOfMemberReference (
1987
1993
Type baseTy, ValueDecl *value, DeclContext *useDC,
0 commit comments