Skip to content

Commit d80854e

Browse files
Merge pull request #72256 from nate-chandler/gh70234
[ConsumeChecker] Check guaranteed arguments.
2 parents df94b58 + 863c919 commit d80854e

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

lib/SILOptimizer/Mandatory/ConsumeOperatorCopyableValuesChecker.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,9 @@ bool ConsumeOperatorCopyableValuesChecker::check() {
400400
llvm::SmallSetVector<SILValue, 32> valuesToCheck;
401401

402402
for (auto *arg : fn->getEntryBlock()->getSILFunctionArguments()) {
403-
if (arg->getOwnershipKind() == OwnershipKind::Owned &&
403+
auto ownership = arg->getOwnershipKind();
404+
if ((ownership == OwnershipKind::Owned ||
405+
ownership == OwnershipKind::Guaranteed) &&
404406
!arg->getType().isMoveOnly()) {
405407
LLVM_DEBUG(llvm::dbgs() << "Found owned arg to check: " << *arg);
406408
valuesToCheck.insert(arg);

test/SILOptimizer/consume_operator_kills_copyable_loadable_vars.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,12 @@ func consumeInitdArray() {
723723
_ = consume x
724724
}
725725

726+
func isNegative(_ c: consuming Int) -> Bool { return c < 0 }
727+
func consumeInt() {
728+
var g = 0 // expected-warning{{variable 'g' was never mutated; consider changing to 'let' constant}}
729+
isNegative(consume g) // expected-warning{{result of call to 'isNegative' is unused}}
730+
}
731+
726732
//////////////////////
727733
// Reinit in pieces //
728734
//////////////////////

test/SILOptimizer/consume_operator_kills_copyable_values.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,31 @@ func g()
371371
f(x: consume x)
372372
}
373373

374+
func consumeArrayAny() {
375+
let a: [Any] = []
376+
_ = consume a
377+
}
378+
379+
func consumeConsuming(_ k: consuming Klass) {
380+
_ = consume k
381+
}
382+
383+
func consumeBorrowing(_ k: borrowing Klass) { // expected-error{{'k' is borrowed and cannot be consumed}}
384+
_ = consume k // expected-note{{consumed here}}
385+
}
386+
387+
func consumeOwned(_ k: __owned Klass) {
388+
_ = consume k
389+
}
390+
391+
func consumeShared(_ k: __shared Klass) {
392+
_ = consume k
393+
}
394+
395+
func consumeBare(_ k: Klass) {
396+
_ = consume k
397+
}
398+
374399
/////////////////////////
375400
// Partial Apply Tests //
376401
/////////////////////////
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-swift-frontend -emit-sil -verify %s
2+
3+
// REQUIRES: objc_interop
4+
5+
import Foundation
6+
7+
func mutateText(_ rawText: NSString?) -> String {
8+
guard let text = consume rawText else {
9+
return "text unavailable"
10+
}
11+
return String(text)
12+
}

0 commit comments

Comments
 (0)