Skip to content

Commit 2f325fa

Browse files
committed
[Diagnostics] Don't store contextual type in missing argument fix/diagnostic
Instead of storing contextual function type in the fix/diagnostic, let's fetch it from context (solution and/or locator) because it's only used when it is a trailing closure missing some arguments anyway.
1 parent 387d2a9 commit 2f325fa

File tree

6 files changed

+37
-33
lines changed

6 files changed

+37
-33
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4092,7 +4092,7 @@ bool FailureDiagnosis::diagnoseClosureExpr(
40924092
}
40934093

40944094
MissingArgumentsFailure failure(
4095-
expr, CS, fnType, inferredArgCount - actualArgCount,
4095+
expr, CS, inferredArgCount - actualArgCount,
40964096
CS.getConstraintLocator(CE, LocatorPathElt::ContextualType()));
40974097
return failure.diagnoseAsError();
40984098
}

lib/Sema/CSDiagnostics.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3511,7 +3511,20 @@ bool MissingArgumentsFailure::diagnoseAsError() {
35113511
}
35123512

35133513
bool MissingArgumentsFailure::diagnoseTrailingClosure(ClosureExpr *closure) {
3514-
auto diff = Fn->getNumParams() - NumSynthesized;
3514+
auto &cs = getConstraintSystem();
3515+
FunctionType *funcType = nullptr;
3516+
3517+
auto *locator = getLocator();
3518+
if (locator->isForContextualType()) {
3519+
funcType = cs.getContextualType()->getAs<FunctionType>();
3520+
} else if (auto info = getFunctionArgApplyInfo(locator)) {
3521+
funcType = info->getParamType()->getAs<FunctionType>();
3522+
}
3523+
3524+
if (!funcType)
3525+
return false;
3526+
3527+
auto diff = funcType->getNumParams() - NumSynthesized;
35153528

35163529
// If the closure didn't specify any arguments and it is in a context that
35173530
// needs some, produce a fixit to turn "{...}" into "{ _,_ in ...}".
@@ -3521,10 +3534,10 @@ bool MissingArgumentsFailure::diagnoseTrailingClosure(ClosureExpr *closure) {
35213534
diag::closure_argument_list_missing, NumSynthesized);
35223535

35233536
std::string fixText; // Let's provide fixits for up to 10 args.
3524-
if (Fn->getNumParams() <= 10) {
3537+
if (funcType->getNumParams() <= 10) {
35253538
fixText += " ";
35263539
interleave(
3527-
Fn->getParams(),
3540+
funcType->getParams(),
35283541
[&fixText](const AnyFunctionType::Param &param) { fixText += '_'; },
35293542
[&fixText] { fixText += ','; });
35303543
fixText += " in ";
@@ -3550,9 +3563,9 @@ bool MissingArgumentsFailure::diagnoseTrailingClosure(ClosureExpr *closure) {
35503563
std::all_of(params->begin(), params->end(),
35513564
[](ParamDecl *param) { return !param->hasName(); });
35523565

3553-
auto diag =
3554-
emitDiagnostic(params->getStartLoc(), diag::closure_argument_list_tuple,
3555-
resolveType(Fn), Fn->getNumParams(), diff, diff == 1);
3566+
auto diag = emitDiagnostic(
3567+
params->getStartLoc(), diag::closure_argument_list_tuple,
3568+
resolveType(funcType), funcType->getNumParams(), diff, diff == 1);
35563569

35573570
// If the number of parameters is less than number of inferred
35583571
// let's try to suggest a fix-it with the rest of the missing parameters.

lib/Sema/CSDiagnostics.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,16 +1182,12 @@ class ImplicitInitOnNonConstMetatypeFailure final
11821182
class MissingArgumentsFailure final : public FailureDiagnostic {
11831183
using Param = AnyFunctionType::Param;
11841184

1185-
FunctionType *Fn;
11861185
unsigned NumSynthesized;
11871186

11881187
public:
11891188
MissingArgumentsFailure(Expr *root, ConstraintSystem &cs,
1190-
FunctionType *funcType,
1191-
unsigned numSynthesized,
1192-
ConstraintLocator *locator)
1193-
: FailureDiagnostic(root, cs, locator), Fn(funcType),
1194-
NumSynthesized(numSynthesized) {}
1189+
unsigned numSynthesized, ConstraintLocator *locator)
1190+
: FailureDiagnostic(root, cs, locator), NumSynthesized(numSynthesized) {}
11951191

11961192
bool diagnoseAsError() override;
11971193

lib/Sema/CSFix.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,18 +477,18 @@ AllowClosureParamDestructuring::create(ConstraintSystem &cs,
477477
}
478478

479479
bool AddMissingArguments::diagnose(Expr *root, bool asNote) const {
480-
MissingArgumentsFailure failure(root, getConstraintSystem(), Fn,
481-
NumSynthesized, getLocator());
480+
auto &cs = getConstraintSystem();
481+
MissingArgumentsFailure failure(root, cs, NumSynthesized, getLocator());
482482
return failure.diagnose(asNote);
483483
}
484484

485485
AddMissingArguments *
486-
AddMissingArguments::create(ConstraintSystem &cs, FunctionType *funcType,
486+
AddMissingArguments::create(ConstraintSystem &cs,
487487
llvm::ArrayRef<Param> synthesizedArgs,
488488
ConstraintLocator *locator) {
489489
unsigned size = totalSizeToAlloc<Param>(synthesizedArgs.size());
490490
void *mem = cs.getAllocator().Allocate(size, alignof(AddMissingArguments));
491-
return new (mem) AddMissingArguments(cs, funcType, synthesizedArgs, locator);
491+
return new (mem) AddMissingArguments(cs, synthesizedArgs, locator);
492492
}
493493

494494
bool MoveOutOfOrderArgument::diagnose(Expr *root, bool asNote) const {

lib/Sema/CSFix.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,13 +1001,12 @@ class AddMissingArguments final
10011001

10021002
using Param = AnyFunctionType::Param;
10031003

1004-
FunctionType *Fn;
10051004
unsigned NumSynthesized;
10061005

1007-
AddMissingArguments(ConstraintSystem &cs, FunctionType *funcType,
1008-
llvm::ArrayRef<AnyFunctionType::Param> synthesizedArgs,
1006+
AddMissingArguments(ConstraintSystem &cs,
1007+
llvm::ArrayRef<Param> synthesizedArgs,
10091008
ConstraintLocator *locator)
1010-
: ConstraintFix(cs, FixKind::AddMissingArguments, locator), Fn(funcType),
1009+
: ConstraintFix(cs, FixKind::AddMissingArguments, locator),
10111010
NumSynthesized(synthesizedArgs.size()) {
10121011
std::uninitialized_copy(synthesizedArgs.begin(), synthesizedArgs.end(),
10131012
getSynthesizedArgumentsBuf().begin());
@@ -1022,7 +1021,7 @@ class AddMissingArguments final
10221021

10231022
bool diagnose(Expr *root, bool asNote = false) const override;
10241023

1025-
static AddMissingArguments *create(ConstraintSystem &cs, FunctionType *fnType,
1024+
static AddMissingArguments *create(ConstraintSystem &cs,
10261025
llvm::ArrayRef<Param> synthesizedArgs,
10271026
ConstraintLocator *locator);
10281027

lib/Sema/CSSimplify.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,9 +1167,8 @@ static ConstraintFix *fixRequirementFailure(ConstraintSystem &cs, Type type1,
11671167
/// Attempt to fix missing arguments by introducing type variables
11681168
/// and inferring their types from parameters.
11691169
static bool fixMissingArguments(ConstraintSystem &cs, Expr *anchor,
1170-
FunctionType *funcType,
11711170
SmallVectorImpl<AnyFunctionType::Param> &args,
1172-
SmallVectorImpl<AnyFunctionType::Param> &params,
1171+
ArrayRef<AnyFunctionType::Param> params,
11731172
unsigned numMissing,
11741173
ConstraintLocatorBuilder locator) {
11751174
assert(args.size() < params.size());
@@ -1219,9 +1218,8 @@ static bool fixMissingArguments(ConstraintSystem &cs, Expr *anchor,
12191218
}
12201219

12211220
ArrayRef<AnyFunctionType::Param> argsRef(args);
1222-
auto *fix =
1223-
AddMissingArguments::create(cs, funcType, argsRef.take_back(numMissing),
1224-
cs.getConstraintLocator(locator));
1221+
auto *fix = AddMissingArguments::create(cs, argsRef.take_back(numMissing),
1222+
cs.getConstraintLocator(locator));
12251223

12261224
if (cs.recordFix(fix))
12271225
return true;
@@ -1459,7 +1457,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
14591457
// If there are missing arguments, let's add them
14601458
// using parameter as a template.
14611459
if (diff < 0) {
1462-
if (fixMissingArguments(*this, anchor, func2, func1Params, func2Params,
1460+
if (fixMissingArguments(*this, anchor, func1Params, func2Params,
14631461
abs(diff), locator))
14641462
return getTypeMatchFailure(argumentLocator);
14651463
} else {
@@ -2662,11 +2660,9 @@ bool ConstraintSystem::repairFailures(
26622660
// But if `T.Element` didn't get resolved to `Void` we'd like
26632661
// to diagnose this as a missing argument which can't be ignored.
26642662
if (arg != getTypeVariables().end()) {
2665-
auto fnType = FunctionType::get({FunctionType::Param(lhs)},
2666-
getASTContext().TheEmptyTupleType);
2667-
conversionsOrFixes.push_back(AddMissingArguments::create(
2668-
*this, fnType, {FunctionType::Param(*arg)},
2669-
getConstraintLocator(anchor, path)));
2663+
conversionsOrFixes.push_back(
2664+
AddMissingArguments::create(*this, {FunctionType::Param(*arg)},
2665+
getConstraintLocator(anchor, path)));
26702666
}
26712667

26722668
if ((lhs->is<InOutType>() && !rhs->is<InOutType>()) ||

0 commit comments

Comments
 (0)