Skip to content

[cxx-interop] Fix import virtual methods with rvalue ref params #83453

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
Aug 1, 2025
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
17 changes: 12 additions & 5 deletions lib/ClangImporter/SwiftDeclSynthesizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2176,11 +2176,18 @@ clang::CXXMethodDecl *SwiftDeclSynthesizer::synthesizeCXXForwardingMethod(
for (size_t i = 0; i < newMethod->getNumParams(); ++i) {
auto *param = newMethod->getParamDecl(i);
auto type = param->getType();
if (type->isReferenceType())
type = type->getPointeeType();
args.push_back(new (clangCtx) clang::DeclRefExpr(
clangCtx, param, false, type, clang::ExprValueKind::VK_LValue,
clang::SourceLocation()));
clang::Expr *argExpr = new (clangCtx) clang::DeclRefExpr(
clangCtx, param, false, type.getNonReferenceType(),
clang::ExprValueKind::VK_LValue, clang::SourceLocation());
if (type->isRValueReferenceType()) {
argExpr = clangSema
.BuildCXXNamedCast(
clang::SourceLocation(), clang::tok::kw_static_cast,
clangCtx.getTrivialTypeSourceInfo(type), argExpr,
clang::SourceRange(), clang::SourceRange())
.get();
}
args.push_back(argExpr);
}
auto memberCall = clangSema.BuildCallExpr(
nullptr, memberExpr, clang::SourceLocation(), args,
Expand Down
5 changes: 5 additions & 0 deletions test/Interop/Cxx/foreign-reference/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ module Printed {
header "printed.h"
requires cplusplus
}

module VirtMethodWithRvalRef {
header "virtual-methods-with-rvalue-reference.h"
requires cplusplus
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "swift/bridging"

class CxxForeignRef;

void retain(CxxForeignRef * obj);
void release(CxxForeignRef * obj);

struct NonTrivial {
NonTrivial(const NonTrivial &other);
~NonTrivial();
};

class CxxForeignRef {
public:
CxxForeignRef(const CxxForeignRef &) = delete;
CxxForeignRef() = default;

virtual void takesRValRef(NonTrivial &&);
} SWIFT_SHARED_REFERENCE(retain, release);

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %target-swift-frontend -typecheck -verify -I %S/Inputs -I %swift_src_root/lib/ClangImporter/SwiftBridging %s -cxx-interoperability-mode=default -disable-availability-checking

import VirtMethodWithRvalRef

func f(_ x: CxxForeignRef, _ y: NonTrivial) {
x.takesRValRef(consuming: y)
}