Skip to content

[Sema] Always create memberwise init when deriving AdditiveArithmetic. #25495

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 2 commits into from
Jun 15, 2019
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
26 changes: 16 additions & 10 deletions lib/Sema/DerivedConformanceAdditiveArithmetic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ static ValueDecl *getProtocolRequirement(ProtocolDecl *proto, Identifier name) {
return lookup.front();
}

// Get the effective memberwise initializer of the given nominal type, or create
// it if it does not exist.
static ConstructorDecl *getOrCreateEffectiveMemberwiseInitializer(
TypeChecker &TC, NominalTypeDecl *nominal) {
auto &C = nominal->getASTContext();
if (auto *initDecl = nominal->getEffectiveMemberwiseInitializer())
return initDecl;
auto *initDecl = createImplicitConstructor(
TC, nominal, ImplicitConstructorKind::Memberwise);
nominal->addMember(initDecl);
C.addSynthesizedDecl(initDecl);
return initDecl;
}

// Return true if given nominal type has a `let` stored with an initial value.
static bool hasLetStoredPropertyWithInitialValue(NominalTypeDecl *nominal) {
return llvm::any_of(nominal->getStoredProperties(), [&](VarDecl *v) {
Expand Down Expand Up @@ -302,16 +316,6 @@ static ValueDecl *deriveAdditiveArithmetic_zero(DerivedConformance &derived) {
auto &TC = derived.TC;
auto &C = TC.Context;

// The implicit memberwise constructor must be explicitly created so that it
// can called when synthesizing the `zero` property getter. Normally, the
// memberwise constructor is synthesized during SILGen, which is too late.
if (!nominal->getEffectiveMemberwiseInitializer()) {
auto *initDecl = createImplicitConstructor(
TC, nominal, ImplicitConstructorKind::Memberwise);
nominal->addMember(initDecl);
C.addSynthesizedDecl(initDecl);
}

auto returnInterfaceTy = nominal->getDeclaredInterfaceType();
auto returnTy = parentDC->mapTypeIntoContext(returnInterfaceTy);

Expand All @@ -338,6 +342,8 @@ DerivedConformance::deriveAdditiveArithmetic(ValueDecl *requirement) {
// Diagnose conformances in disallowed contexts.
if (checkAndDiagnoseDisallowedContext(requirement))
return nullptr;
// Create memberwise initializer for nominal type if it doesn't already exist.
getOrCreateEffectiveMemberwiseInitializer(TC, Nominal);
if (requirement->getBaseName() == TC.Context.getIdentifier("+"))
return deriveMathOperator(*this, Add);
if (requirement->getBaseName() == TC.Context.getIdentifier("-"))
Expand Down
7 changes: 7 additions & 0 deletions test/Sema/struct_additive_arithmetic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ struct NoMemberwiseInitializer<T : AdditiveArithmetic> : AdditiveArithmetic {
var value: T
init(randomLabel value: T) { self.value = value }
}
struct NoMemberwiseInitializerCustomZero: AdditiveArithmetic {
var x: Float
static var zero: Self { return NoMemberwiseInitializerCustomZero(0) }
init(_ x: Float) {
self.x = x
}
}
struct NoMemberwiseInitializerExtended<T> {
var value: T
init(_ value: T) {
Expand Down