Skip to content

Commit 88dc3ad

Browse files
committed
Sema: Fix interface type computation for observer parameters
1 parent f239dbc commit 88dc3ad

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,7 @@ bool TypeChecker::isAvailabilitySafeForConformance(
16111611
return requirementInfo.isContainedIn(witnessInfo);
16121612
}
16131613

1614-
// Returns 'nullptr' if this is the setter's 'newValue' parameter;
1614+
// Returns 'nullptr' if this is the 'newValue' or 'oldValue' parameter;
16151615
// otherwise, returns the corresponding parameter of the subscript
16161616
// declaration.
16171617
static ParamDecl *getOriginalParamFromAccessor(AbstractStorageDecl *storage,
@@ -1623,11 +1623,9 @@ static ParamDecl *getOriginalParamFromAccessor(AbstractStorageDecl *storage,
16231623
switch (accessor->getAccessorKind()) {
16241624
case AccessorKind::DidSet:
16251625
case AccessorKind::WillSet:
1626-
return nullptr;
1627-
16281626
case AccessorKind::Set:
16291627
if (param == accessorParams->get(0)) {
1630-
// This is the 'newValue' parameter.
1628+
// This is the 'newValue' or 'oldValue' parameter.
16311629
return nullptr;
16321630
}
16331631

test/decl/protocol/existential_member_accesses_self_assoctype_fixit.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ struct Test {
189189
}
190190

191191
subscript(p: any P & Q) -> any P {
192-
_read { p.method(false) } // expected-error {{member 'method' cannot be used on value of protocol type 'P & Q'; consider using a generic constraint instead}} {{-1:20--1:25=<#generic parameter name#>}} {{-1:16--1:20=}} {{-1:12--1:12=<<#generic parameter name#>: P & Q>}} {{none}}
192+
_read { p.method(false) } // expected-error {{member 'method' cannot be used on value of protocol type 'P & Q'; consider using a generic constraint instead}} {{-1:20--1:25=<#generic parameter name#>}} {{-1:16--1:20=}} {{-1:12--1:12=<<#generic parameter name#>: P & Q>}} {{none}}
193193
_modify { p.method(false) } // expected-error {{member 'method' cannot be used on value of protocol type 'P & Q'; consider using a generic constraint instead}} {{-2:20--2:25=<#generic parameter name#>}} {{-2:16--2:20=}} {{-2:12--2:12=<<#generic parameter name#>: P & Q>}} {{none}}
194-
willSet { p.method(false) } // expected-error {{member 'method' cannot be used on value of protocol type 'P'; consider using a generic constraint instead}} {{-3:20--3:25=<#generic parameter name#>}} {{-3:16--3:20=}} {{-3:12--3:12=<<#generic parameter name#>: P & Q>}} {{none}}
195-
didSet { p.method(false) } // expected-error {{member 'method' cannot be used on value of protocol type 'P'; consider using a generic constraint instead}} {{-4:20--4:25=<#generic parameter name#>}} {{-4:16--4:20=}} {{-4:12--4:12=<<#generic parameter name#>: P & Q>}} {{none}}
194+
willSet { p.method(false) } // expected-error {{member 'method' cannot be used on value of protocol type 'P & Q'; consider using a generic constraint instead}} {{-3:20--3:25=<#generic parameter name#>}} {{-3:16--3:20=}} {{-3:12--3:12=<<#generic parameter name#>: P & Q>}} {{none}}
195+
didSet { p.method(false) } // expected-error {{member 'method' cannot be used on value of protocol type 'P & Q'; consider using a generic constraint instead}} {{-4:20--4:25=<#generic parameter name#>}} {{-4:16--4:20=}} {{-4:12--4:12=<<#generic parameter name#>: P & Q>}} {{none}}
196196
// expected-error@-2 {{'willSet' is not allowed in subscripts}}
197197
// expected-error@-2 {{'didSet' is not allowed in subscripts}}
198198
}

test/decl/subscript/subscripting.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,22 @@ protocol ProtocolWillSetDidSet4 {
165165
}
166166

167167
class DidSetInSubscript {
168-
subscript(_: Int) -> Int {
168+
subscript(x: Int) -> Bool {
169169
didSet { // expected-error {{'didSet' is not allowed in subscripts}}
170170
print("eek")
171+
// Make sure implicit observer parameters pick up the right type.
172+
let _: Int = x
171173
}
172174
get {}
173175
}
174176
}
175177

176178
class WillSetInSubscript {
177-
subscript(_: Int) -> Int {
179+
subscript(x: Int) -> Bool {
178180
willSet { // expected-error {{'willSet' is not allowed in subscripts}}
179181
print("eek")
182+
// Make sure implicit observer parameters pick up the right type.
183+
let _: Int = x
180184
}
181185
get {}
182186
}

0 commit comments

Comments
 (0)