Skip to content

Commit 090d45d

Browse files
Merge pull request #4587 from swiftwasm/main
[pull] swiftwasm from main
2 parents f8cea25 + 3c0b1ab commit 090d45d

File tree

55 files changed

+4426
-391
lines changed

Some content is hidden

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

55 files changed

+4426
-391
lines changed

CHANGELOG.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,86 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
55

66
## Swift 5.7
77

8+
* [SE-0309][]:
9+
10+
Protocols with associated types and `Self` requirements can now be used as the
11+
types of values with the `any` keyword.
12+
13+
Protocol methods that return associated types can be called on an `any` type;
14+
the result is type-erased to the associated type's upper bound, which is another
15+
`any` type having the same constraints as the associated type. For example:
16+
17+
```swift
18+
protocol Surface {...}
19+
20+
protocol Solid {
21+
associatedtype SurfaceType: Surface
22+
func boundary() -> SurfaceType
23+
}
24+
25+
let solid: any Solid = ...
26+
27+
// Type of 'boundary' is 'any Surface'
28+
let boundary = solid.boundary()
29+
```
30+
31+
Protocol methods that take an associated type or `Self` cannot be used with `any`,
32+
however in conjunction with [SE-0352][], you can pass the `any` type to a function
33+
taking a generic parameter constrained to the protocol. Within the generic context,
34+
type relationships are explicit and all protocol methods can be used.
35+
36+
* [SE-0346][]:
37+
38+
Protocols can now declare a list of one or more primary associated types:
39+
40+
```swift
41+
protocol Graph<Vertex, Edge> {
42+
associatedtype Vertex
43+
associatedtype Edge
44+
}
45+
```
46+
47+
A protocol-constrained type like `Graph<Int>` can now be written anywhere that
48+
expects the right-hand side of a protocol conformance requirement:
49+
50+
```swift
51+
func shortestPath<V, E>(_: some Graph<V>, from: V, to: V) -> [E]
52+
53+
extension Graph<Int> {...}
54+
55+
func build() -> some Graph<String> {}
56+
```
57+
58+
A protocol-constrained type is equivalent to a conformance requirement to the protocol
59+
itself together with a same-type requirement constraining the primary associated type.
60+
The first two examples above are equivalent to the following:
61+
62+
```swift
63+
func shortestPath<V, E, G>(_: G, from: V, to: V) -> [E]
64+
where G: Graph, G.Vertex == V, G.Edge == V
65+
66+
extension Graph where Vertex == Int {...}
67+
```
68+
69+
The `build()` function returning `some Graph<String>` cannot be written using a `where`
70+
clause; this is an example of a constrained opaque result type, which could not be written
71+
before.
72+
73+
* [SE-0353][]:
74+
75+
Further generalizing the above, protocol-constrained types can also be used with `any`:
76+
77+
```swift
78+
func findBestGraph(_: [any Graph<Int>]) -> any Graph<Int> {...}
79+
```
80+
81+
* [SE-0358][]:
82+
83+
Various protocols in the standard library now declare primary associated types, for
84+
example `Sequence` and `Collection` declare a single primary associated type `Element`.
85+
For example, this allows writing down the types `some Collection<Int>` and
86+
`any Collection<Int>`.
87+
888
* References to `optional` methods on a protocol metatype, as well as references to dynamically looked up methods on the `AnyObject` metatype are now supported. These references always have the type of a function that accepts a single argument and returns an optional value of function type:
989

1090
```swift
@@ -9261,6 +9341,7 @@ Swift 1.0
92619341
[SE-0300]: <https://github.com/apple/swift-evolution/blob/main/proposals/0300-continuation.md>
92629342
[SE-0302]: <https://github.com/apple/swift-evolution/blob/main/proposals/0302-concurrent-value-and-concurrent-closures.md>
92639343
[SE-0306]: <https://github.com/apple/swift-evolution/blob/main/proposals/0306-actors.md>
9344+
[SE-0309]: <https://github.com/apple/swift-evolution/blob/main/proposals/0309-unlock-existential-types-for-all-protocols.md>
92649345
[SE-0310]: <https://github.com/apple/swift-evolution/blob/main/proposals/0310-effectful-readonly-properties.md>
92659346
[SE-0311]: <https://github.com/apple/swift-evolution/blob/main/proposals/0311-task-locals.md>
92669347
[SE-0313]: <https://github.com/apple/swift-evolution/blob/main/proposals/0313-actor-isolation-control.md>
@@ -9283,9 +9364,12 @@ Swift 1.0
92839364
[SE-0341]: <https://github.com/apple/swift-evolution/blob/main/proposals/0341-opaque-parameters.md>
92849365
[SE-0343]: <https://github.com/apple/swift-evolution/blob/main/proposals/0343-top-level-concurrency.md>
92859366
[SE-0345]: <https://github.com/apple/swift-evolution/blob/main/proposals/0345-if-let-shorthand.md>
9367+
[SE-0346]: <https://github.com/apple/swift-evolution/blob/main/proposals/0346-light-weight-same-type-syntax.md>
92869368
[SE-0347]: <https://github.com/apple/swift-evolution/blob/main/proposals/0347-type-inference-from-default-exprs.md>
92879369
[SE-0349]: <https://github.com/apple/swift-evolution/blob/main/proposals/0349-unaligned-loads-and-stores.md>
92889370
[SE-0352]: <https://github.com/apple/swift-evolution/blob/main/proposals/0352-implicit-open-existentials.md>
9371+
[SE-0353]: <https://github.com/apple/swift-evolution/blob/main/proposals/0353-constrained-existential-types.md>
9372+
[SE-0358]: <https://github.com/apple/swift-evolution/blob/main/proposals/0358-primary-associated-types-in-stdlib.md>
92899373

92909374
[SR-75]: <https://bugs.swift.org/browse/SR-75>
92919375
[SR-106]: <https://bugs.swift.org/browse/SR-106>

docs/DevelopmentTips.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ Going further, for various reasons the standard library has lots of warnings. Th
2020

2121
Copy the invocation that has ` -o <build-path>/swift-macosx-x86_64/stdlib/public/core/iphonesimulator/i386/Swift.o`, so that we can perform the actual call to swiftc ourselves. Tack on `-suppress-warnings` at the end, and now we have the command to just build `Swift.o` for i386 while only displaying the actual errors.
2222

23+
### Choosing the bootstrapping mode
24+
By default, the compiler builds with the `boostrapping-with-hostlibs` (macOS) or `bootstrapping` (Linux) bootstrapping mode. To speed up local development it's recommended to build with the `hosttools` mode: `utils/build-script --bootstrapping=hosttools`.
25+
26+
It requires a recently new swift toolchain to be installed on your build machine. On macOS this comes with your Xcode installation.
27+
28+
Not that changing the bootstrapping mode needs a reconfiguration.
29+
30+
### Working with two build directories
31+
For developing and debugging you are probably building a debug configuration of swift. But it's often beneficial to also build a release-assert configuration in parallel (`utils/build-script -R`).
32+
33+
The standard library takes very long to build with a debug compiler. It's much faster to build everything (including the standard library) with a release compiler and only the swift-frontend (with `ninja swift-frontend`) in debug. Then copy the release-built standard library to the debug build:
34+
```
35+
src=/path/to/build/Ninja-ReleaseAssert/swift-macosx-x86_64
36+
dst=/path/to/build/Ninja-DebugAssert/swift-macosx-x86_64
37+
cp -r $src/stdlib $dst/
38+
cp -r $src/lib/swift/macosx $dst/lib/swift/
39+
cp -r $src/lib/swift/shims $dst/lib/swift/
40+
```
41+
2342
### Use sccache to cache build artifacts
2443

2544
Compilation times for the compiler and the standard library can be agonizing, especially for cold builds. This is particularly painful if
@@ -65,3 +84,29 @@ For example, to have `build-script` spawn only one link job at a time, we can in
6584
```
6685
build-script --llvm-cmake-options==-DLLVM_PARALLEL_LINK_JOBS=1 --swift-cmake-options=-DSWIFT_PARALLEL_LINK_JOBS=1
6786
```
87+
88+
## Using ninja with Xcode
89+
90+
Although it's possible to build the swift compiler entirely with Xcode (`--xcode`), often it's better to build with _ninja_ and use Xcode for editing and debugging.
91+
This is very convenient because you get the benefits of the ninja build system and all the benefits of the Xcode IDE, like code completion, refactoring, debugging, etc.
92+
93+
To setup this environment a few steps are necessary:
94+
* Create a new workspace.
95+
* Create Xcode projects for LLVM and Swift with `utils/build-script --skip-build --xcode --skip-early-swift-driver`. Beside configuring, this needs to build a few LLVM files which are needed to configure the swift project.
96+
* Add the generated LLVM and Swift projects to your workspace. They can be found in the build directories `build/Xcode-DebugAssert/llvm-macosx-x86_64/LLVM.xcodeproj` and `build/Xcode-DebugAssert/swift-macosx-x86_64/Swift.xcodeproj`.
97+
* Add the `swift/SwiftCompilerSources` package to the workspace.
98+
* Create a new empty project `build-targets` (or however you want to name it) in the workspace, using the "External Build System" template.
99+
* For each compiler tool you want to build (`swift-frontend`, `sil-opt`, etc.), add an "External Build System" target to the `build-targets` project.
100+
* In the "Info" section of the target configuration, set:
101+
* the _Build Tool_ to the full path of the `ninja` command
102+
* the _Argument_ to the tool name (e.g. `swift-frontend`)
103+
* the _Directory_ to the ninja swift build directory, e.g. `/absolute/path/to/build/Ninja-DebugAssert/swift-macosx-x86_64`. For debugging to work, this has to be a debug build of course.
104+
* For each target, create a new scheme:
105+
* In the _Build_ section add the corresponding build target that you created before.
106+
* In the _Run/Info_ section select the built _Executable_ in the build directory (e.g. `/absolute/path/to/build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swift-frontend`).
107+
* In the _Run/Arguments_ section you can set the command line arguments with which you want to run the compiler tool.
108+
* In the _Run/Options_ section you can set the working directory for debugging.
109+
110+
Now you are all set. You can build and debug like with a native Xcode project.
111+
112+
If the project structure changes, e.g. new source files are added or deleted, you just have to re-create the LLVM and Swift projects with `utils/build-script --skip-build --xcode --skip-early-swift-driver`.

docs/HowToGuides/GettingStarted.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ The additional flexibility comes with two issues: (1) consuming much more disk
282282
space and (2) you need to maintain the two builds in sync, which needs extra
283283
care when moving across branches.
284284

285+
It is even possible to integrate the Ninja build into Xcode. For details on how to set this up see [Using Ninja with Xcode in DevelopmentTips.md](/docs/DevelopmentTips.md#using-ninja-with-xcode).
286+
285287
### Troubleshooting build issues
286288

287289
- Double-check that all projects are checked out at the right branches.

include/swift/AST/DiagnosticsClangImporter.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ WARNING(nonmutating_without_mutable_fields,none,
118118
ERROR(module_map_not_found, none, "module map file '%0' not found", (StringRef))
119119

120120
WARNING(libstdcxx_not_found, none,
121-
"libstdc++ not found for '$0'; C++ stdlib may be unavailable",
121+
"libstdc++ not found for '%0'; C++ stdlib may be unavailable",
122122
(StringRef))
123123

124124
NOTE(macro_not_imported_unsupported_operator, none, "operator not supported in macro arithmetic", ())

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4709,6 +4709,9 @@ ERROR(unchecked_not_inheritance_clause,none,
47094709
ERROR(unchecked_not_existential,none,
47104710
"'unchecked' attribute cannot apply to non-protocol type %0", (Type))
47114711

4712+
ERROR(redundant_any_in_existential,none,
4713+
"redundant 'any' has no effect on existential type %0",
4714+
(Type))
47124715
ERROR(any_not_existential,none,
47134716
"'any' has no effect on %select{concrete type|type parameter}0 %1",
47144717
(bool, Type))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===--- SyntaxRegexFallbackLexing.h --------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
namespace swift {
14+
/// SwiftSyntax parsing currently doesn't link against
15+
/// SwiftExperimentalStringProcessing and is thus missing the regex lexing
16+
/// functions defined in it. This registers a fallback regex-lexing function
17+
/// implemented in C++ that is sufficient to generate a valid SwiftSyntax tree.
18+
/// The regex parser registered by this function will accept all regex literals
19+
/// and is not suited for normal compilation.
20+
void registerSyntaxFallbackRegexParser();
21+
} // end namespace swift

include/swift/SIL/BasicBlockUtils.h

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
#ifndef SWIFT_SIL_BASICBLOCKUTILS_H
1414
#define SWIFT_SIL_BASICBLOCKUTILS_H
1515

16-
#include "swift/SIL/SILValue.h"
1716
#include "swift/SIL/BasicBlockBits.h"
17+
#include "swift/SIL/BasicBlockDatastructures.h"
18+
#include "swift/SIL/SILValue.h"
1819
#include "llvm/ADT/SetVector.h"
1920
#include "llvm/ADT/SmallPtrSet.h"
2021
#include "llvm/ADT/SmallVector.h"
@@ -155,6 +156,81 @@ void findJointPostDominatingSet(
155156
bool checkDominates(SILBasicBlock *sourceBlock, SILBasicBlock *destBlock);
156157
#endif
157158

159+
/// Walk depth-first the region backwards reachable from the provided roots
160+
/// constrained by \p region's \p isInRegion member function.
161+
///
162+
/// interface Region {
163+
/// /// Whether the indicated basic block is within the region of the graph
164+
/// /// that should be traversed.
165+
/// bool isInRegion(SILBasicBlock *)
166+
/// }
167+
template <typename Region>
168+
struct SILCFGBackwardDFS {
169+
Region &region;
170+
ArrayRef<SILBasicBlock *> roots;
171+
Optional<SmallVector<SILBasicBlock *, 16>> cachedPostOrder;
172+
Optional<BasicBlockSet> cachedVisited;
173+
174+
SILCFGBackwardDFS(Region &region, ArrayRef<SILBasicBlock *> roots)
175+
: region(region), roots(roots) {}
176+
177+
/// Visit the blocks of the region in post-order.
178+
///
179+
/// interface Visitor {
180+
/// /// Visit each block in topological order.
181+
/// void visit(SILBasicBlock *)
182+
/// }
183+
template <typename Visitor>
184+
void visitPostOrder(Visitor &visitor) {
185+
if (roots.empty())
186+
return;
187+
auto *function = roots.front()->getParent();
188+
cachedVisited.emplace(function);
189+
for (auto *root : roots) {
190+
SmallVector<std::pair<SILBasicBlock *, SILBasicBlock::pred_iterator>, 32>
191+
stack;
192+
if (!region.isInRegion(root))
193+
continue;
194+
stack.push_back({root, root->pred_begin()});
195+
while (!stack.empty()) {
196+
while (stack.back().second != stack.back().first->pred_end()) {
197+
auto predecessor = *stack.back().second;
198+
stack.back().second++;
199+
if (!region.isInRegion(predecessor))
200+
continue;
201+
if (cachedVisited->insert(predecessor))
202+
stack.push_back({predecessor, predecessor->pred_begin()});
203+
}
204+
visitor.visit(stack.back().first);
205+
stack.pop_back();
206+
}
207+
}
208+
}
209+
210+
/// Visit the region in post-order and cache the visited blocks.
211+
void cachePostOrder() {
212+
if (cachedPostOrder)
213+
return;
214+
struct Visitor {
215+
SILCFGBackwardDFS<Region> &dfs;
216+
void visit(SILBasicBlock *block) {
217+
dfs.cachedPostOrder->push_back(block);
218+
}
219+
};
220+
cachedPostOrder.emplace();
221+
Visitor visitor{*this};
222+
visitPostOrder(visitor);
223+
}
224+
225+
/// The region in post-order.
226+
ArrayRef<SILBasicBlock *> postOrder() {
227+
cachePostOrder();
228+
return *cachedPostOrder;
229+
};
230+
231+
/// The region in reverse post-order.
232+
auto reversePostOrder() { return llvm::reverse(postOrder()); }
233+
};
158234
} // namespace swift
159235

160236
#endif

include/swift/SIL/SILBasicBlock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public SwiftObjectHeader {
4848
/// A backreference to the containing SILFunction.
4949
SILFunction *Parent;
5050

51-
/// PrevList - This is a list of all of the terminator operands that are
51+
/// PredList - This is a list of all of the terminator operands that are
5252
/// branching to this block, forming the predecessor list. This is
5353
/// automatically managed by the SILSuccessor class.
5454
SILSuccessor *PredList = nullptr;

0 commit comments

Comments
 (0)