Skip to content

Commit 5437935

Browse files
committed
Sema: Record opened type variables in PreparedOverload
1 parent b925630 commit 5437935

File tree

8 files changed

+136
-73
lines changed

8 files changed

+136
-73
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2924,15 +2924,15 @@ class ConstraintSystem {
29242924
SolverTrail *getTrail() const {
29252925
return solverState ? &solverState->Trail : nullptr;
29262926
}
2927-
2928-
/// Add a new type variable that was already created.
2929-
void addTypeVariable(TypeVariableType *typeVar);
29302927

29312928
/// Add a constraint from the subscript base to the root of the key
29322929
/// path literal to the constraint system.
29332930
void addKeyPathApplicationRootConstraint(Type root, ConstraintLocatorBuilder locator);
29342931

29352932
public:
2933+
/// Add a new type variable that was already created.
2934+
void addTypeVariable(TypeVariableType *typeVar);
2935+
29362936
/// Lookup for a member with the given name which is in the given base type.
29372937
///
29382938
/// This routine caches the results of member lookups in the top constraint
@@ -2953,7 +2953,9 @@ class ConstraintSystem {
29532953

29542954
/// Create a new type variable.
29552955
TypeVariableType *createTypeVariable(ConstraintLocator *locator,
2956-
unsigned options);
2956+
unsigned options,
2957+
PreparedOverload *preparedOverload
2958+
= nullptr);
29572959

29582960
/// Retrieve the set of active type variables.
29592961
ArrayRef<TypeVariableType *> getTypeVariables() const {
@@ -4333,7 +4335,8 @@ class ConstraintSystem {
43334335
///
43344336
/// \returns The opened type, or \c type if there are no archetypes in it.
43354337
Type openType(Type type, ArrayRef<OpenedType> replacements,
4336-
ConstraintLocatorBuilder locator);
4338+
ConstraintLocatorBuilder locator,
4339+
PreparedOverload *preparedOverload);
43374340

43384341
/// "Open" an opaque archetype type, similar to \c openType.
43394342
Type openOpaqueType(OpaqueTypeArchetypeType *type,
@@ -4349,7 +4352,8 @@ class ConstraintSystem {
43494352
/// aforementioned variable via special constraints.
43504353
Type openPackExpansionType(PackExpansionType *expansion,
43514354
ArrayRef<OpenedType> replacements,
4352-
ConstraintLocatorBuilder locator);
4355+
ConstraintLocatorBuilder locator,
4356+
PreparedOverload *preparedOverload);
43534357

43544358
/// Update OpenedPackExpansionTypes and record a change in the trail.
43554359
void recordOpenedPackExpansionType(PackExpansionType *expansion,
@@ -4378,26 +4382,30 @@ class ConstraintSystem {
43784382
FunctionType *openFunctionType(AnyFunctionType *funcType,
43794383
ConstraintLocatorBuilder locator,
43804384
SmallVectorImpl<OpenedType> &replacements,
4381-
DeclContext *outerDC);
4385+
DeclContext *outerDC,
4386+
PreparedOverload *preparedOverload);
43824387

43834388
/// Open the generic parameter list and its requirements,
43844389
/// creating type variables for each of the type parameters.
43854390
void openGeneric(DeclContext *outerDC,
43864391
GenericSignature signature,
43874392
ConstraintLocatorBuilder locator,
4388-
SmallVectorImpl<OpenedType> &replacements);
4393+
SmallVectorImpl<OpenedType> &replacements,
4394+
PreparedOverload *preparedOverload);
43894395

43904396
/// Open the generic parameter list creating type variables for each of the
43914397
/// type parameters.
43924398
void openGenericParameters(DeclContext *outerDC,
43934399
GenericSignature signature,
43944400
SmallVectorImpl<OpenedType> &replacements,
4395-
ConstraintLocatorBuilder locator);
4401+
ConstraintLocatorBuilder locator,
4402+
PreparedOverload *preparedOverload);
43964403

43974404
/// Open a generic parameter into a type variable and record
43984405
/// it in \c replacements.
43994406
TypeVariableType *openGenericParameter(GenericTypeParamType *parameter,
4400-
ConstraintLocatorBuilder locator);
4407+
ConstraintLocatorBuilder locator,
4408+
PreparedOverload *preparedOverload);
44014409

44024410
/// Given generic signature open its generic requirements,
44034411
/// using substitution function, and record them in the
@@ -4436,7 +4444,8 @@ class ConstraintSystem {
44364444
FunctionType *adjustFunctionTypeForConcurrency(
44374445
FunctionType *fnType, Type baseType, ValueDecl *decl, DeclContext *dc,
44384446
unsigned numApplies, bool isMainDispatchQueue,
4439-
ArrayRef<OpenedType> replacements, ConstraintLocatorBuilder locator);
4447+
ArrayRef<OpenedType> replacements, ConstraintLocatorBuilder locator,
4448+
PreparedOverload *preparedOverload);
44404449

44414450
/// Retrieve the type of a reference to the given value declaration.
44424451
///

include/swift/Sema/PreparedOverload.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@
1212
#ifndef SWIFT_SEMA_PREPAREDOVERLOAD_H
1313
#define SWIFT_SEMA_PREPAREDOVERLOAD_H
1414

15-
#include "swift/AST/AvailabilityRange.h"
16-
#include "swift/AST/FunctionRefInfo.h"
17-
#include "swift/AST/Types.h"
1815
#include "llvm/ADT/PointerIntPair.h"
19-
#include "llvm/Support/ErrorHandling.h"
16+
#include "llvm/ADT/SmallVector.h"
2017

2118
namespace swift {
19+
20+
class TypeVariableType;
21+
2222
namespace constraints {
2323

24+
class ConstraintSystem;
25+
2426
struct PreparedOverload {
27+
SmallVector<TypeVariableType *, 2> TypeVariables;
2528

29+
void discharge(ConstraintSystem &cs) const;
2630
};
2731

2832
}

lib/Sema/CSRanking.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,8 @@ static bool isProtocolExtensionAsSpecializedAs(DeclContext *dc1,
423423
// the second protocol extension.
424424
ConstraintSystem cs(dc1, std::nullopt);
425425
SmallVector<OpenedType, 4> replacements;
426-
cs.openGeneric(dc2, sig2, ConstraintLocatorBuilder(nullptr), replacements);
426+
cs.openGeneric(dc2, sig2, ConstraintLocatorBuilder(nullptr), replacements,
427+
/*preparedOverload=*/nullptr);
427428

428429
// Bind the 'Self' type from the first extension to the type parameter from
429430
// opening 'Self' of the second extension.
@@ -581,13 +582,15 @@ bool CompareDeclSpecializationRequest::evaluate(
581582
SmallVectorImpl<OpenedType> &replacements,
582583
ConstraintLocator *locator) -> Type {
583584
if (auto *funcType = type->getAs<AnyFunctionType>()) {
584-
return cs.openFunctionType(funcType, locator, replacements, outerDC);
585+
return cs.openFunctionType(funcType, locator, replacements, outerDC,
586+
/*preparedOverload=*/nullptr);
585587
}
586588

587589
cs.openGeneric(outerDC, innerDC->getGenericSignatureOfContext(), locator,
588-
replacements);
590+
replacements, /*preparedOverload=*/nullptr);
589591

590-
return cs.openType(type, replacements, locator);
592+
return cs.openType(type, replacements, locator,
593+
/*preparedOverload=*/nullptr);
591594
};
592595

593596
bool knownNonSubtype = false;

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12046,8 +12046,10 @@ static Type getOpenedResultBuilderTypeFor(ConstraintSystem &cs,
1204612046
// Find the opened type for this callee and substitute in the type
1204712047
// parameters.
1204812048
auto substitutions = cs.getOpenedTypes(calleeLocator);
12049-
if (!substitutions.empty())
12050-
builderType = cs.openType(builderType, substitutions, locator);
12049+
if (!substitutions.empty()) {
12050+
builderType = cs.openType(builderType, substitutions, locator,
12051+
/*preparedOverload=*/nullptr);
12052+
}
1205112053

1205212054
assert(!builderType->hasTypeParameter());
1205312055
}

lib/Sema/CSSolver.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/Basic/Defer.h"
2323
#include "swift/Sema/ConstraintGraph.h"
2424
#include "swift/Sema/ConstraintSystem.h"
25+
#include "swift/Sema/PreparedOverload.h"
2526
#include "swift/Sema/SolutionResult.h"
2627
#include "llvm/ADT/STLExtras.h"
2728
#include "llvm/ADT/SetVector.h"
@@ -58,11 +59,15 @@ STATISTIC(LargestSolutionAttemptNumber, "# of the largest solution attempt");
5859

5960
TypeVariableType *ConstraintSystem::createTypeVariable(
6061
ConstraintLocator *locator,
61-
unsigned options) {
62+
unsigned options,
63+
PreparedOverload *preparedOverload) {
6264
++TotalNumTypeVariables;
6365
auto tv = TypeVariableType::getNew(getASTContext(), assignTypeVariableID(),
6466
locator, options);
65-
addTypeVariable(tv);
67+
if (preparedOverload)
68+
preparedOverload->TypeVariables.push_back(tv);
69+
else
70+
addTypeVariable(tv);
6671
return tv;
6772
}
6873

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
627627
if (auto *typeVar = findParam(GP))
628628
return typeVar;
629629

630-
auto *typeVar = cs.openGenericParameter(GP, locator);
630+
auto *typeVar = cs.openGenericParameter(GP, locator, nullptr);
631631
genericParameters.emplace_back(GP, typeVar);
632632

633633
return typeVar;
@@ -720,8 +720,8 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
720720
cs.openGenericRequirement(DC->getParent(), signature, index, requirement,
721721
/*skipSelfProtocolConstraint=*/false, locator,
722722
[&](Type type) -> Type {
723-
return cs.openType(type, genericParameters,
724-
locator);
723+
return cs.openType(type, genericParameters, locator,
724+
/*preparedOverload=*/nullptr);
725725
});
726726
};
727727

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,11 +1251,11 @@ swift::matchWitness(WitnessChecker::RequirementEnvironmentCache &reqEnvCache,
12511251

12521252
reqThrownError = getThrownErrorType(reqASD);
12531253
reqThrownError = cs->openType(reqThrownError, reqReplacements,
1254-
reqLocator);
1254+
reqLocator, /*preparedOverload=*/nullptr);
12551255

12561256
witnessThrownError = getThrownErrorType(witnessASD);
12571257
witnessThrownError = cs->openType(witnessThrownError, witnessReplacements,
1258-
witnessLocator);
1258+
witnessLocator, /*preparedOverload=*/nullptr);
12591259
}
12601260

12611261
return std::make_tuple(std::nullopt, reqType, openWitnessType,

0 commit comments

Comments
 (0)