Skip to content

[cxx-interop] Do not import arithmetic operators with rvalue reference parameters #80763

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
Apr 14, 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
8 changes: 8 additions & 0 deletions lib/ClangImporter/ImportName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1967,6 +1967,14 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
case clang::OverloadedOperatorKind::OO_GreaterEqual:
case clang::OverloadedOperatorKind::OO_AmpAmp:
case clang::OverloadedOperatorKind::OO_PipePipe: {
// If the operator has a parameter that is an rvalue reference, it would
// cause name lookup collision with an overload that has lvalue reference
// parameter, if it exists.
for (auto paramDecl : functionDecl->parameters()) {
if (paramDecl->getType()->isRValueReferenceType())
return ImportedName();
}

auto operatorName =
isa<clang::CXXMethodDecl>(functionDecl)
? getOperatorName(swiftCtx, op)
Expand Down
23 changes: 23 additions & 0 deletions test/Interop/Cxx/operators/Inputs/non-member-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ inline bool operator==(const ClassWithOperatorEqualsParamUnnamed &,
return false;
}

struct RValueArithmetic {
int value;
};

RValueArithmetic operator+(const RValueArithmetic &lhs,
RValueArithmetic &&rhs) {
return {lhs.value + rhs.value};
}

struct LValueAndRValueArithmetic {
int value;
};

LValueAndRValueArithmetic operator+(const LValueAndRValueArithmetic &lhs,
const LValueAndRValueArithmetic &rhs) {
return {lhs.value + rhs.value};
}

LValueAndRValueArithmetic operator+(const LValueAndRValueArithmetic &lhs,
LValueAndRValueArithmetic &&rhs) {
return {lhs.value + rhs.value};
}

// Make sure that we don't crash on templated operators
template<typename T> struct S {};
template<typename T> S<T> operator+(S<T> lhs, S<T> rhs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@

// CHECK: func && (lhs: LoadableBoolWrapper, rhs: LoadableBoolWrapper) -> LoadableBoolWrapper
// CHECK-NEXT: func || (lhs: LoadableBoolWrapper, rhs: LoadableBoolWrapper) -> LoadableBoolWrapper

// CHECK-NOT: func + (lhs: RValueArithmetic

// CHECK: func + (lhs: LValueAndRValueArithmetic, rhs: LValueAndRValueArithmetic) -> LValueAndRValueArithmetic
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ var rhsBool = LoadableBoolWrapper(value: false)

let resultAmpAmp = lhsBool && rhsBool
let resultPipePipe = lhsBool && rhsBool

let lhsRValue = RValueArithmetic(value: 123)
let rhsRValue = RValueArithmetic(value: 146)
let resultRValue = lhsRValue + rhsRValue // expected-error {{binary operator '+' cannot be applied to two 'RValueArithmetic' operands}}

let lhsLRValue = LValueAndRValueArithmetic(value: 123)
let rhsLRValue = LValueAndRValueArithmetic(value: 146)
let resultLRValue = lhsLRValue + rhsLRValue
7 changes: 7 additions & 0 deletions test/Interop/Cxx/operators/non-member-inline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,11 @@ OperatorsTestSuite.test("UnnamedParameterInOperator.equal") {
expectFalse(lhs == rhs)
}

OperatorsTestSuite.test("LValueAndRValueArithmetic.+") {
let lhs = LValueAndRValueArithmetic(value: 123)
let rhs = LValueAndRValueArithmetic(value: 146)

expectEqual(269, (lhs + rhs).value)
}

runAllTests()