Skip to content

Commit 426c7a3

Browse files
committed
Sema: Collect constraints in PreparedOverload
1 parent 5437935 commit 426c7a3

File tree

6 files changed

+109
-57
lines changed

6 files changed

+109
-57
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3695,12 +3695,14 @@ class ConstraintSystem {
36953695
/// Add a constraint to the constraint system.
36963696
void addConstraint(ConstraintKind kind, Type first, Type second,
36973697
ConstraintLocatorBuilder locator,
3698-
bool isFavored = false);
3698+
bool isFavored = false,
3699+
PreparedOverload *preparedOverload = nullptr);
36993700

37003701
/// Add a requirement as a constraint to the constraint system.
37013702
void addConstraint(Requirement req, ConstraintLocatorBuilder locator,
37023703
bool isFavored,
3703-
bool prohibitNonisolatedConformance);
3704+
bool prohibitNonisolatedConformance,
3705+
PreparedOverload *preparedOverload = nullptr);
37043706

37053707
void addApplicationConstraint(
37063708
FunctionType *appliedFn, Type calleeType,
@@ -4414,7 +4416,8 @@ class ConstraintSystem {
44144416
GenericSignature signature,
44154417
bool skipProtocolSelfConstraint,
44164418
ConstraintLocatorBuilder locator,
4417-
llvm::function_ref<Type(Type)> subst);
4419+
llvm::function_ref<Type(Type)> subst,
4420+
PreparedOverload *preparedOverload);
44184421

44194422
// Record the given requirement in the constraint system.
44204423
void openGenericRequirement(DeclContext *outerDC,
@@ -4423,7 +4426,8 @@ class ConstraintSystem {
44234426
const Requirement &requirement,
44244427
bool skipProtocolSelfConstraint,
44254428
ConstraintLocatorBuilder locator,
4426-
llvm::function_ref<Type(Type)> subst);
4429+
llvm::function_ref<Type(Type)> subst,
4430+
PreparedOverload *preparedOverload);
44274431

44284432
/// Update OpenedTypes and record a change in the trail.
44294433
void recordOpenedType(
@@ -4432,8 +4436,9 @@ class ConstraintSystem {
44324436
/// Record the set of opened types for the given locator.
44334437
void recordOpenedTypes(
44344438
ConstraintLocatorBuilder locator,
4435-
SmallVectorImpl<OpenedType> &replacements,
4436-
bool fixmeAllowDuplicates=false);
4439+
const SmallVectorImpl<OpenedType> &replacements,
4440+
PreparedOverload *preparedOverload = nullptr,
4441+
bool fixmeAllowDuplicates = false);
44374442

44384443
/// Check whether the given type conforms to the given protocol and if
44394444
/// so return a valid conformance reference.

include/swift/Sema/PreparedOverload.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,24 @@
1717

1818
namespace swift {
1919

20+
class GenericTypeParamType;
2021
class TypeVariableType;
2122

2223
namespace constraints {
2324

25+
class ConstraintLocatorBuilder;
2426
class ConstraintSystem;
2527

28+
/// Describes a dependent type that has been opened to a particular type
29+
/// variable.
30+
using OpenedType = std::pair<GenericTypeParamType *, TypeVariableType *>;
31+
2632
struct PreparedOverload {
2733
SmallVector<TypeVariableType *, 2> TypeVariables;
34+
SmallVector<Constraint *, 2> Constraints;
35+
SmallVector<OpenedType, 2> Replacements;
2836

29-
void discharge(ConstraintSystem &cs) const;
37+
void discharge(ConstraintSystem &cs, ConstraintLocatorBuilder locator) const;
3038
};
3139

3240
}

lib/Sema/CSSimplify.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "swift/Sema/CSFix.h"
4141
#include "swift/Sema/ConstraintSystem.h"
4242
#include "swift/Sema/IDETypeChecking.h"
43+
#include "swift/Sema/PreparedOverload.h"
4344
#include "llvm/ADT/SetVector.h"
4445
#include "llvm/Support/Compiler.h"
4546

@@ -16269,15 +16270,17 @@ ConstraintSystem::addKeyPathConstraint(
1626916270
void ConstraintSystem::addConstraint(Requirement req,
1627016271
ConstraintLocatorBuilder locator,
1627116272
bool isFavored,
16272-
bool prohibitNonisolatedConformance) {
16273+
bool prohibitNonisolatedConformance,
16274+
PreparedOverload *preparedOverload) {
1627316275
bool conformsToAnyObject = false;
1627416276
std::optional<ConstraintKind> kind;
1627516277
switch (req.getKind()) {
1627616278
case RequirementKind::SameShape: {
1627716279
auto type1 = req.getFirstType();
1627816280
auto type2 = req.getSecondType();
1627916281

16280-
addConstraint(ConstraintKind::SameShape, type1, type2, locator);
16282+
addConstraint(ConstraintKind::SameShape, type1, type2, locator,
16283+
/*isFavored=*/false, preparedOverload);
1628116284
return;
1628216285
}
1628316286

@@ -16318,19 +16321,32 @@ void ConstraintSystem::addConstraint(Requirement req,
1631816321
auto firstType = req.getFirstType();
1631916322
if (kind) {
1632016323
addConstraint(*kind, req.getFirstType(), req.getSecondType(), locator,
16321-
isFavored);
16324+
isFavored, preparedOverload);
1632216325
}
1632316326

1632416327
if (conformsToAnyObject) {
1632516328
auto anyObject = getASTContext().getAnyObjectConstraint();
16326-
addConstraint(ConstraintKind::ConformsTo, firstType, anyObject, locator);
16329+
addConstraint(ConstraintKind::ConformsTo, firstType, anyObject, locator,
16330+
/*isFavored=*/false, preparedOverload);
1632716331
}
1632816332
}
1632916333

1633016334
void ConstraintSystem::addConstraint(ConstraintKind kind, Type first,
1633116335
Type second,
1633216336
ConstraintLocatorBuilder locator,
16333-
bool isFavored) {
16337+
bool isFavored,
16338+
PreparedOverload *preparedOverload) {
16339+
if (preparedOverload) {
16340+
ASSERT(PreparingOverload);
16341+
auto c = Constraint::create(*this, kind, first, second,
16342+
getConstraintLocator(locator));
16343+
if (isFavored) c->setFavored();
16344+
preparedOverload->Constraints.push_back(c);
16345+
return;
16346+
}
16347+
16348+
ASSERT(!PreparingOverload);
16349+
1633416350
switch (addConstraintImpl(kind, first, second, locator, isFavored)) {
1633516351
case SolutionKind::Error:
1633616352
// Add a failing constraint, if needed.

lib/Sema/CSSolver.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ TypeVariableType *ConstraintSystem::createTypeVariable(
6464
++TotalNumTypeVariables;
6565
auto tv = TypeVariableType::getNew(getASTContext(), assignTypeVariableID(),
6666
locator, options);
67-
if (preparedOverload)
67+
if (preparedOverload) {
68+
ASSERT(PreparingOverload);
6869
preparedOverload->TypeVariables.push_back(tv);
69-
else
70+
} else {
7071
addTypeVariable(tv);
72+
}
7173
return tv;
7274
}
7375

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
722722
[&](Type type) -> Type {
723723
return cs.openType(type, genericParameters, locator,
724724
/*preparedOverload=*/nullptr);
725-
});
725+
}, /*preparedOverload=*/nullptr);
726726
};
727727

728728
auto diagnoseInvalidRequirement = [&](Requirement requirement) {

0 commit comments

Comments
 (0)