-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Sema] Synthesize default values for memberwise init #19743
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
Changes from all commits
6f7d20b
6d00a57
e8bc662
bc7cb33
2bd99ee
dcedba7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
#include "swift/AST/ASTContext.h" | ||
#include "swift/AST/DiagnosticsSIL.h" | ||
#include "swift/AST/ForeignErrorConvention.h" | ||
#include "swift/AST/GenericEnvironment.h" | ||
Azoy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include "swift/AST/GenericSignature.h" | ||
#include "swift/AST/ParameterList.h" | ||
#include "swift/AST/Module.h" | ||
|
@@ -3340,12 +3341,41 @@ void DelayedArgument::emitDefaultArgument(SILGenFunction &SGF, | |
const DefaultArgumentStorage &info, | ||
SmallVectorImpl<ManagedValue> &args, | ||
size_t &argIndex) { | ||
auto value = SGF.emitApplyOfDefaultArgGenerator(info.loc, | ||
info.defaultArgsOwner, | ||
info.destIndex, | ||
info.resultType, | ||
info.origResultType); | ||
|
||
RValue value; | ||
bool isStoredPropertyDefaultArg = false; | ||
|
||
// Check if this is a synthesized memberwise constructor for stored property | ||
// default values | ||
if (auto ctor = dyn_cast<ConstructorDecl>(info.defaultArgsOwner.getDecl())) { | ||
if (ctor->isMemberwiseInitializer() && ctor->isImplicit()) { | ||
auto param = ctor->getParameters()->get(info.destIndex); | ||
|
||
if (auto var = param->getStoredProperty()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work across modules? Normally you can’t call a memberwise initializer since they’re internal, but if a module is built with -enable-testing and imported with @testable import you can. I might be wrong but I don’t see ParamDecl::getStoredProperty() serialized anywhere. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to spin up a quick test to test the current behavior, and I don't believe that we currently deserialize any default value from a module, but rather just the arg kind and the string representation. I'd be curious to hear how we could go about setting the stored property arg after we deserialize (maybe just take param name and look for the corresponding property?). Right now this emits an apply to the default arg generator (which there is none, so I believe the linker will complain...). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the right solution is to change the code they serializes a ParamDecl to serialize a cross reference to the corresponding VarDecl. I would try to avoid a name-based lookup here. @jrose-apple might have more suggestions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think emitting an apply to the default arg generator is the correct behavior. The fact that the expression comes from a stored property shouldn't matter except when generating the function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Emitting a default arg generator in this case doesn't work because the module with the stored property default arg doesn't emit a default arg generator function. So when we go to deserialize a stored property default arg, if we emit a default arg generator apply then we're going to be linking against undefined symbols. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hm. Why doesn't it? Isn't this a bug? (under |
||
// This is a stored property default arg. Do not emit a call to the | ||
// default arg generator, but rather the variable initializer expression | ||
isStoredPropertyDefaultArg = true; | ||
|
||
auto pbd = var->getParentPatternBinding(); | ||
auto entry = pbd->getPatternEntryForVarDecl(var); | ||
auto subs = info.defaultArgsOwner.getSubstitutions(); | ||
|
||
value = SGF.emitApplyOfStoredPropertyInitializer(info.loc, | ||
entry, subs, | ||
info.resultType, | ||
info.origResultType, | ||
SGFContext()); | ||
} | ||
} | ||
} | ||
|
||
if (!isStoredPropertyDefaultArg) { | ||
value = SGF.emitApplyOfDefaultArgGenerator(info.loc, | ||
info.defaultArgsOwner, | ||
info.destIndex, | ||
info.resultType, | ||
info.origResultType); | ||
} | ||
|
||
SmallVector<ManagedValue, 4> loweredArgs; | ||
SmallVector<DelayedArgument, 4> delayedArgs; | ||
Optional<ForeignErrorConvention> errorConvention = None; | ||
|
Uh oh!
There was an error while loading. Please reload this page.