Skip to content

Commit ad324b7

Browse files
committed
Sema: Clean up ConstraintSystem::resolveOverload() a bit
1 parent 8744e8a commit ad324b7

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

lib/Sema/TypeOfReference.cpp

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,11 +2891,7 @@ void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC
28912891
ConstraintLocator *locator, Type boundType,
28922892
PreparedOverload *preparedOverload) {
28932893
// Determine the type to which we'll bind the overload set's type.
2894-
Type openedType;
2895-
Type adjustedOpenedType;
2896-
Type refType;
2897-
Type adjustedRefType;
2898-
Type thrownErrorType;
2894+
DeclReferenceType declRefType;
28992895

29002896
switch (choice.getKind()) {
29012897
case OverloadChoiceKind::Decl:
@@ -2904,6 +2900,8 @@ void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC
29042900
case OverloadChoiceKind::DeclViaUnwrappedOptional:
29052901
case OverloadChoiceKind::DynamicMemberLookup:
29062902
case OverloadChoiceKind::KeyPathDynamicMemberLookup: {
2903+
Type openedType, thrownErrorType;
2904+
29072905
if (preparedOverload) {
29082906
replayChanges(locator, preparedOverload);
29092907

@@ -2917,25 +2915,19 @@ void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC
29172915
auto semantics =
29182916
TypeChecker::getDeclTypeCheckingSemantics(choice.getDecl());
29192917
if (semantics != DeclTypeCheckingSemantics::Normal) {
2920-
adjustedOpenedType = openedType;
2921-
refType = openedType;
2922-
adjustedRefType = openedType;
2918+
declRefType.openedType = openedType;
2919+
declRefType.adjustedOpenedType = openedType;
2920+
declRefType.referenceType = openedType;
2921+
declRefType.adjustedReferenceType = openedType;
2922+
declRefType.thrownErrorTypeOnAccess = thrownErrorType;
29232923
} else {
2924-
DeclReferenceType declRefType;
2925-
29262924
if (choice.getBaseType()) {
29272925
declRefType = getTypeOfMemberReferencePost(
29282926
choice, useDC, locator, openedType, thrownErrorType);
29292927
} else {
29302928
declRefType = getTypeOfReferencePost(
29312929
choice, useDC, locator, openedType, thrownErrorType);
29322930
}
2933-
2934-
openedType = declRefType.openedType;
2935-
adjustedOpenedType = declRefType.adjustedOpenedType;
2936-
refType = declRefType.referenceType;
2937-
adjustedRefType = declRefType.adjustedReferenceType;
2938-
thrownErrorType = declRefType.thrownErrorTypeOnAccess;
29392931
}
29402932

29412933
break;
@@ -2945,14 +2937,16 @@ void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC
29452937
if (auto lvalueTy = choice.getBaseType()->getAs<LValueType>()) {
29462938
// When the base of a tuple lvalue, the member is always an lvalue.
29472939
auto tuple = lvalueTy->getObjectType()->castTo<TupleType>();
2948-
adjustedRefType = tuple->getElementType(choice.getTupleIndex())->getRValueType();
2949-
adjustedRefType = LValueType::get(adjustedRefType);
2940+
declRefType.adjustedReferenceType =
2941+
LValueType::get(
2942+
tuple->getElementType(choice.getTupleIndex())->getRValueType());
29502943
} else {
29512944
// When the base is a tuple rvalue, the member is always an rvalue.
29522945
auto tuple = choice.getBaseType()->castTo<TupleType>();
2953-
adjustedRefType = tuple->getElementType(choice.getTupleIndex())->getRValueType();
2946+
declRefType.adjustedReferenceType =
2947+
tuple->getElementType(choice.getTupleIndex())->getRValueType();
29542948
}
2955-
refType = adjustedRefType;
2949+
declRefType.referenceType = declRefType.adjustedReferenceType;
29562950
break;
29572951

29582952
case OverloadChoiceKind::MaterializePack: {
@@ -2962,18 +2956,18 @@ void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC
29622956
// In the future, _if_ the syntax allows for multiple expansions
29632957
// this code would have to be adjusted to project l-value from the
29642958
// base type just like TupleIndex does.
2965-
adjustedRefType =
2959+
declRefType.adjustedReferenceType =
29662960
getPatternTypeOfSingleUnlabeledPackExpansionTuple(choice.getBaseType());
2967-
refType = adjustedRefType;
2961+
declRefType.referenceType = declRefType.adjustedReferenceType;
29682962
break;
29692963
}
29702964

29712965
case OverloadChoiceKind::ExtractFunctionIsolation: {
29722966
// The type of `.isolation` is `(any Actor)?`
29732967
auto actor = getASTContext().getProtocol(KnownProtocolKind::Actor);
2974-
adjustedRefType =
2968+
declRefType.adjustedReferenceType =
29752969
OptionalType::get(actor->getDeclaredExistentialType());
2976-
refType = adjustedRefType;
2970+
declRefType.referenceType = declRefType.adjustedReferenceType;
29772971
break;
29782972
}
29792973

@@ -3004,11 +2998,11 @@ void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC
30042998
// FIXME: Verify ExtInfo state is correct, not working by accident.
30052999
FunctionType::ExtInfo fullInfo;
30063000
auto fullTy = FunctionType::get({baseParam}, subscriptTy, fullInfo);
3007-
openedType = fullTy;
3008-
adjustedOpenedType = fullTy;
3001+
declRefType.openedType = fullTy;
3002+
declRefType.adjustedOpenedType = fullTy;
30093003
// FIXME: @preconcurrency
3010-
refType = subscriptTy;
3011-
adjustedRefType = subscriptTy;
3004+
declRefType.referenceType = subscriptTy;
3005+
declRefType.adjustedReferenceType = subscriptTy;
30123006

30133007
// Increase the score so that actual subscripts get preference.
30143008
// ...except if we're solving for code completion and the index expression
@@ -3021,8 +3015,9 @@ void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC
30213015
break;
30223016
}
30233017
}
3024-
assert(!refType->hasTypeParameter() && "Cannot have a dependent type here");
3025-
assert(!adjustedRefType->hasTypeParameter() &&
3018+
assert(!declRefType.referenceType->hasTypeParameter() &&
3019+
"Cannot have a dependent type here");
3020+
assert(!declRefType.adjustedReferenceType->hasTypeParameter() &&
30263021
"Cannot have a dependent type here");
30273022

30283023
if (auto *decl = choice.getDeclOrNull()) {
@@ -3042,7 +3037,7 @@ void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC
30423037
if (locator->isResultOfKeyPathDynamicMemberLookup() ||
30433038
locator->isKeyPathSubscriptComponent()) {
30443039
// Subscript type has a format of (Self[.Type) -> (Arg...) -> Result
3045-
auto declTy = adjustedOpenedType->castTo<FunctionType>();
3040+
auto declTy = declRefType.adjustedOpenedType->castTo<FunctionType>();
30463041
auto subscriptTy = declTy->getResult()->castTo<FunctionType>();
30473042
// If we have subscript, each of the arguments has to conform to
30483043
// Hashable, because it would be used as a component inside key path.
@@ -3114,7 +3109,7 @@ void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC
31143109

31153110
// The default type of the #isolation builtin macro is `(any Actor)?`
31163111
if (macro->getBuiltinKind() == BuiltinMacroKind::IsolationMacro) {
3117-
auto *fnType = openedType->getAs<FunctionType>();
3112+
auto *fnType = declRefType.openedType->getAs<FunctionType>();
31183113
auto actor = getASTContext().getProtocol(KnownProtocolKind::Actor);
31193114
addConstraint(
31203115
ConstraintKind::Defaultable, fnType->getResult(),
@@ -3126,14 +3121,20 @@ void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC
31263121

31273122
// If accessing this declaration could throw an error, record this as a
31283123
// potential throw site.
3129-
if (thrownErrorType) {
3124+
if (declRefType.thrownErrorTypeOnAccess) {
31303125
recordPotentialThrowSite(
3131-
PotentialThrowSite::PropertyAccess, thrownErrorType, locator);
3126+
PotentialThrowSite::PropertyAccess,
3127+
declRefType.thrownErrorTypeOnAccess,
3128+
locator);
31323129
}
31333130

31343131
// Note that we have resolved this overload.
31353132
auto overload = SelectedOverload{
3136-
choice, openedType, adjustedOpenedType, refType, adjustedRefType,
3133+
choice,
3134+
declRefType.openedType,
3135+
declRefType.adjustedOpenedType,
3136+
declRefType.referenceType,
3137+
declRefType.adjustedReferenceType,
31373138
boundType};
31383139
recordResolvedOverload(locator, overload);
31393140

@@ -3149,7 +3150,7 @@ void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC
31493150
log << "(overload set choice binding ";
31503151
boundType->print(log, PO);
31513152
log << " := ";
3152-
adjustedRefType->print(log, PO);
3153+
declRefType.adjustedReferenceType->print(log, PO);
31533154

31543155
auto openedAtLoc = getOpenedTypes(locator);
31553156
if (!openedAtLoc.empty()) {
@@ -3181,8 +3182,9 @@ void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC
31813182
OverloadChoiceKind::DeclViaDynamic)) {
31823183

31833184
// Strip curried 'self' parameters.
3184-
auto fromTy = openedType->castTo<AnyFunctionType>()->getResult();
3185-
auto toTy = refType;
3185+
auto fromTy = declRefType.openedType
3186+
->castTo<AnyFunctionType>()->getResult();
3187+
auto toTy = declRefType.referenceType;
31863188
if (!doesMemberRefApplyCurriedSelf(baseTy, decl)) {
31873189
toTy = toTy->castTo<AnyFunctionType>()->getResult();
31883190
}

0 commit comments

Comments
 (0)