From 0923c044683aead45384f083fb1ac146a3a0c31a Mon Sep 17 00:00:00 2001 From: Mykola Pokhylets Date: Sun, 16 Mar 2025 16:13:18 +0100 Subject: [PATCH] [WIP] Fix https://github.com/swiftlang/swift/issues/80014 --- lib/SIL/IR/SILType.cpp | 7 ++++++- lib/SILOptimizer/Analysis/RegionAnalysis.cpp | 4 +++- .../sending_mutable_captures.swift | 20 +++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/Concurrency/sending_mutable_captures.swift diff --git a/lib/SIL/IR/SILType.cpp b/lib/SIL/IR/SILType.cpp index 78f027f3ede9b..551ff2a6584ac 100644 --- a/lib/SIL/IR/SILType.cpp +++ b/lib/SIL/IR/SILType.cpp @@ -1247,7 +1247,12 @@ SILType SILType::removingAnyMoveOnlyWrapping(const SILFunction *fn) { } bool SILType::isSendable(SILFunction *fn) const { - return getASTType()->isSendableType(); + switch (getCategory()) { + case SILValueCategory::Object: + return getASTType()->isSendableType(); + case SILValueCategory::Address: + return false; + } } Type SILType::getRawLayoutSubstitutedLikeType() const { diff --git a/lib/SILOptimizer/Analysis/RegionAnalysis.cpp b/lib/SILOptimizer/Analysis/RegionAnalysis.cpp index ac959e18f6733..70b7d2b9d1f85 100644 --- a/lib/SILOptimizer/Analysis/RegionAnalysis.cpp +++ b/lib/SILOptimizer/Analysis/RegionAnalysis.cpp @@ -2539,11 +2539,13 @@ class PartitionOpTranslator { auto trackableDest = tryToTrackValue(dest); if (!trackableDest) return; + if (requireOperands) { + builder.addRequire(trackableDest->getRepresentative().getValue()); + } for (Operand *op : srcCollection) { if (auto trackableSrc = tryToTrackValue(op->get())) { if (requireOperands) { builder.addRequire(trackableSrc->getRepresentative().getValue()); - builder.addRequire(trackableDest->getRepresentative().getValue()); } builder.addMerge(trackableDest->getRepresentative().getValue(), op); } diff --git a/test/Concurrency/sending_mutable_captures.swift b/test/Concurrency/sending_mutable_captures.swift new file mode 100644 index 0000000000000..3b5b144619485 --- /dev/null +++ b/test/Concurrency/sending_mutable_captures.swift @@ -0,0 +1,20 @@ +// RUN: %target-swift-frontend -emit-sil -swift-version 6 %s -o /dev/null -verify + +func foo() { + var x: X? = X() + // expected-error@+2{{sending value of non-Sendable type '() -> Void' risks causing data races}} + // expected-note@+1{{Passing value of non-Sendable type '() -> Void' as a 'sending' argument to global function 'user' risks causing races in between local and caller code}} + user { + x?.bar() + x = nil + } + x = nil // expected-note{{access can happen concurrently}} +} + +func user(_ block: sending @escaping @isolated(any) () -> Void) { + +} + +final class X: Sendable { + func bar() {} +}