Skip to content

Commit e8a89cc

Browse files
committed
[CSGen] Don't increase optionality of weak var patterns
If the externally-imposed type is already optional, let's not add optionality to the pattern.
1 parent 2282160 commit e8a89cc

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

lib/Sema/CSGen.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,12 +2209,17 @@ namespace {
22092209

22102210
switch (optionality) {
22112211
case ReferenceOwnershipOptionality::Required:
2212-
varType = TypeChecker::getOptionalType(var->getLoc(), varType);
2213-
assert(!varType->hasError());
2214-
2215-
if (oneWayVarType) {
2216-
oneWayVarType =
2217-
TypeChecker::getOptionalType(var->getLoc(), oneWayVarType);
2212+
// If the externally-imposed type is already optional,
2213+
// let's not add optionality to the pattern.
2214+
if (!externalPatternType ||
2215+
!externalPatternType->getOptionalObjectType()) {
2216+
varType = TypeChecker::getOptionalType(var->getLoc(), varType);
2217+
assert(!varType->hasError());
2218+
2219+
if (oneWayVarType) {
2220+
oneWayVarType =
2221+
TypeChecker::getOptionalType(var->getLoc(), oneWayVarType);
2222+
}
22182223
}
22192224
break;
22202225

test/Constraints/result_builder.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,3 +788,19 @@ let ts1 = MyTupleStruct {
788788

789789
// CHECK: MyTupleStruct<(Int, String, Optional<String>), (Double, String)>(first: (Function), second: (3.14159, "blah"))
790790
print(ts1)
791+
792+
// Make sure that `weakV` is `Test?` and not `Test??`
793+
func test_weak_optionality_stays_the_same() {
794+
class Test {
795+
func fn() -> Int { 42 }
796+
}
797+
798+
tuplify(true) { c in
799+
weak var weakV: Test? = Test()
800+
801+
0
802+
if let v = weakV {
803+
v.fn()
804+
}
805+
}
806+
}

0 commit comments

Comments
 (0)