Skip to content

Commit 1e2ef1b

Browse files
committed
[SE-0258] Ban get/set in properties with attached delegates.
1 parent db30c77 commit 1e2ef1b

File tree

4 files changed

+17
-40
lines changed

4 files changed

+17
-40
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4379,6 +4379,9 @@ ERROR(property_delegate_local,none,
43794379
ERROR(property_delegate_let, none,
43804380
"property delegate can only be applied to a 'var'",
43814381
())
4382+
ERROR(property_delegate_computed, none,
4383+
"property delegate cannot be applied to a computed property",
4384+
())
43824385

43834386
ERROR(property_with_delegate_conflict_attribute,none,
43844387
"property %0 with a delegate cannot also be "

lib/Sema/TypeCheckPropertyDelegate.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,12 @@ AttachedPropertyDelegateRequest::evaluate(Evaluator &evaluator,
343343
}
344344
}
345345

346+
// Properties with delegates must not declare a getter or setter.
347+
if (!var->hasStorage()) {
348+
ctx.Diags.diagnose(attr->getLocation(), diag::property_delegate_computed);
349+
return nullptr;
350+
}
351+
346352
return mutableAttr;
347353
}
348354

test/SILGen/property_delegates.swift

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,12 @@ struct HasDefaultInit {
142142

143143
struct DelegateWithAccessors {
144144
@Wrapper
145-
var x: Int {
146-
// CHECK-LABEL: sil hidden [ossa] @$s18property_delegates21DelegateWithAccessorsV1xSivg
147-
// CHECK-NOT: return
148-
// CHECK: integer_literal $Builtin.IntLiteral, 42
149-
return 42
150-
151-
// Synthesized setter
152-
// CHECK-LABEL: sil hidden [transparent] [ossa] @$s18property_delegates21DelegateWithAccessorsV1xSivs : $@convention(method) (Int, @inout DelegateWithAccessors) -> ()
153-
// CHECK-NOT: return
154-
// CHECK: struct_element_addr {{%.*}} : $*DelegateWithAccessors, #DelegateWithAccessors.$x
155-
}
145+
var x: Int
146+
147+
// Synthesized setter
148+
// CHECK-LABEL: sil hidden [transparent] [ossa] @$s18property_delegates21DelegateWithAccessorsV1xSivs : $@convention(method) (Int, @inout DelegateWithAccessors) -> ()
149+
// CHECK-NOT: return
150+
// CHECK: struct_element_addr {{%.*}} : $*DelegateWithAccessors, #DelegateWithAccessors.$x
156151

157152
mutating func test() {
158153
x = 17

test/decl/var/property_delegates.swift

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -322,25 +322,9 @@ func testBackingStore<T>(bs: BackingStore<T>) {
322322
// Explicitly-specified accessors
323323
// ---------------------------------------------------------------------------
324324
struct DelegateWithAccessors {
325-
@Wrapper
325+
@Wrapper // expected-error{{property delegate cannot be applied to a computed property}}
326326
var x: Int {
327-
return $x.value * 2
328-
}
329-
330-
@WrapperWithInitialValue
331-
var y: Int {
332-
get {
333-
return $y.value
334-
}
335-
336-
set {
337-
$y.value = newValue / 2
338-
}
339-
}
340-
341-
mutating func test() {
342-
x = y
343-
y = x
327+
return 17
344328
}
345329
}
346330

@@ -520,24 +504,13 @@ class Box<Value> {
520504
struct UseBox {
521505
@Box
522506
var x = 17
523-
524-
@Box
525-
var y: Int {
526-
get { return $y.value }
527-
set { }
528-
}
529507
}
530508

531509
func testBox(ub: UseBox) {
532510
_ = ub.x
533511
ub.x = 5 // expected-error{{cannot assign to property: 'x' is a get-only property}}
534512

535-
_ = ub.y
536-
ub.y = 20 // expected-error{{cannot assign to property: 'ub' is a 'let' constant}}
537-
538513
var mutableUB = ub
539-
_ = mutableUB.y
540-
mutableUB.y = 20
541514
mutableUB = ub
542515
}
543516

0 commit comments

Comments
 (0)