From 3dfbf156c767f75995699ec92c4b6b747a1c4dab Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 2 Feb 2021 23:06:16 -0500 Subject: [PATCH] Sema: Fix inherited designated init synthesis when there's a 'where' clause but no new generic parameters SE-0267 allows where clauses on declarations without their own generic parameters, which means we can longer assume that if getGenericParams() returns nullptr on a constructor, then the constructor has the same generic signature as its parent class. Instead, explicitly compare the superclass constructor's generic signature with that of the superclass, and if they don't match, compute a new generic signature for the override. Fixes https://bugs.swift.org/browse/SR-14118 / rdar://73764827. --- lib/Sema/CodeSynthesis.cpp | 80 ++++++++++--------- ...d_init_inheritance_with_where_clause.swift | 22 +++++ 2 files changed, 63 insertions(+), 39 deletions(-) create mode 100644 test/SILGen/designated_init_inheritance_with_where_clause.swift diff --git a/lib/Sema/CodeSynthesis.cpp b/lib/Sema/CodeSynthesis.cpp index 657799e92f174..3a94c12c83402 100644 --- a/lib/Sema/CodeSynthesis.cpp +++ b/lib/Sema/CodeSynthesis.cpp @@ -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 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(), - SourceLoc()); + if (superclassCtorSig.getPointer() != superclassSig.getPointer()) { + SmallVector newParams; + SmallVector 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 newParamTypes; - for (auto *newParam : newParams) { - newParamTypes.push_back( - newParam->getDeclaredInterfaceType()->castTo()); + // We don't have to clone the requirements, because they're not + // used for anything. + genericParams = GenericParamList::create(ctx, + SourceLoc(), + newParams, + SourceLoc(), + ArrayRef(), + SourceLoc()); + + // Add the generic parameter types. + for (auto *newParam : newParams) { + newParamTypes.push_back( + newParam->getDeclaredInterfaceType()->castTo()); + } } - 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. @@ -490,13 +492,13 @@ configureGenericDesignatedInitOverride(ASTContext &ctx, }; SmallVector 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( diff --git a/test/SILGen/designated_init_inheritance_with_where_clause.swift b/test/SILGen/designated_init_inheritance_with_where_clause.swift new file mode 100644 index 0000000000000..f36d51855b2d6 --- /dev/null +++ b/test/SILGen/designated_init_inheritance_with_where_clause.swift @@ -0,0 +1,22 @@ +// RUN: %target-swift-emit-silgen -primary-file %s | %FileCheck %s + +public protocol Ungulate {} +public protocol Domesticated {} + +public class Horse { + // CHECK-LABEL: sil [serialized] [exact_self_class] [ossa] @$s45designated_init_inheritance_with_where_clause5HorseCACyxGycfC : $@convention(method) (@thick Horse.Type) -> @owned Horse { + // CHECK-LABEL: sil [ossa] @$s45designated_init_inheritance_with_where_clause5HorseCACyxGycfc : $@convention(method) (@owned Horse) -> @owned Horse { + public init() { } + + // CHECK-LABEL: sil [serialized] [exact_self_class] [ossa] @$s45designated_init_inheritance_with_where_clause5HorseCACyxGycAA12DomesticatedRzrlufC : $@convention(method) (@thick Horse.Type) -> @owned Horse { + // CHECK-LABEL: sil [ossa] @$s45designated_init_inheritance_with_where_clause5HorseCACyxGycAA12DomesticatedRzrlufc : $@convention(method) (@owned Horse) -> @owned Horse { + public init() where U: Domesticated { } +} + +public class Pony : Horse { + // CHECK-LABEL: sil [serialized] [exact_self_class] [ossa] @$s45designated_init_inheritance_with_where_clause4PonyCACyxGycfC : $@convention(method) (@thick Pony.Type) -> @owned Pony { + // CHECK-LABEL: sil [ossa] @$s45designated_init_inheritance_with_where_clause4PonyCACyxGycfc : $@convention(method) (@owned Pony) -> @owned Pony { + + // CHECK-LABEL: sil [serialized] [exact_self_class] [ossa] @$s45designated_init_inheritance_with_where_clause4PonyCACyxGycAA12DomesticatedRzrlufC : $@convention(method) (@thick Pony.Type) -> @owned Pony { + // CHECK-LABEL: sil [ossa] @$s45designated_init_inheritance_with_where_clause4PonyCACyxGycAA12DomesticatedRzrlufc : $@convention(method) (@owned Pony) -> @owned Pony { +}