diff --git a/src/Controls/src/SourceGen/GeneratorHelpers.cs b/src/Controls/src/SourceGen/GeneratorHelpers.cs index b9aded9ba653..80b65cb0dee7 100644 --- a/src/Controls/src/SourceGen/GeneratorHelpers.cs +++ b/src/Controls/src/SourceGen/GeneratorHelpers.cs @@ -393,14 +393,34 @@ public static (string? source, XamlProjectItemForCB? xamlItem, IList /// /// Formats a value as a culture-independent C# literal for source generation. - /// Uses SymbolDisplay.FormatPrimitive to ensure proper handling of special values like NaN and Infinity - /// but also numeric types and makes sure they are formatted correctly. + /// Handles special floating-point values (NaN, Infinity) and uses SymbolDisplay.FormatPrimitive + /// for regular numeric types to ensure they are formatted correctly. /// /// The value to format /// Whether to include quotes around the formatted value /// A culture-independent string representation suitable for source generation public static string FormatInvariant(object value, bool quoted = false) { + // Handle special floating-point values that SymbolDisplay.FormatPrimitive doesn't prefix correctly + if (value is double d) + { + if (double.IsNaN(d)) + return "double.NaN"; + if (double.IsPositiveInfinity(d)) + return "double.PositiveInfinity"; + if (double.IsNegativeInfinity(d)) + return "double.NegativeInfinity"; + } + else if (value is float f) + { + if (float.IsNaN(f)) + return "float.NaN"; + if (float.IsPositiveInfinity(f)) + return "float.PositiveInfinity"; + if (float.IsNegativeInfinity(f)) + return "float.NegativeInfinity"; + } + return SymbolDisplay.FormatPrimitive(value, quoteStrings: quoted, useHexadecimalNumbers: false); } diff --git a/src/Controls/tests/SourceGen.UnitTests/Maui33532Tests.cs b/src/Controls/tests/SourceGen.UnitTests/Maui33532Tests.cs new file mode 100644 index 000000000000..07241eec92a0 --- /dev/null +++ b/src/Controls/tests/SourceGen.UnitTests/Maui33532Tests.cs @@ -0,0 +1,212 @@ +using System; +using System.Linq; +using Xunit; + +namespace Microsoft.Maui.Controls.SourceGen.UnitTests; + +/// +/// Tests for issue #33532: NaN value in XAML generates invalid code +/// When Padding="NaN" is used, the source generator was generating bare "NaN" instead of "double.NaN" +/// +public class Maui33532Tests : SourceGenXamlInitializeComponentTestBase +{ + [Fact] + public void ThicknessWithSingleNaNValue() + { + // Issue #33532: Padding="NaN" generates invalid code with bare "NaN" instead of "double.NaN" + var xaml = +""" + + +