Skip to content

[cxx-interop] Require lifetime annotations in safe mode #78771

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
Jan 22, 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
20 changes: 18 additions & 2 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3980,8 +3980,8 @@ namespace {
if (decl->getTemplatedKind() == clang::FunctionDecl::TK_FunctionTemplate)
return;

if (!result->getASTContext().LangOpts.hasFeature(
Feature::LifetimeDependence))
auto &ASTContext = result->getASTContext();
if (!ASTContext.LangOpts.hasFeature(Feature::LifetimeDependence))
return;

SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
Expand Down Expand Up @@ -4013,9 +4013,11 @@ namespace {
const auto dependencyVecSize = swiftParams->size() + hasSelf;
SmallBitVector inheritLifetimeParamIndicesForReturn(dependencyVecSize);
SmallBitVector scopedLifetimeParamIndicesForReturn(dependencyVecSize);
SmallBitVector paramHasAnnotation(dependencyVecSize);
std::map<unsigned, SmallBitVector> inheritedArgDependences;
auto processLifetimeBound = [&](unsigned idx, Type ty) {
warnForEscapableReturnType();
paramHasAnnotation[idx] = true;
if (ty->isEscapable())
scopedLifetimeParamIndicesForReturn[idx] = true;
else
Expand All @@ -4039,6 +4041,7 @@ namespace {
param == clang::LifetimeCaptureByAttr::INVALID)
continue;

paramHasAnnotation[idx] = true;
if (isa<clang::CXXMethodDecl>(decl) &&
param == clang::LifetimeCaptureByAttr::THIS) {
auto [it, inserted] = inheritedArgDependences.try_emplace(
Expand Down Expand Up @@ -4110,6 +4113,19 @@ namespace {
LifetimeDependenceInfoRequest{result},
Impl.SwiftContext.AllocateCopy(lifetimeDependencies));
}
if (ASTContext.LangOpts.hasFeature(Feature::AllowUnsafeAttribute) &&
ASTContext.LangOpts.hasFeature(Feature::SafeInterop)) {
for (auto [idx, param] : llvm::enumerate(decl->parameters())) {
if (swiftParams->get(idx)->getInterfaceType()->isEscapable())
continue;
if (param->hasAttr<clang::NoEscapeAttr>() || paramHasAnnotation[idx])
continue;
// We have a nonescapabe parameter that does not have its lifetime
// annotated nor is it marked noescape.
auto attr = new (ASTContext) UnsafeAttr(/*implicit=*/true);
result->getAttrs().add(attr);
}
}
Impl.diagnoseTargetDirectly(decl);
}

Expand Down
13 changes: 13 additions & 0 deletions test/Interop/Cxx/class/safe-interop-mode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ using VecOfInt = std::vector<int>;
using SafeTuple = std::tuple<int, int, int>;
using UnsafeTuple = std::tuple<int, int*, int>;

View safeFunc(View v1 [[clang::noescape]], View v2 [[clang::lifetimebound]]);
// Second non-escapable type is not annotated in any way.
void unsafeFunc(View v1 [[clang::noescape]], View v2);

//--- test.swift

import Test
Expand Down Expand Up @@ -112,3 +116,12 @@ func useCppSpan(x: SpanOfInt) { // expected-note{{reference to unsafe type alias
// expected-warning@+1{{global function 'useCppSpan2' has an interface that is not memory-safe}}
func useCppSpan2(x: SpanOfIntAlias) { // expected-note{{reference to unsafe type alias 'SpanOfIntAlias'}}
}

func useSafeLifetimeAnnotated(v: View) {
let _ = safeFunc(v, v)
}

func useUnsafeLifetimeAnnotated(v: View) {
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}
unsafeFunc(v, v) // expected-note{{reference to unsafe global function 'unsafeFunc'}}
}