Skip to content

Commit 44bd5be

Browse files
authored
Merge pull request #82976 from slavapestov/prepared-overloads-part-2
Prepared overloads, part 2
2 parents d9d7db9 + 9a8f3ba commit 44bd5be

File tree

11 files changed

+388
-228
lines changed

11 files changed

+388
-228
lines changed

include/swift/Sema/Constraint.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ namespace constraints {
4949
class ConstraintFix;
5050
class ConstraintLocator;
5151
class ConstraintSystem;
52+
class PreparedOverload;
5253
enum class TrailingClosureMatching;
5354

5455
/// Describes the kind of constraint placed on one or more types.
@@ -437,6 +438,9 @@ class Constraint final : public llvm::ilist_node<Constraint>,
437438
struct {
438439
/// The first type.
439440
Type First;
441+
442+
/// The prepared overload, if any.
443+
PreparedOverload *Prepared;
440444
} Overload;
441445

442446
struct {
@@ -490,7 +494,9 @@ class Constraint final : public llvm::ilist_node<Constraint>,
490494
SmallPtrSetImpl<TypeVariableType *> &typeVars);
491495

492496
/// Construct a new overload-binding constraint, which might have a fix.
493-
Constraint(Type type, OverloadChoice choice, DeclContext *useDC,
497+
Constraint(Type type, OverloadChoice choice,
498+
PreparedOverload *preparedOverload,
499+
DeclContext *useDC,
494500
ConstraintFix *fix, ConstraintLocator *locator,
495501
SmallPtrSetImpl<TypeVariableType *> &typeVars);
496502

@@ -871,6 +877,13 @@ class Constraint final : public llvm::ilist_node<Constraint>,
871877
return *getTrailingObjects<OverloadChoice>();
872878
}
873879

880+
/// Retrieve the prepared overload choice for an overload-binding
881+
/// constraint.
882+
PreparedOverload *getPreparedOverload() const {
883+
ASSERT(Kind == ConstraintKind::BindOverload);
884+
return Overload.Prepared;
885+
}
886+
874887
FunctionType *getAppliedFunctionType() const {
875888
assert(Kind == ConstraintKind::ApplicableFunction);
876889
return Apply.AppliedFn;

include/swift/Sema/ConstraintSystem.h

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

6565
class ConstraintSystem;
6666
class SyntacticElementTarget;
67-
struct PreparedOverload;
67+
68+
// PreparedOverload.h
69+
struct DeclReferenceType;
70+
class PreparedOverload;
71+
struct PreparedOverloadBuilder;
6872

6973
} // end namespace constraints
7074

@@ -2143,34 +2147,6 @@ struct ClosureIsolatedByPreconcurrency {
21432147
bool operator()(const ClosureExpr *expr) const;
21442148
};
21452149

2146-
/// Describes the type produced when referencing a declaration.
2147-
struct DeclReferenceType {
2148-
/// The "opened" type, which is the type of the declaration where any
2149-
/// generic parameters have been replaced with type variables.
2150-
///
2151-
/// The mapping from generic parameters to type variables will have been
2152-
/// recorded by \c recordOpenedTypes when this type is produced.
2153-
Type openedType;
2154-
2155-
/// The opened type, after performing contextual type adjustments such as
2156-
/// removing concurrency-related annotations for a `@preconcurrency`
2157-
/// operation.
2158-
Type adjustedOpenedType;
2159-
2160-
/// The type of the reference, based on the original opened type. This is the
2161-
/// type that the expression used to form the declaration reference would
2162-
/// have if no adjustments had been applied.
2163-
Type referenceType;
2164-
2165-
/// The type of the reference, which is the adjusted opened type after
2166-
/// (e.g.) applying the base of a member access. This is the type of the
2167-
/// expression used to form the declaration reference.
2168-
Type adjustedReferenceType;
2169-
2170-
/// The type that could be thrown by accessing this declaration.
2171-
Type thrownErrorTypeOnAccess;
2172-
};
2173-
21742150
/// Describes a system of constraints on type variables, the
21752151
/// solution of which assigns concrete types to each of the type variables.
21762152
/// Constraint systems are typically generated given an (untyped) expression.
@@ -2954,7 +2930,7 @@ class ConstraintSystem {
29542930
/// Create a new type variable.
29552931
TypeVariableType *createTypeVariable(ConstraintLocator *locator,
29562932
unsigned options,
2957-
PreparedOverload *preparedOverload
2933+
PreparedOverloadBuilder *preparedOverload
29582934
= nullptr);
29592935

29602936
/// Retrieve the set of active type variables.
@@ -3414,7 +3390,8 @@ class ConstraintSystem {
34143390
/// Update OpenedExistentials and record a change in the trail.
34153391
void recordOpenedExistentialType(ConstraintLocator *locator,
34163392
ExistentialArchetypeType *opened,
3417-
PreparedOverload *preparedOverload = nullptr);
3393+
PreparedOverloadBuilder *preparedOverload
3394+
= nullptr);
34183395

34193396
/// Retrieve the generic environment for the opened element of a given pack
34203397
/// expansion, or \c nullptr if no environment was recorded yet.
@@ -3622,7 +3599,7 @@ class ConstraintSystem {
36223599
/// Log and record the application of the fix. Return true iff any
36233600
/// subsequent solution would be worse than the best known solution.
36243601
bool recordFix(ConstraintFix *fix, unsigned impact = 1,
3625-
PreparedOverload *preparedOverload = nullptr);
3602+
PreparedOverloadBuilder *preparedOverload = nullptr);
36263603

36273604
void recordPotentialHole(TypeVariableType *typeVar);
36283605
void recordAnyTypeVarAsPotentialHole(Type type);
@@ -3698,13 +3675,13 @@ class ConstraintSystem {
36983675
void addConstraint(ConstraintKind kind, Type first, Type second,
36993676
ConstraintLocatorBuilder locator,
37003677
bool isFavored = false,
3701-
PreparedOverload *preparedOverload = nullptr);
3678+
PreparedOverloadBuilder *preparedOverload = nullptr);
37023679

37033680
/// Add a requirement as a constraint to the constraint system.
37043681
void addConstraint(Requirement req, ConstraintLocatorBuilder locator,
37053682
bool isFavored,
37063683
bool prohibitNonisolatedConformance,
3707-
PreparedOverload *preparedOverload = nullptr);
3684+
PreparedOverloadBuilder *preparedOverload = nullptr);
37083685

37093686
void addApplicationConstraint(
37103687
FunctionType *appliedFn, Type calleeType,
@@ -3814,9 +3791,9 @@ class ConstraintSystem {
38143791

38153792
/// Add a constraint that binds an overload set to a specific choice.
38163793
void addBindOverloadConstraint(Type boundTy, OverloadChoice choice,
3817-
ConstraintLocator *locator,
3818-
DeclContext *useDC) {
3819-
resolveOverload(locator, boundTy, choice, useDC);
3794+
ConstraintLocator *locator, DeclContext *useDC) {
3795+
resolveOverload(locator, boundTy, choice, useDC,
3796+
/*preparedOverload=*/nullptr);
38203797
}
38213798

38223799
/// Add a value member constraint to the constraint system.
@@ -4319,7 +4296,7 @@ class ConstraintSystem {
43194296
Type openUnboundGenericType(GenericTypeDecl *decl, Type parentTy,
43204297
ConstraintLocatorBuilder locator,
43214298
bool isTypeResolution,
4322-
PreparedOverload *preparedOverload = nullptr);
4299+
PreparedOverloadBuilder *preparedOverload = nullptr);
43234300

43244301
/// Replace placeholder types with fresh type variables, and unbound generic
43254302
/// types with bound generic types whose generic args are fresh type
@@ -4330,7 +4307,7 @@ class ConstraintSystem {
43304307
/// \returns The converted type.
43314308
Type replaceInferableTypesWithTypeVars(Type type,
43324309
ConstraintLocatorBuilder locator,
4333-
PreparedOverload *preparedOverload
4310+
PreparedOverloadBuilder *preparedOverload
43344311
= nullptr);
43354312

43364313
/// "Open" the given type by replacing any occurrences of generic
@@ -4343,7 +4320,7 @@ class ConstraintSystem {
43434320
/// \returns The opened type, or \c type if there are no archetypes in it.
43444321
Type openType(Type type, ArrayRef<OpenedType> replacements,
43454322
ConstraintLocatorBuilder locator,
4346-
PreparedOverload *preparedOverload);
4323+
PreparedOverloadBuilder *preparedOverload);
43474324

43484325
/// "Open" an opaque archetype type, similar to \c openType.
43494326
Type openOpaqueType(OpaqueTypeArchetypeType *type,
@@ -4360,12 +4337,12 @@ class ConstraintSystem {
43604337
Type openPackExpansionType(PackExpansionType *expansion,
43614338
ArrayRef<OpenedType> replacements,
43624339
ConstraintLocatorBuilder locator,
4363-
PreparedOverload *preparedOverload);
4340+
PreparedOverloadBuilder *preparedOverload);
43644341

43654342
/// Update OpenedPackExpansionTypes and record a change in the trail.
43664343
void recordOpenedPackExpansionType(PackExpansionType *expansion,
43674344
TypeVariableType *expansionVar,
4368-
PreparedOverload *preparedOverload
4345+
PreparedOverloadBuilder *preparedOverload
43694346
= nullptr);
43704347

43714348
/// Undo the above change.
@@ -4392,29 +4369,29 @@ class ConstraintSystem {
43924369
ConstraintLocatorBuilder locator,
43934370
SmallVectorImpl<OpenedType> &replacements,
43944371
DeclContext *outerDC,
4395-
PreparedOverload *preparedOverload);
4372+
PreparedOverloadBuilder *preparedOverload);
43964373

43974374
/// Open the generic parameter list and its requirements,
43984375
/// creating type variables for each of the type parameters.
43994376
void openGeneric(DeclContext *outerDC,
44004377
GenericSignature signature,
44014378
ConstraintLocatorBuilder locator,
44024379
SmallVectorImpl<OpenedType> &replacements,
4403-
PreparedOverload *preparedOverload);
4380+
PreparedOverloadBuilder *preparedOverload);
44044381

44054382
/// Open the generic parameter list creating type variables for each of the
44064383
/// type parameters.
44074384
void openGenericParameters(DeclContext *outerDC,
44084385
GenericSignature signature,
44094386
SmallVectorImpl<OpenedType> &replacements,
44104387
ConstraintLocatorBuilder locator,
4411-
PreparedOverload *preparedOverload);
4388+
PreparedOverloadBuilder *preparedOverload);
44124389

44134390
/// Open a generic parameter into a type variable and record
44144391
/// it in \c replacements.
44154392
TypeVariableType *openGenericParameter(GenericTypeParamType *parameter,
44164393
ConstraintLocatorBuilder locator,
4417-
PreparedOverload *preparedOverload);
4394+
PreparedOverloadBuilder *preparedOverload);
44184395

44194396
/// Given generic signature open its generic requirements,
44204397
/// using substitution function, and record them in the
@@ -4424,7 +4401,7 @@ class ConstraintSystem {
44244401
bool skipProtocolSelfConstraint,
44254402
ConstraintLocatorBuilder locator,
44264403
llvm::function_ref<Type(Type)> subst,
4427-
PreparedOverload *preparedOverload);
4404+
PreparedOverloadBuilder *preparedOverload);
44284405

44294406
// Record the given requirement in the constraint system.
44304407
void openGenericRequirement(DeclContext *outerDC,
@@ -4434,18 +4411,18 @@ class ConstraintSystem {
44344411
bool skipProtocolSelfConstraint,
44354412
ConstraintLocatorBuilder locator,
44364413
llvm::function_ref<Type(Type)> subst,
4437-
PreparedOverload *preparedOverload);
4414+
PreparedOverloadBuilder *preparedOverload);
44384415

44394416
/// Update OpenedTypes and record a change in the trail.
44404417
void recordOpenedType(
44414418
ConstraintLocator *locator, ArrayRef<OpenedType> openedTypes,
4442-
PreparedOverload *preparedOverload = nullptr);
4419+
PreparedOverloadBuilder *preparedOverload = nullptr);
44434420

44444421
/// Record the set of opened types for the given locator.
44454422
void recordOpenedTypes(
44464423
ConstraintLocatorBuilder locator,
44474424
const SmallVectorImpl<OpenedType> &replacements,
4448-
PreparedOverload *preparedOverload = nullptr,
4425+
PreparedOverloadBuilder *preparedOverload = nullptr,
44494426
bool fixmeAllowDuplicates = false);
44504427

44514428
/// Check whether the given type conforms to the given protocol and if
@@ -4458,7 +4435,7 @@ class ConstraintSystem {
44584435
FunctionType *fnType, Type baseType, ValueDecl *decl, DeclContext *dc,
44594436
unsigned numApplies, bool isMainDispatchQueue,
44604437
ArrayRef<OpenedType> replacements, ConstraintLocatorBuilder locator,
4461-
PreparedOverload *preparedOverload);
4438+
PreparedOverloadBuilder *preparedOverload);
44624439

44634440
/// Retrieve the type of a reference to the given value declaration.
44644441
///
@@ -4474,7 +4451,7 @@ class ConstraintSystem {
44744451
FunctionRefInfo functionRefInfo,
44754452
ConstraintLocatorBuilder locator,
44764453
DeclContext *useDC,
4477-
PreparedOverload *preparedOverload);
4454+
PreparedOverloadBuilder *preparedOverload);
44784455

44794456
/// Return the type-of-reference of the given value.
44804457
///
@@ -4516,7 +4493,7 @@ class ConstraintSystem {
45164493
Type baseTy, ValueDecl *decl, DeclContext *useDC, bool isDynamicLookup,
45174494
FunctionRefInfo functionRefInfo, ConstraintLocator *locator,
45184495
SmallVectorImpl<OpenedType> *replacements = nullptr,
4519-
PreparedOverload *preparedOverload = nullptr);
4496+
PreparedOverloadBuilder *preparedOverload = nullptr);
45204497

45214498
/// Retrieve a list of generic parameter types solver has "opened" (replaced
45224499
/// with a type variable) at the given location.
@@ -4929,9 +4906,28 @@ class ConstraintSystem {
49294906
void recordResolvedOverload(ConstraintLocator *locator,
49304907
SelectedOverload choice);
49314908

4909+
/// Build and allocate a prepared overload in the solver arena.
4910+
PreparedOverload *prepareOverload(ConstraintLocator *locator,
4911+
OverloadChoice choice,
4912+
DeclContext *useDC);
4913+
4914+
/// Populate the prepared overload with all type variables and constraints
4915+
/// that are to be introduced into the constraint system when this choice
4916+
/// is taken.
4917+
DeclReferenceType
4918+
prepareOverloadImpl(ConstraintLocator *locator,
4919+
OverloadChoice choice,
4920+
DeclContext *useDC,
4921+
PreparedOverloadBuilder *preparedOverload);
4922+
4923+
void replayChanges(
4924+
ConstraintLocator *locator,
4925+
PreparedOverload *preparedOverload);
4926+
49324927
/// Resolve the given overload set to the given choice.
49334928
void resolveOverload(ConstraintLocator *locator, Type boundType,
4934-
OverloadChoice choice, DeclContext *useDC);
4929+
OverloadChoice choice, DeclContext *useDC,
4930+
PreparedOverload *preparedOverload);
49354931

49364932
/// Simplify a type, by replacing type variables with either their
49374933
/// fixed types (if available) or their representatives.
@@ -5342,13 +5338,13 @@ class ConstraintSystem {
53425338
ConstraintKind matchKind,
53435339
ConstraintLocator *locator,
53445340
ConstraintLocator *calleeLocator,
5345-
PreparedOverload *preparedOverload = nullptr);
5341+
PreparedOverloadBuilder *preparedOverload = nullptr);
53465342

53475343
/// Used by applyPropertyWrapperToParameter() to update appliedPropertyWrappers
53485344
/// and record a change in the trail.
53495345
void applyPropertyWrapper(Expr *anchor,
53505346
AppliedPropertyWrapper applied,
5351-
PreparedOverload *preparedOverload = nullptr);
5347+
PreparedOverloadBuilder *preparedOverload = nullptr);
53525348

53535349
/// Undo the above change.
53545350
void removePropertyWrapper(Expr *anchor);

include/swift/Sema/OverloadChoice.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,23 @@ class OverloadChoice {
250250
return (OverloadChoiceKind)kind;
251251
}
252252

253+
bool canBePrepared() const {
254+
switch (getKind()) {
255+
case OverloadChoiceKind::Decl:
256+
case OverloadChoiceKind::DeclViaBridge:
257+
case OverloadChoiceKind::DeclViaDynamic:
258+
case OverloadChoiceKind::DeclViaUnwrappedOptional:
259+
case OverloadChoiceKind::DynamicMemberLookup:
260+
case OverloadChoiceKind::KeyPathDynamicMemberLookup:
261+
return true;
262+
case OverloadChoiceKind::TupleIndex:
263+
case OverloadChoiceKind::MaterializePack:
264+
case OverloadChoiceKind::ExtractFunctionIsolation:
265+
case OverloadChoiceKind::KeyPathApplication:
266+
return false;
267+
}
268+
}
269+
253270
/// Determine whether this choice is for a declaration.
254271
bool isDecl() const {
255272
return DeclOrKind.is<ValueDecl*>();

0 commit comments

Comments
 (0)