Skip to content

Commit 9dc6710

Browse files
committed
Fix codegen for 'bool' default values
1 parent 21f3684 commit 9dc6710

File tree

3 files changed

+122
-4
lines changed

3 files changed

+122
-4
lines changed

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.SourceGenerators/Extensions/IOperationExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ public static bool IsConstantValueDefault(this IOperation operation)
3636
return true;
3737
}
3838

39-
// Manually match for known primitive types
39+
// Manually match for known primitive types (this should be kept in sync with 'IsWellKnownWinRTProjectedValueType')
4040
return (operation.Type.SpecialType, operation.ConstantValue.Value) switch
4141
{
4242
(SpecialType.System_Byte, default(byte)) or
43-
(SpecialType.System_Char, default(char)) or
43+
(SpecialType.System_SByte, default(sbyte)) or
4444
(SpecialType.System_Int16, default(short)) or
4545
(SpecialType.System_UInt16, default(ushort)) or
46+
(SpecialType.System_Char, default(char)) or
4647
(SpecialType.System_Int32, default(int)) or
4748
(SpecialType.System_UInt32, default(uint)) or
4849
(SpecialType.System_Int64, default(long)) or

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.SourceGenerators/Extensions/WinRTExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,16 @@ public static bool IsWellKnownWinRTProjectedValueType(this ITypeSymbol symbol, b
5757

5858
// Lastly, special case the well known primitive types
5959
if (symbol.SpecialType is
60-
SpecialType.System_Int32 or
6160
SpecialType.System_Byte or
6261
SpecialType.System_SByte or
6362
SpecialType.System_Int16 or
6463
SpecialType.System_UInt16 or
64+
SpecialType.System_Char or
65+
SpecialType.System_Int32 or
6566
SpecialType.System_UInt32 or
6667
SpecialType.System_Int64 or
6768
SpecialType.System_UInt64 or
68-
SpecialType.System_Char or
69+
SpecialType.System_Boolean or
6970
SpecialType.System_Single or
7071
SpecialType.System_Double)
7172
{

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/Test_DependencyPropertyGenerator.cs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4282,4 +4282,120 @@ public partial string? Name
42824282

42834283
CSharpGeneratorTest<DependencyPropertyGenerator>.VerifySources(source, ("MyNamespace.MyControl.g.cs", result), languageVersion: LanguageVersion.Preview);
42844284
}
4285+
4286+
[TestMethod]
4287+
public void SingleProperty_Bool_WithNoCaching()
4288+
{
4289+
const string source = """
4290+
using CommunityToolkit.WinUI;
4291+
using Windows.UI.Xaml;
4292+
4293+
namespace MyNamespace;
4294+
4295+
public partial class MyControl : DependencyObject
4296+
{
4297+
[GeneratedDependencyProperty]
4298+
public partial bool IsSelected { get; set; }
4299+
}
4300+
""";
4301+
4302+
const string result = """
4303+
// <auto-generated/>
4304+
#pragma warning disable
4305+
#nullable enable
4306+
4307+
namespace MyNamespace
4308+
{
4309+
/// <inheritdoc cref="MyControl"/>
4310+
partial class MyControl
4311+
{
4312+
/// <summary>
4313+
/// The backing <see cref="global::Windows.UI.Xaml.DependencyProperty"/> instance for <see cref="IsSelected"/>.
4314+
/// </summary>
4315+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
4316+
public static readonly global::Windows.UI.Xaml.DependencyProperty IsSelectedProperty = global::Windows.UI.Xaml.DependencyProperty.Register(
4317+
name: "IsSelected",
4318+
propertyType: typeof(bool),
4319+
ownerType: typeof(MyControl),
4320+
typeMetadata: null);
4321+
4322+
/// <inheritdoc/>
4323+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
4324+
[global::System.Diagnostics.DebuggerNonUserCode]
4325+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
4326+
public partial bool IsSelected
4327+
{
4328+
get
4329+
{
4330+
object? __boxedValue = GetValue(IsSelectedProperty);
4331+
4332+
OnIsSelectedGet(ref __boxedValue);
4333+
4334+
bool __unboxedValue = (bool)__boxedValue;
4335+
4336+
OnIsSelectedGet(ref __unboxedValue);
4337+
4338+
return __unboxedValue;
4339+
}
4340+
set
4341+
{
4342+
OnIsSelectedSet(ref value);
4343+
4344+
object? __boxedValue = value;
4345+
4346+
OnIsSelectedSet(ref __boxedValue);
4347+
4348+
SetValue(IsSelectedProperty, __boxedValue);
4349+
4350+
OnIsSelectedChanged(value);
4351+
}
4352+
}
4353+
4354+
/// <summary>Executes the logic for when the <see langword="get"/> accessor <see cref="IsSelected"/> is invoked</summary>
4355+
/// <param name="propertyValue">The raw property value that has been retrieved from <see cref="IsSelectedProperty"/>.</param>
4356+
/// <remarks>This method is invoked on the boxed value retrieved via <see cref="GetValue"/> on <see cref="IsSelectedProperty"/>.</remarks>
4357+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
4358+
partial void OnIsSelectedGet(ref object propertyValue);
4359+
4360+
/// <summary>Executes the logic for when the <see langword="get"/> accessor <see cref="IsSelected"/> is invoked</summary>
4361+
/// <param name="propertyValue">The unboxed property value that has been retrieved from <see cref="IsSelectedProperty"/>.</param>
4362+
/// <remarks>This method is invoked on the unboxed value retrieved via <see cref="GetValue"/> on <see cref="IsSelectedProperty"/>.</remarks>
4363+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
4364+
partial void OnIsSelectedGet(ref bool propertyValue);
4365+
4366+
/// <summary>Executes the logic for when the <see langword="set"/> accessor <see cref="IsSelected"/> is invoked</summary>
4367+
/// <param name="propertyValue">The boxed property value that has been produced before assigning to <see cref="IsSelectedProperty"/>.</param>
4368+
/// <remarks>This method is invoked on the boxed value that is about to be passed to <see cref="SetValue"/> on <see cref="IsSelectedProperty"/>.</remarks>
4369+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
4370+
partial void OnIsSelectedSet(ref object propertyValue);
4371+
4372+
/// <summary>Executes the logic for when the <see langword="set"/> accessor <see cref="IsSelected"/> is invoked</summary>
4373+
/// <param name="propertyValue">The property value that is being assigned to <see cref="IsSelected"/>.</param>
4374+
/// <remarks>This method is invoked on the raw value being assigned to <see cref="IsSelected"/>, before <see cref="SetValue"/> is used.</remarks>
4375+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
4376+
partial void OnIsSelectedSet(ref bool propertyValue);
4377+
4378+
/// <summary>Executes the logic for when <see cref="IsSelected"/> has just changed.</summary>
4379+
/// <param name="value">The new property value that has been set.</param>
4380+
/// <remarks>This method is invoked right after the value of <see cref="IsSelected"/> is changed.</remarks>
4381+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
4382+
partial void OnIsSelectedChanged(bool newValue);
4383+
4384+
/// <summary>Executes the logic for when <see cref="IsSelected"/> has just changed.</summary>
4385+
/// <param name="e">Event data that is issued by any event that tracks changes to the effective value of this property.</param>
4386+
/// <remarks>This method is invoked by the <see cref="global::Windows.UI.Xaml.DependencyProperty"/> infrastructure, after the value of <see cref="IsSelected"/> is changed.</remarks>
4387+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
4388+
partial void OnIsSelectedPropertyChanged(global::Windows.UI.Xaml.DependencyPropertyChangedEventArgs e);
4389+
4390+
/// <summary>Executes the logic for when any dependency property has just changed.</summary>
4391+
/// <param name="e">Event data that is issued by any event that tracks changes to the effective value of this property.</param>
4392+
/// <remarks>This method is invoked by the <see cref="global::Windows.UI.Xaml.DependencyProperty"/> infrastructure, after the value of any dependency property has just changed.</remarks>
4393+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
4394+
partial void OnPropertyChanged(global::Windows.UI.Xaml.DependencyPropertyChangedEventArgs e);
4395+
}
4396+
}
4397+
""";
4398+
4399+
CSharpGeneratorTest<DependencyPropertyGenerator>.VerifySources(source, ("MyNamespace.MyControl.g.cs", result), languageVersion: LanguageVersion.Preview);
4400+
}
42854401
}

0 commit comments

Comments
 (0)