Skip to content

Commit e11057d

Browse files
committed
Fix regressed virtual read-only properties with setters in subclasses
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 9d365a6 commit e11057d

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

src/Generator/Passes/GetterSetterToPropertyPass.cs

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -143,27 +143,8 @@ private static void ProcessProperties(Class @class, HashSet<Property> newPropert
143143
{
144144
foreach (var property in newProperties)
145145
{
146-
if (property.IsOverride)
147-
{
148-
Property baseProperty = GetBaseProperty(@class, property);
149-
if (baseProperty == null)
150-
{
151-
if (property.SetMethod != null)
152-
{
153-
property.SetMethod.GenerationKind = GenerationKind.Generate;
154-
property.SetMethod = null;
155-
}
156-
else
157-
{
158-
property.GetMethod.GenerationKind = GenerationKind.Generate;
159-
property.GetMethod = null;
160-
}
161-
}
162-
else if (property.GetMethod == null && baseProperty.SetMethod != null)
163-
property.GetMethod = baseProperty.GetMethod;
164-
else if (property.SetMethod == null)
165-
property.SetMethod = baseProperty.SetMethod;
166-
}
146+
ProcessOverridden(@class, property);
147+
167148
if (property.GetMethod == null)
168149
{
169150
if (property.SetMethod != null)
@@ -185,14 +166,45 @@ private static void ProcessProperties(Class @class, HashSet<Property> newPropert
185166
}
186167
}
187168

169+
private static void ProcessOverridden(Class @class, Property property)
170+
{
171+
if (!property.IsOverride)
172+
return;
173+
174+
Property baseProperty = GetBaseProperty(@class, property);
175+
if (baseProperty == null)
176+
{
177+
if (property.SetMethod != null)
178+
{
179+
property.SetMethod.GenerationKind = GenerationKind.Generate;
180+
property.SetMethod = null;
181+
}
182+
else
183+
{
184+
property.GetMethod.GenerationKind = GenerationKind.Generate;
185+
property.GetMethod = null;
186+
}
187+
}
188+
else if (property.GetMethod == null && baseProperty.SetMethod != null)
189+
property.GetMethod = baseProperty.GetMethod;
190+
else if (property.SetMethod == null || baseProperty.SetMethod == null)
191+
{
192+
if (property.SetMethod != null)
193+
property.SetMethod.GenerationKind = GenerationKind.Generate;
194+
property.SetMethod = baseProperty.SetMethod;
195+
}
196+
}
197+
188198
private static Property GetBaseProperty(Class @class, Property @override)
189199
{
190200
foreach (var @base in @class.Bases)
191201
{
192202
Class baseClass = @base.Class.OriginalClass ?? @base.Class;
193203
Property baseProperty = baseClass.Properties.Find(p =>
194-
(@override.GetMethod != null && @override.GetMethod.BaseMethod == p.GetMethod) ||
195-
(@override.SetMethod != null && @override.SetMethod.BaseMethod == p.SetMethod) ||
204+
(@override.GetMethod?.IsOverride == true &&
205+
@override.GetMethod.BaseMethod == p.GetMethod) ||
206+
(@override.SetMethod?.IsOverride == true &&
207+
@override.SetMethod.BaseMethod == p.SetMethod) ||
196208
(@override.Field != null && @override.Field == p.Field));
197209
if (baseProperty != null)
198210
return baseProperty;

tests/Common/Common.Tests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,13 @@ public void TestProperties()
522522

523523
Assert.That(prop.Get32Bit, Is.EqualTo(10));
524524
Assert.That(prop.IsEmpty, Is.EqualTo(prop.Empty));
525+
526+
Assert.That(prop.VirtualGetter, Is.EqualTo(15));
527+
}
528+
using (var prop = new HasOverridenSetter())
529+
{
530+
Assert.That(prop.VirtualGetter, Is.EqualTo(20));
531+
prop.SetVirtualGetter(5);
525532
}
526533
}
527534

tests/Common/Common.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,11 @@ bool TestProperties::empty()
628628
return false;
629629
}
630630

631+
int TestProperties::virtualGetter()
632+
{
633+
return 15;
634+
}
635+
631636
HasOverridenSetter::HasOverridenSetter()
632637
{
633638
}
@@ -646,6 +651,15 @@ bool HasOverridenSetter::setVirtualSetterReturnsBoolean(int value)
646651
return TestProperties::setVirtualSetterReturnsBoolean(value);
647652
}
648653

654+
int HasOverridenSetter::virtualGetter()
655+
{
656+
return 20;
657+
}
658+
659+
void HasOverridenSetter::setVirtualGetter(int value)
660+
{
661+
}
662+
649663
TypeMappedIndex::TypeMappedIndex()
650664
{
651665
}

tests/Common/Common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,8 @@ struct DLL_API TestProperties
614614
int get32Bit();
615615
bool isEmpty();
616616
bool empty();
617+
618+
virtual int virtualGetter();
617619
private:
618620
int FieldValue;
619621
double _refToPrimitiveInSetter;
@@ -630,6 +632,9 @@ class DLL_API HasOverridenSetter : public TestProperties
630632

631633
int virtualSetterReturnsBoolean() override;
632634
bool setVirtualSetterReturnsBoolean(int value) override;
635+
636+
int virtualGetter() override;
637+
void setVirtualGetter(int value);
633638
};
634639

635640
class DLL_API TypeMappedIndex

0 commit comments

Comments
 (0)