|
| 1 | +// RUN: %target-swift-frontend -emit-sil -disable-objc-attr-requires-foundation-module -verify %s |
| 2 | + |
| 3 | +// High-level tests that DI rejects passing let constants to |
| 4 | +// mutating witness methods |
| 5 | + |
| 6 | + |
| 7 | +// Mark: General Definitions |
| 8 | + |
| 9 | +protocol TestProtocol { |
| 10 | + var foo: Int { get set } |
| 11 | +} |
| 12 | + |
| 13 | +struct TestStruct: TestProtocol { |
| 14 | + var foo: Int |
| 15 | +} |
| 16 | + |
| 17 | +// Mark: - Case1: Illegaly mutating let property of class in initializer |
| 18 | + |
| 19 | +class TestClass { |
| 20 | + let testObject: TestProtocol // expected-note {{change 'let' to 'var' to make it mutable}} |
| 21 | + init() { |
| 22 | + testObject = TestStruct(foo: 42) |
| 23 | + testObject.foo = 666 // expected-error {{cannot perform mutating operation: 'self.testObject' is a 'let' constant}} |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// Mark: - Case2: Illegaly mutating global let constant |
| 28 | + |
| 29 | +let testObject: TestProtocol // expected-note {{change 'let' to 'var' to make it mutable}} |
| 30 | +testObject = TestStruct(foo: 42) |
| 31 | + |
| 32 | +testObject.foo = 666 // expected-error {{cannot perform mutating operation: 'testObject' is a 'let' constant}} |
| 33 | + |
| 34 | +extension TestProtocol { |
| 35 | + mutating func messThingsUp() { |
| 36 | + foo = 666 |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Mark: - Case3: Illegaly muatating let constant in a function scope |
| 41 | + |
| 42 | +let testObject2: TestProtocol // expected-note {{change 'let' to 'var' to make it mutable}} |
| 43 | +testObject2 = TestStruct(foo: 42) |
| 44 | +testObject2.messThingsUp() // expected-error {{cannot perform mutating operation: 'testObject2' is a 'let' constant}} |
| 45 | + |
| 46 | +func testFunc() { |
| 47 | + let testObject: TestProtocol // expected-note {{change 'let' to 'var' to make it mutable}} |
| 48 | + |
| 49 | + testObject = TestStruct(foo: 42) |
| 50 | + testObject.foo = 666 // expected-error {{cannot perform mutating operation: 'testObject' is a 'let' constant}} |
| 51 | +} |
| 52 | + |
| 53 | +// Mark: - Case4: Illegaly passing a let constants property as an inout parameter |
| 54 | + |
| 55 | +let testObject3: TestProtocol // expected-note {{change 'let' to 'var' to make it mutable}} |
| 56 | +testObject3 = TestStruct(foo: 42) |
| 57 | + |
| 58 | +func mutateThis(mutatee: inout Int) { |
| 59 | + mutatee = 666 |
| 60 | +} |
| 61 | +mutateThis(mutatee: &testObject3.foo) // expected-error {{cannot perform mutating operation: 'testObject3' is a 'let' constant}} |
0 commit comments