Skip to content

Commit 0baf8db

Browse files
Merge remote-tracking branch 'origin/main' into katei/merge-main-2022-10-01
2 parents f46ff7e + 1a79d93 commit 0baf8db

File tree

95 files changed

+1350
-730
lines changed

Some content is hidden

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

95 files changed

+1350
-730
lines changed

CHANGELOG.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,48 @@ CHANGELOG
33

44
_**Note:** This is in reverse chronological order, so newer entries are added to the top._
55

6+
## Swift 6.0
7+
8+
* [SE-0365][]:
9+
10+
Implicit `self` is now permitted for `weak self` captures, after `self` is unwrapped.
11+
12+
For example, the usage of implicit `self` below is now permitted:
13+
14+
```swift
15+
class ViewController {
16+
let button: Button
17+
18+
func setup() {
19+
button.tapHandler = { [weak self] in
20+
guard let self else { return }
21+
dismiss() // refers to `self.dismiss()`
22+
}
23+
}
24+
25+
func dismiss() { ... }
26+
}
27+
```
28+
29+
In Swift 5 language modes, implicit `self` is permitted for `weak self` captures in _non-escaping_ closures even before `self` is unwrapped. For example, this code compiles successfully in Swift 5 language mode:
30+
31+
```swift
32+
class ExampleClass {
33+
func makeArray() -> [String] {
34+
// `Array.map` takes a non-escaping closure:
35+
["foo", "bar", "baaz"].map { [weak self] string in
36+
double(string) // implicitly refers to `self!.double(string)`
37+
}
38+
}
39+
40+
func double(_ string: String) -> String {
41+
string + string
42+
}
43+
}
44+
```
45+
46+
In Swift 6, the above code will no longer compile. `weak self` captures in non-escaping closures now have the same behavior as captures in escaping closures (as described in [SE-0365][]). Code relying on the previous behavior will need to be updated to either unwrap `self` (e.g. by adding a `guard let self else return` statement), or to use a different capture method (e.g. using `[self]` or `[unowned self]` instead of `[weak self]`).
47+
648
## Swift 5.8
749

850
* [SE-0362][]:
@@ -9559,6 +9601,7 @@ Swift 1.0
95599601
[SE-0357]: <https://github.com/apple/swift-evolution/blob/main/proposals/0357-regex-string-processing-algorithms.md>
95609602
[SE-0358]: <https://github.com/apple/swift-evolution/blob/main/proposals/0358-primary-associated-types-in-stdlib.md>
95619603
[SE-0362]: <https://github.com/apple/swift-evolution/blob/main/proposals/0362-piecemeal-future-features.md>
9604+
[SE-0365]: <https://github.com/apple/swift-evolution/blob/main/proposals/0365-implicit-self-weak-capture.md>
95629605

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

docs/CppInteroperability/GettingStartedWithC++Interop.md

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@ This document is designed to get you started with bidirectional API-level intero
1111

1212
## Creating a Module to contain your C++ source code
1313

14-
- Create a new C++ implementation and header file
15-
- For this example we will call the files Cxx, so we should have a Cxx.cpp and Cxx.hpp.
14+
- Create a new target in Xcode via _File_ | _New_ | _Target_, select _Library_.
15+
- Within the directory of the newly created target, create a new C++ implementation and header file
16+
- For this example we will call the files CxxTest, so we should have a CxxTest.cpp and CxxTest.hpp.
1617
- Next create an empty file and call it `module.modulemap`, in this file create the module for your source code, and define your C++ header (`requires cplusplus` isn't required but it's convention for C++ modules, especially if they use C++ features).
1718

1819
```
19-
//In module.modulemap
20-
module Cxx {
21-
//note that your header should be the file that contains your method implementations
22-
header "Cxx.hpp"
20+
// In module.modulemap
21+
module CxxTest {
22+
header "CxxTest.hpp"
2323
requires cplusplus
2424
}
2525
```
2626

27-
- Move the newly created files (Cxx.cpp, Cxx.hpp, module.modulemap) into a separate directory (this should remain in your project directory)
28-
29-
<img width="252" alt="Screen Shot 2022-02-26 at 9 14 06 PM" src="https://user-images.githubusercontent.com/62521716/155867937-9d9d6c62-4418-414d-bc4e-5d12c2055022.png">
27+
<img width="127" alt="cxx-interop-ctest" src="https://user-images.githubusercontent.com/3801618/192995602-f37137f3-ec15-4fdd-bf2c-591728945a68.png">
3028

3129
## Adding C++ to an Xcode project
3230
- In your xcode project, follow the steps [Creating a Module to contain your C++ source code](#creating-a-module-to-contain-your-c-source-code) in your project directory
@@ -35,44 +33,44 @@ Add the C++ module to the include path and enable C++ interop:
3533
- Navigate to your project directory
3634
- In `Project` navigate to `Build Settings` -> `Swift Compiler`
3735
- Under `Custom Flags` -> `Other Swift Flags` add`-enable-experimental-cxx-interop`
38-
- Under `Search Paths` -> `Import Paths` add your search path to the C++ module (i.e, `./ProjectName/Cxx`). Repeat this step in `Other Swift Flags`
36+
- Under `Search Paths` -> `Import Paths` add your search path to the C++ module (i.e, `./ProjectName/CxxTest`). Repeat this step in `Other Swift Flags`
3937

4038
```
4139
//Add to Other Swift Flags and Import Paths respectively
4240
-enable-experimental-cxx-interop
43-
-I./ProjectName/Cxx
41+
-I./ProjectName/CxxTest
4442
```
4543

4644
- This should now allow your to import your C++ Module into any `.swift` file.
4745

4846
```
4947
//In ContentView.swift
5048
import SwiftUI
51-
import Cxx
49+
import CxxTest
5250
5351
struct ContentView: View {
5452
var body: some View {
55-
Text("Cxx function result: \(cxxFunction(7))")
53+
Text("CxxTest function result: \(cxxFunction(7))")
5654
.padding()
5755
}
5856
}
5957
```
6058

6159
```
62-
//In Cxx.hpp
60+
// In CxxTest.hpp
6361
64-
#ifndef Cxx_hpp
65-
#define Cxx_hpp
62+
#ifndef CxxTest_hpp
63+
#define CxxTest_hpp
6664
6765
int cxxFunction(int n);
6866
6967
#endif
7068
```
7169

7270
```
73-
//In Cxx.cpp
71+
// In CxxTest.cpp
7472
75-
#include "Cxx.hpp"
73+
#include "CxxTest.hpp"
7674
7775
int cxxFunction(int n) {
7876
return n;
@@ -101,24 +99,24 @@ let package = Package(
10199
platforms: [.macOS(.v12)],
102100
products: [
103101
.library(
104-
name: "Cxx",
105-
targets: ["Cxx"]),
102+
name: "CxxTest",
103+
targets: ["CxxTest"]),
106104
.library(
107105
name: "CxxInterop",
108106
targets: ["CxxInterop"]),
109107
],
110108
targets: [
111109
.target(
112-
name: "Cxx",
110+
name: "CxxTest",
113111
dependencies: []
114112
),
115113
.executableTarget(
116114
name: "CxxInterop",
117-
dependencies: ["Cxx"],
115+
dependencies: ["CxxTest"],
118116
path: "./Sources/CxxInterop",
119117
sources: [ "main.swift" ],
120118
swiftSettings: [.unsafeFlags([
121-
"-I", "Sources/Cxx",
119+
"-I", "Sources/CxxTest",
122120
"-enable-experimental-cxx-interop",
123121
])]
124122
),
@@ -132,7 +130,7 @@ let package = Package(
132130
```
133131
//In main.swift
134132
135-
import Cxx
133+
import CxxTest
136134
137135
public struct CxxInterop {
138136
@@ -151,12 +149,12 @@ After creating your project follow the steps [Creating a Module to contain your
151149

152150
- Create a `CMakeLists.txt` file and configure for your project
153151
- In`add_library` invoke `cxx-support` with the path to the C++ implementation file
154-
- Add the `target_include_directories` with `cxx-support` and path to the C++ Module `${CMAKE_SOURCE_DIR}/Sources/Cxx`
152+
- Add the `target_include_directories` with `cxx-support` and path to the C++ Module `${CMAKE_SOURCE_DIR}/Sources/CxxTest`
155153
- Add the `add_executable` to the specific files/directory you would like to generate source, with`SHELL:-enable-experimental-cxx-interop`.
156154
- In the example below we will be following the file structure used in [Creating a Swift Package](#Creating-a-Swift-Package)
157155

158156
```
159-
//In CMakeLists.txt
157+
// In CMakeLists.txt
160158
161159
cmake_minimum_required(VERSION 3.18)
162160
@@ -166,14 +164,12 @@ set(CMAKE_CXX_STANDARD 11)
166164
set(CMAKE_CXX_STANDARD_REQUIRED YES)
167165
set(CMAKE_CXX_EXTENSIONS OFF)
168166
169-
add_library(cxx-support ./Sources/Cxx/Cxx.cpp)
167+
add_library(cxx-support ./Sources/CxxTest/CxxTest.cpp)
170168
target_compile_options(cxx-support PRIVATE
171-
-I${SWIFT_CXX_TOOLCHAIN}/usr/include/c++/v1
172169
-fno-exceptions
173-
-fignore-exceptions
174-
-nostdinc++)
170+
-fignore-exceptions)
175171
target_include_directories(cxx-support PUBLIC
176-
${CMAKE_SOURCE_DIR}/Sources/Cxx)
172+
${CMAKE_SOURCE_DIR}/Sources/CxxTest)
177173
178174
add_executable(CxxInterop ./Sources/CxxInterop/main.swift)
179175
target_compile_options(CxxInterop PRIVATE
@@ -185,7 +181,7 @@ target_link_libraries(CxxInterop PRIVATE cxx-support)
185181
```
186182
//In main.swift
187183
188-
import Cxx
184+
import CxxTest
189185
190186
public struct CxxInterop {
191187
public static func main() {

docs/HowToGuides/GettingStarted.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ Double-check that running `pwd` prints a path ending with `swift`.
154154
155155
1. The latest Linux dependencies are listed in the respective Dockerfiles:
156156
* [Ubuntu 20.04](https://github.com/apple/swift-docker/blob/main/swift-ci/master/ubuntu/20.04/Dockerfile)
157+
* [Ubuntu 22.04](https://github.com/apple/swift-docker/blob/main/swift-ci/master/ubuntu/22.04/Dockerfile)
157158
* [CentOS 7](https://github.com/apple/swift-docker/blob/main/swift-ci/master/centos/7/Dockerfile)
158159
* [Amazon Linux 2](https://github.com/apple/swift-docker/blob/main/swift-ci/master/amazon-linux/2/Dockerfile)
159160

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,12 @@ NOTE(optional_key_path_root_base_chain, none,
12041204
NOTE(optional_key_path_root_base_unwrap, none,
12051205
"unwrap the optional using '!.' to access unwrapped type member %0",
12061206
(DeclNameRef))
1207+
1208+
ERROR(optional_self_not_unwrapped,none,
1209+
"explicit use of 'self' is required when 'self' is optional, "
1210+
"to make control flow explicit", ())
1211+
NOTE(optional_self_chain,none,
1212+
"reference 'self?.' explicitly", ())
12071213

12081214
ERROR(missing_unwrap_optional_try,none,
12091215
"value of optional type %0 not unwrapped; did you mean to use 'try!' "
@@ -3939,8 +3945,6 @@ NOTE(note_reference_self_explicitly,none,
39393945
NOTE(note_other_self_capture,none,
39403946
"variable other than 'self' captured here under the name 'self' does not "
39413947
"enable implicit 'self'", ())
3942-
NOTE(note_self_captured_weakly,none,
3943-
"weak capture of 'self' here does not enable implicit 'self'", ())
39443948
ERROR(implicit_use_of_self_in_closure,none,
39453949
"implicit use of 'self' in closure; use 'self.' to make"
39463950
" capture semantics explicit", ())

include/swift/AST/NameLookup.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,20 @@ struct LookupResultEntry {
107107
/// extension (if it found something at that level).
108108
DeclContext *BaseDC;
109109

110+
/// The declaration that defines the base of the call to `Value`.
111+
/// This is always available, as long as `BaseDC` is not null.
112+
ValueDecl *BaseDecl;
113+
110114
/// The declaration corresponds to the given name; i.e. the decl we are
111115
/// looking up.
112116
ValueDecl *Value;
113117

114118
public:
115-
LookupResultEntry(ValueDecl *value) : BaseDC(nullptr), Value(value) {}
119+
LookupResultEntry(ValueDecl *value)
120+
: BaseDC(nullptr), BaseDecl(nullptr), Value(value) {}
116121

117-
LookupResultEntry(DeclContext *baseDC, ValueDecl *value)
118-
: BaseDC(baseDC), Value(value) {}
122+
LookupResultEntry(DeclContext *baseDC, ValueDecl *baseDecl, ValueDecl *value)
123+
: BaseDC(baseDC), BaseDecl(baseDecl), Value(value) {}
119124

120125
ValueDecl *getValueDecl() const { return Value; }
121126

include/swift/AST/Stmt.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,14 +398,15 @@ class alignas(8) PoundAvailableInfo final :
398398
class PoundHasSymbolInfo final : public ASTAllocated<PoundHasSymbolInfo> {
399399
Expr *SymbolExpr;
400400
ConcreteDeclRef ReferencedDecl;
401+
bool Invalid;
401402

402403
SourceLoc PoundLoc;
403404
SourceLoc LParenLoc;
404405
SourceLoc RParenLoc;
405406

406407
PoundHasSymbolInfo(SourceLoc PoundLoc, SourceLoc LParenLoc, Expr *SymbolExpr,
407408
SourceLoc RParenLoc)
408-
: SymbolExpr(SymbolExpr), ReferencedDecl(), PoundLoc(PoundLoc),
409+
: SymbolExpr(SymbolExpr), ReferencedDecl(), Invalid(), PoundLoc(PoundLoc),
409410
LParenLoc(LParenLoc), RParenLoc(RParenLoc){};
410411

411412
public:
@@ -419,6 +420,10 @@ class PoundHasSymbolInfo final : public ASTAllocated<PoundHasSymbolInfo> {
419420
ConcreteDeclRef getReferencedDecl() { return ReferencedDecl; }
420421
void setReferencedDecl(ConcreteDeclRef CDR) { ReferencedDecl = CDR; }
421422

423+
/// Returns true if the referenced decl has been diagnosed as invalid.
424+
bool isInvalid() const { return Invalid; }
425+
void setInvalid() { Invalid = true; }
426+
422427
SourceLoc getLParenLoc() const { return LParenLoc; }
423428
SourceLoc getRParenLoc() const { return RParenLoc; }
424429
SourceLoc getStartLoc() const { return PoundLoc; }
@@ -630,7 +635,7 @@ class DoStmt : public LabeledStmt {
630635
};
631636

632637
/// Either an "if let" case or a simple boolean expression can appear as the
633-
/// condition of an 'if' or 'while' statement.
638+
/// condition of an 'if', 'guard', or 'while' statement.
634639
using StmtCondition = MutableArrayRef<StmtConditionElement>;
635640

636641
/// This is the common base class between statements that can have labels, and

include/swift/SIL/SILDeclRef.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,6 @@ struct SILDeclRef {
483483
return result;
484484
}
485485

486-
/// True is the decl ref references any kind of thunk.
487-
bool isAnyThunk() const;
488-
489486
/// True if the decl ref references a thunk from a natively foreign
490487
/// declaration to Swift calling convention.
491488
bool isForeignToNativeThunk() const;

include/swift/SIL/SILProfiler.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ class SILCoverageMap;
3131
class SILFunction;
3232
class SILModule;
3333

34-
/// Returns whether the given AST node requires profiling instrumentation.
35-
bool doesASTRequireProfiling(SILModule &M, ASTNode N, SILDeclRef Constant);
36-
3734
/// SILProfiler - Maps AST nodes to profile counters.
3835
class SILProfiler : public SILAllocated<SILProfiler> {
3936
private:

include/swift/SILOptimizer/Utils/CanonicalOSSALifetime.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,6 @@ class CanonicalizeOSSALifetime {
249249
/// liveness may be pruned during canonicalization.
250250
bool pruneDebugMode;
251251

252-
/// If true, then new destroy_value instructions will be poison.
253-
bool poisonRefsMode;
254-
255252
/// If true and we are processing a value of move_only type, emit a diagnostic
256253
/// when-ever we need to insert a copy_value.
257254
std::function<void(Operand *)> moveOnlyCopyValueNotification;
@@ -348,12 +345,11 @@ class CanonicalizeOSSALifetime {
348345
}
349346

350347
CanonicalizeOSSALifetime(
351-
bool pruneDebugMode, bool poisonRefsMode,
352-
NonLocalAccessBlockAnalysis *accessBlockAnalysis, DominanceInfo *domTree,
353-
InstructionDeleter &deleter,
348+
bool pruneDebugMode, NonLocalAccessBlockAnalysis *accessBlockAnalysis,
349+
DominanceInfo *domTree, InstructionDeleter &deleter,
354350
std::function<void(Operand *)> moveOnlyCopyValueNotification = nullptr,
355351
std::function<void(Operand *)> moveOnlyFinalConsumingUse = nullptr)
356-
: pruneDebugMode(pruneDebugMode), poisonRefsMode(poisonRefsMode),
352+
: pruneDebugMode(pruneDebugMode),
357353
moveOnlyCopyValueNotification(moveOnlyCopyValueNotification),
358354
moveOnlyFinalConsumingUse(moveOnlyFinalConsumingUse),
359355
accessBlockAnalysis(accessBlockAnalysis), domTree(domTree),
@@ -414,11 +410,9 @@ class CanonicalizeOSSALifetime {
414410
void findOrInsertDestroys();
415411

416412
void findOrInsertDestroyOnCFGEdge(SILBasicBlock *predBB,
417-
SILBasicBlock *succBB, bool needsPoison);
413+
SILBasicBlock *succBB);
418414

419415
void rewriteCopies();
420-
421-
void injectPoison();
422416
};
423417

424418
} // end namespace swift

0 commit comments

Comments
 (0)