Skip to content

Commit 51fec5b

Browse files
committed
Adjust for inout diagnostics and fall back to original mutation strategy
1 parent 2c8e6b1 commit 51fec5b

12 files changed

+126
-100
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4054,6 +4054,8 @@ NOTE(because_rethrows_argument_throws,none,
40544054
NOTE(because_rethrows_default_argument_throws,none,
40554055
"call is to 'rethrows' function, but a defaulted argument function"
40564056
" can throw", ())
4057+
NOTE(because_rethrows_default_conformance_throws,none,
4058+
"call is to 'rethrows' function, but a conformance can throw", ())
40574059

40584060
ERROR(throwing_call_in_nonthrowing_autoclosure,none,
40594061
"call can throw, but it is executed in a non-throwing "

lib/IRGen/IRGenSIL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4360,7 +4360,7 @@ void IRGenSILFunction::visitDebugValueAddrInst(DebugValueAddrInst *i) {
43604360
break;
43614361
}
43624362
assert(llvm::isa<llvm::Argument>(Storage) &&
4363-
"arg expected to be load from inside %swift.context");
4363+
"arg expected to be load from inside swift.context");
43644364
#endif
43654365
Indirection = CoroIndirectValue;
43664366
}

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,8 @@ namespace {
965965

966966
Expr *subArg = arg->getSubExpr();
967967
ValueDecl *valueDecl = nullptr;
968+
if (auto binding = dyn_cast<BindOptionalExpr>(subArg))
969+
subArg = binding->getSubExpr();
968970
if (LookupExpr *baseArg = dyn_cast<LookupExpr>(subArg)) {
969971
while (LookupExpr *nextLayer = dyn_cast<LookupExpr>(baseArg->getBase()))
970972
baseArg = nextLayer;

lib/Sema/TypeCheckEffects.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ class PotentialThrowReason {
309309
/// The function is 'rethrows', and it was passed a default
310310
/// argument that was not rethrowing-only in this context.
311311
CallRethrowsWithDefaultThrowingArgument,
312+
313+
/// The the function is 'rethrows', and it is a member that
314+
/// is a conformance to a rethrowing protocol.
315+
CallRethrowsWithConformance,
312316
};
313317

314318
static StringRef kindToString(Kind k) {
@@ -320,6 +324,8 @@ class PotentialThrowReason {
320324
return "CallRethrowsWithExplicitThrowingArgument";
321325
case Kind::CallRethrowsWithDefaultThrowingArgument:
322326
return "CallRethrowsWithDefaultThrowingArgument";
327+
case Kind::CallRethrowsWithConformance:
328+
return "CallRethrowsWithConformance";
323329
}
324330
}
325331

@@ -337,6 +343,11 @@ class PotentialThrowReason {
337343
static PotentialThrowReason forDefaultArgument() {
338344
return PotentialThrowReason(Kind::CallRethrowsWithDefaultThrowingArgument);
339345
}
346+
static PotentialThrowReason forRethrowsConformance(Expr *E) {
347+
PotentialThrowReason result(Kind::CallRethrowsWithConformance);
348+
result.TheExpression = E;
349+
return result;
350+
}
340351
static PotentialThrowReason forThrowingApply() {
341352
return PotentialThrowReason(Kind::CallThrows);
342353
}
@@ -353,7 +364,8 @@ class PotentialThrowReason {
353364
bool isThrow() const { return getKind() == Kind::Throw; }
354365
bool isRethrowsCall() const {
355366
return (getKind() == Kind::CallRethrowsWithExplicitThrowingArgument ||
356-
getKind() == Kind::CallRethrowsWithDefaultThrowingArgument);
367+
getKind() == Kind::CallRethrowsWithDefaultThrowingArgument ||
368+
getKind() == Kind::CallRethrowsWithConformance);
357369
}
358370

359371
/// If this was built with forRethrowsArgument, return the expression.
@@ -576,7 +588,7 @@ class ApplyClassifier {
576588
auto substitutions = fnRef.getDeclRef().getSubstitutions();
577589
if (classifyWitnessAsThrows(fnRef.getModuleContext(), substitutions)) {
578590
return Classification::forRethrowingOnly(
579-
PotentialThrowReason::forThrowingApply(), isAsync);
591+
PotentialThrowReason::forRethrowsConformance(E), isAsync);
580592
}
581593
} else if (fnRef.isBodyRethrows() &&
582594
fnRef.getRethrowingKind() == FunctionRethrowingKind::Throws) {
@@ -1256,6 +1268,9 @@ class Context {
12561268
case PotentialThrowReason::Kind::CallRethrowsWithDefaultThrowingArgument:
12571269
Diags.diagnose(loc, diag::because_rethrows_default_argument_throws);
12581270
return;
1271+
case PotentialThrowReason::Kind::CallRethrowsWithConformance:
1272+
Diags.diagnose(loc, diag::because_rethrows_default_conformance_throws);
1273+
return;
12591274
}
12601275
llvm_unreachable("bad reason kind");
12611276
}

stdlib/public/Concurrency/AsyncCompactMapSequence.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ public struct AsyncCompactMapSequence<Upstream, ElementOfResult>: AsyncSequence
2727
public typealias AsyncIterator = Iterator
2828

2929
public struct Iterator: AsyncIteratorProtocol {
30-
var upstreamIterator: Upstream.AsyncIterator?
30+
var upstreamIterator: _OptionalAsyncIterator<Upstream.AsyncIterator>
3131
let transform: (Upstream.Element) async -> ElementOfResult?
3232

3333
init(_ upstreamIterator: Upstream.AsyncIterator, transform: @escaping (Upstream.Element) async -> ElementOfResult?) {
34-
self.upstreamIterator = upstreamIterator
34+
self.upstreamIterator = _OptionalAsyncIterator(upstreamIterator)
3535
self.transform = transform
3636
}
3737

3838
public mutating func next() async rethrows -> ElementOfResult? {
3939
while true {
40-
guard let item = try await upstreamIterator?.next() else {
40+
guard let item = try await upstreamIterator.next() else {
4141
return nil
4242
}
4343
if let transformed = await transform(item) {
@@ -47,8 +47,7 @@ public struct AsyncCompactMapSequence<Upstream, ElementOfResult>: AsyncSequence
4747
}
4848

4949
public mutating func cancel() {
50-
upstreamIterator?.cancel()
51-
upstreamIterator = nil
50+
upstreamIterator.cancel()
5251
}
5352
}
5453

@@ -70,34 +69,32 @@ public struct AsyncTryCompactMapSequence<Upstream, ElementOfResult>: AsyncSequen
7069
public typealias AsyncIterator = Iterator
7170

7271
public struct Iterator: AsyncIteratorProtocol {
73-
var upstreamIterator: Upstream.AsyncIterator?
72+
var upstreamIterator: _OptionalAsyncIterator<Upstream.AsyncIterator>
7473
let transform: (Upstream.Element) async throws -> ElementOfResult?
7574

7675
init(_ upstreamIterator: Upstream.AsyncIterator, transform: @escaping (Upstream.Element) async throws -> ElementOfResult?) {
77-
self.upstreamIterator = upstreamIterator
76+
self.upstreamIterator = _OptionalAsyncIterator(upstreamIterator)
7877
self.transform = transform
7978
}
8079

8180
public mutating func next() async throws -> ElementOfResult? {
8281
while true {
83-
guard let item = try await upstreamIterator?.next() else {
82+
guard let item = try await upstreamIterator.next() else {
8483
return nil
8584
}
8685
do {
8786
if let transformed = try await transform(item) {
8887
return transformed
8988
}
9089
} catch {
91-
upstreamIterator?.cancel()
92-
upstreamIterator = nil
90+
upstreamIterator.cancel()
9391
throw error
9492
}
9593
}
9694
}
9795

9896
public mutating func cancel() {
99-
upstreamIterator?.cancel()
100-
upstreamIterator = nil
97+
upstreamIterator.cancel()
10198
}
10299
}
103100

stdlib/public/Concurrency/AsyncConcatSequence.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,25 @@ public struct AsyncConcatSequence<Prefix, Suffix>: AsyncSequence where Prefix: A
2727
public typealias AsyncIterator = Iterator
2828

2929
public struct Iterator: AsyncIteratorProtocol {
30-
var prefixIterator: Prefix.AsyncIterator?
31-
var suffixIterator: Suffix.AsyncIterator?
30+
var prefixIterator: _OptionalAsyncIterator<Prefix.AsyncIterator>
31+
var suffixIterator: _OptionalAsyncIterator<Suffix.AsyncIterator>
3232

3333
init(_ prefixIterator: Prefix.AsyncIterator, _ suffixIterator: Suffix.AsyncIterator) {
34-
self.prefixIterator = prefixIterator
35-
self.suffixIterator = suffixIterator
34+
self.prefixIterator = _OptionalAsyncIterator(prefixIterator)
35+
self.suffixIterator = _OptionalAsyncIterator(suffixIterator)
3636
}
3737

3838
public mutating func next() async rethrows -> Prefix.Element? {
39-
if let item = try await prefixIterator?.next() {
39+
if let item = try await prefixIterator.next() {
4040
return item
4141
}
42-
prefixIterator = nil
43-
return try await suffixIterator?.next()
42+
prefixIterator = .none
43+
return try await suffixIterator.next()
4444
}
4545

4646
public mutating func cancel() {
47-
prefixIterator?.cancel()
48-
prefixIterator = nil
49-
suffixIterator?.cancel()
50-
suffixIterator = nil
47+
prefixIterator.cancel()
48+
suffixIterator.cancel()
5149
}
5250
}
5351

stdlib/public/Concurrency/AsyncDropWhileSequence.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ public struct AsyncDropWhileSequence<Upstream>: AsyncSequence where Upstream: As
2727
public typealias AsyncIterator = Iterator
2828

2929
public struct Iterator: AsyncIteratorProtocol {
30-
var upstreamIterator: Upstream.AsyncIterator?
30+
var upstreamIterator: _OptionalAsyncIterator<Upstream.AsyncIterator>
3131
var predicate: ((Element) async -> Bool)?
3232

3333
init(_ upstreamIterator: Upstream.AsyncIterator, predicate: @escaping (Element) async -> Bool) {
34-
self.upstreamIterator = upstreamIterator
34+
self.upstreamIterator = _OptionalAsyncIterator(upstreamIterator)
3535
self.predicate = predicate
3636
}
3737

3838
public mutating func next() async rethrows -> Upstream.Element? {
3939
while true {
40-
guard let item = try await upstreamIterator?.next() else {
40+
guard let item = try await upstreamIterator.next() else {
4141
return nil
4242
}
4343
if let predicate = self.predicate {
@@ -52,8 +52,7 @@ public struct AsyncDropWhileSequence<Upstream>: AsyncSequence where Upstream: As
5252
}
5353

5454
public mutating func cancel() {
55-
upstreamIterator?.cancel()
56-
upstreamIterator = nil
55+
upstreamIterator.cancel()
5756
}
5857
}
5958

@@ -75,17 +74,17 @@ public struct AsyncTryDropWhileSequence<Upstream>: AsyncSequence where Upstream:
7574
public typealias AsyncIterator = Iterator
7675

7776
public struct Iterator: AsyncIteratorProtocol {
78-
var upstreamIterator: Upstream.AsyncIterator?
77+
var upstreamIterator: _OptionalAsyncIterator<Upstream.AsyncIterator>
7978
var predicate: ((Element) async throws -> Bool)?
8079

8180
init(_ upstreamIterator: Upstream.AsyncIterator, predicate: @escaping (Element) async throws -> Bool) {
82-
self.upstreamIterator = upstreamIterator
81+
self.upstreamIterator = _OptionalAsyncIterator(upstreamIterator)
8382
self.predicate = predicate
8483
}
8584

8685
public mutating func next() async throws -> Upstream.Element? {
8786
while true {
88-
guard let item = try await upstreamIterator?.next() else {
87+
guard let item = try await upstreamIterator.next() else {
8988
return nil
9089
}
9190
if let predicate = self.predicate {
@@ -95,8 +94,7 @@ public struct AsyncTryDropWhileSequence<Upstream>: AsyncSequence where Upstream:
9594
return item
9695
}
9796
} catch {
98-
upstreamIterator?.cancel()
99-
upstreamIterator = nil
97+
upstreamIterator.cancel()
10098
throw error
10199
}
102100
} else {
@@ -106,8 +104,7 @@ public struct AsyncTryDropWhileSequence<Upstream>: AsyncSequence where Upstream:
106104
}
107105

108106
public mutating func cancel() {
109-
upstreamIterator?.cancel()
110-
upstreamIterator = nil
107+
upstreamIterator.cancel()
111108
}
112109
}
113110

stdlib/public/Concurrency/AsyncFilterSequence.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,29 @@ public struct AsyncFilterSequence<Upstream>: AsyncSequence where Upstream: Async
3030
public let predicate: (Element) async -> Bool
3131

3232
public struct Iterator: AsyncIteratorProtocol {
33-
var upstreamIterator: Upstream.AsyncIterator?
33+
var upstreamIterator: _OptionalAsyncIterator<Upstream.AsyncIterator>
3434
let predicate: (Element) async -> Bool
3535

3636
init(_ upstreamIterator: Upstream.AsyncIterator,
3737
predicate: @escaping (Element) async -> Bool
3838
) {
39-
self.upstreamIterator = upstreamIterator
39+
self.upstreamIterator = _OptionalAsyncIterator(upstreamIterator)
4040
self.predicate = predicate
4141
}
4242

4343
public mutating func next() async rethrows -> Upstream.Element? {
44-
guard let item = try await upstreamIterator?.next() else {
44+
guard let item = try await upstreamIterator.next() else {
4545
return nil
4646
}
4747
guard await predicate(item) else {
48-
upstreamIterator?.cancel()
49-
upstreamIterator = nil
50-
return nil
48+
upstreamIterator.cancel()
49+
return nil
5150
}
5251
return item
5352
}
5453

5554
public mutating func cancel() {
56-
upstreamIterator?.cancel()
57-
upstreamIterator = nil
55+
upstreamIterator.cancel()
5856
}
5957
}
6058

@@ -76,31 +74,29 @@ public struct AsyncTryFilterSequence<Upstream>: AsyncSequence where Upstream: As
7674
public let predicate: (Element) async throws -> Bool
7775

7876
public struct Iterator: AsyncIteratorProtocol {
79-
var upstreamIterator: Upstream.AsyncIterator?
77+
var upstreamIterator: _OptionalAsyncIterator<Upstream.AsyncIterator>
8078
let predicate: (Element) async throws -> Bool
8179

8280
init(_ upstreamIterator: Upstream.AsyncIterator,
8381
predicate: @escaping (Element) async throws -> Bool
8482
) {
85-
self.upstreamIterator = upstreamIterator
83+
self.upstreamIterator = _OptionalAsyncIterator(upstreamIterator)
8684
self.predicate = predicate
8785
}
8886

8987
public mutating func next() async throws -> Upstream.Element? {
90-
guard let item = try await upstreamIterator?.next() else {
88+
guard let item = try await upstreamIterator.next() else {
9189
return nil
9290
}
9391
guard try await predicate(item) else {
94-
upstreamIterator?.cancel()
95-
upstreamIterator = nil
96-
return nil
92+
upstreamIterator.cancel()
93+
return nil
9794
}
9895
return item
9996
}
10097

10198
public mutating func cancel() {
102-
upstreamIterator?.cancel()
103-
upstreamIterator = nil
99+
upstreamIterator.cancel()
104100
}
105101
}
106102

0 commit comments

Comments
 (0)