Skip to content

Commit 7f2787e

Browse files
committed
AST: Lazily compute initializer interface type
1 parent f3f07ec commit 7f2787e

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5902,7 +5902,6 @@ class ConstructorDecl : public AbstractFunctionDecl {
59025902

59035903
/// Get the interface type of the initializing constructor.
59045904
Type getInitializerInterfaceType();
5905-
void setInitializerInterfaceType(Type t);
59065905

59075906
/// Get the typechecked call to super.init expression, which needs to be
59085907
/// inserted at the end of the initializer by SILGen.

lib/AST/Decl.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5417,18 +5417,6 @@ void AbstractFunctionDecl::computeType(AnyFunctionType::ExtInfo info) {
54175417

54185418
// (Self) -> (Args...) -> Result
54195419
if (hasSelf) {
5420-
// Constructors have an initializer type that takes an instance
5421-
// instead of a metatype.
5422-
if (auto *ctor = dyn_cast<ConstructorDecl>(this)) {
5423-
auto initSelfParam = computeSelfParam(this, /*isInitializingCtor=*/true);
5424-
Type initFuncTy;
5425-
if (sig)
5426-
initFuncTy = GenericFunctionType::get(sig, {initSelfParam}, funcTy);
5427-
else
5428-
initFuncTy = FunctionType::get({initSelfParam}, funcTy);
5429-
ctor->setInitializerInterfaceType(initFuncTy);
5430-
}
5431-
54325420
// Substitute in our own 'self' parameter.
54335421
auto selfParam = computeSelfParam(this);
54345422
if (sig)
@@ -5806,11 +5794,24 @@ Type ConstructorDecl::getResultInterfaceType() const {
58065794
}
58075795

58085796
Type ConstructorDecl::getInitializerInterfaceType() {
5809-
return InitializerInterfaceType;
5810-
}
5797+
if (InitializerInterfaceType)
5798+
return InitializerInterfaceType;
58115799

5812-
void ConstructorDecl::setInitializerInterfaceType(Type t) {
5813-
InitializerInterfaceType = t;
5800+
// Lazily calculate initializer type.
5801+
auto funcTy = getInterfaceType()->castTo<AnyFunctionType>()->getResult();
5802+
assert(funcTy->is<FunctionType>());
5803+
5804+
// Constructors have an initializer type that takes an instance
5805+
// instead of a metatype.
5806+
auto initSelfParam = computeSelfParam(this, /*isInitializingCtor=*/true);
5807+
Type initFuncTy;
5808+
if (auto *sig = getGenericSignature())
5809+
initFuncTy = GenericFunctionType::get(sig, {initSelfParam}, funcTy);
5810+
else
5811+
initFuncTy = FunctionType::get({initSelfParam}, funcTy);
5812+
InitializerInterfaceType = initFuncTy;
5813+
5814+
return InitializerInterfaceType;
58145815
}
58155816

58165817
ConstructorDecl::BodyInitKind

0 commit comments

Comments
 (0)