Skip to content

Fix property wrapper crasher #27619

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
merged 1 commit into from
Oct 12, 2019
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
38 changes: 38 additions & 0 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5006,6 +5006,44 @@ RValue SILGenFunction::emitApplyMethod(SILLocation loc, ConcreteDeclRef declRef,
return emission.apply(C);
}

RValue SILGenFunction::emitApplyOfPropertyWrapperBackingInitializer(
SILLocation loc,
VarDecl *var,
RValue &&originalValue,
SGFContext C) {
SILDeclRef constant(var, SILDeclRef::Kind::PropertyWrapperBackingInitializer);

SubstitutionMap subs;
auto varDC = var->getInnermostDeclContext();
if (auto genericSig = varDC->getGenericSignatureOfContext()) {
subs = SubstitutionMap::get(
genericSig,
[&](SubstitutableType *type) {
if (auto gp = type->getAs<GenericTypeParamType>()) {
return F.mapTypeIntoContext(gp);
}

return Type(type);
},
LookUpConformanceInModule(varDC->getParentModule()));
}

FormalEvaluationScope writebackScope(*this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want an argument scope here instead of a FormalEvaluationScope: @slavapestov your thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or actually, that may already be in CallEmission. I forgot.

Copy link
Contributor Author

@beccadax beccadax Oct 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like CallEmission::apply() calls CallEmission::applyFirstLevelCallee(), which calls CallEmission::applyNormalCall(), which creates an ArgumentScope before it calls CallEmission::emitArgumentsForNormalApply(). So I think ArgumentScope is covered, if I understand its purpose correctly.


auto callee = Callee::forDirect(*this, constant, subs, loc);
auto substFnType = callee.getSubstFormalType();

CallEmission emission(*this, std::move(callee), std::move(writebackScope));

PreparedArguments args(substFnType->getAs<AnyFunctionType>()->getParams());
args.add(loc, std::move(originalValue));
emission.addCallSite(loc, std::move(args),
substFnType->getResult()->getCanonicalType(),
/*throws=*/false);

return emission.apply(C);
}

/// Emit a literal that applies the various initializers.
RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
ConcreteDeclRef builtinInit;
Expand Down
40 changes: 0 additions & 40 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2235,46 +2235,6 @@ RValue SILGenFunction::emitApplyOfStoredPropertyInitializer(
subs, {}, calleeTypeInfo, ApplyOptions::None, C);
}

RValue SILGenFunction::emitApplyOfPropertyWrapperBackingInitializer(
SILLocation loc,
VarDecl *var,
RValue &&originalValue,
SGFContext C) {
SILDeclRef constant(var, SILDeclRef::Kind::PropertyWrapperBackingInitializer);
auto fnRef = ManagedValue::forUnmanaged(emitGlobalFunctionRef(loc, constant));
auto fnType = fnRef.getType().castTo<SILFunctionType>();

SubstitutionMap subs;
auto varDC = var->getInnermostDeclContext();
if (auto genericSig = varDC->getGenericSignatureOfContext()) {
subs = SubstitutionMap::get(
genericSig,
[&](SubstitutableType *type) {
if (auto gp = type->getAs<GenericTypeParamType>()) {
return F.mapTypeIntoContext(gp);
}

return Type(type);
},
LookUpConformanceInModule(varDC->getParentModule()));
}

auto substFnType = fnType->substGenericArgs(SGM.M, subs);

CanType resultType =
F.mapTypeIntoContext(var->getPropertyWrapperBackingPropertyType())
->getCanonicalType();
AbstractionPattern origResultType(resultType);
CalleeTypeInfo calleeTypeInfo(substFnType, origResultType, resultType);
ResultPlanPtr resultPlan =
ResultPlanBuilder::computeResultPlan(*this, calleeTypeInfo, loc, C);
ArgumentScope argScope(*this, loc);
SmallVector<ManagedValue, 2> args;
std::move(originalValue).getAll(args);
return emitApply(std::move(resultPlan), std::move(argScope), loc, fnRef, subs,
args, calleeTypeInfo, ApplyOptions::None, C);
}

RValue RValueEmitter::visitDestructureTupleExpr(DestructureTupleExpr *E,
SGFContext C) {
// Emit the sub-expression tuple and destructure it into elements.
Expand Down
10 changes: 10 additions & 0 deletions test/SILGen/property_wrappers_library_evolution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -o %t -enable-library-evolution %S/Inputs/property_wrapper_defs.swift
// RUN: %target-swift-emit-silgen -primary-file %s -I %t -enable-library-evolution
import property_wrapper_defs

// rdar://problem/55995892
// This is a crash that occurs only with -enable-library-evolution.

public enum E { case a }
struct M { @MyPublished private var e = E.a }