@@ -201,12 +201,6 @@ class FailureDiagnosis :public ASTVisitor<FailureDiagnosis, /*exprresult*/bool>{
201
201
ContextualTypePurpose CTP,
202
202
Type suggestedType = Type());
203
203
204
- // / Attempt to produce a diagnostic for a mismatch between a call's
205
- // / type and its assumed contextual type.
206
- bool diagnoseCallContextualConversionErrors (ApplyExpr *callEpxr,
207
- Type contextualType,
208
- ContextualTypePurpose CTP);
209
-
210
204
bool diagnoseImplicitSelfErrors (Expr *fnExpr, Expr *argExpr,
211
205
CalleeCandidateInfo &CCI,
212
206
ArrayRef<Identifier> argLabels);
@@ -1671,36 +1665,6 @@ namespace {
1671
1665
};
1672
1666
} // end anonymous namespace
1673
1667
1674
- // / Check if there failure associated with expression is related
1675
- // / to given contextual type.
1676
- bool FailureDiagnosis::diagnoseCallContextualConversionErrors (
1677
- ApplyExpr *callExpr, Type contextualType, ContextualTypePurpose CTP) {
1678
- if (!contextualType || contextualType->hasUnresolvedType ())
1679
- return false ;
1680
-
1681
- auto typeCheckExpr = [&](Expr *expr, DeclContext *DC,
1682
- SmallPtrSetImpl<TypeBase *> &types) {
1683
- getPossibleTypesOfExpressionWithoutApplying (
1684
- expr, DC, types, FreeTypeVariableBinding::Disallow);
1685
- };
1686
-
1687
- // First let's type-check expression without contextual type, and
1688
- // see if that's going to produce a type, if so, let's type-check
1689
- // again, this time using given contextual type.
1690
- SmallPtrSet<TypeBase *, 4 > withoutContextual;
1691
- typeCheckExpr (callExpr, CS.DC , withoutContextual);
1692
-
1693
- // If there are no types returned, it means that problem was
1694
- // nothing to do with contextual information, probably parameter/argument
1695
- // mismatch.
1696
- if (withoutContextual.empty ())
1697
- return false ;
1698
-
1699
- Type exprType = withoutContextual.size () == 1 ? *withoutContextual.begin () : Type ();
1700
- return diagnoseContextualConversionError (callExpr, contextualType, CTP,
1701
- exprType);
1702
- }
1703
-
1704
1668
// Check if there is a structural problem in the function expression
1705
1669
// by performing type checking with the option to allow unresolved
1706
1670
// type variables. If that is going to produce a function type with
@@ -1731,45 +1695,8 @@ static bool shouldTypeCheckFunctionExpr(FailureDiagnosis &FD, DeclContext *DC,
1731
1695
return true ;
1732
1696
}
1733
1697
1734
- // Check if any candidate of the overload set can accept a specified
1735
- // number of arguments, regardless of parameter type or label information.
1736
- static bool isViableOverloadSet (const CalleeCandidateInfo &CCI,
1737
- size_t numArgs) {
1738
- for (unsigned i = 0 ; i < CCI.size (); ++i) {
1739
- auto &&cand = CCI[i];
1740
- auto funcDecl = dyn_cast_or_null<AbstractFunctionDecl>(cand.getDecl ());
1741
-
1742
- // If we don't have a func decl or we haven't resolved its parameters,
1743
- // continue. The latter case can occur with `type(of:)`, which is introduced
1744
- // as a type variable.
1745
- if (!funcDecl || !cand.hasParameters ())
1746
- continue ;
1747
-
1748
- auto params = cand.getParameters ();
1749
- bool hasVariadicParameter = false ;
1750
- auto pairMatcher = [&](unsigned argIdx, unsigned paramIdx) {
1751
- hasVariadicParameter |= params[paramIdx].isVariadic ();
1752
- return true ;
1753
- };
1754
-
1755
- auto paramInfo = cand.getParameterListInfo (params);
1756
- InputMatcher IM (params, paramInfo);
1757
- auto result = IM.match (numArgs, pairMatcher);
1758
- if (result == InputMatcher::IM_Succeeded)
1759
- return true ;
1760
- if (result == InputMatcher::IM_HasUnclaimedInput && hasVariadicParameter)
1761
- return true ;
1762
- }
1763
- return false ;
1764
- }
1765
-
1766
1698
bool FailureDiagnosis::visitApplyExpr (ApplyExpr *callExpr) {
1767
- if (diagnoseCallContextualConversionErrors (callExpr, CS.getContextualType (),
1768
- CS.getContextualTypePurpose ()))
1769
- return true ;
1770
-
1771
1699
auto *fnExpr = callExpr->getFn ();
1772
- auto originalFnType = CS.getType (callExpr->getFn ());
1773
1700
1774
1701
if (shouldTypeCheckFunctionExpr (*this , CS.DC , fnExpr)) {
1775
1702
// Type check the function subexpression to resolve a type for it if
@@ -1792,162 +1719,12 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
1792
1719
1793
1720
auto fnType = getFuncType (CS.getType (fnExpr));
1794
1721
1795
- // Let's see if this has to do with member vs. property error
1796
- // because sometimes when there is a member and a property declared
1797
- // on the nominal type with the same name. Type-checking function
1798
- // expression separately from arguments might produce solution for
1799
- // the property instead of the member.
1800
- if (!fnType->is <AnyFunctionType>() &&
1801
- isa<UnresolvedDotExpr>(callExpr->getFn ())) {
1802
- fnExpr = callExpr->getFn ();
1803
-
1804
- SmallPtrSet<TypeBase *, 4 > types;
1805
- getPossibleTypesOfExpressionWithoutApplying (fnExpr, CS.DC , types);
1806
-
1807
- auto isFunctionType = [getFuncType](Type type) -> bool {
1808
- return type && getFuncType (type)->is <AnyFunctionType>();
1809
- };
1810
-
1811
- auto fnTypes = std::find_if (types.begin (), types.end (), isFunctionType);
1812
- if (fnTypes != types.end ()) {
1813
- auto funcType = getFuncType (*fnTypes);
1814
- // If there is only one function type, let's use it.
1815
- if (std::none_of (std::next (fnTypes), types.end (), isFunctionType))
1816
- fnType = funcType;
1817
- } else {
1818
- fnType = getFuncType (originalFnType);
1819
- }
1820
- }
1821
-
1822
- // If we have a contextual type, and if we have an ambiguously typed function
1823
- // result from our previous check, we re-type-check it using this contextual
1824
- // type to inform the result type of the callee.
1825
- //
1826
- // We only do this as a second pass because the first pass we just did may
1827
- // return something of obviously non-function-type. If this happens, we
1828
- // produce better diagnostics below by diagnosing this here rather than trying
1829
- // to peel apart the failed conversion to function type.
1830
- if (CS.getContextualType () &&
1831
- (isUnresolvedOrTypeVarType (fnType) ||
1832
- (fnType->is <AnyFunctionType>() && fnType->hasUnresolvedType ()))) {
1833
- // FIXME: Prevent typeCheckChildIndependently from transforming expressions,
1834
- // because if we try to typecheck OSR expression with contextual type,
1835
- // it'll end up converting it into DeclRefExpr based on contextual info,
1836
- // instead let's try to get a type without applying and filter callee
1837
- // candidates later on.
1838
- CalleeListener listener (CS.getContextualType ());
1839
-
1840
- if (isa<OverloadSetRefExpr>(fnExpr)) {
1841
- assert (!cast<OverloadSetRefExpr>(fnExpr)->getReferencedDecl () &&
1842
- " unexpected declaration reference" );
1843
-
1844
- ConcreteDeclRef decl = nullptr ;
1845
- Type type = TypeChecker::getTypeOfExpressionWithoutApplying (
1846
- fnExpr, CS.DC , decl, FreeTypeVariableBinding::UnresolvedType,
1847
- &listener);
1848
-
1849
- if (type)
1850
- fnType = getFuncType (type);
1851
- } else {
1852
- fnExpr = typeCheckChildIndependently (callExpr->getFn (), Type (),
1853
- CTP_CalleeResult, TCC_ForceRecheck,
1854
- &listener);
1855
- if (!fnExpr)
1856
- return true ;
1857
-
1858
- fnType = getFuncType (CS.getType (fnExpr));
1859
- }
1860
- }
1861
-
1862
- // If we resolved a concrete expression for the callee, and it has
1863
- // non-function/non-metatype type, then we cannot call it!
1864
- if (!isUnresolvedOrTypeVarType (fnType) &&
1865
- !fnType->is <AnyFunctionType>() && !fnType->is <MetatypeType>()) {
1866
- auto arg = callExpr->getArg ();
1867
-
1868
- // If the argument is a trailing ClosureExpr (i.e. {....}) and it is on
1869
- // the line after the callee, then it's likely the user forgot to
1870
- // write "do" before their brace stmt.
1871
- // Note that line differences of more than 1 are diagnosed during parsing.
1872
- if (auto *PE = dyn_cast<ParenExpr>(arg)) {
1873
- if (PE->hasTrailingClosure () && isa<ClosureExpr>(PE->getSubExpr ())) {
1874
- auto *closure = cast<ClosureExpr>(PE->getSubExpr ());
1875
- auto &SM = CS.getASTContext ().SourceMgr ;
1876
- if (closure->hasAnonymousClosureVars () &&
1877
- closure->getParameters ()->size () == 0 &&
1878
- 1 + SM.getLineNumber (callExpr->getFn ()->getEndLoc ()) ==
1879
- SM.getLineNumber (closure->getStartLoc ())) {
1880
- diagnose (closure->getStartLoc (), diag::brace_stmt_suggest_do)
1881
- .fixItInsert (closure->getStartLoc (), " do " );
1882
- return true ;
1883
- }
1884
- }
1885
- }
1886
-
1887
- auto isExistentialMetatypeType = fnType->is <ExistentialMetatypeType>();
1888
- if (isExistentialMetatypeType) {
1889
- auto diag = diagnose (arg->getStartLoc (),
1890
- diag::missing_init_on_metatype_initialization);
1891
- diag.highlight (fnExpr->getSourceRange ());
1892
- return true ;
1893
- } else {
1894
- auto diag = diagnose (arg->getStartLoc (),
1895
- diag::cannot_call_non_function_value, fnType);
1896
- diag.highlight (fnExpr->getSourceRange ());
1897
-
1898
- // If the argument is an empty tuple, then offer a
1899
- // fix-it to remove the empty tuple and use the value
1900
- // directly.
1901
- if (auto tuple = dyn_cast<TupleExpr>(arg)) {
1902
- if (tuple->getNumElements () == 0 ) {
1903
- diag.fixItRemove (arg->getSourceRange ());
1904
- }
1905
- }
1906
- return true ;
1907
- }
1908
- }
1909
-
1910
1722
bool hasTrailingClosure = callArgHasTrailingClosure (callExpr->getArg ());
1911
1723
1912
1724
// Collect a full candidate list of callees based on the partially type
1913
1725
// checked function.
1914
1726
CalleeCandidateInfo calleeInfo (fnExpr, hasTrailingClosure, CS);
1915
1727
1916
- // In the case that function subexpression was resolved independently in
1917
- // the first place, the resolved type may not provide the best diagnostic.
1918
- // We consider the number of arguments to decide whether we'd go with it or
1919
- // stay with the original one.
1920
- if (fnExpr != callExpr->getFn ()) {
1921
- bool isInstanceMethodAsCurriedMemberOnType = false ;
1922
- if (!calleeInfo.empty ()) {
1923
- auto &&cand = calleeInfo[0 ];
1924
- auto decl = cand.getDecl ();
1925
- if (decl && decl->isInstanceMember () && !cand.skipCurriedSelf &&
1926
- cand.getParameters ().size () == 1 )
1927
- isInstanceMethodAsCurriedMemberOnType = true ;
1928
- }
1929
-
1930
- // In terms of instance method as curried member on type, we should not
1931
- // take the number of arguments into account.
1932
- if (!isInstanceMethodAsCurriedMemberOnType) {
1933
- size_t numArgs = 1 ;
1934
- auto arg = callExpr->getArg ();
1935
- if (auto tuple = dyn_cast<TupleExpr>(arg)) {
1936
- numArgs = tuple->getNumElements ();
1937
- }
1938
-
1939
- if (!isViableOverloadSet (calleeInfo, numArgs)) {
1940
- CalleeCandidateInfo calleeInfoOrig (callExpr->getFn (),
1941
- hasTrailingClosure, CS);
1942
- if (isViableOverloadSet (calleeInfoOrig, numArgs)) {
1943
- fnExpr = callExpr->getFn ();
1944
- fnType = getFuncType (CS.getType (fnExpr));
1945
- calleeInfo = calleeInfoOrig;
1946
- }
1947
- }
1948
- }
1949
- }
1950
-
1951
1728
// Filter list of the candidates based on the known function type.
1952
1729
if (auto fn = fnType->getAs <AnyFunctionType>()) {
1953
1730
using Closeness = CalleeCandidateInfo::ClosenessResultTy;
0 commit comments