Skip to content

Commit ebd62db

Browse files
authored
Merge pull request #4309 from swiftwasm/main
[pull] swiftwasm from main
2 parents 0ea3ab1 + 7462169 commit ebd62db

File tree

135 files changed

+2933
-507
lines changed

Some content is hidden

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

135 files changed

+2933
-507
lines changed

CHANGELOG.md

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

66
## Swift 5.7
77

8+
* The compiler now emits a warning when a non-final class conforms to a protocol that imposes a same-type requirement between `Self` and an associated type. This is because such a requirement makes the conformance unsound for subclasses.
9+
10+
For example, Swift 5.6 would allow the following code, which at runtime would construct an instanec of `C` and not `SubC` as expected:
11+
12+
```swift
13+
protocol P {
14+
associatedtype A : Q where Self == Self.A.B
15+
}
16+
17+
protocol Q {
18+
associatedtype B
19+
20+
static func getB() -> B
21+
}
22+
23+
class C : P {
24+
typealias A = D
25+
}
26+
27+
class D : Q {
28+
typealias B = C
29+
30+
static func getB() -> C { return C() }
31+
}
32+
33+
extension P {
34+
static func getAB() -> Self {
35+
// This is well-typed, because `Self.A.getB()` returns
36+
// `Self.A.B`, which is equivalent to `Self`.
37+
return Self.A.getB()
38+
}
39+
}
40+
41+
class SubC : C {}
42+
43+
// P.getAB() declares a return type of `Self`, so it should
44+
// return `SubC`, but it actually returns a `C`.
45+
print(SubC.getAB())
46+
```
47+
48+
To make the above example correct, either the class `C` needs to become `final` (in which case `SubC` cannot be declared) or protocol `P` needs to be re-designed to not include the same-type requirement `Self == Self.A.B`.
49+
850
* [SE-0341][]:
951

1052
Opaque types can now be used in the parameters of functions and subscripts, wher they provide a shorthand syntax for the introduction of a generic parameter. For example, the following:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2022 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
add_swift_compiler_module(AST
10+
DEPENDS
11+
Basic
12+
SOURCES
13+
DiagnosticEngine.swift)
14+
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//===--- DiagnosticEngine.swift -------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 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+
import ASTBridging
14+
15+
import Basic
16+
17+
public typealias DiagID = BridgedDiagID
18+
19+
extension BridgedDiagnosticArgument {
20+
init(stringRef val: BridgedStringRef) {
21+
self.init(kind: .stringRef, value: .init(stringRefValue: val))
22+
}
23+
init(int val: Int) {
24+
self.init(kind: .int, value: .init(intValue: val))
25+
}
26+
}
27+
28+
public protocol DiagnosticArgument {
29+
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void)
30+
}
31+
extension String: DiagnosticArgument {
32+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
33+
withBridgedStringRef { fn(BridgedDiagnosticArgument(stringRef: $0)) }
34+
}
35+
}
36+
extension Int: DiagnosticArgument {
37+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
38+
fn(BridgedDiagnosticArgument(int: self))
39+
}
40+
}
41+
42+
public struct DiagnosticFixIt {
43+
public let start: SourceLoc
44+
public let byteLength: Int
45+
public let text: String
46+
47+
public init(start: SourceLoc, byteLength: Int, replacement text: String) {
48+
self.start = start
49+
self.byteLength = byteLength
50+
self.text = text
51+
}
52+
53+
func withBridgedDiagnosticFixIt(_ fn: (BridgedDiagnosticFixIt) -> Void) {
54+
text.withBridgedStringRef { bridgedTextRef in
55+
let bridgedDiagnosticFixIt = BridgedDiagnosticFixIt(
56+
start: start.bridged,
57+
byteLength: byteLength,
58+
text: bridgedTextRef)
59+
fn(bridgedDiagnosticFixIt)
60+
}
61+
}
62+
}
63+
64+
public struct DiagnosticEngine {
65+
private let bridged: BridgedDiagnosticEngine
66+
67+
public init(bridged: BridgedDiagnosticEngine) {
68+
self.bridged = bridged
69+
}
70+
71+
public func diagnose(_ position: SourceLoc?,
72+
_ id: DiagID,
73+
_ args: [DiagnosticArgument],
74+
highlight: CharSourceRange? = nil,
75+
fixIts: [DiagnosticFixIt] = []) {
76+
77+
let bridgedSourceLoc: BridgedSourceLoc = position.bridged
78+
let bridgedHighlightRange: BridgedCharSourceRange = highlight.bridged
79+
var bridgedArgs: [BridgedDiagnosticArgument] = []
80+
var bridgedFixIts: [BridgedDiagnosticFixIt] = []
81+
82+
// Build a higher-order function to wrap every 'withBridgedXXX { ... }'
83+
// calls, so we don't escape anything from the closure. 'bridgedArgs' and
84+
// 'bridgedFixIts' are temporary storage to store bridged values. So they
85+
// should not be used after the closue is executed.
86+
87+
var closure: () -> Void = {
88+
bridgedArgs.withBridgedArrayRef { bridgedArgsRef in
89+
bridgedFixIts.withBridgedArrayRef { bridgedFixItsRef in
90+
DiagnosticEngine_diagnose(bridged, bridgedSourceLoc,
91+
id, bridgedArgsRef,
92+
bridgedHighlightRange, bridgedFixItsRef)
93+
}
94+
}
95+
}
96+
for arg in args {
97+
closure = { [closure, arg] in
98+
arg._withBridgedDiagnosticArgument { bridgedArg in
99+
bridgedArgs.append(bridgedArg)
100+
closure()
101+
}
102+
}
103+
}
104+
for fixIt in fixIts {
105+
closure = { [closure, fixIt] in
106+
fixIt.withBridgedDiagnosticFixIt { bridgedFixIt in
107+
bridgedFixIts.append(bridgedFixIt)
108+
closure()
109+
}
110+
}
111+
}
112+
113+
closure()
114+
}
115+
116+
public func diagnose(_ position: SourceLoc?,
117+
_ id: DiagID,
118+
_ args: DiagnosticArgument...,
119+
highlight: CharSourceRange? = nil,
120+
fixIts: DiagnosticFixIt...) {
121+
diagnose(position, id, args, highlight: highlight, fixIts: fixIts)
122+
}
123+
}

SwiftCompilerSources/Sources/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

99
add_swift_compiler_module(Basic
10+
SourceLoc.swift
1011
Utils.swift)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===--- SourceLoc.swift - SourceLoc bridiging utilities ------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 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+
public struct SourceLoc {
14+
/// Points into a source file.
15+
let locationInFile: UnsafePointer<UInt8>
16+
17+
public init?(locationInFile: UnsafePointer<UInt8>?) {
18+
guard let locationInFile = locationInFile else {
19+
return nil
20+
}
21+
self.locationInFile = locationInFile
22+
}
23+
24+
public init?(bridged: BridgedSourceLoc) {
25+
guard let locationInFile = bridged.pointer else {
26+
return nil
27+
}
28+
self.init(locationInFile: locationInFile)
29+
}
30+
31+
public var bridged: BridgedSourceLoc {
32+
.init(pointer: locationInFile)
33+
}
34+
}
35+
36+
extension Optional where Wrapped == SourceLoc {
37+
public var bridged: BridgedSourceLoc {
38+
self?.bridged ?? .init(pointer: nil)
39+
}
40+
}
41+
42+
public struct CharSourceRange {
43+
private let start: SourceLoc
44+
private let byteLength: Int
45+
46+
public init(start: SourceLoc, byteLength: Int) {
47+
self.start = start
48+
self.byteLength = byteLength
49+
}
50+
51+
public init?(bridged: BridgedCharSourceRange) {
52+
guard let start = SourceLoc(bridged: bridged.start) else {
53+
return nil
54+
}
55+
self.init(start: start, byteLength: bridged.byteLength)
56+
}
57+
58+
public var bridged: BridgedCharSourceRange {
59+
.init(start: start.bridged, byteLength: byteLength)
60+
}
61+
}
62+
63+
extension Optional where Wrapped == CharSourceRange {
64+
public var bridged: BridgedCharSourceRange {
65+
self?.bridged ?? .init(start: .init(pointer: nil), byteLength: 0)
66+
}
67+
}

SwiftCompilerSources/Sources/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# NOTE: Subdirectories must be added in dependency order.
1010

1111
add_subdirectory(Basic)
12+
add_subdirectory(AST)
1213
if(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
1314
add_subdirectory(ExperimentalRegex)
1415
endif()

docs/ABI/Mangling.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ types where the metadata itself has unknown layout.)
223223
global ::= global 'TI' // implementation of a dynamic_replaceable function
224224
global ::= global 'Tu' // async function pointer of a function
225225
global ::= global 'TX' // function pointer of a dynamic_replaceable function
226+
global ::= global 'Twb' // back deployment thunk
227+
global ::= global 'TwB' // back deployment fallback function
226228
global ::= entity entity 'TV' // vtable override thunk, derived followed by base
227229
global ::= type label-list? 'D' // type mangling for the debugger with label list for function types.
228230
global ::= type 'TC' // continuation prototype (not actually used for real symbols)

include/swift/ABI/MetadataValues.h

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,57 +2201,6 @@ class TaskOptionRecordFlags : public FlagSet<size_t> {
22012201
getKind, setKind)
22022202
};
22032203

2204-
/// Kinds of async context.
2205-
enum class AsyncContextKind {
2206-
/// An ordinary asynchronous function.
2207-
Ordinary = 0,
2208-
2209-
/// A context which can yield to its caller.
2210-
Yielding = 1,
2211-
2212-
/// A continuation context.
2213-
Continuation = 2,
2214-
2215-
// Other kinds are reserved for interesting special
2216-
// intermediate contexts.
2217-
2218-
// Kinds >= 192 are private to the implementation.
2219-
First_Reserved = 192
2220-
};
2221-
2222-
/// Flags for async contexts.
2223-
class AsyncContextFlags : public FlagSet<uint32_t> {
2224-
public:
2225-
enum {
2226-
Kind = 0,
2227-
Kind_width = 8,
2228-
2229-
CanThrow = 8,
2230-
2231-
// Kind-specific flags should grow down from 31.
2232-
2233-
Continuation_IsExecutorSwitchForced = 31,
2234-
};
2235-
2236-
explicit AsyncContextFlags(uint32_t bits) : FlagSet(bits) {}
2237-
constexpr AsyncContextFlags() {}
2238-
AsyncContextFlags(AsyncContextKind kind) {
2239-
setKind(kind);
2240-
}
2241-
2242-
/// The kind of context this represents.
2243-
FLAGSET_DEFINE_FIELD_ACCESSORS(Kind, Kind_width, AsyncContextKind,
2244-
getKind, setKind)
2245-
2246-
/// Whether this context is permitted to throw.
2247-
FLAGSET_DEFINE_FLAG_ACCESSORS(CanThrow, canThrow, setCanThrow)
2248-
2249-
/// See AsyncContinuationFlags::isExecutorSwitchForced.
2250-
FLAGSET_DEFINE_FLAG_ACCESSORS(Continuation_IsExecutorSwitchForced,
2251-
continuation_isExecutorSwitchForced,
2252-
continuation_setIsExecutorSwitchForced)
2253-
};
2254-
22552204
/// Flags passed to swift_continuation_init.
22562205
class AsyncContinuationFlags : public FlagSet<size_t> {
22572206
public:

0 commit comments

Comments
 (0)