|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +// This test file has been translated from swift/test/Parse/copy_expr.swift |
| 14 | + |
| 15 | +import XCTest |
| 16 | + |
| 17 | +final class CopyExprTests: XCTestCase { |
| 18 | + func testGlobal() { |
| 19 | + assertParse( |
| 20 | + """ |
| 21 | + var global: Int = 5 |
| 22 | + func testGlobal() { |
| 23 | + let _ = copy global |
| 24 | + } |
| 25 | + """ |
| 26 | + ) |
| 27 | + } |
| 28 | + |
| 29 | + func testLet() { |
| 30 | + assertParse( |
| 31 | + """ |
| 32 | + func testLet() { |
| 33 | + let t = String() |
| 34 | + let _ = copy t |
| 35 | + } |
| 36 | + """ |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + func testVar() { |
| 41 | + assertParse( |
| 42 | + """ |
| 43 | + func testVar() { |
| 44 | + var t = String() |
| 45 | + t = String() |
| 46 | + let _ = copy t |
| 47 | + } |
| 48 | + """ |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + func testStillAbleToCallFunctionCalledCopy() { |
| 53 | + assertParse( |
| 54 | + """ |
| 55 | + func copy() {} |
| 56 | + func copy(_: String) {} |
| 57 | + func copy(_: String, _: Int) {} |
| 58 | + func copy(x: String, y: Int) {} |
| 59 | +
|
| 60 | + func useCopyFunc() { |
| 61 | + var s = String() |
| 62 | + var i = global |
| 63 | +
|
| 64 | + copy() |
| 65 | + copy(s) |
| 66 | + copy(i) // expected-error{{cannot convert value of type 'Int' to expected argument type 'String'}} |
| 67 | + copy(s, i) |
| 68 | + copy(i, s) // expected-error{{unnamed argument #2 must precede unnamed argument #1}} |
| 69 | + copy(x: s, y: i) |
| 70 | + copy(y: i, x: s) // expected-error{{argument 'x' must precede argument 'y'}} |
| 71 | + } |
| 72 | + """ |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + // Ensure we can still parse a variable named copy. |
| 77 | + func testUseCopyVariable() { |
| 78 | + assertParse( |
| 79 | + """ |
| 80 | + func useCopyVar(copy: inout String) { |
| 81 | + let s = copy |
| 82 | + copy = s |
| 83 | +
|
| 84 | + // We can copy from a variable named `copy` |
| 85 | + let t = copy copy |
| 86 | + copy = t |
| 87 | +
|
| 88 | + // We can do member access and subscript a variable named `copy` |
| 89 | + let i = copy.startIndex |
| 90 | + let _ = copy[i] |
| 91 | + } |
| 92 | + """ |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | + func testPropertyWrapperWithCopy() { |
| 97 | + assertParse( |
| 98 | + """ |
| 99 | + @propertyWrapper |
| 100 | + struct FooWrapper<T> { |
| 101 | + var value: T |
| 102 | +
|
| 103 | + init(wrappedValue: T) { value = wrappedValue } |
| 104 | +
|
| 105 | + var wrappedValue: T { |
| 106 | + get { value } |
| 107 | + nonmutating set {} |
| 108 | + } |
| 109 | + var projectedValue: T { |
| 110 | + get { value } |
| 111 | + nonmutating set {} |
| 112 | + } |
| 113 | + } |
| 114 | +
|
| 115 | + struct Foo { |
| 116 | + @FooWrapper var wrapperTest: String |
| 117 | +
|
| 118 | + func copySelf() { |
| 119 | + _ = copy self |
| 120 | + } |
| 121 | +
|
| 122 | + func copyPropertyWrapper() { |
| 123 | + // should still parse, even if it doesn't semantically work out |
| 124 | + _ = copy wrapperTest // expected-error{{can only be applied to lvalues}} |
| 125 | + _ = copy _wrapperTest // expected-error{{can only be applied to lvalues}} |
| 126 | + _ = copy $wrapperTest // expected-error{{can only be applied to lvalues}} |
| 127 | + } |
| 128 | + } |
| 129 | + """ |
| 130 | + ) |
| 131 | + } |
| 132 | +} |
0 commit comments