Skip to content

Commit 0eeeb3a

Browse files
xedinnate-chandler
authored andcommitted
---
yaml --- r: 294905 b: refs/heads/swift-5.1-branch c: 5ff0b4d h: refs/heads/master i: 294903: e4076b3
1 parent b5f4508 commit 0eeeb3a

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ refs/heads/marcrasi-astverifier-disable: 3fac766a23a77ebd0640296bfd7fc116ea60a4e
12421242
refs/heads/revert-22227-a-tall-white-fountain-played: adfce60b2eaa54903ea189bed8a783bca609fa53
12431243
refs/heads/revert-22300-revert-22227-a-tall-white-fountain-played: 5f92040224df7dd4e618fdfb367349df64d8acad
12441244
refs/heads/swift-5.1-old-llvm-branch: 9cef8175146f25b72806154b8a0f4a3f52e3e400
1245-
refs/heads/swift-5.1-branch: 145e6d9b7bf599a02083f7270f08b1c71d12a0d2
1245+
refs/heads/swift-5.1-branch: 5ff0b4d335ba29ad262be4b249f0baeddd8b362a
12461246
refs/tags/swift-4.2.2-RELEASE: e429d1f1aaf59e69d38207a96e56265c7f6fccec
12471247
refs/tags/swift-5.0-DEVELOPMENT-SNAPSHOT-2019-02-02-a: 3e5a03d32ff3b1e9af90d6c1198c14f938379a6e
12481248
refs/tags/swift-5.0-DEVELOPMENT-SNAPSHOT-2019-02-03-a: 4591c933063ddcb0d6cd6d0cdd01086b2f9b244d

branches/swift-5.1-branch/lib/Sema/CSApply.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7733,10 +7733,18 @@ Expr *ConstraintSystem::applySolution(Solution &solution, Expr *expr,
77337733
if (hadError)
77347734
return nullptr;
77357735
}
7736-
7736+
7737+
// We are supposed to use contextual type only if it is present and
7738+
// this expression doesn't represent the implicit return of the single
7739+
// expression function which got deduced to be `Never`.
7740+
auto shouldCoerceToContextualType = [&]() {
7741+
return convertType && !(getType(result)->isUninhabited() &&
7742+
getContextualTypePurpose() == CTP_ReturnSingleExpr);
7743+
};
7744+
77377745
// If we're supposed to convert the expression to some particular type,
77387746
// do so now.
7739-
if (convertType) {
7747+
if (shouldCoerceToContextualType()) {
77407748
result = rewriter.coerceToType(result, convertType,
77417749
getConstraintLocator(expr));
77427750
if (!result)

branches/swift-5.1-branch/lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2805,9 +2805,11 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
28052805
}
28062806

28072807
// Allow '() -> T' to '() -> ()' and '() -> Never' to '() -> T' for closure
2808-
// literals.
2808+
// literals and expressions representing an implicit return type of the single
2809+
// expression functions.
28092810
if (auto elt = locator.last()) {
2810-
if (elt->getKind() == ConstraintLocator::ClosureResult) {
2811+
if (elt->getKind() == ConstraintLocator::ClosureResult ||
2812+
elt->getKind() == ConstraintLocator::SingleExprFuncResultType) {
28112813
if (kind >= ConstraintKind::Subtype &&
28122814
(type1->isUninhabited() || type2->isVoid())) {
28132815
increaseScore(SK_FunctionConversion);

branches/swift-5.1-branch/lib/Sema/CSSolver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,16 +1125,16 @@ ConstraintSystem::solveImpl(Expr *&expr,
11251125

11261126
if (getContextualTypePurpose() == CTP_CallArgument)
11271127
constraintKind = ConstraintKind::ArgumentConversion;
1128-
else if (getContextualTypePurpose() == CTP_ReturnSingleExpr)
1129-
constraintKind = ConstraintKind::SingleExpressionFunctionReturnConversion;
11301128

11311129
// In a by-reference yield, we expect the contextual type to be an
11321130
// l-value type, so the result must be bound to that.
11331131
if (getContextualTypePurpose() == CTP_YieldByReference)
11341132
constraintKind = ConstraintKind::Bind;
11351133

11361134
auto *convertTypeLocator = getConstraintLocator(
1137-
getConstraintLocator(expr), ConstraintLocator::ContextualType);
1135+
expr, getContextualTypePurpose() == CTP_ReturnSingleExpr
1136+
? ConstraintLocator::SingleExprFuncResultType
1137+
: ConstraintLocator::ContextualType);
11381138

11391139
if (allowFreeTypeVariables == FreeTypeVariableBinding::UnresolvedType) {
11401140
convertType = convertType.transform([&](Type type) -> Type {

branches/swift-5.1-branch/lib/Sema/TypeCheckStmt.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,8 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
493493
}
494494

495495
ContextualTypePurpose ctp = CTP_ReturnStmt;
496-
if (auto func = dyn_cast_or_null<FuncDecl>(TheFunc->getAbstractFunctionDecl())) {
496+
if (auto func =
497+
dyn_cast_or_null<FuncDecl>(TheFunc->getAbstractFunctionDecl())) {
497498
if (func->hasSingleExpressionBody()) {
498499
ctp = CTP_ReturnSingleExpr;
499500
}
@@ -502,7 +503,6 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
502503
auto exprTy = TC.typeCheckExpression(E, DC, TypeLoc::withoutLoc(ResultTy),
503504
ctp,
504505
options);
505-
506506
RS->setResult(E);
507507

508508
if (!exprTy) {
@@ -1936,7 +1936,7 @@ bool TypeChecker::typeCheckFunctionBodyUntil(FuncDecl *FD,
19361936
// The function returns void. We don't need an explicit return, no matter
19371937
// what the type of the expression is. Take the inserted return back out.
19381938
BS->setElement(0, E);
1939-
// Fall through to type-checking the body as if we were not a single
1939+
// Fall through to type-checking the body as if we were not a single
19401940
// expression function.
19411941
}
19421942
}
@@ -1945,6 +1945,22 @@ bool TypeChecker::typeCheckFunctionBodyUntil(FuncDecl *FD,
19451945
SC.EndTypeCheckLoc = EndTypeCheckLoc;
19461946
bool HadError = SC.typeCheckBody(BS);
19471947

1948+
// If this was a function with a single expression body, let's see
1949+
// if implicit return statement came out to be `Never` which means
1950+
// that we have eagerly converted something like `{ fatalError() }`
1951+
// into `{ return fatalError() }` that has to be corrected here.
1952+
if (FD->hasSingleExpressionBody()) {
1953+
if (auto *stmt = BS->getElement(0).dyn_cast<Stmt *>()) {
1954+
if (auto *RS = dyn_cast<ReturnStmt>(stmt)) {
1955+
if (RS->isImplicit() && RS->hasResult()) {
1956+
auto returnType = RS->getResult()->getType();
1957+
if (returnType && returnType->isUninhabited())
1958+
BS->setElement(0, RS->getResult());
1959+
}
1960+
}
1961+
}
1962+
}
1963+
19481964
FD->setBody(BS);
19491965
return HadError;
19501966
}

0 commit comments

Comments
 (0)