Skip to content

Commit 40b305e

Browse files
committed
[Diagnostics] Switch to using FunctionType::getParams instead of input type
Convert all of the usages of `FunctionType->getInput()` to use `Function->getParams()` instead which is a new preferred API.
1 parent 3717512 commit 40b305e

File tree

5 files changed

+137
-128
lines changed

5 files changed

+137
-128
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 103 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,8 +1805,8 @@ bool FailureDiagnosis::diagnoseGeneralConversionFailure(Constraint *constraint){
18051805
if (!srcFT->isNoEscape()) destExtInfo = destExtInfo.withNoEscape(false);
18061806
if (!srcFT->throws()) destExtInfo = destExtInfo.withThrows(false);
18071807
if (destExtInfo != destFT->getExtInfo())
1808-
toType = FunctionType::get(destFT->getInput(),
1809-
destFT->getResult(), destExtInfo);
1808+
toType = FunctionType::get(destFT->getParams(), destFT->getResult(),
1809+
destExtInfo);
18101810

18111811
// If this is a function conversion that discards throwability or
18121812
// noescape, emit a specific diagnostic about that.
@@ -2978,7 +2978,7 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
29782978
// If we're trying to convert something of type "() -> T" to T, then we
29792979
// probably meant to call the value.
29802980
if (auto srcFT = exprType->getAs<AnyFunctionType>()) {
2981-
if (srcFT->getInput()->isVoid() &&
2981+
if (srcFT->getParams().empty() &&
29822982
!isUnresolvedOrTypeVarType(srcFT->getResult()) &&
29832983
CS.TC.isConvertibleTo(srcFT->getResult(), contextualType, CS.DC)) {
29842984
diagnose(expr->getLoc(), diag::missing_nullary_call, srcFT->getResult())
@@ -3071,7 +3071,7 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
30713071
if (!srcFT->isNoEscape()) destExtInfo = destExtInfo.withNoEscape(false);
30723072
if (!srcFT->throws()) destExtInfo = destExtInfo.withThrows(false);
30733073
if (destExtInfo != destFT->getExtInfo())
3074-
contextualType = FunctionType::get(destFT->getInput(),
3074+
contextualType = FunctionType::get(destFT->getParams(),
30753075
destFT->getResult(), destExtInfo);
30763076

30773077
// If this is a function conversion that discards throwability or
@@ -3896,7 +3896,8 @@ diagnoseInstanceMethodAsCurriedMemberOnType(CalleeCandidateInfo &CCI,
38963896
}
38973897

38983898
static bool diagnoseTupleParameterMismatch(CalleeCandidateInfo &CCI,
3899-
Type paramType, Type argType,
3899+
ArrayRef<AnyFunctionType::Param> params,
3900+
ArrayRef<AnyFunctionType::Param> args,
39003901
Expr *fnExpr, Expr *argExpr,
39013902
bool isTopLevel = true) {
39023903
// Try to diagnose function call tuple parameter splat only if
@@ -3913,72 +3914,96 @@ static bool diagnoseTupleParameterMismatch(CalleeCandidateInfo &CCI,
39133914
}
39143915
}
39153916

3916-
if (auto *paramFnType = paramType->getAs<AnyFunctionType>()) {
3917-
// Only if both of the parameter and argument types are functions
3918-
// let's recur into diagnosing their arguments.
3919-
if (auto *argFnType = argType->getAs<AnyFunctionType>())
3920-
return diagnoseTupleParameterMismatch(CCI, paramFnType->getInput(),
3921-
argFnType->getInput(), fnExpr,
3922-
argExpr, /* isTopLevel */ false);
3923-
return false;
3917+
if (params.size() == 1 && args.size() == 1) {
3918+
auto paramType = params.front().getType();
3919+
auto argType = args.front().getType();
3920+
3921+
if (auto *paramFnType = paramType->getAs<AnyFunctionType>()) {
3922+
// Only if both of the parameter and argument types are functions
3923+
// let's recur into diagnosing their arguments.
3924+
if (auto *argFnType = argType->getAs<AnyFunctionType>())
3925+
return diagnoseTupleParameterMismatch(CCI, paramFnType->getParams(),
3926+
argFnType->getParams(), fnExpr,
3927+
argExpr, /* isTopLevel */ false);
3928+
return false;
3929+
}
39243930
}
39253931

3926-
unsigned parameterCount = 1, argumentCount = 1;
3932+
if (params.size() != 1 || args.empty())
3933+
return false;
39273934

3928-
// Don't try to desugar ParenType which is going to result in incorrect
3929-
// inferred argument/parameter count.
3935+
auto paramType = params.front().getType();
39303936

3931-
if (auto *paramTypeTy = dyn_cast<TupleType>(paramType.getPointer()))
3932-
parameterCount = paramTypeTy->getNumElements();
3937+
if (args.size() == 1) {
3938+
auto argType = args.front().getType();
3939+
if (auto *paramFnType = paramType->getAs<AnyFunctionType>()) {
3940+
// Only if both of the parameter and argument types are functions
3941+
// let's recur into diagnosing their arguments.
3942+
if (auto *argFnType = argType->getAs<AnyFunctionType>())
3943+
return diagnoseTupleParameterMismatch(CCI, paramFnType->getParams(),
3944+
argFnType->getParams(), fnExpr,
3945+
argExpr, /* isTopLevel */ false);
3946+
}
39333947

3934-
if (auto *argTupleTy = dyn_cast<TupleType>(argType.getPointer()))
3935-
argumentCount = argTupleTy->getNumElements();
3948+
return false;
3949+
}
39363950

3937-
if (parameterCount == 1 && argumentCount > 1) {
3938-
// Let's see if inferred argument is actually a tuple inside of Paren.
3939-
auto *paramTupleTy = paramType->getAs<TupleType>();
3940-
if (!paramTupleTy)
3941-
return false;
3951+
// Let's see if inferred argument is actually a tuple inside of Paren.
3952+
auto *paramTupleTy = paramType->getAs<TupleType>();
3953+
if (!paramTupleTy)
3954+
return false;
39423955

3943-
// Looks like the number of tuple elements matches number
3944-
// of function arguments, which means we can we can emit an
3945-
// error about an attempt to make use of tuple splat or tuple
3946-
// destructuring, unfortunately we can't provide a fix-it for
3947-
// this case.
3948-
if (paramTupleTy->getNumElements() == argumentCount) {
3949-
auto &TC = CCI.CS.TC;
3950-
if (isTopLevel) {
3951-
if (auto *decl = CCI[0].getDecl()) {
3952-
Identifier name;
3953-
auto kind = decl->getDescriptiveKind();
3954-
// Constructors/descructors and subscripts don't really have names.
3955-
if (!(isa<ConstructorDecl>(decl) || isa<DestructorDecl>(decl) ||
3956-
isa<SubscriptDecl>(decl))) {
3957-
name = decl->getBaseName().getIdentifier();
3958-
}
3956+
if (paramTupleTy->getNumElements() != args.size())
3957+
return false;
39593958

3960-
TC.diagnose(argExpr->getLoc(), diag::single_tuple_parameter_mismatch,
3961-
kind, name, paramType, !name.empty())
3962-
.highlight(argExpr->getSourceRange())
3963-
.fixItInsertAfter(argExpr->getStartLoc(), "(")
3964-
.fixItInsert(argExpr->getEndLoc(), ")");
3965-
} else {
3966-
TC.diagnose(argExpr->getLoc(),
3967-
diag::unknown_single_tuple_parameter_mismatch, paramType)
3968-
.highlight(argExpr->getSourceRange())
3969-
.fixItInsertAfter(argExpr->getStartLoc(), "(")
3970-
.fixItInsert(argExpr->getEndLoc(), ")");
3971-
}
3972-
} else {
3973-
TC.diagnose(argExpr->getLoc(),
3974-
diag::nested_tuple_parameter_destructuring, paramType,
3975-
CCI.CS.getType(fnExpr));
3959+
// Looks like the number of tuple elements matches number
3960+
// of function arguments, which means we can we can emit an
3961+
// error about an attempt to make use of tuple splat or tuple
3962+
// destructuring, unfortunately we can't provide a fix-it for
3963+
// this case.
3964+
auto &TC = CCI.CS.TC;
3965+
if (isTopLevel) {
3966+
if (auto *decl = CCI[0].getDecl()) {
3967+
Identifier name;
3968+
auto kind = decl->getDescriptiveKind();
3969+
// Constructors/descructors and subscripts don't really have names.
3970+
if (!(isa<ConstructorDecl>(decl) || isa<DestructorDecl>(decl) ||
3971+
isa<SubscriptDecl>(decl))) {
3972+
name = decl->getBaseName().getIdentifier();
39763973
}
3977-
return true;
3974+
3975+
TC.diagnose(argExpr->getLoc(), diag::single_tuple_parameter_mismatch,
3976+
kind, name, paramTupleTy, !name.empty())
3977+
.highlight(argExpr->getSourceRange())
3978+
.fixItInsertAfter(argExpr->getStartLoc(), "(")
3979+
.fixItInsert(argExpr->getEndLoc(), ")");
3980+
} else {
3981+
TC.diagnose(argExpr->getLoc(),
3982+
diag::unknown_single_tuple_parameter_mismatch, paramTupleTy)
3983+
.highlight(argExpr->getSourceRange())
3984+
.fixItInsertAfter(argExpr->getStartLoc(), "(")
3985+
.fixItInsert(argExpr->getEndLoc(), ")");
39783986
}
3987+
} else {
3988+
TC.diagnose(argExpr->getLoc(),
3989+
diag::nested_tuple_parameter_destructuring, paramTupleTy,
3990+
CCI.CS.getType(fnExpr));
39793991
}
39803992

3981-
return false;
3993+
return true;
3994+
}
3995+
3996+
static bool diagnoseTupleParameterMismatch(CalleeCandidateInfo &CCI,
3997+
Type paramType, Type argType,
3998+
Expr *fnExpr, Expr *argExpr,
3999+
bool isTopLevel = true) {
4000+
llvm::SmallVector<AnyFunctionType::Param, 4> params, args;
4001+
4002+
FunctionType::decomposeInput(paramType, params);
4003+
FunctionType::decomposeInput(argType, args);
4004+
4005+
return diagnoseTupleParameterMismatch(CCI, params, args, fnExpr, argExpr,
4006+
isTopLevel);
39824007
}
39834008

39844009
class ArgumentMatcher : public MatchCallArgumentListener {
@@ -4650,7 +4675,9 @@ bool FailureDiagnosis::diagnoseSubscriptErrors(SubscriptExpr *SE,
46504675
auto candType = baseType->getTypeOfMember(CS.DC->getParentModule(),
46514676
cand.getDecl(), nullptr);
46524677
if (auto *candFunc = candType->getAs<FunctionType>()) {
4653-
auto paramsType = candFunc->getInput();
4678+
auto paramsType = FunctionType::composeInput(CS.getASTContext(),
4679+
candFunc->getParams(),
4680+
false);
46544681
if (!typeCheckChildIndependently(
46554682
indexExpr, paramsType, CTP_CallArgument, TCC_ForceRecheck))
46564683
return true;
@@ -5227,25 +5254,11 @@ bool FailureDiagnosis::diagnoseTrailingClosureErrors(ApplyExpr *callExpr) {
52275254
if (!fnType)
52285255
continue;
52295256

5230-
auto paramType = fnType->getInput();
5231-
switch (paramType->getKind()) {
5232-
case TypeKind::Tuple: {
5233-
auto tuple = paramType->getAs<TupleType>();
5234-
if (tuple->getNumElements() != 1)
5235-
continue;
5236-
5237-
paramType = tuple->getElement(0).getType();
5238-
break;
5239-
}
5240-
5241-
case TypeKind::Paren:
5242-
paramType = paramType->getWithoutParens();
5243-
break;
5244-
5245-
default:
5257+
auto params = fnType->getParams();
5258+
if (params.size() != 1)
52465259
return false;
5247-
}
52485260

5261+
Type paramType = params.front().getType();
52495262
if (auto paramFnType = paramType->getAs<AnyFunctionType>()) {
52505263
auto closureType = CS.getType(closureExpr);
52515264
if (auto *argFnType = closureType->getAs<AnyFunctionType>()) {
@@ -5298,7 +5311,7 @@ bool FailureDiagnosis::diagnoseTrailingClosureErrors(ApplyExpr *callExpr) {
52985311
}
52995312
};
53005313

5301-
auto expectedArgType = FunctionType::get(fnType->getInput(), resultType,
5314+
auto expectedArgType = FunctionType::get(fnType->getParams(), resultType,
53025315
fnType->getExtInfo());
53035316

53045317
llvm::SaveAndRestore<DeclContext *> SavedDC(CS.DC, DC);
@@ -5582,10 +5595,11 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
55825595
calleeInfo))
55835596
return true;
55845597

5585-
Type argType; // Type of the argument list, if knowable.
5586-
if (auto FTy = fnType->getAs<AnyFunctionType>())
5587-
argType = FTy->getInput();
5588-
else if (auto MTT = fnType->getAs<AnyMetatypeType>()) {
5598+
Type argType; // argument list, if known.
5599+
if (auto FTy = fnType->getAs<AnyFunctionType>()) {
5600+
argType = FunctionType::composeInput(CS.getASTContext(), FTy->getParams(),
5601+
false);
5602+
} else if (auto MTT = fnType->getAs<AnyMetatypeType>()) {
55895603
// If we are constructing a tuple with initializer syntax, the expected
55905604
// argument list is the tuple type itself - and there is no initdecl.
55915605
auto instanceTy = MTT->getInstanceType();
@@ -6242,7 +6256,7 @@ bool FailureDiagnosis::diagnoseClosureExpr(
62426256
if (contextualType && contextualType->is<AnyFunctionType>()) {
62436257
auto fnType = contextualType->getAs<AnyFunctionType>();
62446258
auto *params = CE->getParameters();
6245-
Type inferredArgType = fnType->getInput();
6259+
auto inferredArgs = fnType->getParams();
62466260

62476261
// It is very common for a contextual type to disagree with the argument
62486262
// list built into the closure expr. This can be because the closure expr
@@ -6252,11 +6266,7 @@ bool FailureDiagnosis::diagnoseClosureExpr(
62526266
// { $0 + $1 }
62536267
// in either case, we want to produce nice and clear diagnostics.
62546268
unsigned actualArgCount = params->size();
6255-
unsigned inferredArgCount = 1;
6256-
// Don't try to desugar ParenType which is going to result in incorrect
6257-
// inferred argument count.
6258-
if (auto *argTupleTy = dyn_cast<TupleType>(inferredArgType.getPointer()))
6259-
inferredArgCount = argTupleTy->getNumElements();
6269+
unsigned inferredArgCount = inferredArgs.size();
62606270

62616271
if (actualArgCount != inferredArgCount) {
62626272
// If the closure didn't specify any arguments and it is in a context that
@@ -6288,8 +6298,9 @@ bool FailureDiagnosis::diagnoseClosureExpr(
62886298
}
62896299

62906300
if (inferredArgCount == 1 && actualArgCount > 1) {
6301+
auto *argTupleTy = inferredArgs.front().getType()->getAs<TupleType>();
62916302
// Let's see if inferred argument is actually a tuple inside of Paren.
6292-
if (auto *argTupleTy = inferredArgType->getAs<TupleType>()) {
6303+
if (argTupleTy) {
62936304
// Looks like the number of closure parameters matches number
62946305
// of inferred arguments, which means we can we can emit an
62956306
// error about an attempt to make use of tuple splat or tuple
@@ -6310,9 +6321,6 @@ bool FailureDiagnosis::diagnoseClosureExpr(
63106321
auto diag = diagnose(params->getStartLoc(),
63116322
diag::closure_tuple_parameter_destructuring,
63126323
argTupleTy);
6313-
Type actualArgType;
6314-
if (auto *actualFnType = CS.getType(CE)->getAs<AnyFunctionType>())
6315-
actualArgType = actualFnType->getInput();
63166324

63176325
auto *closureBody = CE->getBody();
63186326
if (!closureBody)
@@ -6432,7 +6440,8 @@ bool FailureDiagnosis::diagnoseClosureExpr(
64326440
// Okay, the wrong number of arguments was used, complain about that.
64336441
// Before doing so, strip attributes off the function type so that they
64346442
// don't confuse the issue.
6435-
fnType = FunctionType::get(fnType->getInput(), fnType->getResult());
6443+
fnType = FunctionType::get(fnType->getParams(), fnType->getResult(),
6444+
fnType->getExtInfo(), false);
64366445
auto diag = diagnose(
64376446
params->getStartLoc(), diag::closure_argument_list_tuple, fnType,
64386447
inferredArgCount, actualArgCount, (actualArgCount == 1));
@@ -7490,7 +7499,7 @@ bool FailureDiagnosis::diagnoseMemberFailures(
74907499
// call the function, e.g. in "a.b.c" where they had to write "a.b().c".
74917500
// Produce a specific diagnostic + fixit for this situation.
74927501
if (auto baseFTy = baseObjTy->getAs<AnyFunctionType>()) {
7493-
if (baseExpr && baseFTy->getInput()->isVoid()) {
7502+
if (baseExpr && baseFTy->getParams().empty()) {
74947503
SourceLoc insertLoc = baseExpr->getEndLoc();
74957504

74967505
if (auto *DRE = dyn_cast<DeclRefExpr>(baseExpr)) {

0 commit comments

Comments
 (0)