Skip to content

[cxx-interop] Test std::optional with AddressableParameters #82809

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
12 changes: 8 additions & 4 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4326,11 +4326,15 @@ namespace {
Impl.SwiftContext.AllocateCopy(retType.getAsString())),
decl->getLocation());
}
} else {
Impl.SwiftContext.evaluator.cacheOutput(
LifetimeDependenceInfoRequest{result},
Impl.SwiftContext.AllocateCopy(lifetimeDependencies));
}
// Cache the dependencies even if we did not infer any. This prevents
// Swift from trying to infer lifetimes using pure-Swift's means, i.e.
// LifetimeDependenceInfoRequest, which would prematurely populate the
// protocol conformance table for the imported C++ type, causing subtle
// issues with conformances to overlay types, such as CxxOptional.
Impl.SwiftContext.evaluator.cacheOutput(
LifetimeDependenceInfoRequest{result},
Impl.SwiftContext.AllocateCopy(lifetimeDependencies));

for (auto [idx, param] : llvm::enumerate(decl->parameters())) {
if (isEscapable(param->getType()))
Expand Down
14 changes: 12 additions & 2 deletions test/Interop/Cxx/stdlib/use-std-optional.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -cxx-interoperability-mode=swift-5.9)
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -cxx-interoperability-mode=swift-6)
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -cxx-interoperability-mode=upcoming-swift)
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -cxx-interoperability-mode=upcoming-swift -enable-experimental-feature AddressableParameters)
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -cxx-interoperability-mode=upcoming-swift -Xcc -std=c++20)
//
// REQUIRES: executable_test
// REQUIRES: swift_feature_AddressableParameters

import StdlibUnittest
import StdOptional
Expand Down Expand Up @@ -58,10 +60,18 @@ StdOptionalTestSuite.test("std::optional init(_:Wrapped)") {

// FIXME: making these variables immutable triggers a miscompile on Linux
// (https://github.com/swiftlang/swift/issues/82765)
var optBoolT = StdOptionalBool(true)
var optBoolF = StdOptionalBool(false)
var optBoolTMutable = StdOptionalBool(true)
var optBoolFMutable = StdOptionalBool(false)
expectTrue(optBoolTMutable.pointee)
expectFalse(optBoolFMutable.pointee)

// If AddressableParameters are enabled, this issue does not happen.
#if hasFeature(AddressableParameters)
let optBoolT = StdOptionalBool(true)
let optBoolF = StdOptionalBool(false)
expectTrue(optBoolT.pointee)
expectFalse(optBoolF.pointee)
#endif

let optString = StdOptionalString(std.string("abc"))
expectEqual(std.string("abc"), optString.pointee)
Expand Down