Skip to content

Commit 2eb8011

Browse files
authored
Merge branch 'main' into ewilde/async-main-resolution
2 parents a427c05 + ecf8a35 commit 2eb8011

File tree

411 files changed

+11201
-2692
lines changed

Some content is hidden

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

411 files changed

+11201
-2692
lines changed

CHANGELOG.md

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

66
## Swift 5.7
77

8+
* [SE-0326][]:
9+
10+
It's now possible to infer parameter and result types from the body of a multi-statement
11+
closure. The distinction between single- and multi-statement closures has been removed.
12+
13+
Use of closures becomes less cumbersome by removing the need to constantly specify explicit
14+
closure types which sometimes could be pretty large e.g. when there are multiple parameters
15+
or a complex tuple result type.
16+
17+
For example:
18+
19+
```swift
20+
func map<T>(fn: (Int) -> T) -> T {
21+
return fn(42)
22+
}
23+
24+
func computeResult<U: BinaryInteger>(_: U) -> U { /* processing */ }
25+
26+
let _ = map {
27+
if let $0 < 0 {
28+
// do some processing
29+
}
30+
31+
return computeResult($0)
32+
}
33+
```
34+
35+
The result type of `map` can now be inferred from the body of the trailing closure
36+
passed as an argument.
37+
38+
* [SE-0345][]:
39+
40+
It is now possible to unwrap optional variables with a shorthand syntax that
41+
shadows the existing declaration. For example, the following:
42+
43+
```swift
44+
let foo: String? = "hello world"
45+
46+
if let foo {
47+
print(foo) // prints "hello world"
48+
}
49+
```
50+
51+
is equivalent to:
52+
53+
```swift
54+
let foo: String? = "hello world"
55+
56+
if let foo = foo {
57+
print(foo) // prints "hello world"
58+
}
59+
```
60+
861
* [SE-0340][]:
962

1063
It is now possible to make declarations unavailable from use in asynchronous
@@ -9095,6 +9148,8 @@ Swift 1.0
90959148
[SE-0336]: <https://github.com/apple/swift-evolution/blob/main/proposals/0336-distributed-actor-isolation.md>
90969149
[SE-0343]: <https://github.com/apple/swift-evolution/blob/main/proposals/0343-top-level-concurrency.md>
90979150
[SE-0340]: <https://github.com/apple/swift-evolution/blob/main/proposals/0340-swift-noasync.md>
9151+
[SE-0345]: <https://github.com/apple/swift-evolution/blob/main/proposals/0345-if-let-shorthand.md>
9152+
[SE-0326]: <https://github.com/apple/swift-evolution/blob/main/proposals/0326-extending-multi-statement-closure-inference.md>
90989153

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

SwiftCompilerSources/Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let package = Package(
1111
.library(
1212
name: "Swift",
1313
type: .static,
14-
targets: ["SIL", "Optimizer", "ExperimentalRegex"]),
14+
targets: ["SIL", "Optimizer", "_RegexParser"]),
1515
],
1616
dependencies: [
1717
],
@@ -26,15 +26,15 @@ let package = Package(
2626
"-cross-module-optimization"
2727
])]),
2828
.target(
29-
name: "ExperimentalRegex",
29+
name: "_RegexParser",
3030
dependencies: [],
3131
swiftSettings: [SwiftSetting.unsafeFlags([
3232
"-I", "../include/swift",
3333
"-cross-module-optimization"
3434
])]),
3535
.target(
3636
name: "Optimizer",
37-
dependencies: ["SIL", "ExperimentalRegex"],
37+
dependencies: ["SIL", "_RegexParser"],
3838
swiftSettings: [SwiftSetting.unsafeFlags([
3939
"-I", "../include/swift",
4040
"-cross-module-optimization"

SwiftCompilerSources/Sources/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
add_subdirectory(Basic)
1212
add_subdirectory(AST)
1313
if(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
14-
add_subdirectory(ExperimentalRegex)
14+
add_subdirectory(_RegexParser)
1515
endif()
1616
add_subdirectory(SIL)
1717
add_subdirectory(Optimizer)

SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
set(dependencies)
1010
list(APPEND dependencies Basic SIL)
1111
if(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
12-
list(APPEND dependencies ExperimentalRegex)
12+
list(APPEND dependencies _RegexParser)
1313
endif()
1414

1515
add_swift_compiler_module(Optimizer DEPENDS ${dependencies})

SwiftCompilerSources/Sources/Optimizer/DataStructures/BasicBlockRange.swift

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,47 +51,62 @@ struct BasicBlockRange : CustomStringConvertible, CustomReflectable {
5151
/// The dominating begin block.
5252
let begin: BasicBlock
5353

54-
/// The exclusive range, i.e. not containing the end blocks.
55-
private(set) var range: Stack<BasicBlock>
54+
/// The inclusive range, i.e. the exclusive range plus the end blocks.
55+
private(set) var inclusiveRange: Stack<BasicBlock>
5656

57+
/// The exclusive range, i.e. not containing the end blocks.
58+
var range: LazyFilterSequence<Stack<BasicBlock>> {
59+
inclusiveRange.lazy.filter { contains($0) }
60+
}
61+
5762
/// All inserted blocks.
5863
private(set) var inserted: Stack<BasicBlock>
5964

60-
private var insertedSet: BasicBlockSet
65+
private var wasInserted: BasicBlockSet
66+
private var inExclusiveRange: BasicBlockSet
6167
private var worklist: BasicBlockWorklist
6268

6369
init(begin: BasicBlock, _ context: PassContext) {
6470
self.begin = begin
65-
self.range = Stack(context)
71+
self.inclusiveRange = Stack(context)
6672
self.inserted = Stack(context)
67-
self.insertedSet = BasicBlockSet(context)
73+
self.wasInserted = BasicBlockSet(context)
74+
self.inExclusiveRange = BasicBlockSet(context)
6875
self.worklist = BasicBlockWorklist(context)
76+
worklist.pushIfNotVisited(begin)
6977
}
7078

7179
/// Insert a potential end block.
7280
mutating func insert(_ block: BasicBlock) {
73-
if !insertedSet.contains(block) {
74-
insertedSet.insert(block)
81+
if !wasInserted.contains(block) {
82+
wasInserted.insert(block)
7583
inserted.append(block)
7684
}
77-
if block != begin {
78-
worklist.pushIfNotVisited(contentsOf: block.predecessors)
79-
80-
while let b = worklist.pop() {
81-
range.append(b)
82-
if b != begin {
83-
worklist.pushIfNotVisited(contentsOf: b.predecessors)
85+
worklist.pushIfNotVisited(block)
86+
while let b = worklist.pop() {
87+
inclusiveRange.append(b)
88+
if b != begin {
89+
for pred in b.predecessors {
90+
worklist.pushIfNotVisited(pred)
91+
inExclusiveRange.insert(pred)
8492
}
8593
}
8694
}
8795
}
8896

97+
/// Insert a sequence of potential end blocks.
98+
mutating func insert<S: Sequence>(contentsOf other: S) where S.Element == BasicBlock {
99+
for block in other {
100+
insert(block)
101+
}
102+
}
103+
89104
/// Returns true if the exclusive range contains `block`.
90-
func contains(_ block: BasicBlock) -> Bool { worklist.hasBeenPushed(block) }
105+
func contains(_ block: BasicBlock) -> Bool { inExclusiveRange.contains(block) }
91106

92107
/// Returns true if the inclusive range contains `block`.
93108
func inclusiveRangeContains (_ block: BasicBlock) -> Bool {
94-
contains(block) || insertedSet.contains(block)
109+
worklist.hasBeenPushed(block)
95110
}
96111

97112
/// Returns true if the range is valid and that's iff the begin block dominates all blocks of the range.
@@ -104,14 +119,14 @@ struct BasicBlockRange : CustomStringConvertible, CustomReflectable {
104119

105120
/// Returns the end blocks.
106121
var ends: LazyFilterSequence<Stack<BasicBlock>> {
107-
inserted.lazy.filter { !worklist.hasBeenPushed($0) }
122+
inserted.lazy.filter { !contains($0) }
108123
}
109124

110125
/// Returns the exit blocks.
111126
var exits: LazySequence<FlattenSequence<
112-
LazyMapSequence<Stack<BasicBlock>,
127+
LazyMapSequence<LazyFilterSequence<Stack<BasicBlock>>,
113128
LazyFilterSequence<SuccessorArray>>>> {
114-
range.lazy.flatMap {
129+
range.flatMap {
115130
$0.successors.lazy.filter {
116131
!inclusiveRangeContains($0) || $0 == begin
117132
}
@@ -124,22 +139,25 @@ struct BasicBlockRange : CustomStringConvertible, CustomReflectable {
124139
}
125140

126141
var description: String {
127-
"""
128-
begin: \(begin.name)
129-
range: \(range)
130-
ends: \(ends)
131-
exits: \(exits)
132-
interiors: \(interiors)
133-
"""
142+
return (isValid ? "" : "<invalid>\n") +
143+
"""
144+
begin: \(begin.name)
145+
range: \(range)
146+
inclrange: \(inclusiveRange)
147+
ends: \(ends)
148+
exits: \(exits)
149+
interiors: \(interiors)
150+
"""
134151
}
135152

136153
var customMirror: Mirror { Mirror(self, children: []) }
137154

138155
/// TODO: once we have move-only types, make this a real deinit.
139156
mutating func deinitialize() {
140157
worklist.deinitialize()
158+
inExclusiveRange.deinitialize()
159+
wasInserted.deinitialize()
141160
inserted.deinitialize()
142-
insertedSet.deinitialize()
143-
range.deinitialize()
161+
inclusiveRange.deinitialize()
144162
}
145163
}

SwiftCompilerSources/Sources/Optimizer/DataStructures/InstructionRange.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import SIL
1414

15-
/// A range of basic blocks.
15+
/// A range of instructions.
1616
///
1717
/// The `InstructionRange` defines a range from a dominating "begin" instruction to one or more "end" instructions.
1818
/// The range is "exclusive", which means that the "end" instructions are not part of the range.
@@ -60,6 +60,13 @@ struct InstructionRange : CustomStringConvertible, CustomReflectable {
6060
blockRange.insert(inst.block)
6161
}
6262

63+
/// Insert a sequence of potential end instructions.
64+
mutating func insert<S: Sequence>(contentsOf other: S) where S.Element == Instruction {
65+
for inst in other {
66+
insert(inst)
67+
}
68+
}
69+
6370
/// Returns true if the exclusive range contains `inst`.
6471
func contains(_ inst: Instruction) -> Bool {
6572
let block = inst.block
@@ -99,7 +106,7 @@ struct InstructionRange : CustomStringConvertible, CustomReflectable {
99106

100107
/// Returns the exit instructions.
101108
var exits: LazyMapSequence<LazySequence<FlattenSequence<
102-
LazyMapSequence<Stack<BasicBlock>,
109+
LazyMapSequence<LazyFilterSequence<Stack<BasicBlock>>,
103110
LazyFilterSequence<SuccessorArray>>>>,
104111
Instruction> {
105112
blockRange.exits.lazy.map { $0.instructions.first! }
@@ -116,21 +123,20 @@ struct InstructionRange : CustomStringConvertible, CustomReflectable {
116123
let isInterior = include
117124
include = true
118125
return isInterior
119-
120126
}
121127
return false
122128
}
123129
}
124130
}
125131

126132
var description: String {
127-
"""
128-
begin: \(begin)
129-
range: \(blockRange.range)
130-
ends: \(ends.map { $0.description }.joined(separator: "\n "))
131-
exits: \(exits.map { $0.description }.joined(separator: "\n "))
132-
interiors:\(interiors.map { $0.description }.joined(separator: "\n "))
133-
"""
133+
return (isValid ? "" : "<invalid>\n") +
134+
"""
135+
begin: \(begin)
136+
ends: \(ends.map { $0.description }.joined(separator: "\n "))
137+
exits: \(exits.map { $0.description }.joined(separator: "\n "))
138+
interiors:\(interiors.map { $0.description }.joined(separator: "\n "))
139+
"""
134140
}
135141

136142
var customMirror: Mirror { Mirror(self, children: []) }

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ swift_compiler_sources(Optimizer
1010
AssumeSingleThreaded.swift
1111
SILPrinter.swift
1212
MergeCondFails.swift
13+
RangeDumper.swift
1314
ReleaseDevirtualizer.swift
1415
RunUnitTests.swift
1516
)

0 commit comments

Comments
 (0)