Skip to content

[cxx-interop] Do not over-release objects returned by synthesized C++ methods #76139

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 2 commits into from
Aug 30, 2024
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
7 changes: 5 additions & 2 deletions lib/ClangImporter/SwiftDeclSynthesizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2112,6 +2112,8 @@ clang::CXXMethodDecl *SwiftDeclSynthesizer::synthesizeCXXForwardingMethod(
}
newMethod->setParams(params);

clang::Sema::SynthesizedFunctionScope scope(clangSema, newMethod);

// Create a new Clang diagnostic pool to capture any diagnostics
// emitted during the construction of the method.
clang::sema::DelayedDiagnosticPool diagPool{
Expand Down Expand Up @@ -2159,8 +2161,9 @@ clang::CXXMethodDecl *SwiftDeclSynthesizer::synthesizeCXXForwardingMethod(
clang::SourceLocation());
if (!memberCall.isUsable())
return nullptr;
auto returnStmt = clang::ReturnStmt::Create(clangCtx, clang::SourceLocation(),
memberCall.get(), nullptr);
auto returnStmt =
clangSema.BuildReturnStmt(clang::SourceLocation(), memberCall.get())
.get();

// Check if there were any Clang errors during the construction
// of the method body.
Expand Down
12 changes: 12 additions & 0 deletions test/Interop/Cxx/class/inheritance/Inputs/functions-objc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#import <Foundation/Foundation.h>

@interface C : NSObject
@end

struct Base {
C *_Nonnull non_virtual_method() const;
virtual C *_Nonnull virtual_method() const;
int a;
};

struct Derived : Base {};
6 changes: 6 additions & 0 deletions test/Interop/Cxx/class/inheritance/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ module VirtualMethods {
header "virtual-methods.h"
requires cplusplus
}

module FunctionsObjC {
header "functions-objc.h"
requires cplusplus
requires objc
}
31 changes: 31 additions & 0 deletions test/Interop/Cxx/class/inheritance/functions-objc-irgen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %target-swift-emit-irgen %s -I %S/Inputs -cxx-interoperability-mode=default -Xcc -fno-exceptions -Xcc -fno-objc-exceptions | %FileCheck %s
// REQUIRES: objc_interop

import FunctionsObjC

// CHECK: define linkonce_odr noundef ptr @_ZNK7Derived36__synthesizedBaseCall_virtual_methodEv(ptr noundef nonnull align {{4|8}} dereferenceable(12) %[[THIS:.*]])
// CHECK: %[[THIS_ADDR:.*]] = alloca ptr,
// CHECK: store ptr %[[THIS]], ptr %[[THIS_ADDR]],
// CHECK: %[[THIS1:.*]] = load ptr, ptr %[[THIS_ADDR]],
// CHECK: %[[VTABLE:.*]] = load ptr, ptr %[[THIS1]],
// CHECK: %[[VFN:.*]] = getelementptr inbounds ptr, ptr %[[VTABLE]], i64 0
// CHECK: %[[V0:.*]] = load ptr, ptr %[[VFN]],
// CHECK: %[[CALL:.*]] = call noundef ptr %[[V0]](ptr noundef nonnull align {{4|8}} dereferenceable(12) %[[THIS1]])
// CHECK: ret ptr %[[CALL]]

// CHECK: define linkonce_odr noundef ptr @_ZNK7Derived40__synthesizedBaseCall_non_virtual_methodEv(ptr noundef nonnull align {{4|8}} dereferenceable(12) %[[THIS:.*]])
// CHECK: %[[THIS_ADDR:.*]] = alloca ptr,
// CHECK: store ptr %[[THIS]], ptr %[[THIS_ADDR]],
// CHECK: %[[THIS1:.*]] = load ptr, ptr %[[THIS_ADDR]],
// CHECK: %[[CALL:.*]] = call noundef ptr @_ZNK4Base18non_virtual_methodEv(ptr noundef nonnull align {{4|8}} dereferenceable(12) %[[THIS1]])
// CHECK: ret ptr %[[CALL]]

func testBaseMethodCall() -> C {
let derived = Derived()
return derived.non_virtual_method()
}

func testBaseVirtualMethodCall() -> C {
let derived = Derived()
return derived.virtual_method()
}