Skip to content

Commit df63c36

Browse files
authored
When setting a willSet/didSet param's type, also set the TypeLoc (#7377)
We don't actually need the TypeLoc for anything, but it was still getting type-checked, which means it doesn't get the benefit of inference from the initial value. In some cases the actual type of the ParamDecl seems to get reset to the TypeLoc's type as well. Just do the simple thing and set it directly ahead of time. Fixes a source compatibility issue with Swift 3.0. https://bugs.swift.org/browse/SR-3893
1 parent 1d50833 commit df63c36

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4796,6 +4796,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
47964796
auto *newValueParam = firstParamPattern->get(0);
47974797
newValueParam->setType(valueTy);
47984798
newValueParam->setInterfaceType(valueIfaceTy);
4799+
newValueParam->getTypeLoc().setType(valueTy);
47994800
} else if (FD->isGetter() && FD->isImplicit()) {
48004801
FD->getBodyResultTypeLoc().setType(valueIfaceTy, true);
48014802
}

test/decl/var/properties.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,3 +1186,40 @@ class r24314506 { // expected-error {{class 'r24314506' has no initializers}}
11861186
}
11871187

11881188

1189+
// https://bugs.swift.org/browse/SR-3893
1190+
// Generic type is not inferenced from its initial value for properties with
1191+
// will/didSet
1192+
struct SR3893Box<Foo> {
1193+
let value: Foo
1194+
}
1195+
1196+
struct SR3893 {
1197+
// Each of these "bad" properties used to produce errors.
1198+
var bad: SR3893Box = SR3893Box(value: 0) {
1199+
willSet {
1200+
print(newValue.value)
1201+
}
1202+
}
1203+
1204+
var bad2: SR3893Box = SR3893Box(value: 0) {
1205+
willSet(new) {
1206+
print(new.value)
1207+
}
1208+
}
1209+
1210+
var bad3: SR3893Box = SR3893Box(value: 0) {
1211+
didSet {
1212+
print(oldValue.value)
1213+
}
1214+
}
1215+
1216+
var good: SR3893Box<Int> = SR3893Box(value: 0) {
1217+
didSet {
1218+
print(oldValue.value)
1219+
}
1220+
}
1221+
1222+
var plain: SR3893Box = SR3893Box(value: 0)
1223+
}
1224+
1225+

0 commit comments

Comments
 (0)