Skip to content

SILGen: Delay function conversions to types with opened archetype arguments. #18085

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
61 changes: 59 additions & 2 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1867,7 +1867,10 @@ class DelayedArgument {
/// A string r-value needs to be converted to a pointer type.
RValueStringToPointer,

LastRVKind = RValueStringToPointer,
/// A function conversion needs to occur.
FunctionConversion,

LastRVKind = FunctionConversion,

/// A default argument that needs to be evaluated.
DefaultArgument,
Expand Down Expand Up @@ -2038,6 +2041,7 @@ class DelayedArgument {
case LValueArrayToPointer:
case RValueArrayToPointer:
case RValueStringToPointer:
case FunctionConversion:
args[argIndex] = finishOriginalArgument(SGF);
return;
case DefaultArgument:
Expand Down Expand Up @@ -2121,7 +2125,8 @@ class DelayedArgument {
// Done with the recursive cases. Make sure we handled everything.
assert(isa<InOutToPointerExpr>(expr) ||
isa<ArrayToPointerExpr>(expr) ||
isa<StringToPointerExpr>(expr));
isa<StringToPointerExpr>(expr) ||
isa<FunctionConversionExpr>(expr));

switch (Kind) {
case InOut:
Expand Down Expand Up @@ -2154,6 +2159,17 @@ class DelayedArgument {
return SGF.emitStringToPointer(pointerExpr, stringValue,
pointerExpr->getType());
}
case FunctionConversion: {
auto funcConv = cast<FunctionConversionExpr>(expr);
auto optFuncValue = RV().RV;
auto funcValue =
emitBindOptionals(SGF, optFuncValue, funcConv->getSubExpr());
return {SGF.emitTransformedValue(funcConv, funcValue,
funcConv->getSubExpr()->getType()->getCanonicalType(),
funcConv->getType()->getCanonicalType(),
SGFContext()),
ManagedValue()};
}
}
llvm_unreachable("bad kind");
}
Expand Down Expand Up @@ -2747,6 +2763,37 @@ class ArgEmitter {
if (auto stringToPointer = dyn_cast<StringToPointerExpr>(expr)) {
return emitDelayedConversion(stringToPointer, original);
}

// Delay function conversions involving the opened Self type of an
// existential whose opening is itself delayed.
//
// This comes up when invoking protocol methods on an existential that
// have covariant arguments of function type with Self arguments, e.g.:
//
// protocol P {
// mutating func foo(_: (Self) -> Void)
// }
//
// func bar(x: inout P) {
// x.foo { y in return }
// }
//
// Although the type-erased method is presented as formally taking an
// argument of the existential type P, it still has a conversion thunk to
// perform type erasure on the argument coming from the underlying
// implementation. Since the `self` argument is inout, it isn't formally
// opened until late when formal accesses begin, so this closure conversion
// must also be deferred until after that occurs.
if (auto funcConv = dyn_cast<FunctionConversionExpr>(expr)) {
auto destTy = funcConv->getType()->castTo<AnyFunctionType>();
auto srcTy = funcConv->getSubExpr()->getType()->castTo<AnyFunctionType>();

if (destTy->hasOpenedExistential()
&& !srcTy->hasOpenedExistential()
&& destTy->getRepresentation() == srcTy->getRepresentation()) {
return emitDelayedConversion(funcConv, original);
}
}

// Any recursive cases we handle here need to be handled in
// DelayedArgument::finishOriginalExpr.
Expand Down Expand Up @@ -2828,6 +2875,16 @@ class ArgEmitter {
Args.push_back(ManagedValue());
return true;
}

bool emitDelayedConversion(FunctionConversionExpr *funcConv,
OriginalArgument original) {
auto rvalueExpr = lookThroughBindOptionals(funcConv->getSubExpr());
ManagedValue value = SGF.emitRValueAsSingleValue(rvalueExpr);
DelayedArguments.emplace_back(DelayedArgument::FunctionConversion,
value, original);
Args.push_back(ManagedValue());
return true;
}

static Expr *lookThroughBindOptionals(Expr *expr) {
while (true) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %target-swift-emit-silgen -verify %s

protocol P {
mutating func foo(_: (Self) -> Void)
}

func foo(x: inout P) {
x.foo { y in return }
}