-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[XSG] Fix NaN value in XAML generating invalid code #33533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PureWeen
merged 1 commit into
inflight/current
from
fix/33532-nan-sourcegen-invalid-code
Jan 16, 2026
+291
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
src/Controls/tests/SourceGen.UnitTests/Maui33532Tests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Maui.Controls.SourceGen.UnitTests; | ||
|
|
||
| /// <summary> | ||
| /// 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" | ||
| /// </summary> | ||
| public class Maui33532Tests : SourceGenXamlInitializeComponentTestBase | ||
| { | ||
| [Fact] | ||
| public void ThicknessWithSingleNaNValue() | ||
| { | ||
| // Issue #33532: Padding="NaN" generates invalid code with bare "NaN" instead of "double.NaN" | ||
| var xaml = | ||
| """ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ContentPage | ||
| xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| x:Class="Test.TestPage"> | ||
| <Button x:Name="testButton" Padding="NaN" Text="Test"/> | ||
| </ContentPage> | ||
| """; | ||
|
|
||
| var code = | ||
| """ | ||
| using Microsoft.Maui.Controls; | ||
| using Microsoft.Maui.Controls.Xaml; | ||
|
|
||
| namespace Test; | ||
|
|
||
| [XamlProcessing(XamlInflator.SourceGen)] | ||
| public partial class TestPage : ContentPage | ||
| { | ||
| public TestPage() | ||
| { | ||
| InitializeComponent(); | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| var (result, generated) = RunGenerator(xaml, code, targetFramework: "net10.0"); | ||
| Assert.False(result.Diagnostics.Any()); | ||
|
|
||
| // Verify that NaN is correctly generated as double.NaN | ||
| Assert.Contains("double.NaN", generated, StringComparison.Ordinal); | ||
|
|
||
| // Ensure incorrect bare NaN is not generated (would fail with CS0103) | ||
| // The pattern "new global::Microsoft.Maui.Thickness(NaN)" would be incorrect | ||
| Assert.DoesNotContain("Thickness(NaN)", generated, StringComparison.Ordinal); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ThicknessWithTwoNaNValues() | ||
| { | ||
| // Test Padding="NaN,NaN" (horizontal, vertical) | ||
| var xaml = | ||
| """ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ContentPage | ||
| xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| x:Class="Test.TestPage"> | ||
| <Button x:Name="testButton" Padding="NaN,NaN" Text="Test"/> | ||
| </ContentPage> | ||
| """; | ||
|
|
||
| var code = | ||
| """ | ||
| using Microsoft.Maui.Controls; | ||
| using Microsoft.Maui.Controls.Xaml; | ||
|
|
||
| namespace Test; | ||
|
|
||
| [XamlProcessing(XamlInflator.SourceGen)] | ||
| public partial class TestPage : ContentPage | ||
| { | ||
| public TestPage() | ||
| { | ||
| InitializeComponent(); | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| var (result, generated) = RunGenerator(xaml, code, targetFramework: "net10.0"); | ||
| Assert.False(result.Diagnostics.Any()); | ||
|
|
||
| // Verify that NaN is correctly generated as double.NaN | ||
| Assert.Contains("double.NaN", generated, StringComparison.Ordinal); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ThicknessWithFourNaNValues() | ||
| { | ||
| // Test Padding="NaN,NaN,NaN,NaN" (left, top, right, bottom) | ||
| var xaml = | ||
| """ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ContentPage | ||
| xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| x:Class="Test.TestPage"> | ||
| <Button x:Name="testButton" Padding="NaN,NaN,NaN,NaN" Text="Test"/> | ||
| </ContentPage> | ||
| """; | ||
|
|
||
| var code = | ||
| """ | ||
| using Microsoft.Maui.Controls; | ||
| using Microsoft.Maui.Controls.Xaml; | ||
|
|
||
| namespace Test; | ||
|
|
||
| [XamlProcessing(XamlInflator.SourceGen)] | ||
| public partial class TestPage : ContentPage | ||
| { | ||
| public TestPage() | ||
| { | ||
| InitializeComponent(); | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| var (result, generated) = RunGenerator(xaml, code, targetFramework: "net10.0"); | ||
| Assert.False(result.Diagnostics.Any()); | ||
|
|
||
| // Verify that NaN is correctly generated as double.NaN (should appear 4 times) | ||
| Assert.Contains("double.NaN", generated, StringComparison.Ordinal); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ThicknessWithMixedNaNAndRegularValues() | ||
| { | ||
| // Test mixing NaN with regular values: Padding="NaN,10,NaN,20" | ||
| var xaml = | ||
| """ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ContentPage | ||
| xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| x:Class="Test.TestPage"> | ||
| <Button x:Name="testButton" Padding="NaN,10,NaN,20" Text="Test"/> | ||
| </ContentPage> | ||
| """; | ||
|
|
||
| var code = | ||
| """ | ||
| using Microsoft.Maui.Controls; | ||
| using Microsoft.Maui.Controls.Xaml; | ||
|
|
||
| namespace Test; | ||
|
|
||
| [XamlProcessing(XamlInflator.SourceGen)] | ||
| public partial class TestPage : ContentPage | ||
| { | ||
| public TestPage() | ||
| { | ||
| InitializeComponent(); | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| var (result, generated) = RunGenerator(xaml, code, targetFramework: "net10.0"); | ||
| Assert.False(result.Diagnostics.Any()); | ||
|
|
||
| // Verify that both NaN and regular values are generated correctly | ||
| Assert.Contains("double.NaN", generated, StringComparison.Ordinal); | ||
| Assert.Contains("Thickness", generated, StringComparison.Ordinal); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void MarginWithNaNValue() | ||
| { | ||
| // Test Margin (also uses ThicknessConverter) with NaN | ||
| var xaml = | ||
| """ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ContentPage | ||
| xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| x:Class="Test.TestPage"> | ||
| <Label x:Name="testLabel" Margin="NaN" Text="Test"/> | ||
| </ContentPage> | ||
| """; | ||
|
|
||
| var code = | ||
| """ | ||
| using Microsoft.Maui.Controls; | ||
| using Microsoft.Maui.Controls.Xaml; | ||
|
|
||
| namespace Test; | ||
|
|
||
| [XamlProcessing(XamlInflator.SourceGen)] | ||
| public partial class TestPage : ContentPage | ||
| { | ||
| public TestPage() | ||
| { | ||
| InitializeComponent(); | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| var (result, generated) = RunGenerator(xaml, code, targetFramework: "net10.0"); | ||
| Assert.False(result.Diagnostics.Any()); | ||
|
|
||
| // Verify that NaN is correctly generated as double.NaN for Margin | ||
| Assert.Contains("double.NaN", generated, StringComparison.Ordinal); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui33532"> | ||
| <!-- Test: NaN value in XAML should generate valid code with double.NaN --> | ||
| <StackLayout> | ||
| <Button x:Name="buttonNaN" Padding="NaN" Text="Padding = NaN"/> | ||
| <Button x:Name="buttonNaNComma" Padding="NaN,NaN" Text="Padding = NaN,NaN"/> | ||
| <Button x:Name="buttonNaNFull" Padding="NaN,NaN,NaN,NaN" Text="Padding = NaN,NaN,NaN,NaN"/> | ||
| </StackLayout> | ||
| </ContentPage> |
46 changes: 46 additions & 0 deletions
46
src/Controls/tests/Xaml.UnitTests/Issues/Maui33532.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Maui.Controls.Xaml.UnitTests; | ||
|
|
||
| public partial class Maui33532 : ContentPage | ||
| { | ||
| public Maui33532() => InitializeComponent(); | ||
|
|
||
| [Collection("Issue")] | ||
| public class Tests | ||
| { | ||
| [Theory] | ||
| [XamlInflatorData] | ||
| internal void NaNValueInXamlGeneratesValidCode(XamlInflator inflator) | ||
| { | ||
| // This test reproduces issue #33532: | ||
| // When a XAML file contains NaN as a value (e.g., Padding="NaN"), | ||
| // the XAML Source Generator was generating invalid C# code using bare "NaN" | ||
| // instead of "double.NaN", causing CS0103 compiler error. | ||
| var page = new Maui33532(inflator); | ||
|
|
||
| Assert.NotNull(page); | ||
| Assert.NotNull(page.buttonNaN); | ||
| Assert.NotNull(page.buttonNaNComma); | ||
| Assert.NotNull(page.buttonNaNFull); | ||
|
|
||
| // Verify NaN values were applied correctly (all sides should be NaN) | ||
| Assert.True(double.IsNaN(page.buttonNaN.Padding.Left)); | ||
| Assert.True(double.IsNaN(page.buttonNaN.Padding.Top)); | ||
| Assert.True(double.IsNaN(page.buttonNaN.Padding.Right)); | ||
| Assert.True(double.IsNaN(page.buttonNaN.Padding.Bottom)); | ||
|
|
||
| // Verify 2-value NaN syntax (horizontal, vertical) | ||
| Assert.True(double.IsNaN(page.buttonNaNComma.Padding.Left)); | ||
| Assert.True(double.IsNaN(page.buttonNaNComma.Padding.Top)); | ||
|
|
||
| // Verify 4-value NaN syntax | ||
| Assert.True(double.IsNaN(page.buttonNaNFull.Padding.Left)); | ||
| Assert.True(double.IsNaN(page.buttonNaNFull.Padding.Top)); | ||
| Assert.True(double.IsNaN(page.buttonNaNFull.Padding.Right)); | ||
| Assert.True(double.IsNaN(page.buttonNaNFull.Padding.Bottom)); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.