Skip to content

Commit 44e09bc

Browse files
author
Nathan Hawes
authored
Merge branch 'main' into dont-allow-missing-args-if-trailing-and-not-function-type
2 parents cdcaa11 + 0d9ca20 commit 44e09bc

File tree

410 files changed

+8895
-4852
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

410 files changed

+8895
-4852
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ CHANGELOG
2828
Swift Next
2929
----------
3030

31+
* Whenever a reference to `Self` does not impede the usage of a protocol as a value type, or a protocol member on a value of protocol type, the same is now true for references to `[Self]` and `[Key : Self]`:
32+
33+
```swift
34+
protocol Copyable {
35+
func copy() -> Self
36+
func copy(count: Int) -> [Self]
37+
}
38+
39+
func test(c: Copyable) {
40+
let copy: Copyable = c.copy() // OK
41+
let copies: [Copyable] = c.copy(count: 5) // also OK
42+
}
43+
```
44+
3145
* [SE-0296][]:
3246

3347
Asynchronous programming is now natively supported using async/await. Asynchronous functions can be defined using `async`:

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ if(SWIFT_PROFDATA_FILE AND EXISTS ${SWIFT_PROFDATA_FILE})
196196
add_definitions("-fprofile-instr-use=${SWIFT_PROFDATA_FILE}")
197197
endif()
198198

199+
option(USE_SWIFT_ASYNC_LOWERING
200+
"Indicates if Swiftc should use async-specific lowering for async
201+
functions if it is supported for the target. The runtime also checks
202+
this setting before using async-specific attributes. This only applies
203+
to the async calling convention and not to the async context attribute."
204+
FALSE)
205+
199206
#
200207
# User-configurable Swift Standard Library specific options.
201208
#

docs/ABI/Mangling.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ Globals
167167
global ::= protocol-conformance protocol 'Wb' // base protocol witness table accessor
168168
global ::= type protocol-conformance 'Wl' // lazy protocol witness table accessor
169169

170+
global ::= global generic-signature? 'WJ' DIFFERENTIABILITY-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' // differentiability witness
171+
170172
global ::= type 'WV' // value witness table
171173
global ::= entity 'Wvd' // field offset
172174
global ::= entity 'WC' // resilient enum tag index
@@ -222,6 +224,9 @@ types where the metadata itself has unknown layout.)
222224
global ::= from-type to-type generic-signature? 'TR' // reabstraction thunk
223225
global ::= impl-function-type type 'Tz' // objc-to-swift-async completion handler block implementation
224226
global ::= impl-function-type type 'TZ' // objc-to-swift-async completion handler block implementation (predefined by runtime)
227+
global ::= from-type to-type generic-signature? 'TR' // reabstraction thunk
228+
global ::= impl-function-type type generic-signature? 'Tz' // objc-to-swift-async completion handler block implementation
229+
global ::= impl-function-type type generic-signature? 'TZ' // objc-to-swift-async completion handler block implementation (predefined by runtime)
225230
global ::= from-type to-type self-type generic-signature? 'Ty' // reabstraction thunk with dynamic 'Self' capture
226231
global ::= from-type to-type generic-signature? 'Tr' // obsolete mangling for reabstraction thunk
227232
global ::= entity generic-signature? type type* 'TK' // key path getter

docs/SIL.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,12 +1590,15 @@ Differentiability Witnesses
15901590
sil-differentiability-witness ::=
15911591
'sil_differentiability_witness'
15921592
sil-linkage?
1593+
'[' differentiability-kind ']'
15931594
'[' 'parameters' sil-differentiability-witness-function-index-list ']'
15941595
'[' 'results' sil-differentiability-witness-function-index-list ']'
15951596
generic-parameter-clause?
15961597
sil-function-name ':' sil-type
15971598
sil-differentiability-witness-body?
15981599

1600+
differentiability-kind ::= 'forward' | 'reverse' | 'normal' | 'linear'
1601+
15991602
sil-differentiability-witness-body ::=
16001603
'{' sil-differentiability-witness-entry?
16011604
sil-differentiability-witness-entry? '}'
@@ -1625,7 +1628,7 @@ based on the key.
16251628

16261629
::
16271630

1628-
sil_differentiability_witness hidden [parameters 0] [results 0] <T where T : Differentiable> @id : $@convention(thin) (T) -> T {
1631+
sil_differentiability_witness hidden [normal] [parameters 0] [results 0] <T where T : Differentiable> @id : $@convention(thin) (T) -> T {
16291632
jvp: @id_jvp : $@convention(thin) (T) -> (T, @owned @callee_guaranteed (T.TangentVector) -> T.TangentVector)
16301633
vjp: @id_vjp : $@convention(thin) (T) -> (T, @owned @callee_guaranteed (T.TangentVector) -> T.TangentVector)
16311634
}
@@ -7066,6 +7069,7 @@ differentiability_witness_function
70667069
sil-instruction ::=
70677070
'differentiability_witness_function'
70687071
'[' sil-differentiability-witness-function-kind ']'
7072+
'[' differentiability-kind ']'
70697073
'[' 'parameters' sil-differentiability-witness-function-index-list ']'
70707074
'[' 'results' sil-differentiability-witness-function-index-list ']'
70717075
generic-parameter-clause?
@@ -7074,7 +7078,7 @@ differentiability_witness_function
70747078
sil-differentiability-witness-function-kind ::= 'jvp' | 'vjp' | 'transpose'
70757079
sil-differentiability-witness-function-index-list ::= [0-9]+ (' ' [0-9]+)*
70767080

7077-
differentiability_witness_function [jvp] [parameters 0] [results 0] \
7081+
differentiability_witness_function [vjp] [reverse] [parameters 0] [results 0] \
70787082
<T where T: Differentiable> @foo : $(T) -> T
70797083

70807084
Looks up a differentiability witness function (JVP, VJP, or transpose) for
@@ -7086,6 +7090,7 @@ look up: ``[jvp]``, ``[vjp]``, or ``[transpose]``.
70867090
The remaining components identify the SIL differentiability witness:
70877091

70887092
- Original function name.
7093+
- Differentiability kind.
70897094
- Parameter indices.
70907095
- Result indices.
70917096
- Witness generic parameter clause (optional). When parsing SIL, the parsed

include/swift/ABI/Task.h

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,17 @@ class alignas(2 * alignof(void*)) Job {
8080
return Flags.getPriority();
8181
}
8282

83-
/// Run this job.
84-
void run(ExecutorRef currentExecutor);
83+
/// Given that we've fully established the job context in the current
84+
/// thread, actually start running this job. To establish the context
85+
/// correctly, call swift_job_run or runJobInExecutorContext.
86+
void runInFullyEstablishedContext(ExecutorRef currentExecutor);
87+
88+
/// Given that we've fully established the job context in the
89+
/// current thread, and that the job is a simple (non-task) job,
90+
/// actually start running this job.
91+
void runSimpleInFullyEstablishedContext(ExecutorRef currentExecutor) {
92+
RunJob(this, currentExecutor);
93+
}
8594
};
8695

8796
// The compiler will eventually assume these.
@@ -173,7 +182,11 @@ class AsyncTask : public HeapObject, public Job {
173182
assert(flags.isAsyncTask());
174183
}
175184

176-
void run(ExecutorRef currentExecutor) {
185+
/// Given that we've already fully established the job context
186+
/// in the current thread, start running this task. To establish
187+
/// the job context correctly, call swift_job_run or
188+
/// runInExecutorContext.
189+
void runInFullyEstablishedContext(ExecutorRef currentExecutor) {
177190
ResumeTask(this, currentExecutor, ResumeContext);
178191
}
179192

@@ -300,15 +313,15 @@ class AsyncTask : public HeapObject, public Job {
300313
// therefore skip also skip pointing to that parent and point
301314
// to whichever parent it was pointing to as well, it may be its
302315
// immediate parent, or some super-parent.
303-
item->next = reinterpret_cast<uintptr_t>(parentHead->getNext());
316+
item->next = reinterpret_cast<uintptr_t>(parentHead->getNext()) |
304317
static_cast<uintptr_t>(NextLinkType::IsParent);
305318
break;
306319
case NextLinkType::IsNext:
307320
assert(false && "empty taskValue head in parent task, yet parent's 'head' is `IsNext`, "
308321
"this should not happen, as it implies the parent must have stored some value.");
309322
break;
310323
case NextLinkType::IsTerminal:
311-
item->next = reinterpret_cast<uintptr_t>(parentHead->getNext());
324+
item->next = reinterpret_cast<uintptr_t>(parentHead->getNext()) |
312325
static_cast<uintptr_t>(NextLinkType::IsTerminal);
313326
break;
314327
}
@@ -845,8 +858,10 @@ class AsyncTask : public HeapObject, public Job {
845858
/// The type of the result that will be produced by the future.
846859
const Metadata *resultType;
847860

848-
// Trailing storage for the result itself. The storage will be uninitialized,
849-
// contain an instance of \c resultType, or contain an an \c Error.
861+
SwiftError *error = nullptr;
862+
863+
// Trailing storage for the result itself. The storage will be
864+
// uninitialized, contain an instance of \c resultType.
850865

851866
friend class AsyncTask;
852867

@@ -858,32 +873,31 @@ class AsyncTask : public HeapObject, public Job {
858873
/// Destroy the storage associated with the future.
859874
void destroy();
860875

876+
const Metadata *getResultType() const {
877+
return resultType;
878+
}
879+
861880
/// Retrieve a pointer to the storage of result.
862881
OpaqueValue *getStoragePtr() {
863882
return reinterpret_cast<OpaqueValue *>(
864883
reinterpret_cast<char *>(this) + storageOffset(resultType));
865884
}
866885

867886
/// Retrieve the error.
868-
SwiftError *&getError() {
869-
return *reinterpret_cast<SwiftError **>(
870-
reinterpret_cast<char *>(this) + storageOffset(resultType));
871-
}
887+
SwiftError *&getError() { return *&error; }
872888

873889
/// Compute the offset of the storage from the base of the future
874890
/// fragment.
875891
static size_t storageOffset(const Metadata *resultType) {
876892
size_t offset = sizeof(FutureFragment);
877-
size_t alignment =
878-
std::max(resultType->vw_alignment(), alignof(SwiftError *));
893+
size_t alignment = resultType->vw_alignment();
879894
return (offset + alignment - 1) & ~(alignment - 1);
880895
}
881896

882897
/// Determine the size of the future fragment given a particular future
883898
/// result type.
884899
static size_t fragmentSize(const Metadata *resultType) {
885-
return storageOffset(resultType) +
886-
std::max(resultType->vw_size(), sizeof(SwiftError *));
900+
return storageOffset(resultType) + resultType->vw_size();
887901
}
888902
};
889903

@@ -944,11 +958,11 @@ static_assert(sizeof(AsyncTask) == 12 * sizeof(void*),
944958
static_assert(alignof(AsyncTask) == 2 * alignof(void*),
945959
"AsyncTask alignment is wrong");
946960

947-
inline void Job::run(ExecutorRef currentExecutor) {
961+
inline void Job::runInFullyEstablishedContext(ExecutorRef currentExecutor) {
948962
if (auto task = dyn_cast<AsyncTask>(this))
949-
task->run(currentExecutor);
963+
task->runInFullyEstablishedContext(currentExecutor);
950964
else
951-
RunJob(this, currentExecutor);
965+
runSimpleInFullyEstablishedContext(currentExecutor);
952966
}
953967

954968
/// An asynchronous context within a task. Generally contexts are
@@ -1038,7 +1052,7 @@ class YieldingAsyncContext : public AsyncContext {
10381052
/// futures.
10391053
class FutureAsyncContext : public AsyncContext {
10401054
public:
1041-
SwiftError *errorResult = nullptr;
1055+
SwiftError **errorResult = nullptr;
10421056
OpaqueValue *indirectResult;
10431057

10441058
using AsyncContext::AsyncContext;

include/swift/AST/ASTMangler.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ class ASTMangler : public Mangler {
171171
/// predefined in the Swift runtime for the given type signature.
172172
std::string mangleObjCAsyncCompletionHandlerImpl(CanSILFunctionType BlockType,
173173
CanType ResultType,
174+
CanGenericSignature Sig,
174175
bool predefined);
175176

176177
/// Mangle the derivative function (JVP/VJP), or optionally its vtable entry
@@ -203,6 +204,11 @@ class ASTMangler : public Mangler {
203204
CanType fromType, CanType toType, GenericSignature signature,
204205
AutoDiffLinearMapKind linearMapKind);
205206

207+
/// Mangle a SIL differentiability witness.
208+
std::string mangleSILDifferentiabilityWitness(StringRef originalName,
209+
DifferentiabilityKind kind,
210+
AutoDiffConfig config);
211+
206212
/// Mangle the AutoDiff generated declaration for the given:
207213
/// - Generated declaration kind: linear map struct or branching trace enum.
208214
/// - Mangled original function name.
@@ -216,14 +222,6 @@ class ASTMangler : public Mangler {
216222
AutoDiffLinearMapKind linearMapKind,
217223
AutoDiffConfig config);
218224

219-
/// Mangle a SIL differentiability witness key:
220-
/// - Mangled original function name.
221-
/// - Parameter indices.
222-
/// - Result indices.
223-
/// - Derivative generic signature (optional).
224-
std::string
225-
mangleSILDifferentiabilityWitnessKey(SILDifferentiabilityWitnessKey key);
226-
227225
std::string mangleKeyPathGetterThunkHelper(const AbstractStorageDecl *property,
228226
GenericSignature signature,
229227
CanType baseType,

include/swift/AST/ASTTypeIDZone.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ SWIFT_TYPEID(PropertyWrapperTypeInfo)
3131
SWIFT_TYPEID(Requirement)
3232
SWIFT_TYPEID(ResilienceExpansion)
3333
SWIFT_TYPEID(FragileFunctionKind)
34-
SWIFT_TYPEID(FunctionRethrowingKind)
35-
SWIFT_TYPEID(ProtocolRethrowsRequirementList)
34+
SWIFT_TYPEID(PolymorphicEffectKind)
35+
SWIFT_TYPEID(PolymorphicEffectRequirementList)
3636
SWIFT_TYPEID(TangentPropertyInfo)
3737
SWIFT_TYPEID(SymbolSourceMap)
3838
SWIFT_TYPEID(Type)

include/swift/AST/ASTTypeIDs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ class ProtocolDecl;
6464
class Requirement;
6565
enum class ResilienceExpansion : unsigned;
6666
struct FragileFunctionKind;
67-
enum class FunctionRethrowingKind : uint8_t;
68-
class ProtocolRethrowsRequirementList;
67+
enum class PolymorphicEffectKind : uint8_t;
68+
class PolymorphicEffectRequirementList;
6969
class SourceFile;
7070
class SymbolSourceMap;
7171
struct TangentPropertyInfo;

include/swift/AST/Attr.def

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,25 +353,25 @@ SIMPLE_DECL_ATTR(testable, Testable,
353353
DECL_ATTR(_alignment, Alignment,
354354
OnStruct | OnEnum |
355355
UserInaccessible |
356-
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
356+
ABIBreakingToAdd | ABIBreakingToRemove | APIStableToAdd | APIStableToRemove,
357357
56)
358358
SIMPLE_DECL_ATTR(rethrows, Rethrows,
359-
OnFunc | OnAccessor | OnConstructor |
359+
OnFunc | OnConstructor |
360360
RejectByParser |
361-
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
361+
ABIBreakingToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove,
362362
57)
363363
SIMPLE_DECL_ATTR(rethrows, AtRethrows,
364364
OnProtocol |
365-
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
365+
ABIBreakingToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove,
366366
58)
367367
DECL_ATTR(_swift_native_objc_runtime_base, SwiftNativeObjCRuntimeBase,
368368
OnClass |
369369
UserInaccessible |
370-
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
370+
ABIBreakingToAdd | ABIBreakingToRemove | APIStableToAdd | APIStableToRemove,
371371
59)
372372
CONTEXTUAL_SIMPLE_DECL_ATTR(indirect, Indirect, DeclModifier |
373373
OnEnum | OnEnumElement |
374-
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
374+
ABIBreakingToAdd | ABIBreakingToRemove | APIStableToAdd | APIStableToRemove,
375375
60)
376376
SIMPLE_DECL_ATTR(warn_unqualified_access, WarnUnqualifiedAccess,
377377
OnFunc | OnAccessor /*| OnVar*/ |
@@ -469,7 +469,7 @@ DECL_ATTR(_dynamicReplacement, DynamicReplacement,
469469
SIMPLE_DECL_ATTR(_borrowed, Borrowed,
470470
OnVar | OnSubscript | UserInaccessible |
471471
NotSerialized |
472-
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
472+
ABIBreakingToAdd | ABIBreakingToRemove | APIStableToAdd | APIStableToRemove,
473473
81)
474474
DECL_ATTR(_private, PrivateImport,
475475
OnImport |
@@ -479,7 +479,7 @@ DECL_ATTR(_private, PrivateImport,
479479
82)
480480
SIMPLE_DECL_ATTR(_alwaysEmitIntoClient, AlwaysEmitIntoClient,
481481
OnVar | OnSubscript | OnAbstractFunction | UserInaccessible |
482-
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
482+
ABIBreakingToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
483483
83)
484484

485485
SIMPLE_DECL_ATTR(_implementationOnly, ImplementationOnly,
@@ -613,6 +613,23 @@ SIMPLE_DECL_ATTR(_marker, Marker,
613613
APIBreakingToAdd | APIBreakingToRemove,
614614
108)
615615

616+
SIMPLE_DECL_ATTR(reasync, Reasync,
617+
OnFunc | OnConstructor |
618+
RejectByParser |
619+
ABIBreakingToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove,
620+
109)
621+
622+
SIMPLE_DECL_ATTR(reasync, AtReasync,
623+
OnProtocol |
624+
ABIBreakingToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove,
625+
110)
626+
627+
DECL_ATTR(hasAsyncAlternative, HasAsyncAlternative,
628+
OnAbstractFunction | ConcurrencyOnly |
629+
ABIStableToAdd | ABIStableToRemove |
630+
APIStableToAdd | APIStableToRemove,
631+
111)
632+
616633
#undef TYPE_ATTR
617634
#undef DECL_ATTR_ALIAS
618635
#undef CONTEXTUAL_DECL_ATTR_ALIAS

include/swift/AST/Attr.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,30 @@ class TransposeAttr final
21402140
}
21412141
};
21422142

2143+
/// The `@hasAsyncAlternative` attribute marks a function as having an async
2144+
/// alternative, optionally providing a name (for cases when the alternative
2145+
/// has a different name).
2146+
class HasAsyncAlternativeAttr final : public DeclAttribute {
2147+
public:
2148+
/// An optional name of the async alternative function, where the name of the
2149+
/// attributed function is used otherwise.
2150+
const DeclNameRef Name;
2151+
2152+
HasAsyncAlternativeAttr(DeclNameRef Name, SourceLoc AtLoc, SourceRange Range)
2153+
: DeclAttribute(DAK_HasAsyncAlternative, AtLoc, Range, false),
2154+
Name(Name) {}
2155+
2156+
HasAsyncAlternativeAttr(SourceLoc AtLoc, SourceRange Range)
2157+
: DeclAttribute(DAK_HasAsyncAlternative, AtLoc, Range, false) {}
2158+
2159+
/// Determine whether this attribute has a name associated with it.
2160+
bool hasName() const { return !Name.getBaseName().empty(); }
2161+
2162+
static bool classof(const DeclAttribute *DA) {
2163+
return DA->getKind() == DAK_HasAsyncAlternative;
2164+
}
2165+
};
2166+
21432167
/// Attributes that may be applied to declarations.
21442168
class DeclAttributes {
21452169
/// Linked list of declaration attributes.

0 commit comments

Comments
 (0)