Skip to content

Commit 61e1384

Browse files
Merge pull request #5475 from swiftwasm/katei/merge-main-2023-05-26
Merge main 2023-05-26
2 parents a531027 + 0a78621 commit 61e1384

File tree

139 files changed

+6632
-5277
lines changed

Some content is hidden

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

139 files changed

+6632
-5277
lines changed

CHANGELOG.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,69 @@
55
66
## Swift 5.9
77

8+
* [SE-0382][], [SE-0389][], [SE-0394][], [SE-0397][]:
9+
10+
Swift 5.9 includes a new macro system that can be used to eliminate boilerplate and provide new forms of expressive APIs. Macros are declared with the new `macro` introducer:
11+
12+
```swift
13+
@freestanding(expression)
14+
macro assert(_ condition: Bool) = #externalMacro(module: "PowerAssertMacros", type: "AssertMacro")
15+
```
16+
17+
Macros have parameter and result types, like functions, but are defined as separate programs that operate on syntax trees (using [swift-syntax][]) and produce new syntax trees that are incorporated into the program. Freestanding macros, indicated with the `@freestanding` attribute, are expanded in source code with a leading `#`:
18+
19+
```swift
20+
#assert(x + y == z) // expands to check the result of x + y == z and report failure if it's false
21+
```
22+
23+
Macros can also be marked as `@attached`, in which case they will be meaning that they will be expanded using custom attribute syntax. For example:
24+
25+
```swift
26+
@attached(peer, names: overloaded)
27+
macro AddCompletionHandler() = #externalMacro(
28+
module: "ConcurrencyHelperMacros",
29+
type: "AddCompletionHandlerMacro"
30+
)
31+
32+
@AddCompletionHandler
33+
func fetchAvatar(from url: URL) throws -> Image { ... }
34+
35+
// expands to...
36+
func fetchAvatar(from url: URL, completionHandler: @escaping (Result<Image, Error>) -> Void) {
37+
Task.detached {
38+
do {
39+
let result = try await fetchAvatar(from: url)
40+
completionHandler(.success(result))
41+
} catch {
42+
completionHandler(.failure(error))
43+
}
44+
}
45+
}
46+
```
47+
48+
Macros are implemented in separate programs, which are executed by the Swift compiler. The Swift Package Manager's manifest provides a new `macro` target type to describe macros:
49+
50+
```swift
51+
import PackageDescription
52+
import CompilerPluginSupport
53+
54+
let package = Package(
55+
name: "ConcurrencyHelpers",
56+
dependencies: [
57+
.package(url: "https://github.com/apple/swift-syntax", from: "509.0.0"),
58+
],
59+
targets: [
60+
.macro(name: "ConcurrencyHelperMacros",
61+
dependencies: [
62+
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
63+
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
64+
]),
65+
.target(name: "ConcurrencyHelpers", dependencies: ["ConcurrencyHelperMacros"]),
66+
.testTarget(name: "ConcurrencyHelperMacroTests", dependencies: ["ConcurrencyHelperMacros"]),
67+
]
68+
)
69+
```
70+
871
* [SE-0380][]:
972

1073
`if` and `switch` statements may now be used as expressions to:
@@ -9766,7 +9829,10 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
97669829
[SE-0376]: <https://github.com/apple/swift-evolution/blob/main/proposals/0376-function-back-deployment.md>
97679830
[SE-0377]: <https://github.com/apple/swift-evolution/blob/main/proposals/0377-parameter-ownership-modifiers.md>
97689831
[SE-0380]: <https://github.com/apple/swift-evolution/blob/main/proposals/0380-if-switch-expressions.md>
9769-
9832+
[SE-0382]: https://github.com/apple/swift-evolution/blob/main/proposals/0382-expression-macros.md
9833+
[SE-0389]: https://github.com/apple/swift-evolution/blob/main/proposals/0389-attached-macros.md
9834+
[SE-0394]: https://github.com/apple/swift-evolution/blob/main/proposals/0394-swiftpm-expression-macros.md
9835+
[SE-0397]: https://github.com/apple/swift-evolution/blob/main/proposals/0397-freestanding-declaration-macros.md
97709836
[#64927]: <https://github.com/apple/swift/issues/64927>
97719837
[#42697]: <https://github.com/apple/swift/issues/42697>
97729838
[#42728]: <https://github.com/apple/swift/issues/42728>
@@ -9808,3 +9874,4 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
98089874
[#57081]: <https://github.com/apple/swift/issues/57081>
98099875
[#57225]: <https://github.com/apple/swift/issues/57225>
98109876
[#56139]: <https://github.com/apple/swift/issues/56139>
9877+
[swift-syntax]: https://github.com/apple/swift-syntax

docs/SIL.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2805,7 +2805,7 @@ canonical SIL that the value was never copied and thus is a "move only value"
28052805
even though the actual underlying wrapped type is copyable. As an example of
28062806
this, consider the following Swift::
28072807

2808-
func doSomething(@_noImplicitCopy _ x: Klass) -> () { // expected-error {{'x' has guaranteed ownership but was consumed}}
2808+
func doSomething(@_noImplicitCopy _ x: Klass) -> () { // expected-error {{'x' is borrowed and cannot be consumed}}
28092809
x.doSomething()
28102810
let x2 = x // expected-note {{consuming use}}
28112811
x2.doSomething()

include/swift/ABI/MetadataValues.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,7 @@ class TaskCreateFlags : public FlagSet<size_t> {
23342334
Task_InheritContext = 11,
23352335
Task_EnqueueJob = 12,
23362336
Task_AddPendingGroupTaskUnconditionally = 13,
2337+
Task_IsDiscardingTask = 14,
23372338
};
23382339

23392340
explicit constexpr TaskCreateFlags(size_t bits) : FlagSet(bits) {}
@@ -2360,6 +2361,9 @@ class TaskCreateFlags : public FlagSet<size_t> {
23602361
FLAGSET_DEFINE_FLAG_ACCESSORS(Task_AddPendingGroupTaskUnconditionally,
23612362
addPendingGroupTaskUnconditionally,
23622363
setAddPendingGroupTaskUnconditionally)
2364+
FLAGSET_DEFINE_FLAG_ACCESSORS(Task_IsDiscardingTask,
2365+
isDiscardingTask,
2366+
setIsDiscardingTask)
23632367
};
23642368

23652369
/// Flags for schedulable jobs.

include/swift/AST/CASTBridging.h

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,70 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : long {
112112
BridgedBuiltinExternalMacro
113113
} BridgedMacroDefinitionKind;
114114

115+
/// Bridged parameter specifiers
116+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedAttributedTypeSpecifier : long {
117+
BridgedAttributedTypeSpecifierInOut,
118+
BridgedAttributedTypeSpecifierBorrowing,
119+
BridgedAttributedTypeSpecifierConsuming,
120+
BridgedAttributedTypeSpecifierLegacyShared,
121+
BridgedAttributedTypeSpecifierLegacyOwned,
122+
BridgedAttributedTypeSpecifierConst,
123+
BridgedAttributedTypeSpecifierIsolated,
124+
} BridgedAttributedTypeSpecifier;
125+
126+
127+
// Bridged type attribute kinds, which mirror TypeAttrKind exactly.
128+
typedef enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind : long {
129+
BridgedTypeAttrKind_autoclosure,
130+
BridgedTypeAttrKind_convention,
131+
BridgedTypeAttrKind_noescape,
132+
BridgedTypeAttrKind_escaping,
133+
BridgedTypeAttrKind_differentiable,
134+
BridgedTypeAttrKind_noDerivative,
135+
BridgedTypeAttrKind_async,
136+
BridgedTypeAttrKind_Sendable,
137+
BridgedTypeAttrKind_unchecked,
138+
BridgedTypeAttrKind__local,
139+
BridgedTypeAttrKind__noMetadata,
140+
BridgedTypeAttrKind__opaqueReturnTypeOf,
141+
BridgedTypeAttrKind_block_storage,
142+
BridgedTypeAttrKind_box,
143+
BridgedTypeAttrKind_dynamic_self,
144+
BridgedTypeAttrKind_sil_weak,
145+
BridgedTypeAttrKind_sil_unowned,
146+
BridgedTypeAttrKind_sil_unmanaged,
147+
BridgedTypeAttrKind_error,
148+
BridgedTypeAttrKind_out,
149+
BridgedTypeAttrKind_direct,
150+
BridgedTypeAttrKind_in,
151+
BridgedTypeAttrKind_inout,
152+
BridgedTypeAttrKind_inout_aliasable,
153+
BridgedTypeAttrKind_in_guaranteed,
154+
BridgedTypeAttrKind_in_constant,
155+
BridgedTypeAttrKind_pack_owned,
156+
BridgedTypeAttrKind_pack_guaranteed,
157+
BridgedTypeAttrKind_pack_inout,
158+
BridgedTypeAttrKind_pack_out,
159+
BridgedTypeAttrKind_owned,
160+
BridgedTypeAttrKind_unowned_inner_pointer,
161+
BridgedTypeAttrKind_guaranteed,
162+
BridgedTypeAttrKind_autoreleased,
163+
BridgedTypeAttrKind_callee_owned,
164+
BridgedTypeAttrKind_callee_guaranteed,
165+
BridgedTypeAttrKind_objc_metatype,
166+
BridgedTypeAttrKind_opened,
167+
BridgedTypeAttrKind_pack_element,
168+
BridgedTypeAttrKind_pseudogeneric,
169+
BridgedTypeAttrKind_yields,
170+
BridgedTypeAttrKind_yield_once,
171+
BridgedTypeAttrKind_yield_many,
172+
BridgedTypeAttrKind_captures_generics,
173+
BridgedTypeAttrKind_moveOnly,
174+
BridgedTypeAttrKind_thin,
175+
BridgedTypeAttrKind_thick,
176+
BridgedTypeAttrKind_Count
177+
} BridgedTypeAttrKind;
178+
115179
#ifdef __cplusplus
116180
extern "C" {
117181

@@ -261,6 +325,19 @@ void *ImplicitlyUnwrappedOptionalTypeRepr_create(void *ctx, void *base,
261325
void *exclamationLoc);
262326
void *MetatypeTypeRepr_create(void *ctx, void *baseType, void *typeLoc);
263327
void *ProtocolTypeRepr_create(void *ctx, void *baseType, void *protoLoc);
328+
329+
BridgedTypeAttrKind getBridgedTypeAttrKindFromString(
330+
const unsigned char * _Nullable str, long len);
331+
332+
typedef void *BridgedTypeAttributes;
333+
BridgedTypeAttributes BridgedTypeAttributes_create(void);
334+
void BridgedTypeAttributes_addSimpleAttr(
335+
BridgedTypeAttributes typeAttributes, BridgedTypeAttrKind kind, void *atLoc, void *attrLoc);
336+
void *AttributedTypeRepr_create(void *ctx, void *base, BridgedTypeAttributes typeAttributes);
337+
338+
void *AttributedTypeSpecifierRepr_create(
339+
void *ctx, void *base, BridgedAttributedTypeSpecifier specifier, void *specifierLoc);
340+
void *VarargTypeRepr_create(void *ctx, void *base, void *ellipsisLocPtr);
264341
void *PackExpansionTypeRepr_create(void *ctx, void *base, void *repeatLoc);
265342
void *TupleTypeRepr_create(void *ctx, BridgedArrayRef elements, void *lParenLoc,
266343
void *rParenLoc);
@@ -269,8 +346,9 @@ void *MemberTypeRepr_create(void *ctx, void *baseComponent,
269346
void *GenericIdentTypeRepr_create(void *ctx, BridgedIdentifier name,
270347
void *nameLoc, BridgedArrayRef genericArgs,
271348
void *lAngle, void *rAngle);
349+
void *EmptyCompositionTypeRepr_create(void *ctx, void *anyLoc);
272350
void *CompositionTypeRepr_create(void *ctx, BridgedArrayRef types,
273-
void *firstTypeLoc);
351+
void *firstTypeLoc, void *firstAmpLoc);
274352
void *FunctionTypeRepr_create(void *ctx, void *argsTy, void *_Nullable asyncLoc,
275353
void *_Nullable throwsLoc, void *arrowLoc,
276354
void *returnType);

include/swift/AST/DiagnosticsClangImporter.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ ERROR(conforms_to_not_protocol,none,
248248
"%0 %1 referenced in protocol conformance '%2' is not a protocol", (DescriptiveDeclKind, ValueDecl *, StringRef))
249249

250250
ERROR(move_only_requires_move_only,none,
251-
"use of move-only C++ type '%0' requires -enable-experimental-move-only",
251+
"use of noncopyable C++ type '%0' requires -enable-experimental-move-only",
252252
(StringRef))
253253

254254
NOTE(unsupported_builtin_type, none, "built-in type '%0' not supported", (StringRef))

include/swift/AST/DiagnosticsSIL.def

Lines changed: 56 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -745,97 +745,70 @@ NOTE(discard_nontrivial_implicit_storage_note,none,
745745
"type %0 implicitly contains %1 which cannot be trivially destroyed",
746746
(Type, Type))
747747

748-
// move only checker diagnostics
749-
ERROR(sil_moveonlychecker_owned_value_consumed_more_than_once, none,
748+
749+
/// Move Checking / Noncopyable types diagnostics
750+
751+
ERROR(sil_movechecking_owned_value_consumed_more_than_once, none,
750752
"'%0' consumed more than once", (StringRef))
751-
ERROR(sil_moveonlychecker_owned_value_consumed_and_used_at_same_time, none,
753+
ERROR(sil_movechecking_owned_value_consumed_and_used_at_same_time, none,
752754
"'%0' consumed and used at the same time", (StringRef))
753-
ERROR(sil_moveonlychecker_value_used_after_consume, none,
755+
ERROR(sil_movechecking_value_used_after_consume, none,
754756
"'%0' used after consume", (StringRef))
755-
ERROR(sil_moveonlychecker_guaranteed_value_consumed, none,
756-
"'%0' has guaranteed ownership but was consumed", (StringRef))
757-
ERROR(sil_moveonlychecker_guaranteed_value_captured_by_closure, none,
758-
"'%0' has guaranteed ownership but was consumed due to being captured by a closure", (StringRef))
759-
ERROR(sil_moveonlychecker_let_value_consumed_in_closure, none,
760-
"'%0' consumed in closure. This is illegal since if the closure is invoked more than once the binding will be uninitialized on later invocations", (StringRef))
761-
ERROR(sil_moveonlychecker_inout_not_reinitialized_before_end_of_function, none,
762-
"'%0' consumed but not reinitialized before end of function", (StringRef))
763-
ERROR(sil_moveonlychecker_inout_not_reinitialized_before_end_of_closure, none,
764-
"'%0' consumed in closure but not reinitialized before end of closure", (StringRef))
765-
ERROR(sil_moveonlychecker_value_consumed_in_a_loop, none,
766-
"'%0' consumed by a use in a loop", (StringRef))
767-
ERROR(sil_moveonlychecker_exclusivity_violation, none,
768-
"'%0' has consuming use that cannot be eliminated due to a tight exclusivity scope", (StringRef))
769-
ERROR(sil_moveonlychecker_moveonly_field_consumed, none,
770-
"'%0' has a move only field that was consumed before later uses", (StringRef))
771-
772-
ERROR(sil_moveonlychecker_notconsumable_but_assignable_was_consumed_classfield_let, none,
773-
"'%0' was consumed but it is illegal to consume a noncopyable class let field. One can only read from it",
774-
(StringRef))
775-
ERROR(sil_moveonlychecker_notconsumable_but_assignable_was_consumed_classfield_var, none,
776-
"'%0' was consumed but it is illegal to consume a noncopyable class var field. One can only read from it or assign to it",
777-
(StringRef))
778-
ERROR(sil_moveonlychecker_notconsumable_but_assignable_was_consumed_global_var, none,
779-
"'%0' was consumed but it is illegal to consume a noncopyable global var. One can only read from it or assign to it",
780-
(StringRef))
781-
ERROR(sil_moveonlychecker_notconsumable_but_assignable_was_consumed_global_let, none,
782-
"'%0' was consumed but it is illegal to consume a noncopyable global let. One can only read from it",
783-
(StringRef))
784-
ERROR(sil_moveonlychecker_notconsumable_but_assignable_was_consumed_escaping_var, none,
785-
"'%0' was consumed but it is illegal to consume a noncopyable mutable capture of an escaping closure. One can only read from it or assign over it",
786-
(StringRef))
787-
ERROR(sil_moveonlychecker_let_capture_consumed, none,
788-
"'%0' was consumed but it is illegal to consume a noncopyable immutable capture of an escaping closure. One can only read from it", (StringRef))
789-
ERROR(sil_moveonlychecker_cannot_destructure_deinit_nominal_type_self, none,
790-
"Cannot partially consume '%0' since it has a user defined deinit",
757+
ERROR(sil_movechecking_guaranteed_value_consumed, none,
758+
"'%0' is borrowed and cannot be consumed", (StringRef))
759+
760+
// FIXME: this diagnostic shouldn't ever be emitted now. rdar://109742587 (closures may still try to consume captures, e.g., borrowed parameters)
761+
ERROR(sil_movechecking_guaranteed_value_captured_by_closure, none,
762+
"'%0' is borrowed and cannot be consumed by closure capture", (StringRef))
763+
764+
ERROR(sil_movechecking_capture_consumed, none,
765+
"noncopyable '%0' cannot be consumed when captured by a closure", (StringRef))
766+
ERROR(sil_movechecking_inout_not_reinitialized_before_end_of_function, none,
767+
"missing reinitialization of inout parameter '%0' after consume", (StringRef))
768+
ERROR(sil_movechecking_value_consumed_in_a_loop, none,
769+
"'%0' consumed in a loop", (StringRef))
770+
ERROR(sil_movechecking_use_after_partial_consume, none,
771+
"cannot use '%0' after partial consume", (StringRef))
772+
ERROR(sil_movechecking_notconsumable_but_assignable_was_consumed, none,
773+
"cannot consume noncopyable stored property '%0' %select{of a class|that is global}1",
774+
(StringRef, bool))
775+
ERROR(sil_movechecking_cannot_destructure_has_deinit, none,
776+
"cannot partially consume '%0' when it has a deinitializer",
791777
(StringRef))
792-
ERROR(sil_moveonlychecker_cannot_destructure_deinit_nominal_type_field, none,
793-
"Cannot partially consume '%0' since it contains field '%1.%2' whose type %3 has a user defined deinit",
794-
(StringRef, StringRef, StringRef, DeclBaseName))
795-
796-
NOTE(sil_moveonlychecker_moveonly_field_consumed_here, none,
797-
"move only field consumed here", ())
798-
NOTE(sil_moveonlychecker_boundary_use, none,
799-
"boundary use here", ())
800-
NOTE(sil_moveonlychecker_consuming_use_here, none,
801-
"consuming use here", ())
802-
NOTE(sil_moveonlychecker_other_consuming_use_here, none,
803-
"other consuming use here", ())
804-
NOTE(sil_moveonlychecker_two_consuming_uses_here, none,
805-
"two consuming uses here", ())
806-
NOTE(sil_moveonlychecker_consuming_and_non_consuming_uses_here, none,
807-
"consuming and non-consuming uses here", ())
808-
NOTE(sil_moveonlychecker_consuming_closure_use_here, none,
809-
"closure capture here", ())
810-
NOTE(sil_moveonlychecker_nonconsuming_use_here, none,
811-
"non-consuming use here", ())
812-
NOTE(sil_movekillscopyablevalue_value_cyclic_consumed_in_loop_here, none,
813-
"consuming in loop use here", ())
814-
NOTE(sil_moveonlychecker_deinit_here, none,
815-
"deinit declared here", ())
816-
817-
ERROR(sil_moveonlychecker_not_understand_consumable_and_assignable, none,
818-
"Usage of @noImplicitCopy that the move checker does not know how to "
819-
"check!", ())
820-
ERROR(sil_moveonlychecker_not_understand_moveonly, none,
821-
"Usage of a move only type that the move checker does not know how to "
822-
"check!", ())
823-
ERROR(sil_moveonlychecker_missed_copy, none,
824-
"copy of noncopyable typed value. This is a compiler bug. Please file a bug with a small example of the bug", ())
825778

826-
// move kills copyable values checker diagnostics
827-
ERROR(sil_movekillscopyablevalue_value_consumed_more_than_once, none,
828-
"'%0' used after being consumed", (StringRef))
829-
NOTE(sil_movekillscopyablevalue_move_here, none,
830-
"consume here", ())
831-
NOTE(sil_movekillscopyablevalue_use_here, none,
832-
"use here", ())
833-
NOTE(sil_movekillscopyablevalue_value_consumed_in_loop, none,
834-
"consume here would occur multiple times in loop", ())
779+
NOTE(sil_movechecking_partial_consume_here, none,
780+
"partially consumed here", ())
781+
NOTE(sil_movechecking_consuming_use_here, none,
782+
"consumed here", ())
783+
NOTE(sil_movechecking_consumed_again_here, none,
784+
"consumed again here", ())
785+
NOTE(sil_movechecking_two_consuming_uses_here, none,
786+
"multiple consumes here", ())
787+
NOTE(sil_movechecking_consuming_and_non_consuming_uses_here, none,
788+
"consumed and used here", ())
789+
NOTE(sil_movechecking_consuming_closure_use_here, none,
790+
"closure capturing '%0' here", (StringRef))
791+
NOTE(sil_movechecking_nonconsuming_use_here, none,
792+
"used here", ())
793+
NOTE(sil_movechecking_consumed_in_loop_here, none,
794+
"consumed in loop here", ())
795+
NOTE(sil_movechecking_deinit_here, none,
796+
"deinitializer declared here", ())
797+
798+
// errors involving noncopyables that are considered to be bugs in the compiler
799+
ERROR(sil_movechecking_not_understand_consumable_and_assignable, none,
800+
"usage of no-implicit-copy value that the compiler can't verify. This is a compiler bug. Please file a bug with a small example of the bug", ())
801+
ERROR(sil_movechecking_not_understand_moveonly, none,
802+
"usage of a noncopyable type that compiler can't verify. This is a compiler bug. Please file a bug with a small example of the bug", ())
803+
ERROR(sil_movechecking_bug_missed_copy, none,
804+
"copy of noncopyable typed value. This is a compiler bug. Please file a bug with a small example of the bug", ())
805+
ERROR(sil_movechecking_bug_exclusivity_violation, none,
806+
"'%0' has an unexpected exclusivity violation. This is a compiler bug. Please file a bug with a small example of the bug", (StringRef))
835807
ERROR(sil_movekillscopyablevalue_move_applied_to_unsupported_move, none,
836-
"'consume' applied to value that the compiler does not support checking",
808+
"'consume' applied to value that the compiler does not support. This is a compiler bug. Please file a bug with a small example of the bug",
837809
())
838810

811+
839812
// Implicit inout-to-UnsafeRawPointer conversions
840813
WARNING(nontrivial_to_rawpointer_conversion,none,
841814
"forming %1 to a variable of type %0; this is likely incorrect because %2 may contain "

0 commit comments

Comments
 (0)