Skip to content

Commit e73eb24

Browse files
committed
Fix an edge case with XAML enum types
1 parent e3e90d7 commit e73eb24

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.SourceGenerators/Diagnostics/Analyzers/UseGeneratedDependencyPropertyOnManualPropertyAnalyzer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,12 @@ void HandleSetAccessor(IPropertySymbol propertySymbol, PropertyFlags propertyFla
473473
// We have found a valid constant. If it's an enum type, we have a couple special cases to handle.
474474
if (conversionOperation.Operand.Type is { TypeKind: TypeKind.Enum } operandType)
475475
{
476-
// As an optimization, we check whether the constant was the value
476+
// As an optimization, we check whether the constant was the default value (not other enum member)
477477
// of some projected built-in WinRT enum type (ie. not any user-defined enum type). If that is the
478478
// case, the XAML infrastructure can default that values automatically, meaning we can skip the
479479
// overhead of instantiating a 'PropertyMetadata' instance in code, and marshalling default value.
480-
if (operandType.IsContainedInNamespace(WellKnownTypeNames.XamlNamespace(useWindowsUIXaml)))
480+
// We also need to exclude scenarios where the property is actually nullable, but we're assigning a value.
481+
if (operandType.IsContainedInNamespace(WellKnownTypeNames.XamlNamespace(useWindowsUIXaml)) && !isNullableValueType)
481482
{
482483
// Before actually enabling the optimization, validate that the default value is actually
483484
// the same as the default value of the enum (ie. the value of its first declared field).

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/Test_UseGeneratedDependencyPropertyOnManualPropertyCodeFixer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,8 +2443,8 @@ public abstract partial class KeyFrame<TValue, TKeyFrame> : DependencyObject
24432443
[DataRow("Visibility", "object", "new PropertyMetadata(default(Visibility))", "(PropertyType = typeof(object))")]
24442444
[DataRow("Visibility?", "Visibility?", "null", "")]
24452445
[DataRow("Visibility?", "Visibility?", "new PropertyMetadata(null)", "")]
2446-
//[DataRow("Visibility?", "Visibility?", "new PropertyMetadata(default(Visibility))", "(DefaultValue = Visibility.Visible)")]
2447-
//[DataRow("Visibility?", "Visibility?", "new PropertyMetadata(Visibility.Visible)", "(DefaultValue = Visibility.Visible)")]
2446+
[DataRow("Visibility?", "Visibility?", "new PropertyMetadata(default(Visibility))", "(DefaultValue = Visibility.Visible)")]
2447+
[DataRow("Visibility?", "Visibility?", "new PropertyMetadata(Visibility.Visible)", "(DefaultValue = Visibility.Visible)")]
24482448
[DataRow("Visibility?", "Visibility?", "new PropertyMetadata(Visibility.Collapsed)", "(DefaultValue = Visibility.Collapsed)")]
24492449
[DataRow("Visibility?", "object", "null", "(PropertyType = typeof(object))")]
24502450
[DataRow("Visibility?", "object", "new PropertyMetadata(null)", "(PropertyType = typeof(object))")]
@@ -2459,7 +2459,7 @@ public abstract partial class KeyFrame<TValue, TKeyFrame> : DependencyObject
24592459
[DataRow("MyEnum", "object", "new PropertyMetadata(default(MyEnum))", "(PropertyType = typeof(object))")]
24602460
[DataRow("MyEnum?", "MyEnum?", "null", "")]
24612461
[DataRow("MyEnum?", "MyEnum?", "new PropertyMetadata(null)", "")]
2462-
//[DataRow("MyEnum?", "MyEnum?", "new PropertyMetadata(default(MyEnum))", "(DefaultValue = MyEnum.A)")]
2462+
[DataRow("MyEnum?", "MyEnum?", "new PropertyMetadata(default(MyEnum))", "(DefaultValue = MyEnum.A)")]
24632463
[DataRow("MyEnum?", "MyEnum?", "new PropertyMetadata(MyEnum.A)", "(DefaultValue = MyEnum.A)")]
24642464
[DataRow("MyEnum?", "MyEnum?", "new PropertyMetadata(MyEnum.B)", "(DefaultValue = MyEnum.B)")]
24652465
[DataRow("MyEnum?", "object", "null", "(PropertyType = typeof(object))")]

0 commit comments

Comments
 (0)