Skip to content

Sema: Make implicit elementwise struct init insensitive to lazy validation order. #17189

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
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
17 changes: 13 additions & 4 deletions lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1881,12 +1881,21 @@ ConstructorDecl *swift::createImplicitConstructor(TypeChecker &tc,
if (ICK == ImplicitConstructorKind::Memberwise) {
assert(isa<StructDecl>(decl) && "Only struct have memberwise constructor");

// Computed and static properties are not initialized.
for (auto var : decl->getStoredProperties()) {
if (var->isImplicit())
for (auto member : decl->getMembers()) {
auto var = dyn_cast<VarDecl>(member);
if (!var)
continue;
tc.validateDecl(var);

// Implicit, computed, and static properties are not initialized.
// The exception is lazy properties, which due to batch mode we may or
// may not have yet finalized, so they may currently be "stored" or
// "computed" in the current AST state.
if (var->isImplicit() || var->isStatic())
continue;
tc.validateDecl(var);
if (!var->hasStorage() && !var->getAttrs().hasAttribute<LazyAttr>())
continue;

// Initialized 'let' properties have storage, but don't get an argument
// to the memberwise initializer since they already have an initial
// value that cannot be overridden.
Expand Down
7 changes: 7 additions & 0 deletions test/decl/var/Inputs/lazy_properties_batch_mode_b.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
struct B {
var other: Int = 0
lazy var crash: String = {
return ""
}()
}

4 changes: 4 additions & 0 deletions test/decl/var/lazy_properties_batch_mode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -c -primary-file %s -o %t/a.o -primary-file %S/Inputs/lazy_properties_batch_mode_b.swift -o %t/b.o
func foo(_: B) {}