Skip to content

Commit 7ea7d32

Browse files
committed
[Type checker] Allow unowned/unowned(unsafe) optional lets.
Unlike weak values of optional type, an unowned or unowned(unsafe) value of optional type will not silently become 'nil' when the referenced instance goes away: rather, the program will trap. Don't reject unowned or unowned(unsafe) optional lets. Fixes rdar://problem/45732251.
1 parent d2ddbc8 commit 7ea7d32

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,9 +2403,7 @@ void TypeChecker::checkReferenceOwnershipAttr(VarDecl *var,
24032403
}
24042404
break;
24052405
case ReferenceOwnershipOptionality::Allowed:
2406-
if (!isOptional)
2407-
break;
2408-
LLVM_FALLTHROUGH;
2406+
break;
24092407
case ReferenceOwnershipOptionality::Required:
24102408
if (var->isLet()) {
24112409
diagnose(var->getStartLoc(), diag::invalid_ownership_is_let,

test/attr/attributes.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,12 @@ class HasStorage {
273273
@_invalid_attribute_ // expected-error {{unknown attribute '_invalid_attribute_'}}
274274
@inline(__always)
275275
public func sillyFunction() {}
276+
277+
// rdar://problem/45732251: unowned/unowned(unsafe) optional lets are permitted
278+
func unownedOptionals(x: C) {
279+
unowned let y: C? = x
280+
unowned(unsafe) let y2: C? = x
281+
282+
_ = y
283+
_ = y2
284+
}

0 commit comments

Comments
 (0)