Skip to content

Commit 1383130

Browse files
Merge pull request #83473 from jamieQ/fix-use-after-consume-diag
[SILOptimizer]: fix some missing use after consume diagnostics
2 parents 1439408 + 754a300 commit 1383130

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

lib/SILOptimizer/Mandatory/ConsumeOperatorCopyableValuesChecker.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ bool CheckerLivenessInfo::compute() {
157157
return false;
158158
}
159159
}
160+
} else if (isa<StoreBorrowInst>(user)) {
161+
if (liveness->updateForBorrowingOperand(use) !=
162+
InnerBorrowKind::Contained) {
163+
return false;
164+
}
160165
}
161166
// FIXME: this ignores all other forms of Borrow ownership, such as
162167
// partial_apply [onstack] and mark_dependence [nonescaping].

test/SILOptimizer/consume_operator_kills_copyable_values.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ public class Klass {}
66
public class SubKlass1 : Klass {}
77
public class SubKlass2 : Klass {}
88

9+
struct A {
10+
var prop = ""
11+
}
12+
913
//////////////////
1014
// Declarations //
1115
//////////////////
@@ -14,6 +18,14 @@ func consumingUse(_ k: __owned Klass) {}
1418
var booleanValue: Bool { false }
1519
func nonConsumingUse(_ k: Klass) {}
1620

21+
func consumeValue<V>(_ v: consuming V) {}
22+
func borrowValue<V>(_ v: borrowing V) {}
23+
func useValue<V>(_ v: V) {}
24+
25+
func consumeA(_ a: consuming A) {}
26+
func borrowA(_ a: borrowing A) {}
27+
func useA(_ a: A) {}
28+
1729
///////////
1830
// Tests //
1931
///////////
@@ -424,3 +436,37 @@ public func deferTest(_ x: __owned Klass) { // expected-error {{'x' used after c
424436
}
425437
print("do Something")
426438
}
439+
440+
441+
/////////////////////////
442+
// Local Binding Tests //
443+
/////////////////////////
444+
445+
func testLocalBindingUseAfterConsumeGenerics() {
446+
let a = A() // expected-error {{'a' used after consume}}
447+
_ = consume a // expected-note {{consumed here}}
448+
consumeValue(a) // expected-note {{used here}}
449+
borrowValue(a) // expected-note {{used here}}
450+
useValue(a) // expected-note {{used here}}
451+
}
452+
453+
func testLocalBindingUseAfterConsumeNoGenericFuncs() {
454+
let a = A() // expected-error {{'a' used after consume}}
455+
_ = consume a // expected-note {{consumed here}}
456+
457+
consumeA(a) // expected-note {{used here}}
458+
borrowA(a) // expected-note {{used here}}
459+
useA(a) // expected-note {{used here}}
460+
}
461+
462+
// https://github.com/swiftlang/swift/issues/83277
463+
func test_83277() {
464+
let values = [1, 2, 3] // expected-error {{'values' used after consume}}
465+
var newValues = consume values // expected-note {{consumed here}}
466+
newValues.append(4)
467+
468+
_ = values.count // expected-note {{used here}}
469+
_ = values[0] // expected-note {{used here}}
470+
_ = values.first // expected-note {{used here}}
471+
_ = values.last // expected-note {{used here}}
472+
}

0 commit comments

Comments
 (0)