Skip to content

Commit 660b4b8

Browse files
committed
[cxx-interop] Cache empty lifetime dependencies for methods
If we could not infer lifetime dependencies for a C++ method, let's cache the empty result. This prevents the compiler from trying to evaluate this request using Swift Sema's algorithm. This change is meant to fix the following issue: With AddressableParameters enabled, instantiations of `std::optional<T>` lack the conformance to `protocol CxxOptional`, despite the synthesized conformance being added correctly to each instantiation. This happens because when importing `operator*()` of each instantiation, ClangImporter spins up a LifetimeDependenceInfoRequest for the incompletely imported C++ type. In particular, the synthesized protocol conformances aren't yet added at that point, since the conformance logic relies on type's members already being imported. The LifetimeDependenceInfoRequest triggers a ConformanceTable to be built and cached for the instantiation. Later, by the time the conformance synthesis logic runs, the ConformanceTable for the instantiation had already been populated, and won't be updated to add the new conformance.
1 parent b640b52 commit 660b4b8

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4236,11 +4236,15 @@ namespace {
42364236
Impl.SwiftContext.AllocateCopy(retType.getAsString())),
42374237
decl->getLocation());
42384238
}
4239-
} else {
4240-
Impl.SwiftContext.evaluator.cacheOutput(
4241-
LifetimeDependenceInfoRequest{result},
4242-
Impl.SwiftContext.AllocateCopy(lifetimeDependencies));
42434239
}
4240+
// Cache the dependencies even if we did not infer any. This prevents
4241+
// Swift from trying to infer lifetimes using pure-Swift's means, i.e.
4242+
// LifetimeDependenceInfoRequest, which would prematurely populate the
4243+
// protocol conformance table for the imported C++ type, causing subtle
4244+
// issues with conformances to overlay types, such as CxxOptional.
4245+
Impl.SwiftContext.evaluator.cacheOutput(
4246+
LifetimeDependenceInfoRequest{result},
4247+
Impl.SwiftContext.AllocateCopy(lifetimeDependencies));
42444248

42454249
for (auto [idx, param] : llvm::enumerate(decl->parameters())) {
42464250
if (isEscapable(param->getType()))

0 commit comments

Comments
 (0)