Skip to content

Commit

Permalink
[X] don't expant types to Extension for x:Static
Browse files Browse the repository at this point in the history
if a type name exists with an Extension suffix, xaml rules require us to
use that. But not for x:Static

- fixes #11833
  • Loading branch information
StephaneDelcroix committed Sep 12, 2024
1 parent 5180d57 commit 65508e7
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public IEnumerable<Instruction> ProvideValue(IElementNode node, ModuleDefinition
var typename = member.Substring(0, dotIdx);
var membername = member.Substring(dotIdx + 1);

var typeRef = module.ImportReference(XmlTypeExtensions.GetTypeReference(context.Cache, typename, module, node as BaseNode));
var typeRef = module.ImportReference(XmlTypeExtensions.GetTypeReference(context.Cache, typename, module, node as BaseNode, expandToExtension: false));
var fieldRef = GetFieldReference(context.Cache, typeRef, membername, module);
var propertyDef = GetPropertyDefinition(context.Cache, typeRef, membername, module);

Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Build.Tasks/ExpandMarkupsVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public INode Parse(string match, ref string remaining, IServiceProvider serviceP

//The order of lookup is to look for the Extension-suffixed class name first and then look for the class name without the Extension suffix.
XmlType type = new XmlType(namespaceuri, name + "Extension", typeArguments);
if (!type.TryGetTypeReference(contextProvider.Context.Cache, contextProvider.Context.Module, null, out _))
if (!type.TryGetTypeReference(contextProvider.Context.Cache, contextProvider.Context.Module, null, expandToExtension: true, out _))
type = new XmlType(namespaceuri, name, typeArguments);

if (type == null)
Expand Down
14 changes: 7 additions & 7 deletions src/Controls/src/Build.Tasks/XmlTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ static IList<XmlnsDefinitionAttribute> GatherXmlnsDefinitionAttributes(ModuleDef
return xmlnsDefinitions;
}

public static TypeReference GetTypeReference(XamlCache cache, string typeName, ModuleDefinition module, BaseNode node)
public static TypeReference GetTypeReference(XamlCache cache, string typeName, ModuleDefinition module, BaseNode node, bool expandToExtension = true)
{
try
{
XmlType xmlType = TypeArgumentsParser.ParseSingle(typeName, node.NamespaceResolver, (IXmlLineInfo)node);
return GetTypeReference(xmlType, cache, module, node as IXmlLineInfo);
return GetTypeReference(xmlType, cache, module, node as IXmlLineInfo, expandToExtension: expandToExtension);
}
catch (XamlParseException)
{
throw new BuildException(BuildExceptionCode.InvalidXaml, node as IXmlLineInfo, null, typeName);
}
}

public static TypeReference GetTypeReference(XamlCache cache, string namespaceURI, string typename, ModuleDefinition module, IXmlLineInfo xmlInfo)
public static TypeReference GetTypeReference(XamlCache cache, string namespaceURI, string typename, ModuleDefinition module, IXmlLineInfo xmlInfo, bool expandToExtension = true)
{
return new XmlType(namespaceURI, typename, null).GetTypeReference(cache, module, xmlInfo);
}

public static bool TryGetTypeReference(this XmlType xmlType, XamlCache cache, ModuleDefinition module, IXmlLineInfo xmlInfo, out TypeReference typeReference)
public static bool TryGetTypeReference(this XmlType xmlType, XamlCache cache, ModuleDefinition module, IXmlLineInfo xmlInfo, bool expandToExtension, out TypeReference typeReference)
{
IList<XmlnsDefinitionAttribute> xmlnsDefinitions = cache.GetXmlsDefinitions(module, GatherXmlnsDefinitionAttributes);

Expand All @@ -81,17 +81,17 @@ public static bool TryGetTypeReference(this XmlType xmlType, XamlCache cache, Mo
if (type is not null && type.IsPublicOrVisibleInternal(module))
return type;
return null;
});
}, expandToExtension: expandToExtension);

if (type != null && typeArguments != null && type.HasGenericParameters)
type = module.ImportReference(type).MakeGenericInstanceType(typeArguments.Select(x => x.GetTypeReference(cache, module, xmlInfo)).ToArray());

return (typeReference = (type == null) ? null : module.ImportReference(type)) != null;
}

public static TypeReference GetTypeReference(this XmlType xmlType, XamlCache cache, ModuleDefinition module, IXmlLineInfo xmlInfo)
public static TypeReference GetTypeReference(this XmlType xmlType, XamlCache cache, ModuleDefinition module, IXmlLineInfo xmlInfo, bool expandToExtension = true)
{
if (TryGetTypeReference(xmlType, cache, module, xmlInfo, out TypeReference typeReference))
if (TryGetTypeReference(xmlType, cache, module, xmlInfo, expandToExtension: expandToExtension, out TypeReference typeReference))
return typeReference;

throw new BuildException(BuildExceptionCode.TypeResolution, xmlInfo, null, $"{xmlType.NamespaceUri}:{xmlType.Name}");
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/IXamlTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Microsoft.Maui.Controls.Xaml
{
public interface IXamlTypeResolver
{
Type Resolve(string qualifiedTypeName, IServiceProvider serviceProvider = null);
Type Resolve(string qualifiedTypeName, IServiceProvider serviceProvider = null, bool expandToExtension = true);
bool TryResolve(string qualifiedTypeName, out Type type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,7 @@
~Microsoft.Maui.Controls.Xaml.IReferenceProvider.FindByName(string name) -> object
~Microsoft.Maui.Controls.Xaml.IRootObjectProvider.RootObject.get -> object
~Microsoft.Maui.Controls.Xaml.IValueProvider.ProvideValue(System.IServiceProvider serviceProvider) -> object
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null, bool expandToExtension = true) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.TryResolve(string qualifiedTypeName, out System.Type type) -> bool
~Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider.XmlLineInfo.get -> System.Xml.IXmlLineInfo
~Microsoft.Maui.Controls.Xaml.XamlParseException.XamlParseException(string message, System.Exception innerException) -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,7 @@
~Microsoft.Maui.Controls.Xaml.IReferenceProvider.FindByName(string name) -> object
~Microsoft.Maui.Controls.Xaml.IRootObjectProvider.RootObject.get -> object
~Microsoft.Maui.Controls.Xaml.IValueProvider.ProvideValue(System.IServiceProvider serviceProvider) -> object
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null, bool expandToExtension = true) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.TryResolve(string qualifiedTypeName, out System.Type type) -> bool
~Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider.XmlLineInfo.get -> System.Xml.IXmlLineInfo
~Microsoft.Maui.Controls.Xaml.XamlParseException.XamlParseException(string message, System.Exception innerException) -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,7 @@
~Microsoft.Maui.Controls.Xaml.IReferenceProvider.FindByName(string name) -> object
~Microsoft.Maui.Controls.Xaml.IRootObjectProvider.RootObject.get -> object
~Microsoft.Maui.Controls.Xaml.IValueProvider.ProvideValue(System.IServiceProvider serviceProvider) -> object
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null, bool expandToExtension = true) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.TryResolve(string qualifiedTypeName, out System.Type type) -> bool
~Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider.XmlLineInfo.get -> System.Xml.IXmlLineInfo
~Microsoft.Maui.Controls.Xaml.XamlParseException.XamlParseException(string message, System.Exception innerException) -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,7 @@
~Microsoft.Maui.Controls.Xaml.IReferenceProvider.FindByName(string name) -> object
~Microsoft.Maui.Controls.Xaml.IRootObjectProvider.RootObject.get -> object
~Microsoft.Maui.Controls.Xaml.IValueProvider.ProvideValue(System.IServiceProvider serviceProvider) -> object
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null, bool expandToExtension = true) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.TryResolve(string qualifiedTypeName, out System.Type type) -> bool
~Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider.XmlLineInfo.get -> System.Xml.IXmlLineInfo
~Microsoft.Maui.Controls.Xaml.XamlParseException.XamlParseException(string message, System.Exception innerException) -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1736,7 +1736,7 @@
~Microsoft.Maui.Controls.Xaml.IReferenceProvider.FindByName(string name) -> object
~Microsoft.Maui.Controls.Xaml.IRootObjectProvider.RootObject.get -> object
~Microsoft.Maui.Controls.Xaml.IValueProvider.ProvideValue(System.IServiceProvider serviceProvider) -> object
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null, bool expandToExtension = true) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.TryResolve(string qualifiedTypeName, out System.Type type) -> bool
~Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider.XmlLineInfo.get -> System.Xml.IXmlLineInfo
~Microsoft.Maui.Controls.Xaml.XamlParseException.XamlParseException(string message, System.Exception innerException) -> void
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/PublicAPI/net/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@
~Microsoft.Maui.Controls.Xaml.IReferenceProvider.FindByName(string name) -> object
~Microsoft.Maui.Controls.Xaml.IRootObjectProvider.RootObject.get -> object
~Microsoft.Maui.Controls.Xaml.IValueProvider.ProvideValue(System.IServiceProvider serviceProvider) -> object
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null, bool expandToExtension = true) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.TryResolve(string qualifiedTypeName, out System.Type type) -> bool
~Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider.XmlLineInfo.get -> System.Xml.IXmlLineInfo
~Microsoft.Maui.Controls.Xaml.XamlParseException.XamlParseException(string message, System.Exception innerException) -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@
~Microsoft.Maui.Controls.Xaml.IReferenceProvider.FindByName(string name) -> object
~Microsoft.Maui.Controls.Xaml.IRootObjectProvider.RootObject.get -> object
~Microsoft.Maui.Controls.Xaml.IValueProvider.ProvideValue(System.IServiceProvider serviceProvider) -> object
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null, bool expandToExtension = true) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.TryResolve(string qualifiedTypeName, out System.Type type) -> bool
~Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider.XmlLineInfo.get -> System.Xml.IXmlLineInfo
~Microsoft.Maui.Controls.Xaml.XamlParseException.XamlParseException(string message, System.Exception innerException) -> void
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Xaml/ApplyPropertiesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ static bool GetRealNameAndType(ref Type elementType, string namespaceURI, ref st
localname = localname.Substring(dotIdx + 1);
XamlParseException xpe;
elementType = XamlParser.GetElementType(new XmlType(namespaceURI, typename, null), lineInfo,
rootElement.GetType().Assembly, out xpe);
rootElement.GetType().Assembly, true, out xpe);

if (xpe != null)
throw xpe;
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Xaml/CreateValuesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void Visit(ElementNode node, INode parentNode)
{
object value = null;

var type = XamlParser.GetElementType(node.XmlType, node, Context.RootElement?.GetType().Assembly,
var type = XamlParser.GetElementType(node.XmlType, node, Context.RootElement?.GetType().Assembly, true,
out XamlParseException xpe);
if (xpe != null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Xaml/MarkupExtensions/StaticExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public object ProvideValue(IServiceProvider serviceProvider)
var typename = Member.Substring(0, dotIdx);
var membername = Member.Substring(dotIdx + 1);

var type = typeResolver.Resolve(typename, serviceProvider);
var type = typeResolver.Resolve(typename, serviceProvider, expandToExtension: false);

var pinfo = type.GetRuntimeProperties().FirstOrDefault(pi => pi.Name == membername && pi.GetMethod.IsStatic);
if (pinfo != null)
Expand Down
9 changes: 7 additions & 2 deletions src/Controls/src/Xaml/XamlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,15 @@ static void GatherXmlnsDefinitionAttributes()
}
}

<<<<<<< HEAD
[RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)]
#if !NETSTANDARD
[RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)]
#endif
public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly,
=======
public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly, bool expandToExtension,
>>>>>>> 4c8c9eadfe ([X] don't expant types to Extension for x:Static)
out XamlParseException exception)
{
bool hasRetriedNsSearch = false;
Expand All @@ -379,7 +383,8 @@ public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembl
if (t is not null && t.IsPublicOrVisibleInternal(currentAssembly))
return t;
return null;
});
},
expandToExtension);

var typeArguments = xmlType.TypeArguments;
exception = null;
Expand All @@ -403,7 +408,7 @@ public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembl
XamlParseException innerexception = null;
var args = typeArguments.Select(delegate (XmlType xmltype)
{
var t = GetElementType(xmltype, xmlInfo, currentAssembly, out XamlParseException xpe);
var t = GetElementType(xmltype, xmlInfo, currentAssembly, true, out XamlParseException xpe);
if (xpe != null)
{
innerexception = xpe;
Expand Down
14 changes: 7 additions & 7 deletions src/Controls/src/Xaml/XamlServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,28 +207,28 @@ internal XamlTypeResolver(IXmlNamespaceResolver namespaceResolver, GetTypeFromXm
this.getTypeFromXmlName = getTypeFromXmlName ?? throw new ArgumentNullException();
}

Type IXamlTypeResolver.Resolve(string qualifiedTypeName, IServiceProvider serviceProvider)
Type IXamlTypeResolver.Resolve(string qualifiedTypeName, IServiceProvider serviceProvider, bool expandToExtension)
{
var type = Resolve(qualifiedTypeName, serviceProvider, out XamlParseException e);
var type = Resolve(qualifiedTypeName, serviceProvider, expandToExtension: expandToExtension, out XamlParseException e);
if (e != null)
throw e;
return type;
}

bool IXamlTypeResolver.TryResolve(string qualifiedTypeName, out Type type)
{
type = Resolve(qualifiedTypeName, null, out XamlParseException exception);
type = Resolve(qualifiedTypeName, null, true, out XamlParseException exception);
return exception == null;
}

internal bool TryResolve(XmlType xmlType, out Type type)
{
XamlParseException exception;
type = getTypeFromXmlName(xmlType, null, currentAssembly, out exception);
type = getTypeFromXmlName(xmlType, null, currentAssembly, true, out exception);
return exception == null;
}

Type Resolve(string qualifiedTypeName, IServiceProvider serviceProvider, out XamlParseException exception)
Type Resolve(string qualifiedTypeName, IServiceProvider serviceProvider, bool expandToExtension, out XamlParseException exception)
{
IXmlLineInfo xmlLineInfo = null;
if (serviceProvider != null)
Expand All @@ -238,10 +238,10 @@ Type Resolve(string qualifiedTypeName, IServiceProvider serviceProvider, out Xam
}

var xmlType = TypeArgumentsParser.ParseSingle(qualifiedTypeName, namespaceResolver, xmlLineInfo);
return getTypeFromXmlName(xmlType, xmlLineInfo, currentAssembly, out exception);
return getTypeFromXmlName(xmlType, xmlLineInfo, currentAssembly, expandToExtension, out exception);
}

internal delegate Type GetTypeFromXmlName(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly, out XamlParseException exception);
internal delegate Type GetTypeFromXmlName(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly, bool expandToExtension, out XamlParseException exception);
}

class XamlRootObjectProvider : IRootObjectProvider
Expand Down
5 changes: 3 additions & 2 deletions src/Controls/src/Xaml/XmlTypeXamlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ static class XmlTypeXamlExtensions
this XmlType xmlType,
IEnumerable<XmlnsDefinitionAttribute> xmlnsDefinitions,
string defaultAssemblyName,
Func<(string typeName, string clrNamespace, string assemblyName), T> refFromTypeInfo)
Func<(string typeName, string clrNamespace, string assemblyName), T> refFromTypeInfo,
bool expandToExtension = true)
where T : class
{
var lookupAssemblies = new List<XmlnsDefinitionAttribute>();
Expand All @@ -61,7 +62,7 @@ static class XmlTypeXamlExtensions
}

var lookupNames = new List<string>(capacity: 2);
if (elementName != "DataTemplate" && !elementName.EndsWith("Extension", StringComparison.Ordinal))
if (expandToExtension && elementName != "DataTemplate" && !elementName.EndsWith("Extension", StringComparison.Ordinal))
lookupNames.Add(elementName + "Extension");
lookupNames.Add(elementName);

Expand Down
2 changes: 1 addition & 1 deletion src/Controls/tests/Xaml.UnitTests/LoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ public void StyleWithoutTargetTypeThrows()
public void BindingIsResolvedAsBindingExtension()
// https://github.com/xamarin/Microsoft.Maui.Controls/issues/3606#issuecomment-422377338
{
var bindingType = XamlParser.GetElementType(new XmlType("http://schemas.microsoft.com/dotnet/2021/maui", "Binding", null), null, null, out var ex);
var bindingType = XamlParser.GetElementType(new XmlType("http://schemas.microsoft.com/dotnet/2021/maui", "Binding", null), null, null, true, out var ex);
Assert.That(ex, Is.Null);
Assert.That(bindingType, Is.EqualTo(typeof(BindingExtension)));
var module = ModuleDefinition.CreateModule("foo", new ModuleParameters()
Expand Down

0 comments on commit 65508e7

Please sign in to comment.