Skip to content

Commit 03777c3

Browse files
committed
[Diagnostics] Remove ContextualFailure::diagnoseMissingFunctionCall.
Instead, always diagnose missing call errors via MissingCallFailure.
1 parent 9d0d521 commit 03777c3

File tree

4 files changed

+50
-56
lines changed

4 files changed

+50
-56
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,9 +2240,6 @@ bool ContextualFailure::diagnoseAsError() {
22402240
return false;
22412241
}
22422242

2243-
if (diagnoseMissingFunctionCall())
2244-
return true;
2245-
22462243
if (diagnoseExtraneousAssociatedValues())
22472244
return true;
22482245

@@ -2643,36 +2640,6 @@ void ContextualFailure::tryFixIts(InFlightDiagnostic &diagnostic) const {
26432640
return;
26442641
}
26452642

2646-
bool ContextualFailure::diagnoseMissingFunctionCall() const {
2647-
// Don't suggest inserting a function call if the function expression
2648-
// isn't written explicitly in the source code.
2649-
auto *anchor = getAsExpr(simplifyLocatorToAnchor(getLocator()));
2650-
if (!anchor || anchor->isImplicit())
2651-
return false;
2652-
2653-
if (getLocator()
2654-
->isLastElement<LocatorPathElt::UnresolvedMemberChainResult>())
2655-
return false;
2656-
2657-
auto *srcFT = getFromType()->getAs<FunctionType>();
2658-
if (!srcFT ||
2659-
!(srcFT->getParams().empty() ||
2660-
getLocator()->isLastElement<LocatorPathElt::PatternMatch>()))
2661-
return false;
2662-
2663-
auto toType = getToType();
2664-
if (toType->is<AnyFunctionType>() ||
2665-
!TypeChecker::isConvertibleTo(srcFT->getResult(), toType, getDC()))
2666-
return false;
2667-
2668-
emitDiagnostic(diag::missing_nullary_call, srcFT->getResult())
2669-
.highlight(getSourceRange())
2670-
.fixItInsertAfter(getSourceRange().End, "()");
2671-
2672-
tryComputedPropertyFixIts();
2673-
return true;
2674-
}
2675-
26762643
bool ContextualFailure::diagnoseExtraneousAssociatedValues() const {
26772644
if (auto match =
26782645
getLocator()->getLastElementAs<LocatorPathElt::PatternMatch>()) {
@@ -3094,7 +3061,7 @@ bool ContextualFailure::tryProtocolConformanceFixIt(
30943061
return true;
30953062
}
30963063

3097-
void ContextualFailure::tryComputedPropertyFixIts() const {
3064+
void MissingCallFailure::tryComputedPropertyFixIts() const {
30983065
if (!isExpr<ClosureExpr>(getAnchor()))
30993066
return;
31003067

@@ -3305,7 +3272,8 @@ bool MissingCallFailure::diagnoseAsError() {
33053272
if (isExpr<KeyPathExpr>(anchor))
33063273
return false;
33073274

3308-
auto path = getLocator()->getPath();
3275+
auto *locator = getLocator();
3276+
auto path = locator->getPath();
33093277
if (!path.empty()) {
33103278
const auto &last = path.back();
33113279

@@ -3314,8 +3282,17 @@ bool MissingCallFailure::diagnoseAsError() {
33143282
case ConstraintLocator::ApplyArgToParam: {
33153283
auto type = getType(anchor)->lookThroughAllOptionalTypes();
33163284
auto fnType = type->castTo<FunctionType>();
3285+
3286+
if (MissingArgumentsFailure::isMisplacedMissingArgument(getSolution(), locator)) {
3287+
ArgumentMismatchFailure failure(
3288+
getSolution(), fnType, fnType->getResult(), locator);
3289+
return failure.diagnoseMisplacedMissingArgument();
3290+
}
3291+
33173292
emitDiagnostic(diag::missing_nullary_call, fnType->getResult())
3293+
.highlight(getSourceRange())
33183294
.fixItInsertAfter(insertLoc, "()");
3295+
tryComputedPropertyFixIts();
33193296
return true;
33203297
}
33213298

@@ -4708,8 +4685,9 @@ bool MissingArgumentsFailure::isMisplacedMissingArgument(
47084685
auto *argLoc = solution.getConstraintLocator(
47094686
callLocator, LocatorPathElt::ApplyArgToParam(0, 0, argFlags));
47104687

4711-
if (!(hasFixFor(FixKind::AllowArgumentTypeMismatch, argLoc) &&
4712-
hasFixFor(FixKind::AddMissingArguments, callLocator)))
4688+
bool hasArgumentMismatch = hasFixFor(FixKind::AllowArgumentTypeMismatch, argLoc) ||
4689+
hasFixFor(FixKind::InsertCall, argLoc);
4690+
if (!(hasArgumentMismatch && hasFixFor(FixKind::AddMissingArguments, callLocator)))
47134691
return false;
47144692

47154693
auto *anchorExpr = getAsExpr(anchor);

lib/Sema/CSDiagnostics.h

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,6 @@ class ContextualFailure : public FailureDiagnostic {
633633
/// Diagnose failed conversion in a `CoerceExpr`.
634634
bool diagnoseCoercionToUnrelatedType() const;
635635

636-
/// If we're trying to convert something of type "() -> T" to T,
637-
/// then we probably meant to call the value.
638-
bool diagnoseMissingFunctionCall() const;
639-
640636
/// Diagnose cases where a pattern tried to match associated values but
641637
/// the enum case had none.
642638
bool diagnoseExtraneousAssociatedValues() const;
@@ -687,10 +683,6 @@ class ContextualFailure : public FailureDiagnostic {
687683
return resolveType(rawType)->getWithoutSpecifierType();
688684
}
689685

690-
/// Try to add a fix-it to convert a stored property into a computed
691-
/// property
692-
void tryComputedPropertyFixIts() const;
693-
694686
bool isIntegerType(Type type) const {
695687
return conformsToKnownProtocol(
696688
type, KnownProtocolKind::ExpressibleByIntegerLiteral);
@@ -1050,6 +1042,10 @@ class NonOptionalUnwrapFailure final : public FailureDiagnostic {
10501042
};
10511043

10521044
class MissingCallFailure final : public FailureDiagnostic {
1045+
/// Try to add a fix-it to convert a stored property into a computed
1046+
/// property
1047+
void tryComputedPropertyFixIts() const;
1048+
10531049
public:
10541050
MissingCallFailure(const Solution &solution, ConstraintLocator *locator)
10551051
: FailureDiagnostic(solution, locator) {}
@@ -1958,6 +1954,15 @@ class ArgumentMismatchFailure : public ContextualFailure {
19581954
/// result value.
19591955
bool diagnoseKeyPathAsFunctionResultMismatch() const;
19601956

1957+
/// Situations like this:
1958+
///
1959+
/// func foo(_: Int, _: String) {}
1960+
/// foo("")
1961+
///
1962+
/// Are currently impossible to fix correctly,
1963+
/// so we have to attend to that in diagnostics.
1964+
bool diagnoseMisplacedMissingArgument() const;
1965+
19611966
protected:
19621967
/// \returns The position of the argument being diagnosed, starting at 1.
19631968
unsigned getArgPosition() const { return Info.getArgPosition(); }
@@ -2042,15 +2047,6 @@ class ArgumentMismatchFailure : public ContextualFailure {
20422047
ParameterTypeFlags getParameterFlagsAtIndex(unsigned idx) const {
20432048
return Info.getParameterFlagsAtIndex(idx);
20442049
}
2045-
2046-
/// Situations like this:
2047-
///
2048-
/// func foo(_: Int, _: String) {}
2049-
/// foo("")
2050-
///
2051-
/// Are currently impossible to fix correctly,
2052-
/// so we have to attend to that in diagnostics.
2053-
bool diagnoseMisplacedMissingArgument() const;
20542050
};
20552051

20562052
/// Replace a coercion ('as') with a runtime checked cast ('as!' or 'as?').

lib/Sema/CSSimplify.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3664,6 +3664,16 @@ bool ConstraintSystem::repairFailures(
36643664
if (!anchor || anchor->isImplicit())
36653665
return false;
36663666

3667+
if (isArgumentOfPatternMatchingOperator(loc))
3668+
return false;
3669+
3670+
// Don't attempt this fix for trailing closures.
3671+
if (auto elt = loc->getLastElementAs<LocatorPathElt::ApplyArgToParam>()) {
3672+
auto argumentList = getArgumentList(loc);
3673+
if (argumentList->isTrailingClosureIndex(elt->getArgIdx()))
3674+
return false;
3675+
}
3676+
36673677
// If argument is a function type and all of its parameters have
36683678
// default values, let's see whether error is related to missing
36693679
// explicit call.
@@ -3703,7 +3713,14 @@ bool ConstraintSystem::repairFailures(
37033713
convertTo->isAny())
37043714
return false;
37053715

3706-
auto result = matchTypes(resultType, dstType, ConstraintKind::Conversion,
3716+
ConstraintKind matchKind;
3717+
if (resultType->is<TypeVariableType>()) {
3718+
matchKind = ConstraintKind::Equal;
3719+
} else {
3720+
matchKind = ConstraintKind::Conversion;
3721+
}
3722+
3723+
auto result = matchTypes(resultType, dstType, matchKind,
37073724
TypeMatchFlags::TMF_ApplyingFix, locator);
37083725

37093726
if (result.isSuccess()) {
@@ -4540,6 +4557,9 @@ bool ConstraintSystem::repairFailures(
45404557

45414558
case ConstraintLocator::ClosureBody:
45424559
case ConstraintLocator::ClosureResult: {
4560+
if (repairByInsertingExplicitCall(lhs, rhs))
4561+
break;
4562+
45434563
if (repairViaOptionalUnwrap(*this, lhs, rhs, matchKind, conversionsOrFixes,
45444564
locator))
45454565
return true;

test/type/types.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var d2 : () -> Int = { 4 }
1616

1717
var d3 : () -> Float = { 4 }
1818

19-
var d4 : () -> Int = { d2 } // expected-error{{function produces expected type 'Int'; did you mean to call it with '()'?}} {{26-26=()}}
19+
var d4 : () -> Int = { d2 } // expected-error{{function 'd2' was used as a property; add () to call it}} {{26-26=()}}
2020

2121
if #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) {
2222
var e0 : [Int]

0 commit comments

Comments
 (0)