Skip to content

Commit

Permalink
Merge pull request #74133 from DougGregor/mutable-lets-in-inits-tweaks
Browse files Browse the repository at this point in the history
Tweak "initializable values" logic slightly to address source incompatibilities
  • Loading branch information
DougGregor committed Jun 5, 2024
2 parents 79075e9 + 62558b0 commit 67c0fd8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7404,10 +7404,10 @@ VarDecl::mutability(const DeclContext *UseDC,
if (!isLet()) {
if (hasInitAccessor()) {
if (auto *ctor = dyn_cast_or_null<ConstructorDecl>(UseDC)) {
// If we're referencing 'self.', it's initializable.
// If we're referencing 'self.', it's mutable.
if (!base ||
(*base && ctor->getImplicitSelfDecl() == (*base)->getDecl()))
return StorageMutability::Initializable;
return StorageMutability::Mutable;

return storageIsMutable(supportsMutation());
}
Expand Down Expand Up @@ -7475,8 +7475,14 @@ VarDecl::mutability(const DeclContext *UseDC,
return StorageMutability::Immutable;

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

return StorageMutability::Initializable;
}

return StorageMutability::Immutable;
}
Expand Down
33 changes: 33 additions & 0 deletions test/SILOptimizer/definite_init_diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1614,3 +1614,36 @@ class DerivedWrappedProperty : SomeClass {
} // expected-error {{'super.init' isn't called on all paths before returning from initializer}}

}

// rdar://129031705 ([error: ... used before being initialized)
// Related to treating 'let's as immutable RValues.
struct S {
let rotation: (Int, Int)

init() {
rotation.0 = 0
rotation.1 = rotation.0
}
}

// rdar://128890586: Init accessors
final class HasInitAccessors {
private var _ints: [Int] = []

private var ints: [Int] {
@storageRestrictions(initializes: _ints)
init(initialValue) {
_ints = initialValue
}
get {
return _ints
}
set {
_ints = newValue
}
}

init() {
ints.append(0)
}
}

0 comments on commit 67c0fd8

Please sign in to comment.