Skip to content

Commit 23bdb1a

Browse files
committed
Sema: Fix crash when @NoEscape or @autoclosure is applied to parameter
In Swift 3, these attributes were changed to apply to the type, and not the parameter. If they appear on the parameter, we would go down a diagnostic code path. When checking a generic function signature, we might not have a contextual type for the parameter yet, so bail out instead of crashing.
1 parent fd9ba4e commit 23bdb1a

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,9 @@ void TypeChecker::checkDeclAttributes(Decl *D) {
15621562
}
15631563

15641564
void TypeChecker::checkTypeModifyingDeclAttributes(VarDecl *var) {
1565+
if (!var->hasType())
1566+
return;
1567+
15651568
if (auto *attr = var->getAttrs().getAttribute<OwnershipAttr>())
15661569
checkOwnershipAttr(var, attr);
15671570
if (auto *attr = var->getAttrs().getAttribute<AutoClosureAttr>()) {
@@ -1589,7 +1592,7 @@ void TypeChecker::checkTypeModifyingDeclAttributes(VarDecl *var) {
15891592
void TypeChecker::checkAutoClosureAttr(ParamDecl *PD, AutoClosureAttr *attr) {
15901593
// The paramdecl should have function type, and we restrict it to functions
15911594
// taking ().
1592-
auto *FTy = PD->getType()->getAs<FunctionType>();
1595+
auto *FTy = PD->getInterfaceType()->getAs<FunctionType>();
15931596
if (!FTy) {
15941597
diagnose(attr->getLocation(), diag::autoclosure_function_type);
15951598
attr->setInvalid();
@@ -1643,7 +1646,7 @@ void TypeChecker::checkAutoClosureAttr(ParamDecl *PD, AutoClosureAttr *attr) {
16431646

16441647
void TypeChecker::checkNoEscapeAttr(ParamDecl *PD, NoEscapeAttr *attr) {
16451648
// The paramdecl should have function type.
1646-
auto *FTy = PD->getType()->getAs<FunctionType>();
1649+
auto *FTy = PD->getInterfaceType()->getAs<FunctionType>();
16471650
if (FTy == nullptr) {
16481651
diagnose(attr->getLocation(), diag::noescape_function_type);
16491652
attr->setInvalid();

test/attr/attr_noescape.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,11 @@ enum r19997577Type {
330330
// type attribute and decl attribute
331331
func noescapeD(@noescape f: @escaping () -> Bool) {} // expected-error {{@noescape is now an attribute on a parameter type, instead of on the parameter itself}} {{16-25=}} {{29-29=@noescape }}
332332
func noescapeT(f: @noescape () -> Bool) {} // expected-warning{{@noescape is the default and is deprecated}} {{19-29=}}
333+
func noescapeG<T>(@noescape f: () -> T) {} // expected-error{{@noescape is now an attribute on a parameter type, instead of on the parameter itself}}
334+
333335
func autoclosureD(@autoclosure f: () -> Bool) {} // expected-error {{@autoclosure is now an attribute on a parameter type, instead of on the parameter itself}} {{19-31=}} {{35-35=@autoclosure }}
334336
func autoclosureT(f: @autoclosure () -> Bool) {} // ok
337+
func autoclosureG<T>(@autoclosure f: () -> T) {} // expected-error{{@autoclosure is now an attribute on a parameter type, instead of on the parameter itself}}
335338

336339
func noescapeD_noescapeT(@noescape f: @noescape () -> Bool) {} // expected-error {{@noescape is now an attribute on a parameter type, instead of on the parameter itself}}
337340
// expected-warning@-1{{@noescape is the default and is deprecated}} {{39-49=}}

validation-test/compiler_crashers/28595-typeincontext-isnull-no-contextual-type-set-yet.swift renamed to validation-test/compiler_crashers_fixed/28595-typeincontext-isnull-no-contextual-type-set-yet.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
// RUN: not %target-swift-frontend %s -emit-ir
99
// REQUIRES: asserts
1010
protocol b{func a(@autoclosure())

0 commit comments

Comments
 (0)