Skip to content

Commit 0e4ef95

Browse files
committed
[ConstraintSystem] Add a locator to openType and some of its callers
`openType` didn't need a locator before it was simply replacing generic parameters with corresponding type variables but now, with opening of pack expansions types, a locator is needed for pack expansion variables.
1 parent 3b4be42 commit 0e4ef95

File tree

5 files changed

+58
-51
lines changed

5 files changed

+58
-51
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3978,7 +3978,8 @@ class ConstraintSystem {
39783978
/// corresponding opened type variables.
39793979
///
39803980
/// \returns The opened type, or \c type if there are no archetypes in it.
3981-
Type openType(Type type, OpenedTypeMap &replacements);
3981+
Type openType(Type type, OpenedTypeMap &replacements,
3982+
ConstraintLocatorBuilder locator);
39823983

39833984
private:
39843985
/// "Open" an opaque archetype type, similar to \c openType.
@@ -3989,7 +3990,8 @@ class ConstraintSystem {
39893990
/// opening its pattern and shape types and connecting them to the
39903991
/// aforementioned variable via special constraints.
39913992
Type openPackExpansionType(PackExpansionType *expansion,
3992-
OpenedTypeMap &replacements);
3993+
OpenedTypeMap &replacements,
3994+
ConstraintLocatorBuilder locator);
39933995

39943996
public:
39953997
/// Recurse over the given type and open any opaque archetype types.
@@ -4061,9 +4063,9 @@ class ConstraintSystem {
40614063
/// Wrapper over swift::adjustFunctionTypeForConcurrency that passes along
40624064
/// the appropriate closure-type and opening extraction functions.
40634065
AnyFunctionType *adjustFunctionTypeForConcurrency(
4064-
AnyFunctionType *fnType, ValueDecl *decl, DeclContext *dc,
4065-
unsigned numApplies, bool isMainDispatchQueue,
4066-
OpenedTypeMap &replacements);
4066+
AnyFunctionType *fnType, ValueDecl *decl, DeclContext *dc,
4067+
unsigned numApplies, bool isMainDispatchQueue,
4068+
OpenedTypeMap &replacements, ConstraintLocatorBuilder locator);
40674069

40684070
/// Retrieve the type of a reference to the given value declaration.
40694071
///

lib/Sema/CSRanking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ bool CompareDeclSpecializationRequest::evaluate(
473473
cs.openGeneric(outerDC, innerDC->getGenericSignatureOfContext(), locator,
474474
replacements);
475475

476-
return cs.openType(type, replacements);
476+
return cs.openType(type, replacements, locator);
477477
};
478478

479479
bool knownNonSubtype = false;

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11049,7 +11049,7 @@ static Type getOpenedResultBuilderTypeFor(ConstraintSystem &cs,
1104911049
auto substitutions = cs.getOpenedTypes(calleeLocator);
1105011050
if (!substitutions.empty()) {
1105111051
OpenedTypeMap replacements(substitutions.begin(), substitutions.end());
11052-
builderType = cs.openType(builderType, replacements);
11052+
builderType = cs.openType(builderType, replacements, locator);
1105311053
}
1105411054
assert(!builderType->hasTypeParameter());
1105511055
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ Type ConstraintSystem::openUnboundGenericType(GenericTypeDecl *decl,
837837

838838
return result.transform([&](Type type) {
839839
if (auto *expansion = dyn_cast<PackExpansionType>(type.getPointer()))
840-
return openPackExpansionType(expansion, replacements);
840+
return openPackExpansionType(expansion, replacements, locator);
841841
return type;
842842
});
843843
}
@@ -963,7 +963,8 @@ Type ConstraintSystem::replaceInferableTypesWithTypeVars(
963963
return type;
964964
}
965965

966-
Type ConstraintSystem::openType(Type type, OpenedTypeMap &replacements) {
966+
Type ConstraintSystem::openType(Type type, OpenedTypeMap &replacements,
967+
ConstraintLocatorBuilder locator) {
967968
assert(!type->hasUnboundGenericType());
968969

969970
if (!type->hasTypeParameter())
@@ -980,14 +981,15 @@ Type ConstraintSystem::openType(Type type, OpenedTypeMap &replacements) {
980981
if (!elt.hasName() && elt.getType()->is<PackExpansionType>()) {
981982
return TupleType::get(
982983
{openPackExpansionType(
983-
elt.getType()->castTo<PackExpansionType>(), replacements)},
984+
elt.getType()->castTo<PackExpansionType>(), replacements,
985+
locator)},
984986
tuple->getASTContext());
985987
}
986988
}
987989
}
988990

989991
if (auto *expansion = type->getAs<PackExpansionType>()) {
990-
return openPackExpansionType(expansion, replacements);
992+
return openPackExpansionType(expansion, replacements, locator);
991993
}
992994

993995
// Replace a generic type parameter with its corresponding type variable.
@@ -1007,28 +1009,29 @@ Type ConstraintSystem::openType(Type type, OpenedTypeMap &replacements) {
10071009
}
10081010

10091011
Type ConstraintSystem::openPackExpansionType(PackExpansionType *expansion,
1010-
OpenedTypeMap &replacements) {
1011-
auto patternType = openType(expansion->getPatternType(), replacements);
1012-
auto shapeType = openType(expansion->getCountType(), replacements);
1012+
OpenedTypeMap &replacements,
1013+
ConstraintLocatorBuilder locator) {
1014+
auto patternType =
1015+
openType(expansion->getPatternType(), replacements, locator);
1016+
auto shapeType = openType(expansion->getCountType(), replacements, locator);
10131017

10141018
auto openedPackExpansion = PackExpansionType::get(patternType, shapeType);
10151019

10161020
auto known = OpenedPackExpansionTypes.find(openedPackExpansion);
10171021
if (known != OpenedPackExpansionTypes.end())
10181022
return known->second;
10191023

1020-
auto *expansionVar = createTypeVariable(
1021-
getConstraintLocator(
1022-
{}, LocatorPathElt::PackExpansionType(openedPackExpansion)),
1023-
TVO_PackExpansion);
1024+
auto *expansionLoc = getConstraintLocator(locator.withPathElement(
1025+
LocatorPathElt::PackExpansionType(openedPackExpansion)));
1026+
1027+
auto *expansionVar = createTypeVariable(expansionLoc, TVO_PackExpansion);
10241028

10251029
// This constraint is important to make sure that pack expansion always
10261030
// has a binding and connect pack expansion var to any type variables
10271031
// that appear in pattern and shape types.
1028-
addUnsolvedConstraint(Constraint::create(
1029-
*this, ConstraintKind::Defaultable, expansionVar, openedPackExpansion,
1030-
getConstraintLocator(
1031-
{}, LocatorPathElt::PackExpansionType(openedPackExpansion))));
1032+
addUnsolvedConstraint(Constraint::create(*this, ConstraintKind::Defaultable,
1033+
expansionVar, openedPackExpansion,
1034+
expansionLoc));
10321035

10331036
OpenedPackExpansionTypes[openedPackExpansion] = expansionVar;
10341037
return expansionVar;
@@ -1066,7 +1069,7 @@ Type ConstraintSystem::openOpaqueType(OpaqueTypeArchetypeType *opaque,
10661069

10671070
recordOpenedTypes(opaqueLocatorKey, replacements);
10681071

1069-
return openType(opaque->getInterfaceType(), replacements);
1072+
return openType(opaque->getInterfaceType(), replacements, locator);
10701073
}
10711074

10721075
Type ConstraintSystem::openOpaqueType(Type type, ContextualTypePurpose context,
@@ -1113,12 +1116,14 @@ FunctionType *ConstraintSystem::openFunctionType(
11131116

11141117
openGenericParameters(outerDC, signature, replacements, locator);
11151118

1116-
openGenericRequirements(
1117-
outerDC, signature, /*skipProtocolSelfConstraint=*/false, locator,
1118-
[&](Type type) -> Type { return openType(type, replacements); });
1119+
openGenericRequirements(outerDC, signature,
1120+
/*skipProtocolSelfConstraint=*/false, locator,
1121+
[&](Type type) -> Type {
1122+
return openType(type, replacements, locator);
1123+
});
11191124

11201125
funcType = genericFn->substGenericArgs(
1121-
[&](Type type) { return openType(type, replacements); });
1126+
[&](Type type) { return openType(type, replacements, locator); });
11221127
}
11231128

11241129
return funcType->castTo<FunctionType>();
@@ -1558,16 +1563,16 @@ static bool isRequirementOrWitness(const ConstraintLocatorBuilder &locator) {
15581563

15591564
AnyFunctionType *ConstraintSystem::adjustFunctionTypeForConcurrency(
15601565
AnyFunctionType *fnType, ValueDecl *decl, DeclContext *dc,
1561-
unsigned numApplies, bool isMainDispatchQueue,
1562-
OpenedTypeMap &replacements) {
1566+
unsigned numApplies, bool isMainDispatchQueue, OpenedTypeMap &replacements,
1567+
ConstraintLocatorBuilder locator) {
15631568
return swift::adjustFunctionTypeForConcurrency(
15641569
fnType, decl, dc, numApplies, isMainDispatchQueue,
15651570
GetClosureType{*this}, ClosureIsolatedByPreconcurrency{*this},
15661571
[&](Type type) {
15671572
if (replacements.empty())
15681573
return type;
15691574

1570-
return openType(type, replacements);
1575+
return openType(type, replacements, locator);
15711576
});
15721577
}
15731578

@@ -1642,7 +1647,8 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
16421647
if (!isRequirementOrWitness(locator)) {
16431648
unsigned numApplies = getNumApplications(value, false, functionRefKind);
16441649
openedType = cast<FunctionType>(adjustFunctionTypeForConcurrency(
1645-
origOpenedType, func, useDC, numApplies, false, replacements));
1650+
origOpenedType, func, useDC, numApplies, false, replacements,
1651+
locator));
16461652
}
16471653

16481654
// The reference implicitly binds 'self'.
@@ -1668,8 +1674,8 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
16681674
unsigned numApplies = getNumApplications(
16691675
funcDecl, false, functionRefKind);
16701676
openedType = cast<FunctionType>(adjustFunctionTypeForConcurrency(
1671-
origOpenedType->castTo<FunctionType>(), funcDecl, useDC,
1672-
numApplies, false, replacements));
1677+
origOpenedType->castTo<FunctionType>(), funcDecl, useDC, numApplies,
1678+
false, replacements, locator));
16731679
}
16741680

16751681
if (isForCodeCompletion() && openedType->hasError()) {
@@ -1834,7 +1840,7 @@ void ConstraintSystem::openGeneric(
18341840
// Add the requirements as constraints.
18351841
openGenericRequirements(
18361842
outerDC, sig, /*skipProtocolSelfConstraint=*/false, locator,
1837-
[&](Type type) { return openType(type, replacements); });
1843+
[&](Type type) { return openType(type, replacements, locator); });
18381844
}
18391845

18401846
void ConstraintSystem::openGenericParameters(DeclContext *outerDC,
@@ -2431,9 +2437,8 @@ ConstraintSystem::getTypeOfMemberReference(
24312437
openedType = value->getInterfaceType()->castTo<AnyFunctionType>();
24322438

24332439
if (auto *genericFn = openedType->getAs<GenericFunctionType>()) {
2434-
openedType = genericFn->substGenericArgs([&](Type type) {
2435-
return openType(type, replacements);
2436-
});
2440+
openedType = genericFn->substGenericArgs(
2441+
[&](Type type) { return openType(type, replacements, locator); });
24372442
}
24382443
} else {
24392444
// For a property, build a type (Self) -> PropType.
@@ -2477,8 +2482,8 @@ ConstraintSystem::getTypeOfMemberReference(
24772482

24782483
// If the storage is generic, open the self and ref types.
24792484
if (genericSig) {
2480-
selfTy = openType(selfTy, replacements);
2481-
refType = openType(refType, replacements);
2485+
selfTy = openType(selfTy, replacements, locator);
2486+
refType = openType(refType, replacements, locator);
24822487
}
24832488
FunctionType::Param selfParam(selfTy, Identifier(), selfFlags);
24842489

@@ -2506,7 +2511,7 @@ ConstraintSystem::getTypeOfMemberReference(
25062511
getConcreteReplacementForProtocolSelfType(value)) {
25072512
// Concrete type replacing `Self` could be generic, so we need
25082513
// to make sure that it's opened before use.
2509-
baseOpenedTy = openType(concreteSelf, replacements);
2514+
baseOpenedTy = openType(concreteSelf, replacements, locator);
25102515
baseObjTy = baseOpenedTy;
25112516
}
25122517
}
@@ -2544,7 +2549,7 @@ ConstraintSystem::getTypeOfMemberReference(
25442549
openGenericRequirements(
25452550
outerDC, genericSig,
25462551
/*skipProtocolSelfConstraint=*/true, locator,
2547-
[&](Type type) { return openType(type, replacements); });
2552+
[&](Type type) { return openType(type, replacements, locator); });
25482553
}
25492554

25502555
if (auto *funcDecl = dyn_cast<AbstractFunctionDecl>(value)) {
@@ -2569,11 +2574,11 @@ ConstraintSystem::getTypeOfMemberReference(
25692574
value, hasAppliedSelf, functionRefKind);
25702575
openedType = adjustFunctionTypeForConcurrency(
25712576
origOpenedType->castTo<AnyFunctionType>(), value, useDC, numApplies,
2572-
isMainDispatchQueueMember(locator), replacements);
2577+
isMainDispatchQueueMember(locator), replacements, locator);
25732578
} else if (auto subscript = dyn_cast<SubscriptDecl>(value)) {
25742579
openedType = adjustFunctionTypeForConcurrency(
25752580
origOpenedType->castTo<AnyFunctionType>(), subscript, useDC,
2576-
/*numApplies=*/2, /*isMainDispatchQueue=*/false, replacements);
2581+
/*numApplies=*/2, /*isMainDispatchQueue=*/false, replacements, locator);
25772582
} else if (auto var = dyn_cast<VarDecl>(value)) {
25782583
// Adjust the function's result type, since that's the Var's actual type.
25792584
auto origFnType = origOpenedType->castTo<AnyFunctionType>();
@@ -2700,9 +2705,9 @@ Type ConstraintSystem::getEffectiveOverloadType(ConstraintLocator *locator,
27002705
// FIXME: Verify ExtInfo state is correct, not working by accident.
27012706
FunctionType::ExtInfo info;
27022707
type = adjustFunctionTypeForConcurrency(
2703-
FunctionType::get(indices, elementTy, info),
2704-
subscript, useDC, /*numApplies=*/1, /*isMainDispatchQueue=*/false,
2705-
emptyReplacements);
2708+
FunctionType::get(indices, elementTy, info), subscript, useDC,
2709+
/*numApplies=*/1, /*isMainDispatchQueue=*/false, emptyReplacements,
2710+
locator);
27062711
} else if (auto var = dyn_cast<VarDecl>(decl)) {
27072712
type = var->getValueInterfaceType();
27082713
if (doesStorageProduceLValue(var, overload.getBaseType(), useDC)) {
@@ -2748,10 +2753,9 @@ Type ConstraintSystem::getEffectiveOverloadType(ConstraintLocator *locator,
27482753
decl, hasAppliedSelf, overload.getFunctionRefKind());
27492754

27502755
type = adjustFunctionTypeForConcurrency(
2751-
type->castTo<FunctionType>(),
2752-
decl, useDC, numApplies, /*isMainDispatchQueue=*/false,
2753-
emptyReplacements)
2754-
->getResult();
2756+
type->castTo<FunctionType>(), decl, useDC, numApplies,
2757+
/*isMainDispatchQueue=*/false, emptyReplacements, locator)
2758+
->getResult();
27552759
}
27562760
}
27572761

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,8 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
658658
cs.openGenericRequirement(DC->getParent(), index, requirement,
659659
/*skipSelfProtocolConstraint=*/false, locator,
660660
[&](Type type) -> Type {
661-
return cs.openType(type, genericParameters);
661+
return cs.openType(type, genericParameters,
662+
locator);
662663
});
663664
};
664665

0 commit comments

Comments
 (0)