Skip to content

Sema: Don't require properties to have initializers in classes in module interfaces #42408

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
6 changes: 6 additions & 0 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2441,6 +2441,12 @@ class DeclChecker : public DeclVisitor<DeclChecker> {

/// Check that all stored properties have in-class initializers.
void checkRequiredInClassInits(ClassDecl *cd) {
// Initializers may be omitted from property declarations in module
// interface files so don't diagnose in them.
SourceFile *sourceFile = cd->getDeclContext()->getParentSourceFile();
if (sourceFile && sourceFile->Kind == SourceFileKind::Interface)
return;

ClassDecl *source = nullptr;
for (auto member : cd->getMembers()) {
auto pbd = dyn_cast<PatternBindingDecl>(member);
Expand Down
12 changes: 12 additions & 0 deletions test/ModuleInterface/requires-stored-property-inits-attr.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface)
// RUN: %FileCheck %s < %t.swiftinterface

// CHECK: @requires_stored_property_inits public class RequiresStoredPropertyInits
@requires_stored_property_inits
public class RequiresStoredPropertyInits {
// CHECK: final public let a: Swift.Int{{$}}
public let a: Int = 0

public init() {}
}