Skip to content

Commit a6e7fdb

Browse files
committed
Merge pull request #274 from gregomni/master
[AST] Fix for SR-7: Inferring closure param type to inout crashes compiler
2 parents c5bfaa4 + 83a5e2c commit a6e7fdb

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

lib/AST/Verifier.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,13 @@ struct ASTNodeBase {};
814814
}
815815

816816
void verifyChecked(DeclRefExpr *E) {
817+
if (E->getType()->is<InOutType>()) {
818+
PrettyStackTraceExpr debugStack(Ctx, "verifying decl reference", E);
819+
Out << "reference with inout type "
820+
<< E->getType().getString() << "\n";
821+
E->dump(Out);
822+
abort();
823+
}
817824
if (E->getType()->is<PolymorphicFunctionType>()) {
818825
PrettyStackTraceExpr debugStack(Ctx, "verifying decl reference", E);
819826
Out << "unspecialized reference with polymorphic type "

lib/Sema/CSApply.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,16 @@ namespace {
404404
}
405405

406406
auto type = simplifyType(openedType);
407+
408+
// If we've ended up trying to assign an inout type here, it means we're
409+
// missing an ampersand in front of the ref.
410+
if (auto inoutType = type->getAs<InOutType>()) {
411+
auto &tc = cs.getTypeChecker();
412+
tc.diagnose(loc, diag::missing_address_of, inoutType->getInOutObjectType())
413+
.fixItInsert(loc, "&");
414+
return nullptr;
415+
}
416+
407417
return new (ctx) DeclRefExpr(decl, loc, implicit, semantics, type);
408418
}
409419

@@ -5649,6 +5659,8 @@ namespace {
56495659
// Enter the context of the closure when type-checking the body.
56505660
llvm::SaveAndRestore<DeclContext *> savedDC(Rewriter.dc, closure);
56515661
Expr *body = closure->getSingleExpressionBody()->walk(*this);
5662+
if (!body)
5663+
return { false, nullptr };
56525664

56535665
if (body != closure->getSingleExpressionBody())
56545666
closure->setSingleExpressionBody(body);
@@ -5665,7 +5677,7 @@ namespace {
56655677
closure,
56665678
ConstraintLocator::ClosureResult));
56675679
if (!body)
5668-
return { false, nullptr } ;
5680+
return { false, nullptr };
56695681

56705682
closure->setSingleExpressionBody(body);
56715683
}

test/Constraints/lvalues.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,7 @@ func testImmutableUnsafePointer(p: UnsafePointer<Int>) {
203203
p[0] = 1 // expected-error {{cannot assign through subscript: subscript is get-only}}
204204
}
205205

206+
// <https://bugs.swift.org/browse/SR-7> Inferring closure param type to
207+
// inout crashes compiler
208+
let g = { x in f0(x) } // expected-error{{passing value of type 'Int' to an inout parameter requires explicit '&'}} {{19-19=&}}
209+

0 commit comments

Comments
 (0)