@@ -131,165 +131,6 @@ FailureDiagnostic::getChoiceFor(ConstraintLocator *locator) const {
131131 return getOverloadChoiceIfAvailable (cs.getCalleeLocator (locator));
132132}
133133
134- static Type resolveInterfaceType (ConstraintSystem &cs, Type type) {
135- auto resolvedType = type.transform ([&](Type type) -> Type {
136- if (auto *tvt = type->getAs <TypeVariableType>()) {
137- // If this type variable is for a generic parameter, return that.
138- if (auto *gp = tvt->getImpl ().getGenericParameter ())
139- return gp;
140-
141- // Otherwise resolve its fixed type, mapped out of context.
142- if (auto fixed = cs.getFixedType (tvt))
143- return resolveInterfaceType (cs, fixed->mapTypeOutOfContext ());
144-
145- return cs.getRepresentative (tvt);
146- }
147- if (auto *dmt = type->getAs <DependentMemberType>()) {
148- // For a dependent member, first resolve the base.
149- auto newBase = resolveInterfaceType (cs, dmt->getBase ());
150-
151- // Then reconstruct using its associated type.
152- assert (dmt->getAssocType ());
153- return DependentMemberType::get (newBase, dmt->getAssocType ());
154- }
155- return type;
156- });
157-
158- assert (!resolvedType->hasArchetype ());
159- return resolvedType;
160- }
161-
162- // / Given an apply expr, returns true if it is expected to have a direct callee
163- // / overload, resolvable using `getChoiceFor`. Otherwise, returns false.
164- static bool shouldHaveDirectCalleeOverload (const CallExpr *callExpr) {
165- auto *fnExpr = callExpr->getDirectCallee ();
166-
167- // An apply of an apply/subscript doesn't have a direct callee.
168- if (isa<ApplyExpr>(fnExpr) || isa<SubscriptExpr>(fnExpr))
169- return false ;
170-
171- // Applies of closures don't have callee overloads.
172- if (isa<ClosureExpr>(fnExpr))
173- return false ;
174-
175- // No direct callee for a try!/try?.
176- if (isa<ForceTryExpr>(fnExpr) || isa<OptionalTryExpr>(fnExpr))
177- return false ;
178-
179- // If we have an intermediate cast, there's no direct callee.
180- if (isa<ExplicitCastExpr>(fnExpr))
181- return false ;
182-
183- // No direct callee for an if expr.
184- if (isa<IfExpr>(fnExpr))
185- return false ;
186-
187- // Assume that anything else would have a direct callee.
188- return true ;
189- }
190-
191- Optional<FunctionArgApplyInfo>
192- FailureDiagnostic::getFunctionArgApplyInfo (ConstraintSystem &cs,
193- ConstraintLocator *locator) {
194- auto *anchor = locator->getAnchor ();
195- auto path = locator->getPath ();
196-
197- // Look for the apply-arg-to-param element in the locator's path. We may
198- // have to look through other elements that are generated from an argument
199- // conversion such as GenericArgument for an optional-to-optional conversion,
200- // and OptionalPayload for a value-to-optional conversion.
201- auto iter = path.rbegin ();
202- auto applyArgElt = locator->findLast <LocatorPathElt::ApplyArgToParam>(iter);
203- if (!applyArgElt)
204- return None;
205-
206- auto nextIter = iter + 1 ;
207- assert (!locator->findLast <LocatorPathElt::ApplyArgToParam>(nextIter) &&
208- " Multiple ApplyArgToParam components?" );
209-
210- // Form a new locator that ends at the apply-arg-to-param element, and
211- // simplify it to get the full argument expression.
212- auto argPath = path.drop_back (iter - path.rbegin ());
213- auto *argLocator = cs.getConstraintLocator (
214- anchor, argPath, ConstraintLocator::getSummaryFlagsForPath (argPath));
215-
216- auto *argExpr = simplifyLocatorToAnchor (argLocator);
217-
218- // If we were unable to simplify down to the argument expression, we don't
219- // know what this is.
220- if (!argExpr)
221- return None;
222-
223- Optional<OverloadChoice> choice;
224- Type rawFnType;
225- auto *calleeLocator = cs.getCalleeLocator (argLocator);
226- if (auto overload = cs.findSelectedOverloadFor (calleeLocator)) {
227- // If we have resolved an overload for the callee, then use that to get the
228- // function type and callee.
229- choice = overload->choice ;
230- rawFnType = overload->openedType ;
231- } else {
232- // If we didn't resolve an overload for the callee, we should be dealing
233- // with a call of an arbitrary function expr.
234- if (auto *call = dyn_cast<CallExpr>(anchor)) {
235- assert (!shouldHaveDirectCalleeOverload (call) &&
236- " Should we have resolved a callee for this?" );
237- rawFnType = cs.getType (call->getFn ());
238- } else {
239- // FIXME: ArgumentMismatchFailure is currently used from CSDiag, meaning
240- // we can end up a BinaryExpr here with an unresolved callee. It should be
241- // possible to remove this once we've gotten rid of the old CSDiag logic
242- // and just assert that we have a CallExpr.
243- auto *apply = cast<ApplyExpr>(anchor);
244- rawFnType = cs.getType (apply->getFn ());
245- }
246- }
247-
248- // Try to resolve the function type by loading lvalues and looking through
249- // optional types, which can occur for expressions like `fn?(5)`.
250- auto *fnType = cs.simplifyType (rawFnType)
251- ->getRValueType ()
252- ->lookThroughAllOptionalTypes ()
253- ->getAs <FunctionType>();
254- if (!fnType)
255- return None;
256-
257- // Resolve the interface type for the function. Note that this may not be a
258- // function type, for example it could be a generic parameter.
259- Type fnInterfaceType;
260- auto *callee = choice ? choice->getDeclOrNull () : nullptr ;
261- if (callee && callee->hasInterfaceType ()) {
262- // If we have a callee with an interface type, we can use it. This is
263- // preferable to resolveInterfaceType, as this will allow us to get a
264- // GenericFunctionType for generic decls.
265- //
266- // Note that it's possible to find a callee without an interface type. This
267- // can happen for example with closure parameters, where the interface type
268- // isn't set until the solution is applied. In that case, use
269- // resolveInterfaceType.
270- fnInterfaceType = callee->getInterfaceType ();
271-
272- // Strip off the curried self parameter if necessary.
273- if (hasAppliedSelf (cs, *choice))
274- fnInterfaceType = fnInterfaceType->castTo <AnyFunctionType>()->getResult ();
275-
276- if (auto *fn = fnInterfaceType->getAs <AnyFunctionType>()) {
277- assert (fn->getNumParams () == fnType->getNumParams () &&
278- " Parameter mismatch?" );
279- (void )fn;
280- }
281- } else {
282- fnInterfaceType = resolveInterfaceType (cs, rawFnType);
283- }
284-
285- auto argIdx = applyArgElt->getArgIdx ();
286- auto paramIdx = applyArgElt->getParamIdx ();
287-
288- return FunctionArgApplyInfo (cs, argExpr, argIdx,
289- cs.simplifyType (cs.getType (argExpr)),
290- paramIdx, fnInterfaceType, fnType, callee);
291- }
292-
293134Type FailureDiagnostic::restoreGenericParameters (
294135 Type type,
295136 llvm::function_ref<void (GenericTypeParamType *, Type)> substitution) {
@@ -930,7 +771,7 @@ bool NoEscapeFuncToTypeConversionFailure::diagnoseParameterUse() const {
930771 // as an argument to another function which accepts @escaping
931772 // function at that position.
932773 auto &cs = getConstraintSystem ();
933- if (auto argApplyInfo = getFunctionArgApplyInfo (cs, getLocator ())) {
774+ if (auto argApplyInfo = cs. getFunctionArgApplyInfo (getLocator ())) {
934775 auto paramInterfaceTy = argApplyInfo->getParamInterfaceType ();
935776 if (paramInterfaceTy->isTypeParameter ()) {
936777 auto diagnoseGenericParamFailure = [&](GenericTypeParamDecl *decl) {
@@ -1139,7 +980,7 @@ void MissingOptionalUnwrapFailure::offerDefaultValueUnwrapFixIt(
1139980 return ;
1140981
1141982 auto &cs = getConstraintSystem ();
1142- if (auto argApplyInfo = getFunctionArgApplyInfo (cs, getLocator ()))
983+ if (auto argApplyInfo = cs. getFunctionArgApplyInfo (getLocator ()))
1143984 if (argApplyInfo->getParameterFlags ().isInOut ())
1144985 return ;
1145986
@@ -3802,7 +3643,7 @@ bool MissingArgumentsFailure::diagnoseAsError() {
38023643 // foo(bar) // `() -> Void` vs. `(Int) -> Void`
38033644 // ```
38043645 if (locator->isLastElement <LocatorPathElt::ApplyArgToParam>()) {
3805- auto info = *getFunctionArgApplyInfo (cs, locator);
3646+ auto info = *(cs. getFunctionArgApplyInfo ( locator) );
38063647
38073648 auto *argExpr = info.getArgExpr ();
38083649 emitDiagnostic (argExpr->getLoc (), diag::cannot_convert_argument_value,
@@ -4018,7 +3859,7 @@ bool MissingArgumentsFailure::diagnoseClosure(ClosureExpr *closure) {
40183859 auto *locator = getLocator ();
40193860 if (locator->isForContextualType ()) {
40203861 funcType = cs.getContextualType ()->getAs <FunctionType>();
4021- } else if (auto info = getFunctionArgApplyInfo (cs, locator)) {
3862+ } else if (auto info = cs. getFunctionArgApplyInfo (locator)) {
40223863 funcType = info->getParamType ()->getAs <FunctionType>();
40233864 } else if (locator->isLastElement <LocatorPathElt::ClosureResult>()) {
40243865 // Based on the locator we know this this is something like this:
@@ -4732,7 +4573,7 @@ SourceLoc InvalidUseOfAddressOf::getLoc() const {
47324573
47334574bool InvalidUseOfAddressOf::diagnoseAsError () {
47344575 auto &cs = getConstraintSystem ();
4735- if (auto argApplyInfo = getFunctionArgApplyInfo (cs, getLocator ())) {
4576+ if (auto argApplyInfo = cs. getFunctionArgApplyInfo (getLocator ())) {
47364577 if (!argApplyInfo->getParameterFlags ().isInOut ()) {
47374578 auto anchor = getAnchor ();
47384579 emitDiagnostic (anchor->getLoc (), diag::extra_address_of, getToType ())
@@ -5259,7 +5100,7 @@ bool InOutConversionFailure::diagnoseAsError() {
52595100
52605101 if (!path.empty () &&
52615102 path.back ().getKind () == ConstraintLocator::FunctionArgument) {
5262- if (auto argApplyInfo = getFunctionArgApplyInfo (cs, locator)) {
5103+ if (auto argApplyInfo = cs. getFunctionArgApplyInfo (locator)) {
52635104 emitDiagnostic (anchor->getLoc (), diag::cannot_convert_argument_value,
52645105 argApplyInfo->getArgType (), argApplyInfo->getParamType ());
52655106 } else {
0 commit comments