Skip to content

[cxx-interop] Import rvalue references as consuming parameters #77878

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
Dec 3, 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
24 changes: 14 additions & 10 deletions lib/ClangImporter/ImportName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1478,10 +1478,14 @@ static bool suppressFactoryMethodAsInit(const clang::ObjCMethodDecl *method,
}

static void
addEmptyArgNamesForClangFunction(const clang::FunctionDecl *funcDecl,
SmallVectorImpl<StringRef> &argumentNames) {
for (size_t i = 0; i < funcDecl->param_size(); ++i)
argumentNames.push_back(StringRef());
addDefaultArgNamesForClangFunction(const clang::FunctionDecl *funcDecl,
SmallVectorImpl<StringRef> &argumentNames) {
for (size_t i = 0; i < funcDecl->param_size(); ++i) {
if (funcDecl->getParamDecl(i)->getType()->isRValueReferenceType())
argumentNames.push_back("consuming");
else
argumentNames.push_back(StringRef());
}
if (funcDecl->isVariadic())
argumentNames.push_back(StringRef());
}
Expand Down Expand Up @@ -1863,7 +1867,7 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
// If we couldn't find a constructor decl, bail.
if (!ctor)
return ImportedName();
addEmptyArgNamesForClangFunction(ctor, argumentNames);
addDefaultArgNamesForClangFunction(ctor, argumentNames);
break;
}

Expand All @@ -1876,7 +1880,7 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
if (toType->isBooleanType()) {
isFunction = true;
baseName = "__convertToBool";
addEmptyArgNamesForClangFunction(conversionDecl, argumentNames);
addDefaultArgNamesForClangFunction(conversionDecl, argumentNames);
break;
}
return ImportedName();
Expand Down Expand Up @@ -1928,7 +1932,7 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
: clang::getOperatorSpelling(op);
baseName = swiftCtx.getIdentifier(operatorName).str();
isFunction = true;
addEmptyArgNamesForClangFunction(functionDecl, argumentNames);
addDefaultArgNamesForClangFunction(functionDecl, argumentNames);
if (auto cxxMethod = dyn_cast<clang::CXXMethodDecl>(functionDecl)) {
if (op == clang::OverloadedOperatorKind::OO_Star &&
cxxMethod->param_empty()) {
Expand All @@ -1947,7 +1951,7 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
case clang::OverloadedOperatorKind::OO_Call:
baseName = "callAsFunction";
isFunction = true;
addEmptyArgNamesForClangFunction(functionDecl, argumentNames);
addDefaultArgNamesForClangFunction(functionDecl, argumentNames);
break;
case clang::OverloadedOperatorKind::OO_Subscript: {
auto returnType = functionDecl->getReturnType();
Expand All @@ -1965,7 +1969,7 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
result.info.accessorKind = ImportedAccessorKind::SubscriptSetter;
}
isFunction = true;
addEmptyArgNamesForClangFunction(functionDecl, argumentNames);
addDefaultArgNamesForClangFunction(functionDecl, argumentNames);
break;
}
default:
Expand Down Expand Up @@ -1996,7 +2000,7 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,

if (auto function = dyn_cast<clang::FunctionDecl>(D)) {
isFunction = true;
addEmptyArgNamesForClangFunction(function, argumentNames);
addDefaultArgNamesForClangFunction(function, argumentNames);
}
break;

Expand Down
54 changes: 30 additions & 24 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2415,6 +2415,7 @@ ClangImporter::Implementation::importParameterType(
auto attrs = getImportTypeAttrs(param, /*isParam=*/true);
Type swiftParamTy;
bool isInOut = false;
bool isConsuming = false;
bool isParamTypeImplicitlyUnwrapped = false;

// Sometimes we import unavailable typedefs as enums. If that's the case,
Expand Down Expand Up @@ -2448,7 +2449,7 @@ ClangImporter::Implementation::importParameterType(
return std::nullopt;
} else if (isa<clang::ReferenceType>(paramTy) &&
isa<clang::TemplateTypeParmType>(paramTy->getPointeeType())) {
// We don't support rvalue reference / universal perfect ref, bail.
// We don't support universal reference, bail.
if (paramTy->isRValueReferenceType()) {
addImportDiagnosticFn(Diagnostic(diag::rvalue_ref_params_not_imported));
return std::nullopt;
Expand All @@ -2469,26 +2470,25 @@ ClangImporter::Implementation::importParameterType(
if (!swiftParamTy) {
// C++ reference types are brought in as direct
// types most commonly.
auto refPointeeType =
importer::getCxxReferencePointeeTypeOrNone(paramTy.getTypePtr());
if (refPointeeType) {
if (auto refPointeeType =
getCxxReferencePointeeTypeOrNone(paramTy.getTypePtr())) {
// We don't support reference type to a dependent type, just bail.
if ((*refPointeeType)->isDependentType()) {
return std::nullopt;
}

// We don't support rvalue reference types, just bail.
if (paramTy->isRValueReferenceType()) {
addImportDiagnosticFn(Diagnostic(diag::rvalue_ref_params_not_imported));
return std::nullopt;
}

bool isRvalueRef = paramTy->isRValueReferenceType();
// A C++ parameter of type `const <type> &` or `<type> &` becomes `<type>`
// or `inout <type>` in Swift. Note that SILGen will use the indirect
// parameter convention for such a type.
// or `inout <type>`. Moreover, `const <type> &&` or `<type> &&`
// becomes `<type>` or `consuming <type>`. Note that SILGen will use the
// indirect parameter convention for such a type.
paramTy = *refPointeeType;
if (!paramTy.isConstQualified())
isInOut = true;
if (!paramTy.isConstQualified()) {
if (isRvalueRef)
isConsuming = true;
else
isInOut = true;
}
}
}

Expand Down Expand Up @@ -2552,7 +2552,7 @@ ClangImporter::Implementation::importParameterType(
if (isInOut && isDirectUseOfForeignReferenceType(paramTy, swiftParamTy))
isInOut = false;

return ImportParameterTypeResult{swiftParamTy, isInOut,
return ImportParameterTypeResult{swiftParamTy, isInOut, isConsuming,
isParamTypeImplicitlyUnwrapped};
}

Expand Down Expand Up @@ -2606,7 +2606,7 @@ static ParamDecl *getParameterInfo(ClangImporter::Implementation *impl,
const clang::ParmVarDecl *param,
const Identifier &name,
const swift::Type &swiftParamTy,
const bool isInOut,
const bool isInOut, const bool isConsuming,
const bool isParamTypeImplicitlyUnwrapped) {
// Figure out the name for this parameter.
Identifier bodyName = impl->importFullName(param, impl->CurrentVersion)
Expand All @@ -2632,10 +2632,12 @@ static ParamDecl *getParameterInfo(ClangImporter::Implementation *impl,

// Foreign references are already references so they don't need to be passed
// as inout.
paramInfo->setSpecifier(isInOut ? ParamSpecifier::InOut
: (param->getAttr<clang::LifetimeBoundAttr>()
? ParamSpecifier::Borrowing
: ParamSpecifier::Default));
paramInfo->setSpecifier(
isConsuming ? ParamSpecifier::Consuming
: (isInOut ? ParamSpecifier::InOut
: (param->getAttr<clang::LifetimeBoundAttr>()
? ParamSpecifier::Borrowing
: ParamSpecifier::Default)));
paramInfo->setInterfaceType(swiftParamTy);
impl->recordImplicitUnwrapForDecl(paramInfo, isParamTypeImplicitlyUnwrapped);

Expand Down Expand Up @@ -2702,6 +2704,7 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
}
auto swiftParamTy = swiftParamTyOpt->swiftTy;
bool isInOut = swiftParamTyOpt->isInOut;
bool isConsuming = swiftParamTyOpt->isConsuming;
bool isParamTypeImplicitlyUnwrapped =
swiftParamTyOpt->isParamTypeImplicitlyUnwrapped;

Expand All @@ -2710,8 +2713,9 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
if (index < argNames.size())
name = argNames[index];

auto paramInfo = getParameterInfo(this, param, name, swiftParamTy, isInOut,
isParamTypeImplicitlyUnwrapped);
auto paramInfo =
getParameterInfo(this, param, name, swiftParamTy, isInOut, isConsuming,
isParamTypeImplicitlyUnwrapped);
parameters.push_back(paramInfo);
++index;
}
Expand Down Expand Up @@ -3296,6 +3300,7 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
}
auto swiftParamTy = swiftParamTyOpt->swiftTy;
bool isInOut = swiftParamTyOpt->isInOut;
bool isConsuming = swiftParamTyOpt->isConsuming;
bool isParamTypeImplicitlyUnwrapped =
swiftParamTyOpt->isParamTypeImplicitlyUnwrapped;

Expand Down Expand Up @@ -3345,8 +3350,9 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
++nameIndex;

// Set up the parameter info
auto paramInfo = getParameterInfo(this, param, name, swiftParamTy, isInOut,
isParamTypeImplicitlyUnwrapped);
auto paramInfo =
getParameterInfo(this, param, name, swiftParamTy, isInOut, isConsuming,
isParamTypeImplicitlyUnwrapped);

// Determine whether we have a default argument.
if (kind == SpecialMethodKind::Regular ||
Expand Down
2 changes: 2 additions & 0 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,8 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
swift::Type swiftTy;
/// If the parameter is or not inout.
bool isInOut;
/// If the parameter should be imported as consuming.
bool isConsuming;
/// If the parameter is implicitly unwrapped or not.
bool isParamTypeImplicitlyUnwrapped;
};
Expand Down
3 changes: 3 additions & 0 deletions lib/SILOptimizer/Mandatory/MoveOnlyAddressCheckerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3347,6 +3347,9 @@ void MoveOnlyAddressCheckerPImpl::rewriteUses(
bool isFinalConsume = consumes.claimConsume(destroyPair.first, bits);

// Remove destroys that are not the final consuming use.
// TODO: for C++ types we do not want to remove destroys as the caller is
// still responsible for invoking the dtor for the moved-from object.
// See GH Issue #77894.
if (!isFinalConsume) {
destroyPair.first->eraseFromParent();
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// CHECK-NEXT: }

// CHECK: struct UsingBaseConstructorWithParam {
// CHECK-NEXT: init(consuming _: consuming IntBox)
// CHECK-NEXT: init(_: IntBox)
// CHECK-NEXT: init(_: UInt32)
// CHECK-NEXT: init(_: Int32)
Expand All @@ -29,6 +30,7 @@

// CHECK: struct UsingBaseConstructorEmpty {
// CHECK-NEXT: init()
// CHECK-NEXT: init(consuming _: consuming Empty)
// CHECK-NEXT: init(_: Empty)
// CHECK-NEXT: var value: Int32
// CHECK-NEXT: }
6 changes: 3 additions & 3 deletions test/Interop/Cxx/operators/member-inline-typechecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ writeOnlyIntArray[2] = 654
let writeOnlyValue = writeOnlyIntArray[2]

var readOnlyRvalueParam = ReadOnlyRvalueParam()
let readOnlyRvalueVal = readOnlyRvalueParam[1] // expected-error {{value of type 'ReadOnlyRvalueParam' has no subscripts}}
let readOnlyRvalueVal = readOnlyRvalueParam[consuming: 1]

var readWriteRvalueParam = ReadWriteRvalueParam()
let readWriteRvalueVal = readWriteRvalueParam[1] // expected-error {{value of type 'ReadWriteRvalueParam' has no subscripts}}
let readWriteRvalueVal = readWriteRvalueParam[consuming: 1]

var readWriteRvalueGetterParam = ReadWriteRvalueGetterParam()
let readWriteRvalueGetterVal = readWriteRvalueGetterParam[1]
let readWriteRvalueGetterVal = readWriteRvalueGetterParam[consuming: 1]

var diffTypesArray = DifferentTypesArray()
let diffTypesResultInt: Int32 = diffTypesArray[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import Test

public func test() {
var x: CInt = 2
acceptRValueRef(x) // expected-error {{cannot find 'acceptRValueRef' in scope}}
// CHECK: note: function 'acceptRValueRef' unavailable (cannot import)
// CHECK: note: C++ functions with rvalue reference parameters are unavailable in Swift
acceptRValueRef(consuming: x)

notStdMove(x) // expected-error {{cannot find 'notStdMove' in scope}}
// CHECK: note: function 'notStdMove' unavailable (cannot import)
Expand Down