From 57c838820cb00c2e864a2a14a01e411950496745 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Cruz?= Date: Tue, 5 May 2026 14:54:55 +0100 Subject: [PATCH] Fix #20625: remove automatic DataContext fallback to root type Address review feedback by removing the automatic fallback inference to the root type in AvaloniaXamlIlDataContextTypeTransformer, as it is not necessary to fix the issue and is not desired behavior. Also removes the unit tests that validated this specific fallback. --- ...valoniaXamlIlDataContextTypeTransformer.cs | 11 ------ .../Xaml/XamlIlTests.cs | 34 ------------------- 2 files changed, 45 deletions(-) diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlDataContextTypeTransformer.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlDataContextTypeTransformer.cs index 0e2ccd0dfb..a11dea95e4 100644 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlDataContextTypeTransformer.cs +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlDataContextTypeTransformer.cs @@ -118,17 +118,6 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers // Notify the user that we were unable to infer the data context type if they use a compiled binding. inferredDataContextTypeNode = new AvaloniaXamlIlUninferrableDataContextMetadataNode(on); } - - if (inferredDataContextTypeNode is null - && directiveDataContextTypeNode is null - && !context.ParentNodes().OfType().Any()) - { - // Root nodes without an explicit x:DataType still need a deterministic - // starting type for compiled bindings. Falling back to the root CLR type - // allows expressions to compile and avoids stale inference from unrelated - // branches, while runtime DataContext semantics remain unchanged. - inferredDataContextTypeNode = new AvaloniaXamlIlDataContextTypeMetadataNode(on, on.Type.GetClrType()); - } } return directiveDataContextTypeNode ?? inferredDataContextTypeNode ?? node; diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/XamlIlTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/XamlIlTests.cs index f0e27f05d1..e921ad5461 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/XamlIlTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/XamlIlTests.cs @@ -427,23 +427,6 @@ namespace Avalonia.Markup.Xaml.UnitTests } } - [Fact] - public void Compiled_Binding_Should_Fallback_To_Root_Type_Without_Explicit_DataType() - { - using (UnitTestApplication.Start(TestServices.StyledWindow)) - { - var parsed = (CompiledBindingRootMock)AvaloniaRuntimeXamlLoader.Parse(@" - - -"); - parsed.DataContext = parsed; - var textBlock = (TextBlock)parsed.Content!; - Assert.Equal("Hello", textBlock.Text); - } - } - [Fact] public void Compiled_Binding_Should_Resolve_Named_Root_DataContext_In_ItemTemplate() { @@ -465,23 +448,6 @@ namespace Avalonia.Markup.Xaml.UnitTests } } - [Fact] - public void Compiled_Binding_Root_Fallback_Should_Not_Crash_With_Incompatible_Runtime_DataContext() - { - using (UnitTestApplication.Start(TestServices.StyledWindow)) - { - var parsed = (CompiledBindingRootMock)AvaloniaRuntimeXamlLoader.Parse(@" - - -"); - parsed.DataContext = new object(); - var textBlock = (TextBlock)parsed.Content!; - Assert.Null(textBlock.Text); - } - } - [Fact] public void Compiled_Binding_Should_Resolve_Root_Command_From_Nested_ItemTemplate_Namescope() {