Skip to content

Commit 0ab71e1

Browse files
authored
Merge branch 'release/6.0' into pick-wip-ktoso-changelog
2 parents f84a0bc + 38df64a commit 0ab71e1

33 files changed

+353
-194
lines changed

CHANGELOG.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,84 @@ And the module structure to support such applications looks like this:
5959
checking, and now even an actor which is "on a queue which is targeting
6060
another specific queue" can be properly detected using these APIs.
6161

62+
* [SE-0423][]:
63+
You can now use `@preconcurrency` attribute to replace static actor isolation
64+
checking with dynamic checks for witnesses of synchronous nonisolated protocol
65+
requirements when the witness is isolated. This is common when Swift programs
66+
need to interoperate with frameworks written in C/C++/Objective-C whose
67+
implementations cannot participate in static data race safety.
68+
69+
```swift
70+
public protocol ViewDelegateProtocol {
71+
func respondToUIEvent()
72+
}
73+
```
74+
75+
It's now possible for a `@MainActor`-isolated type to conform to
76+
`ViewDelegateProtocol` by marking conformance declaration as `@preconcurrency`:
77+
78+
```swift
79+
@MainActor
80+
class MyViewController: ViewDelegateProtocol {
81+
func respondToUIEvent() {
82+
// implementation...
83+
}
84+
}
85+
```
86+
87+
The compiler would emit dynamic checks into the `respondToUIEvent()` witness
88+
to make sure that it's always executed in `@MainActor` isolated context.
89+
90+
Additionally, the compiler would emit dynamic actor isolation checks for:
91+
92+
- `@objc` thunks of synchronous actor-isolated members of classes.
93+
94+
- Synchronous actor-isolated function values passed to APIs that
95+
erase actor isolation and haven't yet adopted strict concurrency checking.
96+
97+
- Call-sites of synchronous actor-isolated functions imported from Swift 6 libraries.
98+
99+
The dynamic actor isolation checks can be disabled using the flag
100+
`-disable-dynamic-actor-isolation`.
101+
102+
* [SE-0418][]:
103+
104+
The compiler would now automatically employ `Sendable` on functions
105+
and key path literal expressions that cannot capture non-Sendable values.
106+
107+
This includes partially-applied and unapplied instance methods of `Sendable`
108+
types, as well as non-local functions. Additionally, it is now disallowed
109+
to utilize `@Sendable` on instance methods of non-Sendable types.
110+
111+
Let's use the following type to illustrate the new inference rules:
112+
113+
```swift
114+
public struct User {
115+
var name: String
116+
117+
func getAge() -> Int { ... }
118+
}
119+
```
120+
121+
Key path `\User.name` would be inferred as `WritableKeyPath<User, String> & Sendable`
122+
because it doesn't capture any non-Sendable values.
123+
124+
The same applies to keypath-as-function conversions:
125+
126+
```swift
127+
let _: @Sendable (User) -> String = \User.name // Ok
128+
```
129+
130+
A function value produced by an un-applied reference to `getAge`
131+
would be marked as `@Sendable` because `User` is a `Sendable` struct:
132+
133+
```swift
134+
let _ = User.getAge // Inferred as `@Sendable (User) -> @Sendable () -> Int`
135+
136+
let user = User(...)
137+
user.getAge // Inferred as `@Sendable () -> Int`
138+
```
139+
62140
* [SE-0430][]:
63141

64142
Region Based Isolation is now extended to enable the application of an
@@ -10389,6 +10467,8 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
1038910467
[SE-0424]: https://github.com/apple/swift-evolution/blob/main/proposals/0424-custom-isolation-checking-for-serialexecutor.md
1039010468
[SE-0428]: https://github.com/apple/swift-evolution/blob/main/proposals/0428-resolve-distributed-actor-protocols.md
1039110469
[SE-0430]: https://github.com/apple/swift-evolution/blob/main/proposals/0430-transferring-parameters-and-results.md
10470+
[SE-0418]: https://github.com/apple/swift-evolution/blob/main/proposals/0418-inferring-sendable-for-methods.md
10471+
[SE-0423]: https://github.com/apple/swift-evolution/blob/main/proposals/0423-dynamic-actor-isolation.md
1039210472
[#64927]: <https://github.com/apple/swift/issues/64927>
1039310473
[#42697]: <https://github.com/apple/swift/issues/42697>
1039410474
[#42728]: <https://github.com/apple/swift/issues/42728>

include/swift/ABI/GenericContext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/ABI/MetadataRef.h"
2424
#include "swift/ABI/InvertibleProtocols.h"
2525
#include "swift/ABI/TrailingObjects.h"
26+
#include "swift/Basic/MathUtils.h"
2627
#include "swift/Demangling/Demangle.h"
2728

2829
namespace swift {
@@ -696,7 +697,7 @@ class TrailingGenericContextObjects<TargetSelf<Runtime>,
696697
if (!asSelf()->hasConditionalInvertedProtocols())
697698
return 0;
698699

699-
return countBitsUsed(getConditionalInvertedProtocols().rawBits());
700+
return popcount(getConditionalInvertedProtocols().rawBits());
700701
}
701702

702703
size_t numTrailingObjects(

include/swift/AST/Builtins.def

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,19 @@ BUILTIN_MISC_OPERATION_WITH_SILGEN(InjectEnumTag, "injectEnumTag", "", Special)
11071107
/// `any Actor` existential that refers to the local actor.
11081108
BUILTIN_MISC_OPERATION_WITH_SILGEN(DistributedActorAsAnyActor, "distributedActorAsAnyActor", "n", Special)
11091109

1110+
/// addressOfRawLayout: <T: ~Copyable>(_: borrowing T) -> Builtin.RawPointer
1111+
///
1112+
/// Returns a raw pointer to the address of the raw layout type. This address is
1113+
/// only valid during a borrow access of the raw layout type or until the value
1114+
/// is either moved or consumed.
1115+
///
1116+
/// Note: The purpose of this builtin is to get an opaque pointer to the address
1117+
/// of the raw layout type. We explicitly do not want the optimizer looking into
1118+
/// this pointer or address thereof to start assuming things about mutability or
1119+
/// immutability. This builtin _must_ persist throughout all of SIL and must be
1120+
/// lowered away at IRGen, no sooner.
1121+
BUILTIN_MISC_OPERATION_WITH_SILGEN(AddressOfRawLayout, "addressOfRawLayout", "n", Special)
1122+
11101123
/// Builtins for instrumentation added by sanitizers during SILGen.
11111124
#ifndef BUILTIN_SANITIZER_OPERATION
11121125
#define BUILTIN_SANITIZER_OPERATION(Id, Name, Attrs) BUILTIN(Id, Name, Attrs)

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ SUPPRESSIBLE_LANGUAGE_FEATURE(NoncopyableGenerics, 427, "Noncopyable generics")
196196
SUPPRESSIBLE_LANGUAGE_FEATURE(ConformanceSuppression, 426, "Suppressible inferred conformances")
197197
SUPPRESSIBLE_LANGUAGE_FEATURE(BitwiseCopyable2, 426, "BitwiseCopyable feature")
198198
LANGUAGE_FEATURE(BodyMacros, 415, "Function body macros")
199+
LANGUAGE_FEATURE(BuiltinAddressOfRawLayout, 0, "Builtin.addressOfRawLayout")
199200

200201
// Swift 6
201202
UPCOMING_FEATURE(ConciseMagicFile, 274, 6)

include/swift/Basic/LangOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef SWIFT_BASIC_LANGOPTIONS_H
1919
#define SWIFT_BASIC_LANGOPTIONS_H
2020

21+
#include "swift/AST/DiagnosticsFrontend.h"
2122
#include "swift/Basic/Feature.h"
2223
#include "swift/Basic/FixedBitSet.h"
2324
#include "swift/Basic/FunctionBodySkipping.h"
@@ -31,6 +32,7 @@
3132
#include "llvm/ADT/SmallString.h"
3233
#include "llvm/ADT/SmallVector.h"
3334
#include "llvm/ADT/StringRef.h"
35+
#include "llvm/Option/ArgList.h"
3436
#include "llvm/Support/Regex.h"
3537
#include "llvm/Support/VersionTuple.h"
3638
#include "llvm/Support/raw_ostream.h"
@@ -319,6 +321,9 @@ namespace swift {
319321
/// to the Swift language version.
320322
version::Version cxxInteropCompatVersion;
321323

324+
void setCxxInteropFromArgs(llvm::opt::ArgList &Args,
325+
swift::DiagnosticEngine &Diags);
326+
322327
bool CForeignReferenceTypes = false;
323328

324329
/// Imports getters and setters as computed properties.

include/swift/Basic/MathUtils.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
#ifndef SWIFT_BASIC_MATH_UTILS_H
1818
#define SWIFT_BASIC_MATH_UTILS_H
1919

20+
#include "Compiler.h"
2021
#include <cstddef>
22+
#include <cstdint>
23+
24+
#if SWIFT_COMPILER_IS_MSVC
25+
#include <intrin.h>
26+
#endif
2127

2228
namespace swift {
2329

@@ -33,6 +39,21 @@ static inline size_t roundUpToAlignMask(size_t size, size_t alignMask) {
3339
return (size + alignMask) & ~alignMask;
3440
}
3541

42+
static inline unsigned popcount(unsigned value) {
43+
#if SWIFT_COMPILER_IS_MSVC && (defined(_M_IX86) || defined(_M_X64))
44+
// The __popcnt intrinsic is only available when targetting x86{_64} with MSVC.
45+
return __popcnt(value);
46+
#elif __has_builtin(__builtin_popcount)
47+
return __builtin_popcount(value);
48+
#else
49+
// From llvm/ADT/bit.h which the runtime doesn't have access to (yet?)
50+
uint32_t v = value;
51+
v = v - ((v >> 1) & 0x55555555);
52+
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
53+
return int(((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24);
54+
#endif
55+
}
56+
3657
} // namespace swift
3758

3859
#endif // #ifndef SWIFT_BASIC_MATH_UTILS_H

include/swift/Option/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ def enable_experimental_cxx_interop :
758758

759759
def cxx_interoperability_mode :
760760
Joined<["-"], "cxx-interoperability-mode=">,
761-
Flags<[FrontendOption, ModuleInterfaceOption]>,
761+
Flags<[FrontendOption, ModuleInterfaceOption, SwiftSymbolGraphExtractOption]>,
762762
HelpText<"Enables C++ interoperability; pass 'default' to enable or 'off' to disable">;
763763

764764
def experimental_c_foreign_reference_types :

include/swift/SIL/AddressWalker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ TransitiveAddressWalker<Impl>::walk(SILValue projectedAddress) && {
276276
case BuiltinValueKind::ZeroInitializer:
277277
case BuiltinValueKind::GetEnumTag:
278278
case BuiltinValueKind::InjectEnumTag:
279+
case BuiltinValueKind::AddressOfRawLayout:
279280
callVisitUse(op);
280281
continue;
281282
default:

lib/AST/Builtins.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,15 @@ static ValueDecl *getInjectEnumTag(ASTContext &ctx, Identifier id) {
21412141
return builder.build(id);
21422142
}
21432143

2144+
static ValueDecl *getAddressOfRawLayout(ASTContext &ctx, Identifier id) {
2145+
BuiltinFunctionBuilder builder(ctx, /* genericParamCount */ 1);
2146+
2147+
builder.addParameter(makeGenericParam(), ParamSpecifier::Borrowing);
2148+
builder.setResult(makeConcrete(ctx.TheRawPointerType));
2149+
2150+
return builder.build(id);
2151+
}
2152+
21442153
/// An array of the overloaded builtin kinds.
21452154
static const OverloadedBuiltinKind OverloadedBuiltinKinds[] = {
21462155
OverloadedBuiltinKind::None,
@@ -3214,6 +3223,9 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
32143223

32153224
case BuiltinValueKind::DistributedActorAsAnyActor:
32163225
return getDistributedActorAsAnyActor(Context, Id);
3226+
3227+
case BuiltinValueKind::AddressOfRawLayout:
3228+
return getAddressOfRawLayout(Context, Id);
32173229
}
32183230

32193231
llvm_unreachable("bad builtin value!");

lib/AST/FeatureSet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ static bool usesFeatureExpressionMacroDefaultArguments(Decl *decl) {
359359
}
360360

361361
UNINTERESTING_FEATURE(BuiltinStoreRaw)
362+
UNINTERESTING_FEATURE(BuiltinAddressOfRawLayout)
362363

363364
// ----------------------------------------------------------------------------
364365
// MARK: - Upcoming Features

0 commit comments

Comments
 (0)