Skip to content

Commit c7eecd7

Browse files
authored
---
yaml --- r: 343998 b: refs/heads/master-rebranch c: 9a24013 h: refs/heads/master
1 parent 2c5adb9 commit c7eecd7

File tree

8 files changed

+75
-51
lines changed

8 files changed

+75
-51
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ refs/tags/swift-DEVELOPMENT-SNAPSHOT-2019-08-02-a: ddd2b2976aa9bfde5f20fe37f6bd2
14551455
refs/tags/swift-DEVELOPMENT-SNAPSHOT-2019-08-03-a: 171cc166f2abeb5ca2a4003700a8a78a108bd300
14561456
refs/heads/benlangmuir-patch-1: baaebaf39d52f3bf36710d4fe40cf212e996b212
14571457
refs/heads/i-do-redeclare: 8c4e6d5de5c1e3f0a2cedccf319df713ea22c48e
1458-
refs/heads/master-rebranch: 7e3608e007b0540c7520bd61a19f787e9db07481
1458+
refs/heads/master-rebranch: 9a2401322818b2983496a391d2d967f1ea5e89e2
14591459
refs/heads/rdar-53901732: 9bd06af3284e18a109cdbf9aa59d833b24eeca7b
14601460
refs/heads/revert-26776-subst-always-returns-a-type: 1b8e18fdd391903a348970a4c848995d4cdd789c
14611461
refs/heads/tensorflow-merge: 8b854f62f80d4476cb383d43c4aac2001dde3cec

branches/master-rebranch/lib/FrontendTool/FrontendTool.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,14 @@ static bool performCompile(CompilerInstance &Instance,
11221122
if (Action == FrontendOptions::ActionType::Typecheck) {
11231123
if (emitIndexData(Invocation, Instance))
11241124
return true;
1125+
// FIXME: Whole-module outputs with a non-whole-module -typecheck ought to
1126+
// be disallowed, but the driver implements -index-file mode by generating a
1127+
// regular whole-module frontend command line and modifying it to index just
1128+
// one file (by making it a primary) instead of all of them. If that
1129+
// invocation also has flags to emit whole-module supplementary outputs, the
1130+
// compiler can crash trying to access information for non-type-checked
1131+
// declarations in the non-primary files. For now, prevent those crashes by
1132+
// guarding the emission of whole-module supplementary outputs.
11251133
if (opts.InputsAndOutputs.isWholeModule()) {
11261134
if (emitAnyWholeModulePostTypeCheckSupplementaryOutputs(Instance,
11271135
Invocation,

branches/master-rebranch/lib/Sema/CSDiag.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3895,10 +3895,8 @@ bool FailureDiagnosis::diagnoseClosureExpr(
38953895
return true;
38963896
}
38973897

3898-
MissingArgumentsFailure failure(
3899-
expr, CS, fnType, inferredArgCount - actualArgCount,
3900-
CS.getConstraintLocator(CE, LocatorPathElt::ContextualType()));
3901-
return failure.diagnoseAsError();
3898+
// Missing arguments are already diagnosed via new diagnostic framework.
3899+
return false;
39023900
}
39033901

39043902
// Coerce parameter types here only if there are no unresolved

branches/master-rebranch/lib/Sema/CSDiagnostics.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3506,14 +3506,31 @@ bool MissingArgumentsFailure::diagnoseAsError() {
35063506
path.back().getKind() == ConstraintLocator::ContextualType))
35073507
return false;
35083508

3509-
if (auto *closure = dyn_cast<ClosureExpr>(getAnchor()))
3510-
return diagnoseTrailingClosure(closure);
3509+
auto *anchor = getAnchor();
3510+
if (auto *captureList = dyn_cast<CaptureListExpr>(anchor))
3511+
anchor = captureList->getClosureBody();
3512+
3513+
if (auto *closure = dyn_cast<ClosureExpr>(anchor))
3514+
return diagnoseClosure(closure);
35113515

35123516
return false;
35133517
}
35143518

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

35183535
// If the closure didn't specify any arguments and it is in a context that
35193536
// needs some, produce a fixit to turn "{...}" into "{ _,_ in ...}".
@@ -3523,10 +3540,10 @@ bool MissingArgumentsFailure::diagnoseTrailingClosure(ClosureExpr *closure) {
35233540
diag::closure_argument_list_missing, NumSynthesized);
35243541

35253542
std::string fixText; // Let's provide fixits for up to 10 args.
3526-
if (Fn->getNumParams() <= 10) {
3543+
if (funcType->getNumParams() <= 10) {
35273544
fixText += " ";
35283545
interleave(
3529-
Fn->getParams(),
3546+
funcType->getParams(),
35303547
[&fixText](const AnyFunctionType::Param &param) { fixText += '_'; },
35313548
[&fixText] { fixText += ','; });
35323549
fixText += " in ";
@@ -3552,9 +3569,9 @@ bool MissingArgumentsFailure::diagnoseTrailingClosure(ClosureExpr *closure) {
35523569
std::all_of(params->begin(), params->end(),
35533570
[](ParamDecl *param) { return !param->hasName(); });
35543571

3555-
auto diag =
3556-
emitDiagnostic(params->getStartLoc(), diag::closure_argument_list_tuple,
3557-
resolveType(Fn), Fn->getNumParams(), diff, diff == 1);
3572+
auto diag = emitDiagnostic(
3573+
params->getStartLoc(), diag::closure_argument_list_tuple,
3574+
resolveType(funcType), funcType->getNumParams(), diff, diff == 1);
35583575

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

branches/master-rebranch/lib/Sema/CSDiagnostics.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,23 +1182,19 @@ 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

11981194
private:
1199-
/// If missing arguments come from trailing closure,
1195+
/// If missing arguments come from a closure,
12001196
/// let's produce tailored diagnostics.
1201-
bool diagnoseTrailingClosure(ClosureExpr *closure);
1197+
bool diagnoseClosure(ClosureExpr *closure);
12021198
};
12031199

12041200
class OutOfOrderArgumentFailure final : public FailureDiagnostic {

branches/master-rebranch/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 {

branches/master-rebranch/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

branches/master-rebranch/lib/Sema/CSSimplify.cpp

Lines changed: 26 additions & 20 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());
@@ -1180,16 +1179,26 @@ static bool fixMissingArguments(ConstraintSystem &cs, Expr *anchor,
11801179
// tuple e.g. `$0.0`.
11811180
Optional<TypeBase *> argumentTuple;
11821181
if (isa<ClosureExpr>(anchor) && isSingleTupleParam(ctx, args)) {
1183-
auto isParam = [](const Expr *expr) {
1184-
if (auto *DRE = dyn_cast<DeclRefExpr>(expr)) {
1185-
if (auto *decl = DRE->getDecl())
1186-
return isa<ParamDecl>(decl);
1182+
auto argType = args.back().getPlainType();
1183+
// Let's unpack argument tuple into N arguments, this corresponds
1184+
// to something like `foo { (bar: (Int, Int)) in }` where `foo`
1185+
// has a single parameter of type `(Int, Int) -> Void`.
1186+
if (auto *tuple = argType->getAs<TupleType>()) {
1187+
args.pop_back();
1188+
for (const auto &elt : tuple->getElements()) {
1189+
args.push_back(AnyFunctionType::Param(elt.getType(), elt.getName(),
1190+
elt.getParameterFlags()));
11871191
}
1188-
return false;
1189-
};
1192+
} else if (auto *typeVar = argType->getAs<TypeVariableType>()) {
1193+
auto isParam = [](const Expr *expr) {
1194+
if (auto *DRE = dyn_cast<DeclRefExpr>(expr)) {
1195+
if (auto *decl = DRE->getDecl())
1196+
return isa<ParamDecl>(decl);
1197+
}
1198+
return false;
1199+
};
11901200

1191-
const auto &arg = args.back();
1192-
if (auto *argTy = arg.getPlainType()->getAs<TypeVariableType>()) {
1201+
// Something like `foo { x in }` or `foo { $0 }`
11931202
anchor->forEachChildExpr([&](Expr *expr) -> Expr * {
11941203
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(expr)) {
11951204
if (!isParam(UDE->getBase()))
@@ -1201,7 +1210,7 @@ static bool fixMissingArguments(ConstraintSystem &cs, Expr *anchor,
12011210
llvm::any_of(params, [&](const AnyFunctionType::Param &param) {
12021211
return param.getLabel() == name;
12031212
})) {
1204-
argumentTuple.emplace(argTy);
1213+
argumentTuple.emplace(typeVar);
12051214
args.pop_back();
12061215
return nullptr;
12071216
}
@@ -1219,9 +1228,8 @@ static bool fixMissingArguments(ConstraintSystem &cs, Expr *anchor,
12191228
}
12201229

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

12261234
if (cs.recordFix(fix))
12271235
return true;
@@ -1459,7 +1467,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
14591467
// If there are missing arguments, let's add them
14601468
// using parameter as a template.
14611469
if (diff < 0) {
1462-
if (fixMissingArguments(*this, anchor, func2, func1Params, func2Params,
1470+
if (fixMissingArguments(*this, anchor, func1Params, func2Params,
14631471
abs(diff), locator))
14641472
return getTypeMatchFailure(argumentLocator);
14651473
} else {
@@ -2662,11 +2670,9 @@ bool ConstraintSystem::repairFailures(
26622670
// But if `T.Element` didn't get resolved to `Void` we'd like
26632671
// to diagnose this as a missing argument which can't be ignored.
26642672
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)));
2673+
conversionsOrFixes.push_back(
2674+
AddMissingArguments::create(*this, {FunctionType::Param(*arg)},
2675+
getConstraintLocator(anchor, path)));
26702676
}
26712677

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

0 commit comments

Comments
 (0)