Skip to content

[TypeChecker] InitAccessors: Fix default initialization of init accessor properties #67769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,12 @@ void LifetimeChecker::doIt() {
while (returnBB != F.end()) {
auto *terminator = returnBB->getTerminator();

// If this is an unreachable block, let's ignore it.
if (isa<UnreachableInst>(terminator)) {
++returnBB;
continue;
}

if (!isInitializedAtUse(DIMemoryUse(terminator, DIUseKind::Load, 0, 1)))
diagnoseMissingInit();

Expand Down
9 changes: 8 additions & 1 deletion lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,12 +877,19 @@ bool AreAllStoredPropertiesDefaultInitableRequest::evaluate(
if (llvm::any_of(initAccessorProperties, [&](const auto &entry) {
auto *property =
entry.second->getParentPatternBinding();
return property->isInitialized(0);
return property->isInitialized(0) ||
property->isDefaultInitializable();
}))
return;

if (VD->hasStorageOrWrapsStorage())
HasStorage = true;

// Treat an init accessor property that doesn't initialize other
// properties as stored for initialization purposes.
if (auto *initAccessor = VD->getAccessor(AccessorKind::Init)) {
HasStorage |= initAccessor->getInitializedProperties().empty();
}
});

if (!HasStorage) continue;
Expand Down
7 changes: 6 additions & 1 deletion lib/Sema/TypeCheckStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,16 @@ const PatternBindingEntry *PatternBindingEntryRequest::evaluate(
}
}

auto isInitAccessorProperty = [](Pattern *pattern) {
auto *var = pattern->getSingleVar();
return var && var->getAccessor(AccessorKind::Init);
};

// If we have a type but no initializer, check whether the type is
// default-initializable. If so, do it.
if (!pbe.isInitialized() &&
binding->isDefaultInitializable(entryNumber) &&
pattern->hasStorage()) {
(pattern->hasStorage() || isInitAccessorProperty(pattern))) {
if (auto defaultInit = TypeChecker::buildDefaultInitializer(patternType)) {
// If we got a default initializer, install it and re-type-check it
// to make sure it is properly coerced to the pattern type.
Expand Down
46 changes: 46 additions & 0 deletions test/Interpreter/init_accessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -801,3 +801,49 @@ do {
}
// CHECK: Person(name: P)
// CHECK-NEXT: Person(name: O)

do {
struct TestDefaultInitializable : CustomStringConvertible {
var description: String {
"TestDefaultInitializable(a: \(a))"
}

var _a: Int?
var a: Int? {
@storageRestrictions(initializes: _a)
init { _a = newValue }
get { _a }
}
}

print(TestDefaultInitializable())
print(TestDefaultInitializable(a: 42))

struct TestMixedDefaultInitalizable : CustomStringConvertible {
var description: String {
"TestMixedDefaultInitalizable(a: \(a), b: \(b))"
}

var a: Int? {
init {}
get { nil }
}

var _b: String
var b: String? {
@storageRestrictions(initializes: _b)
init { self._b = (newValue ?? "") }
get { _b }
set { _b = newValue ?? "" }
}
}

print(TestMixedDefaultInitalizable())
print(TestMixedDefaultInitalizable(b: "Hello"))
print(TestMixedDefaultInitalizable(a: 42))
}
// CHECK: TestDefaultInitializable(a: nil)
// CHECK-NEXT: TestDefaultInitializable(a: Optional(42))
// CHECK-NEXT: TestMixedDefaultInitalizable(a: nil, b: Optional(""))
// CHECK-NEXT: TestMixedDefaultInitalizable(a: nil, b: Optional("Hello"))
// CHECK-NEXT: TestMixedDefaultInitalizable(a: nil, b: Optional(""))
11 changes: 11 additions & 0 deletions test/SILOptimizer/init_accessor_definite_init_diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ class TestInitWithGuard {
}
}

do {
struct TestPropertyInitWithUnreachableBlocks {
var _a: Int
var a: Int? {
@storageRestrictions(initializes: _a)
init { _a = newValue ?? 0 } // Ok
get { 42 }
}
}
}

do {
class Base<T: Collection> {
private var _v: T
Expand Down
70 changes: 69 additions & 1 deletion test/decl/var/init_accessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ extension TestStructPropWithoutSetter {
}

do {
class TestClassPropWithoutSetter {
class TestClassPropWithoutSetter { // expected-error {{class 'TestClassPropWithoutSetter' has no initializers}}
var x: Int {
init {
}
Expand All @@ -539,6 +539,44 @@ do {
}
}

// This should generate only memberwise initializer because `a` doesn't have a default
struct TestStructWithoutSetter { // expected-note {{'init(a:)' declared here}}
var a: Int {
init {
}
get { 0 }
}
}

_ = TestStructWithoutSetter() // expected-error {{missing argument for parameter 'a' in call}}
_ = TestStructWithoutSetter(a: 42) // Ok

struct TestDefaultInitializable {
var a: Int? {
init {}
get { nil }
}
}

_ = TestDefaultInitializable() // Ok (`a` is initialized to `nil`)

struct TestMixedDefaultInitializable {
var a: Int? {
init {}
get { nil }
}

var _b: String
var b: String? {
@storageRestrictions(initializes: _b)
init { _b = newValue ?? "" }
get { _b }
set { _b = newValue ?? "" }
}
}

_ = TestMixedDefaultInitializable() // Ok (both `a` and `b` are initialized to `nil`)

class SubTestPropWithoutSetter : TestClassPropWithoutSetter {
init(otherV: Int) {
x = otherV // Ok
Expand Down Expand Up @@ -620,3 +658,33 @@ func test_invalid_storage_restrictions() {
init() {}
}
}

do {
struct Test1 {
var q: String
var a: Int? {
init {}
get { 42 }
}
}

_ = Test1(q: "some question") // Ok `a` is default initialized to `nil`
_ = Test1(q: "ultimate question", a: 42)

struct Test2 {
var q: String

var _a: Int?
var a: Int? {
@storageRestrictions(initializes: _a)
init {
_a = newValue
}
get { _a }
set { _a = newValue }
}
}

_ = Test2(q: "some question") // Ok `a` is default initialized to `nil`
_ = Test2(q: "ultimate question", a: 42)
}