Skip to content

Sema: Fix inherited designated init synthesis when there's a 'where' clause but no new generic parameters #35727

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
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
80 changes: 41 additions & 39 deletions lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,51 +424,53 @@ configureGenericDesignatedInitOverride(ASTContext &ctx,
moduleDecl, superclassDecl);

GenericSignature genericSig;

// Inheriting initializers that have their own generic parameters
auto *genericParams = superclassCtor->getGenericParams();
if (genericParams) {
SmallVector<GenericTypeParamDecl *, 4> newParams;

// First, clone the superclass constructor's generic parameter list,
// but change the depth of the generic parameters to be one greater
// than the depth of the subclass.
unsigned depth = 0;
if (auto genericSig = classDecl->getGenericSignature())
depth = genericSig->getGenericParams().back()->getDepth() + 1;

for (auto *param : genericParams->getParams()) {
auto *newParam = new (ctx) GenericTypeParamDecl(classDecl,
param->getName(),
SourceLoc(),
depth,
param->getIndex());
newParams.push_back(newParam);
}
auto superclassCtorSig = superclassCtor->getGenericSignature();
auto superclassSig = superclassDecl->getGenericSignature();

// We don't have to clone the requirements, because they're not
// used for anything.
genericParams = GenericParamList::create(ctx,
SourceLoc(),
newParams,
SourceLoc(),
ArrayRef<RequirementRepr>(),
SourceLoc());
if (superclassCtorSig.getPointer() != superclassSig.getPointer()) {
SmallVector<GenericTypeParamDecl *, 4> newParams;
SmallVector<GenericTypeParamType *, 1> newParamTypes;

// Build a generic signature for the derived class initializer.
// Inheriting initializers that have their own generic parameters
if (genericParams) {
// First, clone the superclass constructor's generic parameter list,
// but change the depth of the generic parameters to be one greater
// than the depth of the subclass.
unsigned depth = 0;
if (auto genericSig = classDecl->getGenericSignature())
depth = genericSig->getGenericParams().back()->getDepth() + 1;

for (auto *param : genericParams->getParams()) {
auto *newParam = new (ctx) GenericTypeParamDecl(classDecl,
param->getName(),
SourceLoc(),
depth,
param->getIndex());
newParams.push_back(newParam);
}

// Add the generic parameters.
SmallVector<GenericTypeParamType *, 1> newParamTypes;
for (auto *newParam : newParams) {
newParamTypes.push_back(
newParam->getDeclaredInterfaceType()->castTo<GenericTypeParamType>());
// We don't have to clone the requirements, because they're not
// used for anything.
genericParams = GenericParamList::create(ctx,
SourceLoc(),
newParams,
SourceLoc(),
ArrayRef<RequirementRepr>(),
SourceLoc());

// Add the generic parameter types.
for (auto *newParam : newParams) {
newParamTypes.push_back(
newParam->getDeclaredInterfaceType()->castTo<GenericTypeParamType>());
}
}

auto superclassSig = superclassCtor->getGenericSignature();

// Build a generic signature for the derived class initializer.
unsigned superclassDepth = 0;
if (auto genericSig = superclassDecl->getGenericSignature())
superclassDepth = genericSig->getGenericParams().back()->getDepth() + 1;
if (superclassSig)
superclassDepth = superclassSig->getGenericParams().back()->getDepth() + 1;

// We're going to be substituting the requirements of the base class
// initializer to form the requirements of the derived class initializer.
Expand All @@ -490,13 +492,13 @@ configureGenericDesignatedInitOverride(ASTContext &ctx,
};

SmallVector<Requirement, 2> requirements;
for (auto reqt : superclassSig->getRequirements())
for (auto reqt : superclassCtorSig->getRequirements())
if (auto substReqt = reqt.subst(substFn, lookupConformanceFn))
requirements.push_back(*substReqt);

// Now form the substitution map that will be used to remap parameter
// types.
subMap = SubstitutionMap::get(superclassSig,
subMap = SubstitutionMap::get(superclassCtorSig,
substFn, lookupConformanceFn);

genericSig = evaluateOrDefault(
Expand Down
22 changes: 22 additions & 0 deletions test/SILGen/designated_init_inheritance_with_where_clause.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %target-swift-emit-silgen -primary-file %s | %FileCheck %s

public protocol Ungulate {}
public protocol Domesticated {}

public class Horse<U: Ungulate> {
// CHECK-LABEL: sil [serialized] [exact_self_class] [ossa] @$s45designated_init_inheritance_with_where_clause5HorseCACyxGycfC : $@convention(method) <U where U : Ungulate> (@thick Horse<U>.Type) -> @owned Horse<U> {
// CHECK-LABEL: sil [ossa] @$s45designated_init_inheritance_with_where_clause5HorseCACyxGycfc : $@convention(method) <U where U : Ungulate> (@owned Horse<U>) -> @owned Horse<U> {
public init() { }

// CHECK-LABEL: sil [serialized] [exact_self_class] [ossa] @$s45designated_init_inheritance_with_where_clause5HorseCACyxGycAA12DomesticatedRzrlufC : $@convention(method) <U where U : Domesticated, U : Ungulate> (@thick Horse<U>.Type) -> @owned Horse<U> {
// CHECK-LABEL: sil [ossa] @$s45designated_init_inheritance_with_where_clause5HorseCACyxGycAA12DomesticatedRzrlufc : $@convention(method) <U where U : Domesticated, U : Ungulate> (@owned Horse<U>) -> @owned Horse<U> {
public init() where U: Domesticated { }
}

public class Pony<U : Ungulate> : Horse<U> {
// CHECK-LABEL: sil [serialized] [exact_self_class] [ossa] @$s45designated_init_inheritance_with_where_clause4PonyCACyxGycfC : $@convention(method) <U where U : Ungulate> (@thick Pony<U>.Type) -> @owned Pony<U> {
// CHECK-LABEL: sil [ossa] @$s45designated_init_inheritance_with_where_clause4PonyCACyxGycfc : $@convention(method) <U where U : Ungulate> (@owned Pony<U>) -> @owned Pony<U> {

// CHECK-LABEL: sil [serialized] [exact_self_class] [ossa] @$s45designated_init_inheritance_with_where_clause4PonyCACyxGycAA12DomesticatedRzrlufC : $@convention(method) <U where U : Domesticated, U : Ungulate> (@thick Pony<U>.Type) -> @owned Pony<U> {
// CHECK-LABEL: sil [ossa] @$s45designated_init_inheritance_with_where_clause4PonyCACyxGycAA12DomesticatedRzrlufc : $@convention(method) <U where U : Domesticated, U : Ungulate> (@owned Pony<U>) -> @owned Pony<U> {
}