Skip to content

Commit b181fcf

Browse files
committed
Sema: Get prepared overloads working again with the Pre()/Post() split
1 parent c344d6b commit b181fcf

File tree

3 files changed

+75
-54
lines changed

3 files changed

+75
-54
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4931,7 +4931,12 @@ class ConstraintSystem {
49314931
/// Populate the prepared overload with all type variables and constraints
49324932
/// that are to be introduced into the constraint system when this choice
49334933
/// is taken.
4934-
DeclReferenceType
4934+
///
4935+
/// Returns a pair consisting of the opened type, and the thrown error type.
4936+
///
4937+
/// FIXME: As a transitional mechanism, if preparedOverload is nullptr, this
4938+
/// immediately performs all operations.
4939+
std::pair<Type, Type>
49354940
prepareOverloadImpl(OverloadChoice choice,
49364941
DeclContext *useDC,
49374942
ConstraintLocator *locator,

include/swift/Sema/PreparedOverload.h

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,38 +144,29 @@ class PreparedOverload final :
144144
using Change = PreparedOverloadChange;
145145

146146
private:
147+
Type OpenedType;
148+
Type ThrownErrorType;
147149
size_t Count;
148-
DeclReferenceType DeclType;
149150

150151
size_t numTrailingObjects(OverloadToken<Change>) const {
151152
return Count;
152153
}
153154

154155
public:
155-
PreparedOverload(const DeclReferenceType &declType, ArrayRef<Change> changes)
156-
: Count(changes.size()), DeclType(declType) {
156+
PreparedOverload(Type openedType, Type thrownErrorType,
157+
ArrayRef<Change> changes)
158+
: OpenedType(openedType), ThrownErrorType(thrownErrorType),
159+
Count(changes.size()) {
157160
std::uninitialized_copy(changes.begin(), changes.end(),
158161
getTrailingObjects<Change>());
159162
}
160163

161164
Type getOpenedType() const {
162-
return DeclType.openedType;
165+
return OpenedType;
163166
}
164167

165-
Type getAdjustedOpenedType() const {
166-
return DeclType.adjustedOpenedType;
167-
}
168-
169-
Type getReferenceType() const {
170-
return DeclType.referenceType;
171-
}
172-
173-
Type getAdjustedReferenceType() const {
174-
return DeclType.adjustedReferenceType;
175-
}
176-
177-
Type getThrownErrorTypeOnAccess() const {
178-
return DeclType.thrownErrorTypeOnAccess;
168+
Type getThrownErrorType() const {
169+
return ThrownErrorType;
179170
}
180171

181172
ArrayRef<Change> getChanges() const {

lib/Sema/TypeOfReference.cpp

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,7 +2625,7 @@ isInvalidPartialApplication(ConstraintSystem &cs,
26252625
/// checking semantics, compute the type of the reference. For now, follow
26262626
/// the lead of \c getTypeOfMemberReference and return a pair of
26272627
/// the full opened type and the reference's type.
2628-
static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
2628+
static Type getTypeOfReferenceWithSpecialTypeCheckingSemantics(
26292629
ConstraintSystem &CS, ConstraintLocator *locator,
26302630
DeclTypeCheckingSemantics semantics,
26312631
PreparedOverloadBuilder *preparedOverload) {
@@ -2654,8 +2654,7 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
26542654
/*isFavored=*/false, preparedOverload);
26552655
// FIXME: Verify ExtInfo state is correct, not working by accident.
26562656
FunctionType::ExtInfo info;
2657-
auto refType = FunctionType::get({inputArg}, output, info);
2658-
return {refType, refType, refType, refType, Type()};
2657+
return FunctionType::get({inputArg}, output, info);
26592658
}
26602659
case DeclTypeCheckingSemantics::WithoutActuallyEscaping: {
26612660
// Proceed with a "WithoutActuallyEscaping" operation. The body closure
@@ -2702,14 +2701,13 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
27022701
withoutEscapingIsolation = FunctionTypeIsolation::forNonIsolatedCaller();
27032702
}
27042703

2705-
auto refType = FunctionType::get(args, result,
2706-
FunctionType::ExtInfoBuilder()
2707-
.withNoEscape(false)
2708-
.withIsolation(withoutEscapingIsolation)
2709-
.withAsync(true)
2710-
.withThrows(true, thrownError)
2711-
.build());
2712-
return {refType, refType, refType, refType, Type()};
2704+
return FunctionType::get(args, result,
2705+
FunctionType::ExtInfoBuilder()
2706+
.withNoEscape(false)
2707+
.withIsolation(withoutEscapingIsolation)
2708+
.withAsync(true)
2709+
.withThrows(true, thrownError)
2710+
.build());
27132711
}
27142712
case DeclTypeCheckingSemantics::OpenExistential: {
27152713
// The body closure receives a freshly-opened archetype constrained by the
@@ -2755,14 +2753,13 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
27552753
openExistentialIsolation = FunctionTypeIsolation::forNonIsolatedCaller();
27562754
}
27572755

2758-
auto refType = FunctionType::get(args, result,
2759-
FunctionType::ExtInfoBuilder()
2760-
.withNoEscape(false)
2761-
.withThrows(true, thrownError)
2762-
.withIsolation(openExistentialIsolation)
2763-
.withAsync(true)
2764-
.build());
2765-
return {refType, refType, refType, refType, Type()};
2756+
return FunctionType::get(args, result,
2757+
FunctionType::ExtInfoBuilder()
2758+
.withNoEscape(false)
2759+
.withThrows(true, thrownError)
2760+
.withIsolation(openExistentialIsolation)
2761+
.withAsync(true)
2762+
.build());
27662763
}
27672764
}
27682765

@@ -2840,9 +2837,11 @@ void ConstraintSystem::replayChanges(
28402837
/// that are to be introduced into the constraint system when this choice
28412838
/// is taken.
28422839
///
2840+
/// Returns a pair consisting of the opened type, and the thrown error type.
2841+
///
28432842
/// FIXME: As a transitional mechanism, if preparedOverload is nullptr, this
28442843
/// immediately performs all operations.
2845-
DeclReferenceType
2844+
std::pair<Type, Type>
28462845
ConstraintSystem::prepareOverloadImpl(OverloadChoice choice,
28472846
DeclContext *useDC,
28482847
ConstraintLocator *locator,
@@ -2852,20 +2851,21 @@ ConstraintSystem::prepareOverloadImpl(OverloadChoice choice,
28522851
auto semantics =
28532852
TypeChecker::getDeclTypeCheckingSemantics(choice.getDecl());
28542853
if (semantics != DeclTypeCheckingSemantics::Normal) {
2855-
return getTypeOfReferenceWithSpecialTypeCheckingSemantics(
2854+
auto openedType = getTypeOfReferenceWithSpecialTypeCheckingSemantics(
28562855
*this, locator, semantics, preparedOverload);
2856+
return {openedType, Type()};
28572857
} else if (auto baseTy = choice.getBaseType()) {
28582858
// Retrieve the type of a reference to the specific declaration choice.
28592859
assert(!baseTy->hasTypeParameter());
28602860

28612861
// If the base is a module type, it's an unqualified reference.
28622862
if (baseTy->getMetatypeInstanceType()->is<ModuleType>()) {
2863-
return getTypeOfReference(choice, useDC, locator, preparedOverload);
2863+
return getTypeOfReferencePre(choice, useDC, locator, preparedOverload);
28642864
}
28652865

2866-
return getTypeOfMemberReference(choice, useDC, locator, preparedOverload);
2866+
return getTypeOfMemberReferencePre(choice, useDC, locator, preparedOverload);
28672867
} else {
2868-
return getTypeOfReference(choice, useDC, locator, preparedOverload);
2868+
return getTypeOfReferencePre(choice, useDC, locator, preparedOverload);
28692869
}
28702870
}
28712871

@@ -2876,15 +2876,18 @@ PreparedOverload *ConstraintSystem::prepareOverload(OverloadChoice choice,
28762876
PreparingOverload = true;
28772877

28782878
PreparedOverloadBuilder builder;
2879-
auto declRefType = prepareOverloadImpl(choice, useDC, locator, &builder);
2879+
Type openedType;
2880+
Type thrownErrorType;
2881+
std::tie(openedType, thrownErrorType) = prepareOverloadImpl(
2882+
choice, useDC, locator, &builder);
28802883

28812884
PreparingOverload = false;
28822885

28832886
size_t count = builder.Changes.size();
28842887
auto size = PreparedOverload::totalSizeToAlloc<PreparedOverload::Change>(count);
28852888
auto mem = Allocator.Allocate(size, alignof(PreparedOverload));
28862889

2887-
return new (mem) PreparedOverload(declRefType, builder.Changes);
2890+
return new (mem) PreparedOverload(openedType, thrownErrorType, builder.Changes);
28882891
}
28892892

28902893
void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC,
@@ -2895,7 +2898,7 @@ void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC
28952898
Type adjustedOpenedType;
28962899
Type refType;
28972900
Type adjustedRefType;
2898-
Type thrownErrorTypeOnAccess;
2901+
Type thrownErrorType;
28992902

29002903
switch (choice.getKind()) {
29012904
case OverloadChoiceKind::Decl:
@@ -2908,18 +2911,40 @@ void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC
29082911
replayChanges(locator, preparedOverload);
29092912

29102913
openedType = preparedOverload->getOpenedType();
2911-
adjustedOpenedType = preparedOverload->getAdjustedOpenedType();
2912-
refType = preparedOverload->getReferenceType();
2913-
adjustedRefType = preparedOverload->getAdjustedReferenceType();
2914-
thrownErrorTypeOnAccess = preparedOverload->getThrownErrorTypeOnAccess();
2914+
thrownErrorType = preparedOverload->getThrownErrorType();
29152915
} else {
2916-
auto declRefType = prepareOverloadImpl(choice, useDC, locator, nullptr);
2916+
std::tie(openedType, thrownErrorType) = prepareOverloadImpl(
2917+
choice, useDC, locator, nullptr);
2918+
}
2919+
2920+
auto semantics =
2921+
TypeChecker::getDeclTypeCheckingSemantics(choice.getDecl());
2922+
if (semantics != DeclTypeCheckingSemantics::Normal) {
2923+
adjustedOpenedType = openedType;
2924+
refType = openedType;
2925+
adjustedRefType = openedType;
2926+
} else {
2927+
DeclReferenceType declRefType;
2928+
2929+
if (auto baseTy = choice.getBaseType()) {
2930+
// If the base is a module type, it's an unqualified reference.
2931+
if (baseTy->getMetatypeInstanceType()->is<ModuleType>()) {
2932+
declRefType = getTypeOfReferencePost(
2933+
choice, useDC, locator, openedType, thrownErrorType);
2934+
} else {
2935+
declRefType = getTypeOfMemberReferencePost(
2936+
choice, useDC, locator, openedType, thrownErrorType);
2937+
}
2938+
} else {
2939+
declRefType = getTypeOfReferencePost(
2940+
choice, useDC, locator, openedType, thrownErrorType);
2941+
}
29172942

29182943
openedType = declRefType.openedType;
29192944
adjustedOpenedType = declRefType.adjustedOpenedType;
29202945
refType = declRefType.referenceType;
29212946
adjustedRefType = declRefType.adjustedReferenceType;
2922-
thrownErrorTypeOnAccess = declRefType.thrownErrorTypeOnAccess;
2947+
thrownErrorType = declRefType.thrownErrorTypeOnAccess;
29232948
}
29242949

29252950
break;
@@ -3110,9 +3135,9 @@ void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC
31103135

31113136
// If accessing this declaration could throw an error, record this as a
31123137
// potential throw site.
3113-
if (thrownErrorTypeOnAccess) {
3138+
if (thrownErrorType) {
31143139
recordPotentialThrowSite(
3115-
PotentialThrowSite::PropertyAccess, thrownErrorTypeOnAccess, locator);
3140+
PotentialThrowSite::PropertyAccess, thrownErrorType, locator);
31163141
}
31173142

31183143
// Note that we have resolved this overload.

0 commit comments

Comments
 (0)