Skip to content

Commit 5ef64c4

Browse files
committed
[AST] Preserve l-valueness of covariant result type after replacement
It's possible that covariant result type is wrapped in l-value. Just like currently preserved optionality, transformation should maintain l-valueness of a result type as well. Resolves: rdar://problem/71167129
1 parent 8474332 commit 5ef64c4

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/AST/Type.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -809,13 +809,16 @@ Type TypeBase::getWithoutParens() {
809809
Type TypeBase::replaceCovariantResultType(Type newResultType,
810810
unsigned uncurryLevel) {
811811
if (uncurryLevel == 0) {
812-
if (auto objectType = getOptionalObjectType()) {
812+
bool isLValue = is<LValueType>();
813+
814+
auto loadedTy = getWithoutSpecifierType();
815+
if (auto objectType = loadedTy->getOptionalObjectType()) {
813816
assert(!newResultType->getOptionalObjectType());
814-
return OptionalType::get(
817+
newResultType = OptionalType::get(
815818
objectType->replaceCovariantResultType(newResultType, uncurryLevel));
816819
}
817820

818-
return newResultType;
821+
return isLValue ? LValueType::get(newResultType) : newResultType;
819822
}
820823

821824
// Determine the input and result types of this function.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: not %target-swift-frontend %s -typecheck
2+
3+
final class Whatever {
4+
static var instance: Self!
5+
6+
static var shared: Self {
7+
return instance ?? {
8+
instance = Self()
9+
return instance
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)