Skip to content

Commit 574a60c

Browse files
committed
Tweak "initializable values" logic slightly to address source incompatibilities
Back of slightly on when we treat a "let" instance property as immutable within an initializer, to deal with two newly-introduced source incompatibilities. Fixes rdar://129253556.
1 parent 3564d14 commit 574a60c

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

lib/AST/Decl.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7324,11 +7324,6 @@ VarDecl::mutability(const DeclContext *UseDC,
73247324
if (!isLet()) {
73257325
if (hasInitAccessor()) {
73267326
if (auto *ctor = dyn_cast_or_null<ConstructorDecl>(UseDC)) {
7327-
// If we're referencing 'self.', it's initializable.
7328-
if (!base ||
7329-
(*base && ctor->getImplicitSelfDecl() == (*base)->getDecl()))
7330-
return StorageMutability::Initializable;
7331-
73327327
return storageIsMutable(supportsMutation());
73337328
}
73347329
}
@@ -7395,8 +7390,14 @@ VarDecl::mutability(const DeclContext *UseDC,
73957390
return StorageMutability::Immutable;
73967391

73977392
// If we were given a base and it is 'self', it's initializable.
7398-
if (!base || (*base && CD->getImplicitSelfDecl() == (*base)->getDecl()))
7393+
if (!base || (*base && CD->getImplicitSelfDecl() == (*base)->getDecl())) {
7394+
// Treat values of tuple type as mutable in these contexts, because
7395+
// SILGen wants to see them as lvalues.
7396+
if (getInterfaceType()->is<TupleType>())
7397+
return StorageMutability::Mutable;
7398+
73997399
return StorageMutability::Initializable;
7400+
}
74007401

74017402
return StorageMutability::Immutable;
74027403
}

test/SILOptimizer/definite_init_diagnostics.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,3 +1614,36 @@ class DerivedWrappedProperty : SomeClass {
16141614
} // expected-error {{'super.init' isn't called on all paths before returning from initializer}}
16151615

16161616
}
1617+
1618+
// rdar://129031705 ([error: ... used before being initialized)
1619+
// Related to treating 'let's as immutable RValues.
1620+
struct S {
1621+
let rotation: (Int, Int)
1622+
1623+
init() {
1624+
rotation.0 = 0
1625+
rotation.1 = rotation.0
1626+
}
1627+
}
1628+
1629+
// rdar://128890586: Init accessors
1630+
final class HasInitAccessors {
1631+
private var _ints: [Int] = []
1632+
1633+
private var ints: [Int] {
1634+
@storageRestrictions(initializes: _ints)
1635+
init(initialValue) {
1636+
_ints = initialValue
1637+
}
1638+
get {
1639+
return _ints
1640+
}
1641+
set {
1642+
_ints = newValue
1643+
}
1644+
}
1645+
1646+
init() {
1647+
ints.append(0)
1648+
}
1649+
}

0 commit comments

Comments
 (0)