Skip to content

SILGen: Fix assertion failure when emitting foreign-to-native thunk for a method with inout 'self' #34456

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
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
26 changes: 20 additions & 6 deletions lib/SILGen/SILGenBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,17 @@ static ManagedValue emitManagedParameter(SILGenFunction &SGF, SILLocation loc,

/// Get the type of each parameter, filtering out empty tuples.
static SmallVector<CanType, 8>
getParameterTypes(AnyFunctionType::CanParamArrayRef params) {
getParameterTypes(AnyFunctionType::CanParamArrayRef params,
bool hasSelfParam=false) {
SmallVector<CanType, 8> results;
for (auto param : params) {
assert(!param.isInOut() && !param.isVariadic());
for (auto n : indices(params)) {
bool isSelf = (hasSelfParam ? n == params.size() - 1 : false);

const auto &param = params[n];
assert(isSelf || !param.isInOut() &&
"Only the 'self' parameter can be inout in a bridging thunk");
assert(!param.isVariadic());

if (param.getPlainType()->isVoid())
continue;
results.push_back(param.getPlainType());
Expand Down Expand Up @@ -1723,10 +1730,11 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
};

{
bool hasSelfParam = fd->hasImplicitSelfDecl();
auto foreignFormalParams =
getParameterTypes(foreignCI.LoweredType.getParams());
getParameterTypes(foreignCI.LoweredType.getParams(), hasSelfParam);
auto nativeFormalParams =
getParameterTypes(nativeCI.LoweredType.getParams());
getParameterTypes(nativeCI.LoweredType.getParams(), hasSelfParam);

for (unsigned nativeParamIndex : indices(params)) {
// Bring the parameter to +1.
Expand Down Expand Up @@ -1762,7 +1770,7 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {

maybeAddForeignErrorArg();

bool isSelf = nativeParamIndex == params.size() - 1;
bool isSelf = (hasSelfParam && nativeParamIndex == params.size() - 1);

if (memberStatus.isInstance()) {
// Leave space for `self` to be filled in later.
Expand All @@ -1787,6 +1795,12 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
F.mapTypeIntoContext(foreignFormalParams[nativeParamIndex])
->getCanonicalType();

if (isSelf) {
assert(!nativeCI.LoweredType.getParams()[nativeParamIndex].isInOut() ||
nativeFormalType == foreignFormalType &&
"Cannot bridge 'self' parameter if passed inout");
}

auto foreignParam = foreignFnTy->getParameters()[foreignArgIndex++];
SILType foreignLoweredTy =
F.mapTypeIntoContext(foreignParam.getSILStorageType(
Expand Down
3 changes: 3 additions & 0 deletions test/SILGen/Inputs/foreign_to_native_inout_self_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
typedef struct {} MyIterator __attribute__((swift_name("MyIterator")));

void MyIteratorNext(MyIterator *self) __attribute__((swift_name("MyIterator.next(self:)")));
15 changes: 15 additions & 0 deletions test/SILGen/foreign_to_native_inout_self.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %target-swift-frontend -emit-silgen %s -import-objc-header %S/Inputs/foreign_to_native_inout_self_helper.h | %FileCheck %s

protocol FakeIterator {
mutating func next()
}

extension MyIterator : FakeIterator {}

// CHECK-LABEL: sil shared [serializable] [thunk] [ossa] @$sSo10MyIteratora4nextyyFTO : $@convention(method) (@inout MyIterator) -> () {
// CHECK: bb0(%0 : $*MyIterator):
// CHECK: [[FN:%.*]] = function_ref @MyIteratorNext : $@convention(c) (@inout MyIterator) -> ()
// CHECK: apply [[FN]](%0) : $@convention(c) (@inout MyIterator) -> ()
// CHECK: [[RESULT:%.*]] = tuple ()
// CHECK: return [[RESULT]] : $()
// CHECK: }