Skip to content

Commit 4575525

Browse files
committed
NFC: Cleanup ClosureScope/AccessEnforcementSelection/Tests.
Per Devin and John's review.
1 parent 19b2c3f commit 4575525

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

include/swift/SILOptimizer/Analysis/ClosureScope.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class ClosureScopeAnalysis : public SILAnalysis {
9595
IndexLookupFunc(const std::vector<SILFunction *> &indexedScopes)
9696
: indexedScopes(indexedScopes) {}
9797

98-
Optional<SILFunction *> operator()(int &idx) const {
98+
Optional<SILFunction *> operator()(int idx) const {
9999
if (auto funcPtr = indexedScopes[idx]) {
100100
return funcPtr;
101101
}

lib/SILOptimizer/Mandatory/AccessEnforcementSelection.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ void SelectEnforcement::updateCapture(AddressCapture capture) {
466466
namespace {
467467

468468
// Model the kind of access needed based on analyzing the access's source.
469-
// This is either determined to be static or dynamic, or requries further
469+
// This is either determined to be static or dynamic, or requires further
470470
// analysis of a boxed variable.
471471
struct SourceAccess {
472472
enum { StaticAccess, DynamicAccess, BoxAccess } kind;
@@ -534,8 +534,10 @@ void AccessEnforcementSelection::processFunction(SILFunction *F) {
534534

535535
if (auto access = dyn_cast<BeginAccessInst>(inst))
536536
handleAccess(access);
537+
537538
else if (auto access = dyn_cast<BeginUnpairedAccessInst>(inst))
538539
assert(access->getEnforcement() == SILAccessEnforcement::Dynamic);
540+
539541
else if(auto pa = dyn_cast<PartialApplyInst>(inst))
540542
handlePartialApply(pa);
541543
}

test/SILOptimizer/access_enforcement_noescape.swift

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
// (Some static/dynamic enforcement selection is done in SILGen, and some is
77
// deferred. That may change over time but we want the outcome to be the same).
88
//
9-
// Each FIXME line is a case that the current implementation misses.
10-
// The model is currently being refined, so this isn't set in stone.
11-
//
12-
// TODO: Ensure that each dynamic case is covered by
13-
// Interpreter/enforce_exclusive_access.swift.
9+
// These tests attempt to fully cover the possibilities of reads and
10+
// modifications to captures along with `inout` arguments on both the caller and
11+
// callee side.
1412

1513
// Helper
1614
func doOne(_ f: () -> ()) {
@@ -30,10 +28,8 @@ func reentrantNoescape(fn: (() -> ()) -> ()) {
3028
}
3129

3230
// Error: Cannot capture nonescaping closure.
33-
// Verification disabled because it suppresses all the other errors.
34-
// disabled-note@+1{{parameter 'fn' is implicitly non-escaping}}
31+
// This triggers an early diagnostics, so it's handled in inout_capture_disgnostics.swift.
3532
// func reentrantCapturedNoescape(fn: (() -> ()) -> ()) {
36-
// disabled-error@+1{{closure use of non-escaping parameter 'fn' may allow it to escape}}
3733
// let c = { fn {} }
3834
// fn(c)
3935
// }
@@ -314,8 +310,8 @@ func inoutReadWriteInout(x: inout Int) {
314310
// CHECK: end_access [[ACCESS]]
315311
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape19inoutReadWriteInoutySiz1x_tFyycfU_'
316312

317-
// Trap on boxed read + write inout.
318-
// FIXME: Passing a captured var as inout needs dynamic enforcement.
313+
// Traps on boxed read + write inout.
314+
// Covered by Interpreter/enforce_exclusive_access.swift.
319315
func readBoxWriteInout() {
320316
var x = 3
321317
let c = { _ = x }
@@ -339,12 +335,11 @@ func readBoxWriteInout() {
339335
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape17readBoxWriteInoutyyFyycfU_'
340336

341337
// Error: inout cannot be captured.
342-
// Verification disabled because is suppresses other errors.
343-
//func inoutReadBoxWriteInout(x: inout Int) {
344-
// disabled-error@+1{{escaping closures can only capture inout parameters explicitly by value}}
345-
// let c = { _ = x }
346-
// doOneInout(c, &x)
347-
//}
338+
// This triggers an early diagnostics, so it's handled in inout_capture_disgnostics.swift.
339+
// func inoutReadBoxWriteInout(x: inout Int) {
340+
// let c = { _ = x }
341+
// doOneInout(c, &x)
342+
// }
348343

349344
// Allow aliased noescape write + write.
350345
func writeWrite() {
@@ -401,9 +396,8 @@ func inoutWriteWrite(x: inout Int) {
401396
// CHECK: end_access [[ACCESS]]
402397
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape010inoutWriteE0ySiz1x_tFyycfU0_'
403398

404-
// FIXME: Trap on aliased boxed write + noescape write.
405-
//
406-
// See the note above.
399+
// Traps on aliased boxed write + noescape write.
400+
// Covered by Interpreter/enforce_exclusive_access.swift.
407401
func writeWriteBox() {
408402
var x = 3
409403
let c = { x = 87 }
@@ -485,8 +479,8 @@ func inoutWriteWriteInout(x: inout Int) {
485479
// CHECK: end_access [[ACCESS]]
486480
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape010inoutWriteE5InoutySiz1x_tFyycfU_'
487481

488-
// Trap on boxed write + write inout.
489-
// FIXME: Passing a captured var as inout needs dynamic enforcement.
482+
// Traps on boxed write + write inout.
483+
// Covered by Interpreter/enforce_exclusive_access.swift.
490484
func writeBoxWriteInout() {
491485
var x = 3
492486
let c = { x = 42 }
@@ -510,9 +504,8 @@ func writeBoxWriteInout() {
510504
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape18writeBoxWriteInoutyyFyycfU_'
511505

512506
// Error: Cannot capture inout
513-
// Verification disabled because it suppresses other errors.
507+
// This triggers an early diagnostics, so it's handled in inout_capture_disgnostics.swift.
514508
// func inoutWriteBoxWriteInout(x: inout Int) {
515-
// disabled-error@+1{{escaping closures can only capture inout parameters explicitly by value}}
516509
// let c = { x = 42 }
517510
// doOneInout(c, &x)
518511
// }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %target-swift-frontend -enforce-exclusivity=checked -Onone -emit-sil -swift-version 4 -verify -parse-as-library %s
2+
//
3+
// This is an adjunct to access_enforcement_noescape.swift to cover early static diagnostics.
4+
5+
// Helper
6+
func doOneInout(_: ()->(), _: inout Int) {}
7+
8+
// Error: Cannot capture nonescaping closure.
9+
// expected-note@+1{{parameter 'fn' is implicitly non-escaping}}
10+
func reentrantCapturedNoescape(fn: (() -> ()) -> ()) {
11+
// expected-error@+1{{closure use of non-escaping parameter 'fn' may allow it to escape}}
12+
let c = { fn {} }
13+
fn(c)
14+
}
15+
16+
// Error: inout cannot be captured.
17+
func inoutReadBoxWriteInout(x: inout Int) {
18+
// expected-error@+1{{escaping closures can only capture inout parameters explicitly by value}}
19+
let c = { _ = x }
20+
doOneInout(c, &x)
21+
}
22+
23+
// Error: Cannot capture inout
24+
func inoutWriteBoxWriteInout(x: inout Int) {
25+
// expected-error@+1{{escaping closures can only capture inout parameters explicitly by value}}
26+
let c = { x = 42 }
27+
doOneInout(c, &x)
28+
}

0 commit comments

Comments
 (0)