@@ -131,9 +131,7 @@ FailureDiagnostic::getChoiceFor(ConstraintLocator *locator) const {
131
131
return getOverloadChoiceIfAvailable (cs.getCalleeLocator (locator));
132
132
}
133
133
134
- Type FailureDiagnostic::resolveInterfaceType (Type type,
135
- bool reconstituteSugar) const {
136
- auto &cs = getConstraintSystem ();
134
+ static Type resolveInterfaceType (ConstraintSystem &cs, Type type) {
137
135
auto resolvedType = type.transform ([&](Type type) -> Type {
138
136
if (auto *tvt = type->getAs <TypeVariableType>()) {
139
137
// If this type variable is for a generic parameter, return that.
@@ -142,13 +140,13 @@ Type FailureDiagnostic::resolveInterfaceType(Type type,
142
140
143
141
// Otherwise resolve its fixed type, mapped out of context.
144
142
if (auto fixed = cs.getFixedType (tvt))
145
- return resolveInterfaceType (fixed->mapTypeOutOfContext ());
143
+ return resolveInterfaceType (cs, fixed->mapTypeOutOfContext ());
146
144
147
145
return cs.getRepresentative (tvt);
148
146
}
149
147
if (auto *dmt = type->getAs <DependentMemberType>()) {
150
148
// For a dependent member, first resolve the base.
151
- auto newBase = resolveInterfaceType (dmt->getBase ());
149
+ auto newBase = resolveInterfaceType (cs, dmt->getBase ());
152
150
153
151
// Then reconstruct using its associated type.
154
152
assert (dmt->getAssocType ());
@@ -158,8 +156,7 @@ Type FailureDiagnostic::resolveInterfaceType(Type type,
158
156
});
159
157
160
158
assert (!resolvedType->hasArchetype ());
161
- return reconstituteSugar ? resolvedType->reconstituteSugar (/* recursive*/ true )
162
- : resolvedType;
159
+ return resolvedType;
163
160
}
164
161
165
162
// / Given an apply expr, returns true if it is expected to have a direct callee
@@ -192,8 +189,8 @@ static bool shouldHaveDirectCalleeOverload(const CallExpr *callExpr) {
192
189
}
193
190
194
191
Optional<FunctionArgApplyInfo>
195
- FailureDiagnostic::getFunctionArgApplyInfo (ConstraintLocator *locator) const {
196
- auto &cs = getConstraintSystem ();
192
+ FailureDiagnostic::getFunctionArgApplyInfo (ConstraintSystem &cs,
193
+ ConstraintLocator *locator) {
197
194
auto *anchor = locator->getAnchor ();
198
195
auto path = locator->getPath ();
199
196
@@ -225,7 +222,8 @@ FailureDiagnostic::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
225
222
226
223
Optional<OverloadChoice> choice;
227
224
Type rawFnType;
228
- if (auto overload = getChoiceFor (argLocator)) {
225
+ auto *calleeLocator = cs.getCalleeLocator (argLocator);
226
+ if (auto overload = cs.findSelectedOverloadFor (calleeLocator)) {
229
227
// If we have resolved an overload for the callee, then use that to get the
230
228
// function type and callee.
231
229
choice = overload->choice ;
@@ -249,7 +247,8 @@ FailureDiagnostic::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
249
247
250
248
// Try to resolve the function type by loading lvalues and looking through
251
249
// optional types, which can occur for expressions like `fn?(5)`.
252
- auto *fnType = resolveType (rawFnType)
250
+ auto *fnType = cs.simplifyType (rawFnType)
251
+ ->getRValueType ()
253
252
->lookThroughAllOptionalTypes ()
254
253
->getAs <FunctionType>();
255
254
if (!fnType)
@@ -280,13 +279,14 @@ FailureDiagnostic::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
280
279
(void )fn;
281
280
}
282
281
} else {
283
- fnInterfaceType = resolveInterfaceType (rawFnType);
282
+ fnInterfaceType = resolveInterfaceType (cs, rawFnType);
284
283
}
285
284
286
285
auto argIdx = applyArgElt->getArgIdx ();
287
286
auto paramIdx = applyArgElt->getParamIdx ();
288
287
289
- return FunctionArgApplyInfo (cs, argExpr, argIdx, getType (argExpr),
288
+ return FunctionArgApplyInfo (cs, argExpr, argIdx,
289
+ cs.simplifyType (cs.getType (argExpr)),
290
290
paramIdx, fnInterfaceType, fnType, callee);
291
291
}
292
292
@@ -929,7 +929,8 @@ bool NoEscapeFuncToTypeConversionFailure::diagnoseParameterUse() const {
929
929
// Let's check whether this is a function parameter passed
930
930
// as an argument to another function which accepts @escaping
931
931
// function at that position.
932
- if (auto argApplyInfo = getFunctionArgApplyInfo (getLocator ())) {
932
+ auto &cs = getConstraintSystem ();
933
+ if (auto argApplyInfo = getFunctionArgApplyInfo (cs, getLocator ())) {
933
934
auto paramInterfaceTy = argApplyInfo->getParamInterfaceType ();
934
935
if (paramInterfaceTy->isTypeParameter ()) {
935
936
auto diagnoseGenericParamFailure = [&](GenericTypeParamDecl *decl) {
@@ -1137,7 +1138,8 @@ void MissingOptionalUnwrapFailure::offerDefaultValueUnwrapFixIt(
1137
1138
if (isa<InOutExpr>(anchor))
1138
1139
return ;
1139
1140
1140
- if (auto argApplyInfo = getFunctionArgApplyInfo (getLocator ()))
1141
+ auto &cs = getConstraintSystem ();
1142
+ if (auto argApplyInfo = getFunctionArgApplyInfo (cs, getLocator ()))
1141
1143
if (argApplyInfo->getParameterFlags ().isInOut ())
1142
1144
return ;
1143
1145
@@ -2963,8 +2965,12 @@ ContextualFailure::getDiagnosticFor(ContextualTypePurpose context,
2963
2965
bool TupleContextualFailure::diagnoseAsError () {
2964
2966
auto diagnostic = isNumElementsMismatch ()
2965
2967
? diag::tuple_types_not_convertible_nelts
2966
- : diag::tuple_types_not_convertible;
2967
- emitDiagnostic (getAnchor ()->getLoc (), diagnostic, getFromType (), getToType ());
2968
+ : getDiagnosticFor (getContextualTypePurpose (),
2969
+ /* forProtocol=*/ false );
2970
+ if (!diagnostic)
2971
+ return false ;
2972
+
2973
+ emitDiagnostic (getAnchor ()->getLoc (), *diagnostic, getFromType (), getToType ());
2968
2974
return true ;
2969
2975
}
2970
2976
@@ -3796,7 +3802,7 @@ bool MissingArgumentsFailure::diagnoseAsError() {
3796
3802
// foo(bar) // `() -> Void` vs. `(Int) -> Void`
3797
3803
// ```
3798
3804
if (locator->isLastElement <LocatorPathElt::ApplyArgToParam>()) {
3799
- auto info = *getFunctionArgApplyInfo (locator);
3805
+ auto info = *getFunctionArgApplyInfo (cs, locator);
3800
3806
3801
3807
auto *argExpr = info.getArgExpr ();
3802
3808
emitDiagnostic (argExpr->getLoc (), diag::cannot_convert_argument_value,
@@ -4012,7 +4018,7 @@ bool MissingArgumentsFailure::diagnoseClosure(ClosureExpr *closure) {
4012
4018
auto *locator = getLocator ();
4013
4019
if (locator->isForContextualType ()) {
4014
4020
funcType = cs.getContextualType ()->getAs <FunctionType>();
4015
- } else if (auto info = getFunctionArgApplyInfo (locator)) {
4021
+ } else if (auto info = getFunctionArgApplyInfo (cs, locator)) {
4016
4022
funcType = info->getParamType ()->getAs <FunctionType>();
4017
4023
} else if (locator->isLastElement <LocatorPathElt::ClosureResult>()) {
4018
4024
// Based on the locator we know this this is something like this:
@@ -4725,7 +4731,8 @@ SourceLoc InvalidUseOfAddressOf::getLoc() const {
4725
4731
}
4726
4732
4727
4733
bool InvalidUseOfAddressOf::diagnoseAsError () {
4728
- if (auto argApplyInfo = getFunctionArgApplyInfo (getLocator ())) {
4734
+ auto &cs = getConstraintSystem ();
4735
+ if (auto argApplyInfo = getFunctionArgApplyInfo (cs, getLocator ())) {
4729
4736
if (!argApplyInfo->getParameterFlags ().isInOut ()) {
4730
4737
auto anchor = getAnchor ();
4731
4738
emitDiagnostic (anchor->getLoc (), diag::extra_address_of, getToType ())
@@ -5245,18 +5252,19 @@ bool ThrowingFunctionConversionFailure::diagnoseAsError() {
5245
5252
}
5246
5253
5247
5254
bool InOutConversionFailure::diagnoseAsError () {
5255
+ auto &cs = getConstraintSystem ();
5248
5256
auto *anchor = getAnchor ();
5249
5257
auto *locator = getLocator ();
5250
5258
auto path = locator->getPath ();
5251
5259
5252
5260
if (!path.empty () &&
5253
5261
path.back ().getKind () == ConstraintLocator::FunctionArgument) {
5254
- if (auto argApplyInfo = getFunctionArgApplyInfo (locator)) {
5262
+ if (auto argApplyInfo = getFunctionArgApplyInfo (cs, locator)) {
5255
5263
emitDiagnostic (anchor->getLoc (), diag::cannot_convert_argument_value,
5256
5264
argApplyInfo->getArgType (), argApplyInfo->getParamType ());
5257
5265
} else {
5258
5266
assert (locator->findLast <LocatorPathElt::ContextualType>());
5259
- auto contextualType = getConstraintSystem () .getContextualType ();
5267
+ auto contextualType = cs .getContextualType ();
5260
5268
auto purpose = getContextualTypePurpose ();
5261
5269
auto diagnostic = getDiagnosticFor (purpose, /* forProtocol=*/ false );
5262
5270
0 commit comments