From 1b75bd85b1c156061f1757e39a5935040d573faa Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Sat, 19 Oct 2024 16:58:26 +0200 Subject: [PATCH 1/4] Add test --- .../src/Build.Tasks/NodeILExtensions.cs | 12 ----- .../Xaml.UnitTests/Issues/Maui25XXX.xaml | 10 ++++ .../Xaml.UnitTests/Issues/Maui25XXX.xaml.cs | 51 +++++++++++++++++++ 3 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 src/Controls/tests/Xaml.UnitTests/Issues/Maui25XXX.xaml create mode 100644 src/Controls/tests/Xaml.UnitTests/Issues/Maui25XXX.xaml.cs diff --git a/src/Controls/src/Build.Tasks/NodeILExtensions.cs b/src/Controls/src/Build.Tasks/NodeILExtensions.cs index 2e68a52c2e17..91981374471e 100644 --- a/src/Controls/src/Build.Tasks/NodeILExtensions.cs +++ b/src/Controls/src/Build.Tasks/NodeILExtensions.cs @@ -429,10 +429,6 @@ static Instruction PushParsedEnum(XamlCache cache, TypeReference enumRef, string public static IEnumerable PushXmlLineInfo(this INode node, ILContext context) { - if (context.ValidateOnly) - { - yield break; - } var module = context.Body.Method.Module; var xmlLineInfo = node as IXmlLineInfo; @@ -458,10 +454,6 @@ public static IEnumerable PushXmlLineInfo(this INode node, ILContex public static IEnumerable PushParentObjectsArray(this INode node, ILContext context) { - if (context.ValidateOnly) - { - yield break; - } var module = context.Body.Method.Module; var nodes = new List(); @@ -600,10 +592,6 @@ static IEnumerable PushNamescopes(INode node, ILContext context, Mo public static IEnumerable PushServiceProvider(this INode node, ILContext context, TypeReference[] requiredServices, FieldReference bpRef = null, PropertyReference propertyRef = null, TypeReference declaringTypeReference = null) { - if (context.ValidateOnly) - { - yield break; - } var module = context.Body.Method.Module; var createAllServices = requiredServices is null; diff --git a/src/Controls/tests/Xaml.UnitTests/Issues/Maui25XXX.xaml b/src/Controls/tests/Xaml.UnitTests/Issues/Maui25XXX.xaml new file mode 100644 index 000000000000..041b5774d7fe --- /dev/null +++ b/src/Controls/tests/Xaml.UnitTests/Issues/Maui25XXX.xaml @@ -0,0 +1,10 @@ + + + + diff --git a/src/Controls/tests/Xaml.UnitTests/Issues/Maui25XXX.xaml.cs b/src/Controls/tests/Xaml.UnitTests/Issues/Maui25XXX.xaml.cs new file mode 100644 index 000000000000..7dbdfa0969fc --- /dev/null +++ b/src/Controls/tests/Xaml.UnitTests/Issues/Maui25XXX.xaml.cs @@ -0,0 +1,51 @@ +using System; +using System.Linq; +using Microsoft.Maui.ApplicationModel; +using Microsoft.Maui.Controls.Core.UnitTests; +using Microsoft.Maui.Dispatching; +using Microsoft.Maui.UnitTests; +using Mono.Cecil.Cil; +using NUnit.Framework; + +namespace Microsoft.Maui.Controls.Xaml.UnitTests; + +public partial class Maui25XXX : ContentPage +{ + public Maui25XXX() + { + InitializeComponent(); + } + + public Maui25XXX(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + class Test + { + [SetUp] + public void Setup() + { + Application.SetCurrentApplication(new MockApplication()); + DispatcherProvider.SetCurrent(new DispatcherProviderStub()); + } + + [TearDown] public void TearDown() + { + AppInfo.SetCurrent(null); + } + + [Test] + public void WhenBindingIsCompiledBindingExtensionDoesNotReceiveServiceProviderWithXamlTypeResolver() + { + MockCompiler.Compile(typeof(Maui25XXX), out var md); + Assert.That(!md.Body.Instructions.Any(static i => i.OpCode == OpCodes.Newobj && i.Operand.ToString().Contains("XamlServiceProvider", StringComparison.Ordinal))); + } + } +} + +public class Maui25XXXViewModel +{ + public string Text { get; set; } +} From 0f7d1986c4541616d820ded8156fb791f0e2e7e1 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Sat, 19 Oct 2024 18:43:41 +0200 Subject: [PATCH 2/4] Prevent creating ServiceProvider when binding is compiled --- src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs b/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs index b823f24dc347..165e18930d62 100644 --- a/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs +++ b/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs @@ -307,6 +307,9 @@ public static IEnumerable ProvideValue(VariableDefinitionReference { if (TryCompileBindingPath(node, context, vardefref.VariableDefinition, bindingExtensionType.Value, isStandaloneBinding: bpRef is null, out var instructions)) { + // if the binding is compiled, there's no need to pass the ServiceProvider to the extension + acceptEmptyServiceProvider = true; + foreach (var instruction in instructions) yield return instruction; } From ed162a24690e923868460b1ee78da905c2ca0e96 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Mon, 21 Oct 2024 11:18:20 +0200 Subject: [PATCH 3/4] Use issue number --- .../Issues/{Maui25XXX.xaml => Maui25406.xaml} | 4 ++-- .../Issues/{Maui25XXX.xaml.cs => Maui25406.xaml.cs} | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) rename src/Controls/tests/Xaml.UnitTests/Issues/{Maui25XXX.xaml => Maui25406.xaml} (73%) rename src/Controls/tests/Xaml.UnitTests/Issues/{Maui25XXX.xaml.cs => Maui25406.xaml.cs} (84%) diff --git a/src/Controls/tests/Xaml.UnitTests/Issues/Maui25XXX.xaml b/src/Controls/tests/Xaml.UnitTests/Issues/Maui25406.xaml similarity index 73% rename from src/Controls/tests/Xaml.UnitTests/Issues/Maui25XXX.xaml rename to src/Controls/tests/Xaml.UnitTests/Issues/Maui25406.xaml index 041b5774d7fe..a5e1aa8e76b9 100644 --- a/src/Controls/tests/Xaml.UnitTests/Issues/Maui25XXX.xaml +++ b/src/Controls/tests/Xaml.UnitTests/Issues/Maui25406.xaml @@ -3,8 +3,8 @@ xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests" - x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui25XXX" - x:DataType="local:Maui25XXXViewModel"> + x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui25406" + x:DataType="local:Maui25406ViewModel">