Skip to content

Commit 52ac782

Browse files
committed
[DI] InitAccessors: Use of SILModule::getFieldIndex is incompatible with DI data structures
DI cares only about stored fields of the current type but `SILModule::getFieldIndex` goes through all of the supertypes as well.
1 parent 65cfb1c commit 52ac782

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,13 +1200,23 @@ ElementUseCollector::collectAssignOrInitUses(AssignOrInitInst *Inst,
12001200
->getDeclContext()
12011201
->getSelfNominalTypeDecl();
12021202

1203+
auto expansionContext = TypeExpansionContext(TheMemory.getFunction());
1204+
12031205
auto selfTy = Inst->getSelf()->getType();
12041206

12051207
auto addUse = [&](VarDecl *property, DIUseKind useKind) {
1206-
auto expansionContext = TypeExpansionContext(*Inst->getFunction());
1208+
unsigned fieldIdx = 0;
1209+
for (auto *VD : typeDC->getStoredProperties()) {
1210+
if (VD == property)
1211+
break;
1212+
1213+
fieldIdx += getElementCountRec(
1214+
expansionContext, Module,
1215+
selfTy.getFieldType(VD, Module, expansionContext), false);
1216+
}
1217+
12071218
auto type = selfTy.getFieldType(property, Module, expansionContext);
1208-
addElementUses(Module.getFieldIndex(typeDC, property), type, Inst, useKind,
1209-
property);
1219+
addElementUses(fieldIdx, type, Inst, useKind, property);
12101220
};
12111221

12121222
auto initializedElts = Inst->getInitializedProperties();

test/Interpreter/init_accessors.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,3 +709,44 @@ test_properties_with_inits()
709709
// CHECK-NEXT: TestAssign in x.init: self.x = S(x: 0)
710710
// CHECK-NEXT: TestAssign: self.x = S(x: -3)
711711
// CHECK-NEXT: test-init-expr-3: TestDefault(a: 42, b: <<default>>)
712+
713+
func test_inheritance() {
714+
class Entity {
715+
var _age: Int = 0
716+
var age: Int = 0 {
717+
@storageRestrictions(initializes: _age)
718+
init { _age = newValue }
719+
get { _age }
720+
set { _age = newValue }
721+
}
722+
}
723+
724+
class Person : Entity, CustomStringConvertible {
725+
var _firstName: String
726+
var firstName: String = "<<unknown>>" {
727+
@storageRestrictions(initializes: _firstName)
728+
init { _firstName = newValue }
729+
get { _firstName }
730+
set { _firstName = newValue }
731+
}
732+
733+
var description: String {
734+
"Person(firstName: \(firstName), age: \(age))"
735+
}
736+
737+
override init() {}
738+
739+
init(firstName: String, age: Int) {
740+
super.init()
741+
self.firstName = firstName
742+
self.age = age
743+
}
744+
}
745+
746+
print("test-inheritance-1: \(Person())")
747+
print("test-inheritance-2: \(Person(firstName: "Q", age: 42))")
748+
}
749+
750+
test_inheritance()
751+
// CHECK: test-inheritance-1: Person(firstName: <<unknown>>, age: 0)
752+
// CHECK-NEXT: test-inheritance-2: Person(firstName: Q, age: 42)

0 commit comments

Comments
 (0)