Skip to content

Commit cdbbe7a

Browse files
authored
Merge pull request #82928 from slavapestov/prepared-overloads
Sema: Opening overloads at the outer decision level, part 1
2 parents 0bf1fae + 751edee commit cdbbe7a

File tree

10 files changed

+614
-180
lines changed

10 files changed

+614
-180
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ namespace constraints {
6464

6565
class ConstraintSystem;
6666
class SyntacticElementTarget;
67+
struct PreparedOverload;
6768

6869
} // end namespace constraints
6970

@@ -2211,6 +2212,8 @@ class ConstraintSystem {
22112212
unsigned CountDisjunctions = 0;
22122213

22132214
private:
2215+
bool PreparingOverload = false;
2216+
22142217
/// A constraint that has failed along the current solver path.
22152218
/// Do not set directly, call \c recordFailedConstraint instead.
22162219
Constraint *failedConstraint = nullptr;
@@ -2752,6 +2755,7 @@ class ConstraintSystem {
27522755
SolverState *solverState = nullptr;
27532756

27542757
void recordChange(SolverTrail::Change change) {
2758+
ASSERT(!PreparingOverload);
27552759
solverState->Trail.recordChange(change);
27562760
}
27572761

@@ -2920,15 +2924,15 @@ class ConstraintSystem {
29202924
SolverTrail *getTrail() const {
29212925
return solverState ? &solverState->Trail : nullptr;
29222926
}
2923-
2924-
/// Add a new type variable that was already created.
2925-
void addTypeVariable(TypeVariableType *typeVar);
29262927

29272928
/// Add a constraint from the subscript base to the root of the key
29282929
/// path literal to the constraint system.
29292930
void addKeyPathApplicationRootConstraint(Type root, ConstraintLocatorBuilder locator);
29302931

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

29502954
/// Create a new type variable.
29512955
TypeVariableType *createTypeVariable(ConstraintLocator *locator,
2952-
unsigned options);
2956+
unsigned options,
2957+
PreparedOverload *preparedOverload
2958+
= nullptr);
29532959

29542960
/// Retrieve the set of active type variables.
29552961
ArrayRef<TypeVariableType *> getTypeVariables() const {
@@ -3407,7 +3413,8 @@ class ConstraintSystem {
34073413

34083414
/// Update OpenedExistentials and record a change in the trail.
34093415
void recordOpenedExistentialType(ConstraintLocator *locator,
3410-
ExistentialArchetypeType *opened);
3416+
ExistentialArchetypeType *opened,
3417+
PreparedOverload *preparedOverload = nullptr);
34113418

34123419
/// Retrieve the generic environment for the opened element of a given pack
34133420
/// expansion, or \c nullptr if no environment was recorded yet.
@@ -3614,7 +3621,8 @@ class ConstraintSystem {
36143621

36153622
/// Log and record the application of the fix. Return true iff any
36163623
/// subsequent solution would be worse than the best known solution.
3617-
bool recordFix(ConstraintFix *fix, unsigned impact = 1);
3624+
bool recordFix(ConstraintFix *fix, unsigned impact = 1,
3625+
PreparedOverload *preparedOverload = nullptr);
36183626

36193627
void recordPotentialHole(TypeVariableType *typeVar);
36203628
void recordAnyTypeVarAsPotentialHole(Type type);
@@ -3689,12 +3697,14 @@ class ConstraintSystem {
36893697
/// Add a constraint to the constraint system.
36903698
void addConstraint(ConstraintKind kind, Type first, Type second,
36913699
ConstraintLocatorBuilder locator,
3692-
bool isFavored = false);
3700+
bool isFavored = false,
3701+
PreparedOverload *preparedOverload = nullptr);
36933702

36943703
/// Add a requirement as a constraint to the constraint system.
36953704
void addConstraint(Requirement req, ConstraintLocatorBuilder locator,
36963705
bool isFavored,
3697-
bool prohibitNonisolatedConformance);
3706+
bool prohibitNonisolatedConformance,
3707+
PreparedOverload *preparedOverload = nullptr);
36983708

36993709
void addApplicationConstraint(
37003710
FunctionType *appliedFn, Type calleeType,
@@ -4308,7 +4318,8 @@ class ConstraintSystem {
43084318
/// \returns The opened type.
43094319
Type openUnboundGenericType(GenericTypeDecl *decl, Type parentTy,
43104320
ConstraintLocatorBuilder locator,
4311-
bool isTypeResolution);
4321+
bool isTypeResolution,
4322+
PreparedOverload *preparedOverload = nullptr);
43124323

43134324
/// Replace placeholder types with fresh type variables, and unbound generic
43144325
/// types with bound generic types whose generic args are fresh type
@@ -4318,7 +4329,9 @@ class ConstraintSystem {
43184329
///
43194330
/// \returns The converted type.
43204331
Type replaceInferableTypesWithTypeVars(Type type,
4321-
ConstraintLocatorBuilder locator);
4332+
ConstraintLocatorBuilder locator,
4333+
PreparedOverload *preparedOverload
4334+
= nullptr);
43224335

43234336
/// "Open" the given type by replacing any occurrences of generic
43244337
/// parameter types and dependent member types with fresh type variables.
@@ -4329,7 +4342,8 @@ class ConstraintSystem {
43294342
///
43304343
/// \returns The opened type, or \c type if there are no archetypes in it.
43314344
Type openType(Type type, ArrayRef<OpenedType> replacements,
4332-
ConstraintLocatorBuilder locator);
4345+
ConstraintLocatorBuilder locator,
4346+
PreparedOverload *preparedOverload);
43334347

43344348
/// "Open" an opaque archetype type, similar to \c openType.
43354349
Type openOpaqueType(OpaqueTypeArchetypeType *type,
@@ -4345,11 +4359,14 @@ class ConstraintSystem {
43454359
/// aforementioned variable via special constraints.
43464360
Type openPackExpansionType(PackExpansionType *expansion,
43474361
ArrayRef<OpenedType> replacements,
4348-
ConstraintLocatorBuilder locator);
4362+
ConstraintLocatorBuilder locator,
4363+
PreparedOverload *preparedOverload);
43494364

43504365
/// Update OpenedPackExpansionTypes and record a change in the trail.
43514366
void recordOpenedPackExpansionType(PackExpansionType *expansion,
4352-
TypeVariableType *expansionVar);
4367+
TypeVariableType *expansionVar,
4368+
PreparedOverload *preparedOverload
4369+
= nullptr);
43534370

43544371
/// Undo the above change.
43554372
void removeOpenedPackExpansionType(PackExpansionType *expansion) {
@@ -4374,26 +4391,30 @@ class ConstraintSystem {
43744391
FunctionType *openFunctionType(AnyFunctionType *funcType,
43754392
ConstraintLocatorBuilder locator,
43764393
SmallVectorImpl<OpenedType> &replacements,
4377-
DeclContext *outerDC);
4394+
DeclContext *outerDC,
4395+
PreparedOverload *preparedOverload);
43784396

43794397
/// Open the generic parameter list and its requirements,
43804398
/// creating type variables for each of the type parameters.
43814399
void openGeneric(DeclContext *outerDC,
43824400
GenericSignature signature,
43834401
ConstraintLocatorBuilder locator,
4384-
SmallVectorImpl<OpenedType> &replacements);
4402+
SmallVectorImpl<OpenedType> &replacements,
4403+
PreparedOverload *preparedOverload);
43854404

43864405
/// Open the generic parameter list creating type variables for each of the
43874406
/// type parameters.
43884407
void openGenericParameters(DeclContext *outerDC,
43894408
GenericSignature signature,
43904409
SmallVectorImpl<OpenedType> &replacements,
4391-
ConstraintLocatorBuilder locator);
4410+
ConstraintLocatorBuilder locator,
4411+
PreparedOverload *preparedOverload);
43924412

43934413
/// Open a generic parameter into a type variable and record
43944414
/// it in \c replacements.
43954415
TypeVariableType *openGenericParameter(GenericTypeParamType *parameter,
4396-
ConstraintLocatorBuilder locator);
4416+
ConstraintLocatorBuilder locator,
4417+
PreparedOverload *preparedOverload);
43974418

43984419
/// Given generic signature open its generic requirements,
43994420
/// using substitution function, and record them in the
@@ -4402,7 +4423,8 @@ class ConstraintSystem {
44024423
GenericSignature signature,
44034424
bool skipProtocolSelfConstraint,
44044425
ConstraintLocatorBuilder locator,
4405-
llvm::function_ref<Type(Type)> subst);
4426+
llvm::function_ref<Type(Type)> subst,
4427+
PreparedOverload *preparedOverload);
44064428

44074429
// Record the given requirement in the constraint system.
44084430
void openGenericRequirement(DeclContext *outerDC,
@@ -4411,17 +4433,20 @@ class ConstraintSystem {
44114433
const Requirement &requirement,
44124434
bool skipProtocolSelfConstraint,
44134435
ConstraintLocatorBuilder locator,
4414-
llvm::function_ref<Type(Type)> subst);
4436+
llvm::function_ref<Type(Type)> subst,
4437+
PreparedOverload *preparedOverload);
44154438

44164439
/// Update OpenedTypes and record a change in the trail.
44174440
void recordOpenedType(
4418-
ConstraintLocator *locator, ArrayRef<OpenedType> openedTypes);
4441+
ConstraintLocator *locator, ArrayRef<OpenedType> openedTypes,
4442+
PreparedOverload *preparedOverload = nullptr);
44194443

44204444
/// Record the set of opened types for the given locator.
44214445
void recordOpenedTypes(
44224446
ConstraintLocatorBuilder locator,
4423-
SmallVectorImpl<OpenedType> &replacements,
4424-
bool fixmeAllowDuplicates=false);
4447+
const SmallVectorImpl<OpenedType> &replacements,
4448+
PreparedOverload *preparedOverload = nullptr,
4449+
bool fixmeAllowDuplicates = false);
44254450

44264451
/// Check whether the given type conforms to the given protocol and if
44274452
/// so return a valid conformance reference.
@@ -4432,7 +4457,8 @@ class ConstraintSystem {
44324457
FunctionType *adjustFunctionTypeForConcurrency(
44334458
FunctionType *fnType, Type baseType, ValueDecl *decl, DeclContext *dc,
44344459
unsigned numApplies, bool isMainDispatchQueue,
4435-
ArrayRef<OpenedType> replacements, ConstraintLocatorBuilder locator);
4460+
ArrayRef<OpenedType> replacements, ConstraintLocatorBuilder locator,
4461+
PreparedOverload *preparedOverload);
44364462

44374463
/// Retrieve the type of a reference to the given value declaration.
44384464
///
@@ -4447,7 +4473,8 @@ class ConstraintSystem {
44474473
ValueDecl *decl,
44484474
FunctionRefInfo functionRefInfo,
44494475
ConstraintLocatorBuilder locator,
4450-
DeclContext *useDC);
4476+
DeclContext *useDC,
4477+
PreparedOverload *preparedOverload);
44514478

44524479
/// Return the type-of-reference of the given value.
44534480
///
@@ -4488,7 +4515,8 @@ class ConstraintSystem {
44884515
DeclReferenceType getTypeOfMemberReference(
44894516
Type baseTy, ValueDecl *decl, DeclContext *useDC, bool isDynamicLookup,
44904517
FunctionRefInfo functionRefInfo, ConstraintLocator *locator,
4491-
SmallVectorImpl<OpenedType> *replacements = nullptr);
4518+
SmallVectorImpl<OpenedType> *replacements = nullptr,
4519+
PreparedOverload *preparedOverload = nullptr);
44924520

44934521
/// Retrieve a list of generic parameter types solver has "opened" (replaced
44944522
/// with a type variable) at the given location.
@@ -5307,13 +5335,20 @@ class ConstraintSystem {
53075335
/// Matches a wrapped or projected value parameter type to its backing
53085336
/// property wrapper type by applying the property wrapper.
53095337
TypeMatchResult applyPropertyWrapperToParameter(
5310-
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
5311-
ConstraintKind matchKind, ConstraintLocator *locator,
5312-
ConstraintLocator *calleeLocator);
5338+
Type wrapperType,
5339+
Type paramType,
5340+
ParamDecl *param,
5341+
Identifier argLabel,
5342+
ConstraintKind matchKind,
5343+
ConstraintLocator *locator,
5344+
ConstraintLocator *calleeLocator,
5345+
PreparedOverload *preparedOverload = nullptr);
53135346

53145347
/// Used by applyPropertyWrapperToParameter() to update appliedPropertyWrappers
53155348
/// and record a change in the trail.
5316-
void applyPropertyWrapper(Expr *anchor, AppliedPropertyWrapper applied);
5349+
void applyPropertyWrapper(Expr *anchor,
5350+
AppliedPropertyWrapper applied,
5351+
PreparedOverload *preparedOverload = nullptr);
53175352

53185353
/// Undo the above change.
53195354
void removePropertyWrapper(Expr *anchor);

0 commit comments

Comments
 (0)