Files
swift-mirror/test/stdlib/move_function.swift
Michael Gottesman f3081d0e9a [move-keyword] Put the move keyword behind the experimental move only flag for now.
Originally move when it was in the stdlib as _move was behind a keyword but we
moved it in front to allow for some testing. Now that we are going with a
keyword (which we can't leave in/deprecate) move it behind the move only
experimental flag until this gets through evolution.
2022-08-10 12:45:17 -07:00

117 lines
2.6 KiB
Swift

// RUN: %target-run-stdlib-swift(-Xllvm -sil-disable-pass=DestroyHoisting -Xfrontend -enable-experimental-move-only)
//
// NOTE ON ABOVE: I am disabling destroy hoisting on this test since we are
// going to move it out of the mandatory pipeline eventually and it causes the
// test to fail only when optimizations are enabled.
// REQUIRES: executable_test
import Swift
import StdlibUnittest
public class Klass {}
var tests = TestSuite("move_function_uniqueness")
defer {
runAllTests()
}
public enum Enum {
case foo
}
public class K2 {
var array: [Enum] = Array(repeating: .foo, count: 10_000)
subscript(index: Int) -> Enum {
@inline(never)
get {
array[index]
}
@inline(never)
set {
array[index] = newValue
}
@inline(never)
_modify {
yield &array[index]
}
}
}
public class Class {
var k2 = K2()
var array: [Enum] = Array(repeating: .foo, count: 10_000)
}
extension Class {
@inline(never)
func readClassSwitchLetTest(_ userHandle: Int) {
expectTrue(_isUnique(&self.k2))
let x: K2
do {
x = self.k2
}
switch (_move x)[userHandle] {
case .foo:
expectTrue(_isUnique(&self.k2))
}
}
}
tests.test("readClassSwitchLetTest") {
let c = Class()
for f in 0..<10_000 {
c.readClassSwitchLetTest(f)
}
}
extension Class {
@inline(never)
func readArraySwitchLetTest(_ userHandle: Int) {
expectTrue(self.array._buffer.isUniquelyReferenced())
let x: [Enum]
do {
x = self.array
}
switch (_move x)[userHandle] {
case .foo:
expectTrue(self.array._buffer.isUniquelyReferenced())
}
}
}
tests.test("readArraySwitchLetTest") {
let c = Class()
for f in 0..<10_000 {
c.readArraySwitchLetTest(f)
}
}
tests.test("simpleArrayVarTest") {
var x: [Enum] = Array(repeating: .foo, count: 10_000)
expectTrue(x._buffer.isUniquelyReferenced())
var y = x
expectFalse(x._buffer.isUniquelyReferenced())
let _ = _move y
expectTrue(x._buffer.isUniquelyReferenced())
y = []
expectTrue(x._buffer.isUniquelyReferenced())
}
tests.test("simpleArrayInoutVarTest") {
func inOutTest(_ x: inout [Enum]) {
var y = x
expectFalse(x._buffer.isUniquelyReferenced())
let _ = _move y
expectTrue(x._buffer.isUniquelyReferenced())
y = []
expectTrue(x._buffer.isUniquelyReferenced())
}
var outerX: [Enum] = Array(repeating: .foo, count: 10_000)
inOutTest(&outerX)
}