Skip to content

Fix "Undefined symbol" linker error with default argument for inherited initializer #7171

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
11 changes: 8 additions & 3 deletions lib/AST/Parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ ParameterList *ParameterList::clone(const ASTContext &C,

// Remap the ParamDecls inside of the ParameterList.
for (auto &decl : params) {
bool hadDefaultArgument =decl->getDefaultValue() != nullptr;
bool hadDefaultArgument =
decl->getDefaultArgumentKind() == DefaultArgumentKind::Normal;

decl = new (C) ParamDecl(decl);
if (options & Implicit)
Expand All @@ -101,8 +102,12 @@ ParameterList *ParameterList::clone(const ASTContext &C,
decl->setName(C.getIdentifier("argument"));

// If we're inheriting a default argument, mark it as such.
if (hadDefaultArgument && (options & Inherited)) {
decl->setDefaultArgumentKind(DefaultArgumentKind::Inherited);
// FIXME: Figure out how to clone default arguments as well.
if (hadDefaultArgument) {
if (options & Inherited)
decl->setDefaultArgumentKind(DefaultArgumentKind::Inherited);
else
decl->setDefaultArgumentKind(DefaultArgumentKind::None);
}
}

Expand Down
12 changes: 7 additions & 5 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3657,11 +3657,13 @@ void ArgEmitter::emitShuffle(Expr *inner,
// Emit the inner expression.
SmallVector<ManagedValue, 8> innerArgs;
SmallVector<InOutArgument, 2> innerInOutArgs;
ArgEmitter(SGF, Rep, ClaimedParamsRef(innerParams), innerArgs, innerInOutArgs,
/*foreign error*/ None, /*foreign self*/ ImportAsMemberStatus(),
(innerSpecialDests ? ArgSpecialDestArray(*innerSpecialDests)
: Optional<ArgSpecialDestArray>()))
.emitTopLevel(ArgumentSource(inner), innerOrigParamType);
if (!innerParams.empty()) {
ArgEmitter(SGF, Rep, ClaimedParamsRef(innerParams), innerArgs, innerInOutArgs,
/*foreign error*/ None, /*foreign self*/ ImportAsMemberStatus(),
(innerSpecialDests ? ArgSpecialDestArray(*innerSpecialDests)
: Optional<ArgSpecialDestArray>()))
.emitTopLevel(ArgumentSource(inner), innerOrigParamType);
}

// Make a second pass to split the inner arguments correctly.
{
Expand Down
18 changes: 18 additions & 0 deletions test/SILGen/default_arguments_generic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,21 @@ func bar() {
// CHECK: apply [[ZANG_DFLT_1]]<Int, Double>
Zim<Int>.zang(Double.self, 22)
}

protocol Initializable {
init()
}
struct Generic<T: Initializable> {
init(_ value: T = T()) {}
}
struct InitializableImpl: Initializable {
init() {}
}
// CHECK-LABEL: sil hidden @_TF25default_arguments_generic17testInitializableFT_T_
func testInitializable() {
// The ".init" is required to trigger the crash that used to happen.
_ = Generic<InitializableImpl>.init()
// CHECK: [[INIT:%.+]] = function_ref @_TFV25default_arguments_generic7GenericCfxGS0_x_
// CHECK: function_ref @_TIFV25default_arguments_generic7GenericcFxGS0_x_A_ : $@convention(thin) <τ_0_0 where τ_0_0 : Initializable> () -> @out τ_0_0
// CHECK: apply [[INIT]]<InitializableImpl>({{%.+}}, {{%.+}}) : $@convention(method) <τ_0_0 where τ_0_0 : Initializable> (@in τ_0_0, @thin Generic<τ_0_0>.Type) -> Generic<τ_0_0>
} // CHECK: end sil function '_TF25default_arguments_generic17testInitializableFT_T_'
10 changes: 10 additions & 0 deletions test/Serialization/Inputs/inherited-initializer-base.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
open class Base {
public init(_ value: Int = 0) {}
}

public protocol Initializable {
init()
}
open class GenericBase<T: Initializable> {
public init(_ value: T = T()) {}
}
54 changes: 54 additions & 0 deletions test/Serialization/inherited-initializer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// RUN: rm -rf %t && mkdir -p %t
// RUN: %target-swift-frontend -emit-module -o %t -module-name InheritedInitializerBase %S/Inputs/inherited-initializer-base.swift
// RUN: %target-swift-frontend -emit-silgen -I %t %s | %FileCheck %s

import InheritedInitializerBase

class InheritsInit : Base {}

// CHECK-LABEL: sil hidden @_TF4main10testSimpleFT_T_
func testSimple() {
// CHECK: [[INIT:%.+]] = function_ref @_TFC4main12InheritsInitCfSiS0_
// CHECK: [[DEFAULT:%.+]] = function_ref @_TIFC24InheritedInitializerBase4BasecFSiS0_A_
// CHECK: [[ARG:%.+]] = apply [[DEFAULT]]()
// CHECK: apply [[INIT]]([[ARG]], {{%.+}})
_ = InheritsInit()

// CHECK: [[INIT:%.+]] = function_ref @_TFC4main12InheritsInitCfSiS0_
// CHECK: [[VALUE:%.+]] = integer_literal $Builtin.Int2048, 5
// CHECK: [[ARG:%.+]] = apply {{%.+}}([[VALUE]], {{%.+}}) : $@convention(method) (Builtin.Int2048, @thin Int.Type) -> Int
// CHECK: apply [[INIT]]([[ARG]], {{%.+}})
_ = InheritsInit(5)
} // CHECK: end sil function '_TF4main10testSimpleFT_T_'

struct Reinitializable<T>: Initializable {
init() {}
}

class GenericSub<T: Initializable> : GenericBase<T> {}
class ModifiedGenericSub<U> : GenericBase<Reinitializable<U>> {}
class NonGenericSub : GenericBase<Reinitializable<Int>> {}

// CHECK-LABEL: sil hidden @_TF4main11testGenericFT_T_
func testGeneric() {
// CHECK: [[INIT:%.+]] = function_ref @_TFC4main10GenericSubCfxGS0_x_
// CHECK: [[TYPE:%.+]] = metatype $@thick GenericSub<Reinitializable<Int8>>.Type
// CHECK: [[DEFAULT:%.+]] = function_ref @_TIFC24InheritedInitializerBase11GenericBasecFxGS0_x_A_
// CHECK: apply [[DEFAULT]]<Reinitializable<Int8>>({{%.+}})
// CHECK: apply [[INIT]]<Reinitializable<Int8>>({{%.+}}, [[TYPE]])
_ = GenericSub<Reinitializable<Int8>>.init() // works around SR-3806

// CHECK: [[INIT:%.+]] = function_ref @_TFC4main18ModifiedGenericSubCfGVS_15Reinitializablex_GS0_x_
// CHECK: [[TYPE:%.+]] = metatype $@thick ModifiedGenericSub<Int16>.Type
// CHECK: [[DEFAULT:%.+]] = function_ref @_TIFC24InheritedInitializerBase11GenericBasecFxGS0_x_A_
// CHECK: apply [[DEFAULT]]<Reinitializable<Int16>>({{%.+}})
// CHECK: apply [[INIT]]<Int16>({{%.+}}, [[TYPE]])
_ = ModifiedGenericSub<Int16>()

// CHECK: [[INIT:%.+]] = function_ref @_TFC4main13NonGenericSubCfGVS_15ReinitializableSi_S0_
// CHECK: [[TYPE:%.+]] = metatype $@thick NonGenericSub.Type
// CHECK: [[DEFAULT:%.+]] = function_ref @_TIFC24InheritedInitializerBase11GenericBasecFxGS0_x_A_
// CHECK: apply [[DEFAULT]]<Reinitializable<Int>>({{%.+}})
// CHECK: apply [[INIT]]({{%.+}}, [[TYPE]])
_ = NonGenericSub()
} // CHECK: end sil function '_TF4main11testGenericFT_T_'