Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 56 additions & 37 deletions src/System.Management.Automation/engine/CoreAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,27 @@ internal static MethodInformation FindBestMethod(
return methodInfo;
}

private static Type[] ResolveGenericTypeParameters(object[] genericTypeParameters)
{
if (genericTypeParameters is null || genericTypeParameters.Length == 0)
{
return null;
}

Type[] genericParamTypes = new Type[genericTypeParameters.Length];
for (int i = 0; i < genericTypeParameters.Length; i++)
{
genericParamTypes[i] = genericTypeParameters[i] switch
{
Type paramType => paramType,
ITypeName paramTypeName => TypeOps.ResolveTypeName(paramTypeName, paramTypeName.Extent),
_ => throw new ArgumentException("Unexpected value"),
};
}

return genericParamTypes;
}

private static MethodInformation FindBestMethodImpl(
MethodInformation[] methods,
PSMethodInvocationConstraints invocationConstraints,
Expand Down Expand Up @@ -1404,15 +1425,15 @@ private static MethodInformation FindBestMethodImpl(
return methods[0];
}

Type[] argumentTypes = arguments.Select(EffectiveArgumentType).ToArray();
Type[] genericParameters = invocationConstraints?.GenericTypeParameters ?? Array.Empty<Type>();
List<OverloadCandidate> candidates = new List<OverloadCandidate>();
Type[] genericParamTypes = ResolveGenericTypeParameters(invocationConstraints?.GenericTypeParameters);
var candidates = new List<OverloadCandidate>();

for (int i = 0; i < methods.Length; i++)
{
MethodInformation methodInfo = methods[i];

if (methodInfo.method?.DeclaringType.IsGenericTypeDefinition == true
|| (!methodInfo.isGeneric && genericParameters.Length > 0))
|| (!methodInfo.isGeneric && genericParamTypes is not null))
{
// If method is defined by an *open* generic type, or
// if generic parameters were provided and this method isn't generic, skip it.
Expand All @@ -1421,29 +1442,16 @@ private static MethodInformation FindBestMethodImpl(

if (methodInfo.isGeneric)
{
Type[] argumentTypesForTypeInference = new Type[argumentTypes.Length];
Array.Copy(argumentTypes, argumentTypesForTypeInference, argumentTypes.Length);

if (invocationConstraints?.ParameterTypes is not null)
{
int parameterIndex = 0;
foreach (Type typeConstraintFromCallSite in invocationConstraints.ParameterTypes)
{
if (typeConstraintFromCallSite != null)
{
argumentTypesForTypeInference[parameterIndex] = typeConstraintFromCallSite;
}

parameterIndex++;
}
}

if (genericParameters.Length > 0 && methodInfo.method is MethodInfo originalMethod)
if (genericParamTypes is not null)
{
try
{
// This cast is safe, because
// 1. Only ConstructorInfo and MethodInfo derive from MethodBase
// 2. ConstructorInfo.IsGenericMethod is always false
var originalMethod = (MethodInfo)methodInfo.method;
methodInfo = new MethodInformation(
originalMethod.MakeGenericMethod(genericParameters),
originalMethod.MakeGenericMethod(genericParamTypes),
parametersToIgnore: 0);
}
catch (ArgumentException)
Expand All @@ -1453,12 +1461,29 @@ private static MethodInformation FindBestMethodImpl(
continue;
}
}

methodInfo = TypeInference.Infer(methodInfo, argumentTypesForTypeInference);
if (methodInfo is null)
else
{
// Skip generic methods for which we cannot infer type arguments
continue;
// Infer the generic method when generic parameter types are not specified.
Type[] argumentTypes = arguments.Select(EffectiveArgumentType).ToArray();
Type[] paramConstraintTypes = invocationConstraints?.ParameterTypes;

if (paramConstraintTypes is not null)
{
for (int k = 0; k < paramConstraintTypes.Length; k++)
{
if (paramConstraintTypes[k] is not null)
{
argumentTypes[k] = paramConstraintTypes[k];
}
}
}

methodInfo = TypeInference.Infer(methodInfo, argumentTypes);
if (methodInfo is null)
{
// Skip generic methods for which we cannot infer type arguments
continue;
}
}
}

Expand Down Expand Up @@ -1602,7 +1627,7 @@ private static MethodInformation FindBestMethodImpl(

if (candidates.Count == 0)
{
if ((methods.Length > 0) && (methods.All(static m => m.method != null && m.method.DeclaringType.IsGenericTypeDefinition && m.method.IsStatic)))
if (methods.Length > 0 && methods.All(static m => m.method != null && m.method.DeclaringType.IsGenericTypeDefinition && m.method.IsStatic))
{
errorId = "CannotInvokeStaticMethodOnUninstantiatedGenericType";
errorMsg = string.Format(
Expand All @@ -1611,19 +1636,13 @@ private static MethodInformation FindBestMethodImpl(
methods[0].method.DeclaringType.FullName);
return null;
}
else if (genericParameters.Length != 0 && genericParameters.Contains(null))
{
errorId = "TypeNotFoundForGenericMethod";
errorMsg = ExtendedTypeSystem.MethodGenericArgumentTypeNotFoundException;
return null;
}
else if (genericParameters.Length != 0)
else if (genericParamTypes is not null)
{
errorId = "MethodCountCouldNotFindBestGeneric";
errorMsg = string.Format(
ExtendedTypeSystem.MethodGenericArgumentCountException,
methods[0].method.Name,
genericParameters.Length,
genericParamTypes.Length,
arguments.Length);
return null;
}
Expand Down
40 changes: 25 additions & 15 deletions src/System.Management.Automation/engine/MshMemberInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1909,17 +1909,17 @@ internal class PSMethodInvocationConstraints
internal PSMethodInvocationConstraints(
Type methodTargetType,
Type[] parameterTypes)
: this(methodTargetType, genericTypeParameters: null, parameterTypes)
: this(methodTargetType, parameterTypes, genericTypeParameters: null)
{
}

internal PSMethodInvocationConstraints(
Type methodTargetType,
Type[] genericTypeParameters,
Type[] parameterTypes)
Type[] parameterTypes,
object[] genericTypeParameters)
{
MethodTargetType = methodTargetType;
_parameterTypes = parameterTypes;
ParameterTypes = parameterTypes;
GenericTypeParameters = genericTypeParameters;
}

Expand All @@ -1931,14 +1931,12 @@ internal PSMethodInvocationConstraints(
/// <remarks>
/// If <see langword="null"/> then there are no constraints
/// </remarks>
public IEnumerable<Type> ParameterTypes => _parameterTypes;

private readonly Type[] _parameterTypes;
public Type[] ParameterTypes { get; }

/// <summary>
/// Gets the generic type parameters for the method invocation.
/// </summary>
public Type[] GenericTypeParameters { get; }
public object[] GenericTypeParameters { get; }

internal static bool EqualsForCollection<T>(ICollection<T> xs, ICollection<T> ys)
{
Expand Down Expand Up @@ -1977,7 +1975,7 @@ public bool Equals(PSMethodInvocationConstraints other)
return false;
}

if (!EqualsForCollection(_parameterTypes, other._parameterTypes))
if (!EqualsForCollection(ParameterTypes, other.ParameterTypes))
{
return false;
}
Expand Down Expand Up @@ -2017,35 +2015,47 @@ public override string ToString()
{
StringBuilder sb = new StringBuilder();
string separator = string.Empty;
if (MethodTargetType != null)
if (MethodTargetType is not null)
{
sb.Append("this: ");
sb.Append(ToStringCodeMethods.Type(MethodTargetType, dropNamespaces: true));
separator = " ";
}

if (GenericTypeParameters != null)
if (GenericTypeParameters is not null)
{
sb.Append(separator);
sb.Append("genericTypeParams: ");

separator = string.Empty;
foreach (Type parameter in GenericTypeParameters)
foreach (object parameter in GenericTypeParameters)
{
sb.Append(separator);
sb.Append(ToStringCodeMethods.Type(parameter, dropNamespaces: true));

switch (parameter)
{
case Type paramType:
sb.Append(ToStringCodeMethods.Type(paramType, dropNamespaces: true));
break;
case ITypeName paramTypeName:
sb.Append(paramTypeName.ToString());
break;
default:
throw new ArgumentException("Unexpected value");
}

separator = ", ";
}

separator = " ";
}

if (_parameterTypes != null)
if (ParameterTypes is not null)
{
sb.Append(separator);
sb.Append("args: ");
separator = string.Empty;
foreach (var p in _parameterTypes)
foreach (var p in ParameterTypes)
{
sb.Append(separator);
sb.Append(ToStringCodeMethods.Type(p, dropNamespaces: true));
Expand Down
22 changes: 9 additions & 13 deletions src/System.Management.Automation/engine/parser/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,25 +1174,20 @@ internal static Type GetTypeConstraintForMethodResolution(ExpressionAst expr)
return firstConvert?.Type.TypeName.GetReflectionType();
}

internal static PSMethodInvocationConstraints CombineTypeConstraintForMethodResolution(
Type targetType,
Type argType,
Type[] genericArguments = null)
internal static PSMethodInvocationConstraints CombineTypeConstraintForMethodResolution(Type targetType, Type argType)
{
if (targetType is null
&& argType is null
&& (genericArguments is null || genericArguments.Length == 0))
if (targetType is null && argType is null)
{
return null;
}

return new PSMethodInvocationConstraints(targetType, genericArguments, new[] { argType });
return new PSMethodInvocationConstraints(targetType, new[] { argType });
}

internal static PSMethodInvocationConstraints CombineTypeConstraintForMethodResolution(
Type targetType,
Type[] argTypes,
Type[] genericArguments = null)
object[] genericArguments = null)
{
if (targetType is null
&& (argTypes is null || argTypes.Length == 0)
Expand All @@ -1201,7 +1196,7 @@ internal static PSMethodInvocationConstraints CombineTypeConstraintForMethodReso
return null;
}

return new PSMethodInvocationConstraints(targetType, genericArguments, argTypes);
return new PSMethodInvocationConstraints(targetType, argTypes, genericArguments);
}

internal static Expression ConvertValue(TypeConstraintAst typeConstraint, Expression expr)
Expand Down Expand Up @@ -6362,13 +6357,14 @@ internal static PSMethodInvocationConstraints GetInvokeMemberConstraints(InvokeM
var targetTypeConstraint = GetTypeConstraintForMethodResolution(invokeMemberExpressionAst.Expression);

ReadOnlyCollection<ITypeName> genericArguments = invokeMemberExpressionAst.GenericTypeArguments;
Type[] genericTypeArguments = null;
object[] genericTypeArguments = null;
if (genericArguments is not null)
{
genericTypeArguments = new Type[genericArguments.Count];
genericTypeArguments = new object[genericArguments.Count];
for (var i = 0; i < genericArguments.Count; i++)
{
genericTypeArguments[i] = genericArguments[i].GetReflectionType();
Type type = genericArguments[i].GetReflectionType();
genericTypeArguments[i] = (object)type ?? genericArguments[i];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,6 @@
<data name="MethodGenericArgumentCountException" xml:space="preserve">
<value>Could not find a suitable generic method overload for "{0}" with "{1}" type parameters, and the argument count: "{2}".</value>
</data>
<data name="MethodGenericArgumentTypeNotFoundException" xml:space="preserve">
<value>One or more of the generic type parameters provided for the method "{0}" refers to a type which cannot be found.</value>
</data>
<data name="MethodAmbiguousException" xml:space="preserve">
<value>Multiple ambiguous overloads found for "{0}" and the argument count: "{1}".</value>
</data>
Expand Down
Loading