Skip to content

Commit 0fc9d1b

Browse files
committed
[Diagnostics] Move getFunctionArgApplyInfo to Solution
Instead of relying on constraint system having solution applied let's pass a solution directly to `getFunctionArgApplyInfo`.
1 parent af4afe3 commit 0fc9d1b

File tree

5 files changed

+46
-37
lines changed

5 files changed

+46
-37
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,7 @@ bool NoEscapeFuncToTypeConversionFailure::diagnoseParameterUse() const {
790790
// Let's check whether this is a function parameter passed
791791
// as an argument to another function which accepts @escaping
792792
// function at that position.
793-
auto &cs = getConstraintSystem();
794-
if (auto argApplyInfo = cs.getFunctionArgApplyInfo(getLocator())) {
793+
if (auto argApplyInfo = getFunctionArgApplyInfo(getLocator())) {
795794
auto paramInterfaceTy = argApplyInfo->getParamInterfaceType();
796795
if (paramInterfaceTy->isTypeParameter()) {
797796
auto diagnoseGenericParamFailure = [&](GenericTypeParamDecl *decl) {
@@ -1001,8 +1000,7 @@ void MissingOptionalUnwrapFailure::offerDefaultValueUnwrapFixIt(
10011000
if (!anchor || isa<InOutExpr>(anchor))
10021001
return;
10031002

1004-
auto &cs = getConstraintSystem();
1005-
if (auto argApplyInfo = cs.getFunctionArgApplyInfo(getLocator()))
1003+
if (auto argApplyInfo = getFunctionArgApplyInfo(getLocator()))
10061004
if (argApplyInfo->getParameterFlags().isInOut())
10071005
return;
10081006

@@ -3815,7 +3813,7 @@ bool MissingArgumentsFailure::diagnoseAsError() {
38153813
// foo(bar) // `() -> Void` vs. `(Int) -> Void`
38163814
// ```
38173815
if (locator->isLastElement<LocatorPathElt::ApplyArgToParam>()) {
3818-
auto info = *(cs.getFunctionArgApplyInfo(locator));
3816+
auto info = *(getFunctionArgApplyInfo(locator));
38193817

38203818
auto *argExpr = info.getArgExpr();
38213819
emitDiagnostic(argExpr->getLoc(), diag::cannot_convert_argument_value,
@@ -4040,13 +4038,12 @@ bool MissingArgumentsFailure::diagnoseSingleMissingArgument() const {
40404038
}
40414039

40424040
bool MissingArgumentsFailure::diagnoseClosure(ClosureExpr *closure) {
4043-
auto &cs = getConstraintSystem();
40444041
FunctionType *funcType = nullptr;
40454042

40464043
auto *locator = getLocator();
40474044
if (locator->isForContextualType()) {
40484045
funcType = getContextualType(locator->getAnchor())->getAs<FunctionType>();
4049-
} else if (auto info = cs.getFunctionArgApplyInfo(locator)) {
4046+
} else if (auto info = getFunctionArgApplyInfo(locator)) {
40504047
auto paramType = info->getParamType();
40514048
// Drop a single layer of optionality because argument could get injected
40524049
// into optional and that doesn't contribute to the problem.
@@ -4781,8 +4778,7 @@ SourceLoc InvalidUseOfAddressOf::getLoc() const {
47814778
}
47824779

47834780
bool InvalidUseOfAddressOf::diagnoseAsError() {
4784-
auto &cs = getConstraintSystem();
4785-
if (auto argApplyInfo = cs.getFunctionArgApplyInfo(getLocator())) {
4781+
if (auto argApplyInfo = getFunctionArgApplyInfo(getLocator())) {
47864782
if (!argApplyInfo->getParameterFlags().isInOut()) {
47874783
auto anchor = getAnchor();
47884784
emitDiagnostic(anchor->getLoc(), diag::extra_address_of, getToType())
@@ -5298,14 +5294,13 @@ bool ThrowingFunctionConversionFailure::diagnoseAsError() {
52985294
}
52995295

53005296
bool InOutConversionFailure::diagnoseAsError() {
5301-
auto &cs = getConstraintSystem();
53025297
auto *anchor = getAnchor();
53035298
auto *locator = getLocator();
53045299
auto path = locator->getPath();
53055300

53065301
if (!path.empty() &&
53075302
path.back().getKind() == ConstraintLocator::FunctionArgument) {
5308-
if (auto argApplyInfo = cs.getFunctionArgApplyInfo(locator)) {
5303+
if (auto argApplyInfo = getFunctionArgApplyInfo(locator)) {
53095304
emitDiagnostic(anchor->getLoc(), diag::cannot_convert_argument_value,
53105305
argApplyInfo->getArgType(), argApplyInfo->getParamType());
53115306
} else {

lib/Sema/CSDiagnostics.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ class FailureDiagnostic {
178178
return cs.getConstraintLocator(anchor, path);
179179
}
180180

181+
Optional<FunctionArgApplyInfo>
182+
getFunctionArgApplyInfo(ConstraintLocator *locator) const {
183+
return S.getFunctionArgApplyInfo(locator);
184+
}
185+
181186
/// \returns true is locator hasn't been simplified down to expression.
182187
bool hasComplexLocator() const { return HasComplexLocator; }
183188

@@ -1717,7 +1722,7 @@ class ArgumentMismatchFailure : public ContextualFailure {
17171722
ArgumentMismatchFailure(const Solution &solution, Type argType,
17181723
Type paramType, ConstraintLocator *locator)
17191724
: ContextualFailure(solution, argType, paramType, locator),
1720-
Info(*cs.getFunctionArgApplyInfo(getLocator())) {}
1725+
Info(*getFunctionArgApplyInfo(getLocator())) {}
17211726

17221727
bool diagnoseAsError() override;
17231728
bool diagnoseAsNote() override;

lib/Sema/CSFix.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ getStructuralTypeContext(const Solution &solution, ConstraintLocator *locator) {
268268
auto exprType = cs.getType(anchor);
269269
return std::make_tuple(cs.getContextualTypePurpose(anchor), exprType,
270270
contextualType);
271-
} else if (auto argApplyInfo = cs.getFunctionArgApplyInfo(locator)) {
271+
} else if (auto argApplyInfo = solution.getFunctionArgApplyInfo(locator)) {
272272
return std::make_tuple(CTP_CallArgument,
273273
argApplyInfo->getArgType(),
274274
argApplyInfo->getParamType());

lib/Sema/ConstraintSystem.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3547,13 +3547,20 @@ bool constraints::isAutoClosureArgument(Expr *argExpr) {
35473547

35483548
bool constraints::hasAppliedSelf(ConstraintSystem &cs,
35493549
const OverloadChoice &choice) {
3550+
return hasAppliedSelf(choice, [&cs](Type type) -> Type {
3551+
return cs.getFixedTypeRecursive(type, /*wantRValue=*/true);
3552+
});
3553+
}
3554+
3555+
bool constraints::hasAppliedSelf(const OverloadChoice &choice,
3556+
llvm::function_ref<Type(Type)> getFixedType) {
35503557
auto *decl = choice.getDeclOrNull();
35513558
if (!decl)
35523559
return false;
35533560

35543561
auto baseType = choice.getBaseType();
35553562
if (baseType)
3556-
baseType = cs.getFixedTypeRecursive(baseType, /*wantRValue=*/true);
3563+
baseType = getFixedType(baseType)->getRValueType();
35573564

35583565
// In most cases where we reference a declaration with a curried self
35593566
// parameter, it gets dropped from the type of the reference.
@@ -3715,18 +3722,16 @@ static bool shouldHaveDirectCalleeOverload(const CallExpr *callExpr) {
37153722
return true;
37163723
}
37173724

3718-
Type ConstraintSystem::resolveInterfaceType(Type type) const {
3725+
Type Solution::resolveInterfaceType(Type type) const {
37193726
auto resolvedType = type.transform([&](Type type) -> Type {
37203727
if (auto *tvt = type->getAs<TypeVariableType>()) {
37213728
// If this type variable is for a generic parameter, return that.
37223729
if (auto *gp = tvt->getImpl().getGenericParameter())
37233730
return gp;
37243731

37253732
// Otherwise resolve its fixed type, mapped out of context.
3726-
if (auto fixed = getFixedType(tvt))
3727-
return resolveInterfaceType(fixed->mapTypeOutOfContext());
3728-
3729-
return getRepresentative(tvt);
3733+
auto fixed = simplifyType(tvt);
3734+
return resolveInterfaceType(fixed->mapTypeOutOfContext());
37303735
}
37313736
if (auto *dmt = type->getAs<DependentMemberType>()) {
37323737
// For a dependent member, first resolve the base.
@@ -3744,7 +3749,7 @@ Type ConstraintSystem::resolveInterfaceType(Type type) const {
37443749
}
37453750

37463751
Optional<FunctionArgApplyInfo>
3747-
ConstraintSystem::getFunctionArgApplyInfo(ConstraintLocator *locator) {
3752+
Solution::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
37483753
auto *anchor = locator->getAnchor();
37493754
auto path = locator->getPath();
37503755

@@ -3764,8 +3769,7 @@ ConstraintSystem::getFunctionArgApplyInfo(ConstraintLocator *locator) {
37643769
// Form a new locator that ends at the apply-arg-to-param element, and
37653770
// simplify it to get the full argument expression.
37663771
auto argPath = path.drop_back(iter - path.rbegin());
3767-
auto *argLocator = getConstraintLocator(
3768-
anchor, argPath, ConstraintLocator::getSummaryFlagsForPath(argPath));
3772+
auto *argLocator = getConstraintLocator(anchor, argPath);
37693773

37703774
auto *argExpr = simplifyLocatorToAnchor(argLocator);
37713775

@@ -3777,7 +3781,7 @@ ConstraintSystem::getFunctionArgApplyInfo(ConstraintLocator *locator) {
37773781
Optional<OverloadChoice> choice;
37783782
Type rawFnType;
37793783
auto *calleeLocator = getCalleeLocator(argLocator);
3780-
if (auto overload = findSelectedOverloadFor(calleeLocator)) {
3784+
if (auto overload = getOverloadChoiceIfAvailable(calleeLocator)) {
37813785
// If we have resolved an overload for the callee, then use that to get the
37823786
// function type and callee.
37833787
choice = overload->choice;
@@ -3816,7 +3820,8 @@ ConstraintSystem::getFunctionArgApplyInfo(ConstraintLocator *locator) {
38163820
fnInterfaceType = callee->getInterfaceType();
38173821

38183822
// Strip off the curried self parameter if necessary.
3819-
if (hasAppliedSelf(*this, *choice))
3823+
if (hasAppliedSelf(
3824+
*choice, [this](Type type) -> Type { return simplifyType(type); }))
38203825
fnInterfaceType = fnInterfaceType->castTo<AnyFunctionType>()->getResult();
38213826

38223827
if (auto *fn = fnInterfaceType->getAs<AnyFunctionType>()) {
@@ -3831,9 +3836,10 @@ ConstraintSystem::getFunctionArgApplyInfo(ConstraintLocator *locator) {
38313836
auto argIdx = applyArgElt->getArgIdx();
38323837
auto paramIdx = applyArgElt->getParamIdx();
38333838

3834-
return FunctionArgApplyInfo(getParentExpr(argExpr), argExpr, argIdx,
3835-
simplifyType(getType(argExpr)),
3836-
paramIdx, fnInterfaceType, fnType, callee);
3839+
auto &cs = getConstraintSystem();
3840+
return FunctionArgApplyInfo(cs.getParentExpr(argExpr), argExpr, argIdx,
3841+
simplifyType(getType(argExpr)), paramIdx,
3842+
fnInterfaceType, fnType, callee);
38373843
}
38383844

38393845
bool constraints::isKnownKeyPathType(Type type) {

lib/Sema/ConstraintSystem.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,17 @@ class Solution {
984984
return known->second;
985985
}
986986

987+
/// Resolve type variables present in the raw type, using generic parameter
988+
/// types where possible.
989+
Type resolveInterfaceType(Type type) const;
990+
991+
/// For a given locator describing a function argument conversion, or a
992+
/// constraint within an argument conversion, returns information about the
993+
/// application of the argument to its parameter. If the locator is not
994+
/// for an argument conversion, returns \c None.
995+
Optional<FunctionArgApplyInfo>
996+
getFunctionArgApplyInfo(ConstraintLocator *) const;
997+
987998
SWIFT_DEBUG_DUMP;
988999

9891000
/// Dump this solution.
@@ -2103,16 +2114,6 @@ class ConstraintSystem {
21032114
return findSelectedOverloadFor(calleeLoc);
21042115
}
21052116

2106-
/// Resolve type variables present in the raw type, using generic parameter
2107-
/// types where possible.
2108-
Type resolveInterfaceType(Type type) const;
2109-
2110-
/// For a given locator describing a function argument conversion, or a
2111-
/// constraint within an argument conversion, returns information about the
2112-
/// application of the argument to its parameter. If the locator is not
2113-
/// for an argument conversion, returns \c None.
2114-
Optional<FunctionArgApplyInfo> getFunctionArgApplyInfo(ConstraintLocator *);
2115-
21162117
private:
21172118
unsigned assignTypeVariableID() {
21182119
return TypeCounter++;
@@ -4806,6 +4807,8 @@ bool isAutoClosureArgument(Expr *argExpr);
48064807
/// parameter being applied, meaning that it's dropped from the type of the
48074808
/// reference.
48084809
bool hasAppliedSelf(ConstraintSystem &cs, const OverloadChoice &choice);
4810+
bool hasAppliedSelf(const OverloadChoice &choice,
4811+
llvm::function_ref<Type(Type)> getFixedType);
48094812

48104813
/// Check whether type conforms to a given known protocol.
48114814
bool conformsToKnownProtocol(ConstraintSystem &cs, Type type,

0 commit comments

Comments
 (0)