@@ -1695,20 +1695,46 @@ static bool isRequirementOrWitness(const ConstraintLocatorBuilder &locator) {
1695
1695
locator.endsWith <LocatorPathElt::Witness>();
1696
1696
}
1697
1697
1698
- AnyFunctionType *ConstraintSystem::adjustFunctionTypeForConcurrency (
1699
- AnyFunctionType *fnType, ValueDecl *decl, DeclContext *dc,
1698
+ FunctionType *ConstraintSystem::adjustFunctionTypeForConcurrency (
1699
+ FunctionType *fnType, Type baseType , ValueDecl *decl, DeclContext *dc,
1700
1700
unsigned numApplies, bool isMainDispatchQueue, OpenedTypeMap &replacements,
1701
1701
ConstraintLocatorBuilder locator) {
1702
1702
1703
- return swift::adjustFunctionTypeForConcurrency (
1704
- fnType, decl, dc, numApplies, isMainDispatchQueue,
1705
- GetClosureType{*this }, ClosureIsolatedByPreconcurrency{*this },
1706
- [&](Type type) {
1703
+ auto *adjustedTy = swift::adjustFunctionTypeForConcurrency (
1704
+ fnType, decl, dc, numApplies, isMainDispatchQueue, GetClosureType{*this },
1705
+ ClosureIsolatedByPreconcurrency{*this }, [&](Type type) {
1707
1706
if (replacements.empty ())
1708
1707
return type;
1709
1708
1710
1709
return openType (type, replacements, locator);
1711
1710
});
1711
+
1712
+ if (Context.LangOpts .hasFeature (Feature::InferSendableFromCaptures)) {
1713
+ if (auto *FD = dyn_cast<AbstractFunctionDecl>(decl)) {
1714
+ auto *DC = FD->getDeclContext ();
1715
+ // All global functions should be @Sendable
1716
+ if (DC->isModuleScopeContext ()) {
1717
+ if (!adjustedTy->getExtInfo ().isSendable ()) {
1718
+ adjustedTy =
1719
+ adjustedTy->withExtInfo (adjustedTy->getExtInfo ().withSendable ());
1720
+ }
1721
+ } else if (isPartialApplication (getConstraintLocator (locator))) {
1722
+ if (baseType &&
1723
+ (baseType->is <AnyMetatypeType>() || baseType->isSendableType ())) {
1724
+ auto referenceTy = adjustedTy->getResult ()->castTo <FunctionType>();
1725
+ referenceTy =
1726
+ referenceTy->withExtInfo (referenceTy->getExtInfo ().withSendable ())
1727
+ ->getAs <FunctionType>();
1728
+
1729
+ adjustedTy =
1730
+ FunctionType::get (adjustedTy->getParams (), referenceTy,
1731
+ adjustedTy->getExtInfo ().withSendable ());
1732
+ }
1733
+ }
1734
+ }
1735
+ }
1736
+
1737
+ return adjustedTy->castTo <FunctionType>();
1712
1738
}
1713
1739
1714
1740
// / For every parameter in \p type that has an error type, replace that
@@ -1781,9 +1807,9 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
1781
1807
auto origOpenedType = openedType;
1782
1808
if (!isRequirementOrWitness (locator)) {
1783
1809
unsigned numApplies = getNumApplications (value, false , functionRefKind);
1784
- openedType = cast<FunctionType>( adjustFunctionTypeForConcurrency (
1785
- origOpenedType, func, useDC, numApplies, false , replacements ,
1786
- locator) );
1810
+ openedType = adjustFunctionTypeForConcurrency (
1811
+ origOpenedType, /* baseType= */ Type (), func, useDC, numApplies, false ,
1812
+ replacements, locator);
1787
1813
}
1788
1814
1789
1815
// The reference implicitly binds 'self'.
@@ -1799,14 +1825,6 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
1799
1825
auto numLabelsToRemove = getNumRemovedArgumentLabels (
1800
1826
funcDecl, /* isCurriedInstanceReference=*/ false , functionRefKind);
1801
1827
1802
- if (Context.LangOpts .hasFeature (Feature::InferSendableFromCaptures)) {
1803
- // All global functions should be @Sendable
1804
- if (funcDecl->getDeclContext ()->isModuleScopeContext ()) {
1805
- funcType =
1806
- funcType->withExtInfo (funcType->getExtInfo ().withSendable ());
1807
- }
1808
- }
1809
-
1810
1828
auto openedType = openFunctionType (funcType, locator, replacements,
1811
1829
funcDecl->getDeclContext ())
1812
1830
->removeArgumentLabels (numLabelsToRemove);
@@ -1818,9 +1836,9 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
1818
1836
if (!isRequirementOrWitness (locator)) {
1819
1837
unsigned numApplies = getNumApplications (
1820
1838
funcDecl, false , functionRefKind);
1821
- openedType = cast<FunctionType>( adjustFunctionTypeForConcurrency (
1822
- origOpenedType->castTo <FunctionType>(), funcDecl, useDC, numApplies ,
1823
- false , replacements, locator) );
1839
+ openedType = adjustFunctionTypeForConcurrency (
1840
+ origOpenedType->castTo <FunctionType>(), /* baseType= */ Type (), funcDecl ,
1841
+ useDC, numApplies, false , replacements, locator);
1824
1842
}
1825
1843
1826
1844
if (isForCodeCompletion () && openedType->hasError ()) {
@@ -2788,20 +2806,6 @@ ConstraintSystem::getTypeOfMemberReference(
2788
2806
// FIXME: Verify ExtInfo state is correct, not working by accident.
2789
2807
FunctionType::ExtInfo info;
2790
2808
2791
- if (Context.LangOpts .hasFeature (Feature::InferSendableFromCaptures)) {
2792
- if (isPartialApplication (locator) &&
2793
- (resolvedBaseTy->is <AnyMetatypeType>() ||
2794
- baseOpenedTy->isSendableType ())) {
2795
- // Add @Sendable to functions without conditional conformances
2796
- functionType =
2797
- functionType
2798
- ->withExtInfo (functionType->getExtInfo ().withSendable ())
2799
- ->getAs <FunctionType>();
2800
- }
2801
- // Unapplied values should always be Sendable
2802
- info = info.withSendable ();
2803
- }
2804
-
2805
2809
// We'll do other adjustment later, but we need to handle parameter
2806
2810
// isolation to avoid assertions.
2807
2811
if (fullFunctionType->getIsolation ().isParameter ())
@@ -2819,11 +2823,12 @@ ConstraintSystem::getTypeOfMemberReference(
2819
2823
unsigned numApplies = getNumApplications (
2820
2824
value, hasAppliedSelf, functionRefKind);
2821
2825
openedType = adjustFunctionTypeForConcurrency (
2822
- origOpenedType->castTo <AnyFunctionType >(), value, useDC, numApplies ,
2823
- isMainDispatchQueueMember (locator), replacements, locator);
2826
+ origOpenedType->castTo <FunctionType >(), resolvedBaseTy, value, useDC ,
2827
+ numApplies, isMainDispatchQueueMember (locator), replacements, locator);
2824
2828
} else if (auto subscript = dyn_cast<SubscriptDecl>(value)) {
2825
2829
openedType = adjustFunctionTypeForConcurrency (
2826
- origOpenedType->castTo <AnyFunctionType>(), subscript, useDC,
2830
+ origOpenedType->castTo <FunctionType>(), resolvedBaseTy, subscript,
2831
+ useDC,
2827
2832
/* numApplies=*/ 2 , /* isMainDispatchQueue=*/ false , replacements, locator);
2828
2833
} else if (auto var = dyn_cast<VarDecl>(value)) {
2829
2834
// Adjust the function's result type, since that's the Var's actual type.
@@ -2952,7 +2957,8 @@ Type ConstraintSystem::getEffectiveOverloadType(ConstraintLocator *locator,
2952
2957
// FIXME: Verify ExtInfo state is correct, not working by accident.
2953
2958
FunctionType::ExtInfo info;
2954
2959
type = adjustFunctionTypeForConcurrency (
2955
- FunctionType::get (indices, elementTy, info), subscript, useDC,
2960
+ FunctionType::get (indices, elementTy, info), overload.getBaseType (),
2961
+ subscript, useDC,
2956
2962
/* numApplies=*/ 1 , /* isMainDispatchQueue=*/ false , emptyReplacements,
2957
2963
locator);
2958
2964
} else if (auto var = dyn_cast<VarDecl>(decl)) {
@@ -3000,7 +3006,8 @@ Type ConstraintSystem::getEffectiveOverloadType(ConstraintLocator *locator,
3000
3006
decl, hasAppliedSelf, overload.getFunctionRefKind ());
3001
3007
3002
3008
type = adjustFunctionTypeForConcurrency (
3003
- type->castTo <FunctionType>(), decl, useDC, numApplies,
3009
+ type->castTo <FunctionType>(), overload.getBaseType (), decl,
3010
+ useDC, numApplies,
3004
3011
/* isMainDispatchQueue=*/ false , emptyReplacements, locator)
3005
3012
->getResult ();
3006
3013
}
0 commit comments