Skip to content

Commit 57c017a

Browse files
authored
Merge pull request swiftlang#22836 from slavapestov/non-optional-weak-iboutlet
Sema: Fix crash with non-Optional weak @IBOutlet
2 parents 34f8670 + c7f9e27 commit 57c017a

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,9 +2400,14 @@ void TypeChecker::checkReferenceOwnershipAttr(VarDecl *var,
24002400
attr->setInvalid();
24012401
}
24022402

2403-
// While @IBOutlet can be weak, it must be optional. Let it diagnose.
2404-
if (!isOptional && !var->getAttrs().hasAttribute<IBOutletAttr>()) {
2403+
if (!isOptional) {
24052404
attr->setInvalid();
2405+
2406+
// @IBOutlet has its own diagnostic when the property type is
2407+
// non-optional.
2408+
if (var->getAttrs().hasAttribute<IBOutletAttr>())
2409+
break;
2410+
24062411
auto diag = diagnose(var->getStartLoc(),
24072412
diag::invalid_ownership_not_optional,
24082413
ownershipKind,

test/attr/attr_iboutlet.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,12 @@ class NonObjC {}
161161
if outlet4 != nil {}
162162
}
163163
}
164+
165+
// https://bugs.swift.org/browse/SR-9889
166+
@objc class NonOptionalWeak {
167+
// expected-error@+3 {{@IBOutlet property has non-optional type 'OX'}}
168+
// expected-note @+2 {{add '?' to form the optional type 'OX?'}}
169+
// expected-note @+1 {{add '!' to form an implicitly unwrapped optional}}
170+
@IBOutlet weak var something: OX
171+
init() { }
172+
}

0 commit comments

Comments
 (0)