-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[net11.0] Compile x:Reference bindings against resolved element type #34513
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
+136
−25
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
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 |
|---|---|---|
|
|
@@ -343,25 +343,37 @@ private static bool ProvideValueForBindingExtension(ElementNode markupNode, Inde | |
| returnType = context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.BindingBase")!; | ||
| ITypeSymbol? dataTypeSymbol = null; | ||
|
|
||
| // When Source is explicitly set (RelativeSource or x:Reference), x:DataType does not describe | ||
| // the actual source — skip compilation and fall back to runtime binding. | ||
| bool hasExplicitSource = HasExplicitBindingSource(markupNode); | ||
| // When Source is RelativeSource, the type is determined at runtime — skip compilation. | ||
| // When Source is x:Reference, resolve the referenced element's type and compile against it. | ||
| // Otherwise, use x:DataType from the current scope. | ||
| bool hasRelativeSource = HasRelativeSourceBinding(markupNode); | ||
|
|
||
| context.Variables.TryGetValue(markupNode, out ILocalValue? extVariable); | ||
|
|
||
| if ( !hasExplicitSource | ||
| if ( !hasRelativeSource | ||
| && extVariable is not null) | ||
| { | ||
| TryGetXDataType(markupNode, context, out dataTypeSymbol); | ||
| ITypeSymbol? xRefSourceType = TryResolveXReferenceSourceType(markupNode, context); | ||
| dataTypeSymbol = xRefSourceType; | ||
| if (dataTypeSymbol is null) | ||
| TryGetXDataType(markupNode, context, out dataTypeSymbol); | ||
|
|
||
| if (dataTypeSymbol is not null) | ||
| { | ||
| var compiledBindingMarkup = new CompiledBindingMarkup(markupNode, GetBindingPath(markupNode), extVariable, context); | ||
| if (compiledBindingMarkup.TryCompileBinding(dataTypeSymbol, isTemplateBinding, out string? newBindingExpression) && newBindingExpression is not null) | ||
| if (compiledBindingMarkup.TryCompileBinding(dataTypeSymbol, isTemplateBinding, out string? newBindingExpression, out Diagnostic? propertyNotFoundDiagnostic) && newBindingExpression is not null) | ||
| { | ||
| value = newBindingExpression; | ||
| return true; | ||
| } | ||
|
|
||
| // Emit property-not-found diagnostic only for x:DataType-sourced bindings. | ||
| // For x:Reference bindings, silently fall back to runtime — these bindings | ||
| // were never compiled before, so emitting a new warning would be a regression. | ||
| if (propertyNotFoundDiagnostic is not null && xRefSourceType is null) | ||
| { | ||
| context.ReportDiagnostic(propertyNotFoundDiagnostic); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -632,29 +644,71 @@ static bool IsBindingContextBinding(ElementNode node) | |
| && propertyName.LocalName == "BindingContext"; | ||
| } | ||
|
|
||
| // Checks if the binding has a Source property set to RelativeSource or x:Reference. | ||
| // When Source is explicitly set, x:DataType does not describe the actual binding source, | ||
| // Checks if the binding has a Source property set to RelativeSource. | ||
| // When a binding uses RelativeSource, the source type is determined at runtime, | ||
| // so we should NOT compile the binding using x:DataType. | ||
| static bool HasExplicitBindingSource(ElementNode bindingNode) | ||
| static bool HasRelativeSourceBinding(ElementNode bindingNode) | ||
| { | ||
| // Check if Source property exists | ||
| if (!bindingNode.Properties.TryGetValue(new XmlName("", "Source"), out INode? sourceNode) | ||
| && !bindingNode.Properties.TryGetValue(new XmlName(null, "Source"), out sourceNode)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if the Source is a RelativeSourceExtension or ReferenceExtension | ||
| if (sourceNode is ElementNode sourceElementNode) | ||
| { | ||
| return sourceElementNode.XmlType.Name is "RelativeSourceExtension" | ||
| or "RelativeSource" | ||
| or "ReferenceExtension" | ||
| or "Reference"; | ||
| or "RelativeSource"; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| // When Source={x:Reference Name} is set on a binding, resolves the referenced element's type | ||
| // by walking namescopes (same logic as ProvideValueForReferenceExtension). | ||
| // Returns null if Source is not an x:Reference or the name cannot be resolved. | ||
| static ITypeSymbol? TryResolveXReferenceSourceType(ElementNode bindingNode, SourceGenContext context) | ||
| { | ||
| if (!bindingNode.Properties.TryGetValue(new XmlName("", "Source"), out INode? sourceNode) | ||
| && !bindingNode.Properties.TryGetValue(new XmlName(null, "Source"), out sourceNode)) | ||
| return null; | ||
|
|
||
| if (sourceNode is not ElementNode refNode) | ||
| return null; | ||
|
|
||
| if (refNode.XmlType.Name is not "ReferenceExtension" and not "Reference") | ||
| return null; | ||
|
|
||
| // Extract the Name from the x:Reference markup | ||
| if (!refNode.Properties.TryGetValue(new XmlName("", "Name"), out INode? refNameNode) | ||
| && !refNode.Properties.TryGetValue(new XmlName(null, "Name"), out refNameNode)) | ||
| { | ||
| refNameNode = refNode.CollectionItems.Count > 0 ? refNode.CollectionItems[0] : null; | ||
| } | ||
|
|
||
| if (refNameNode is not ValueNode vn || vn.Value is not string name) | ||
| return null; | ||
|
|
||
| // Walk namescopes to find the referenced element's type | ||
| ElementNode? node = bindingNode; | ||
| var currentContext = context; | ||
| while (currentContext is not null && node is not null) | ||
| { | ||
| while (currentContext is not null && !currentContext.Scopes.ContainsKey(node)) | ||
| currentContext = currentContext.ParentContext; | ||
| if (currentContext is null) | ||
| break; | ||
| var namescope = currentContext.Scopes[node]; | ||
| if (namescope.namesInScope != null && namescope.namesInScope.ContainsKey(name)) | ||
| return namescope.namesInScope[name].Type; | ||
| INode n = node; | ||
| while (n.Parent is ListNode ln) | ||
| n = ln.Parent; | ||
| node = n.Parent as ElementNode; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
Comment on lines
+667
to
+711
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with copilot here |
||
| } | ||
|
|
||
| internal static bool ProvideValueForDataTemplateExtension(ElementNode markupNode, IndentedTextWriter writer, SourceGenContext context, NodeSGExtensions.GetNodeValueDelegate? getNodeValue, out ITypeSymbol? returnType, out string value) | ||
|
|
||
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
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 |
|---|---|---|
|
|
@@ -18,12 +18,15 @@ public class Tests : IDisposable | |
|
|
||
| [Theory] | ||
| [XamlInflatorData] | ||
| internal void BindingsWithSourceAndInvalidPathAreNotCompiled(XamlInflator inflator) | ||
| internal void BindingsWithXReferenceSourceResolveAgainstReferencedType(XamlInflator inflator) | ||
| { | ||
| // Source={x:Reference page} points to ContentPage, which has a Content property. | ||
| // The binding path "Content" resolves against ContentPage, so the source generator | ||
| // compiles it instead of falling back to runtime Binding. | ||
| var view = new Gh3606(inflator); | ||
|
|
||
| var binding = view.Label.GetContext(Label.TextProperty).Bindings.GetValue(); | ||
| Assert.IsType<Binding>(binding); | ||
| Assert.IsAssignableFrom<BindingBase>(binding); | ||
| } | ||
|
Comment on lines
+21
to
30
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with copilot here, we should assert that the binding was compiled. |
||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One day I'll make copilot an ext method
TryGetPropertyNode("Source", out INode? sourceNode)😆