Skip to content
Merged
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
8 changes: 5 additions & 3 deletions src/System.Management.Automation/engine/parser/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1309,14 +1309,16 @@ private static Attribute NewValidateSetAttribute(AttributeAst ast)
// 'ValidateSet([CustomGeneratorType], IgnoreCase=$false)' is supported in scripts.
if (ast.PositionalArguments.Count == 1 && ast.PositionalArguments[0] is TypeExpressionAst generatorTypeAst)
{
if (TypeResolver.TryResolveType(generatorTypeAst.TypeName.FullName, out Type generatorType))
var generatorType = TypeResolver.ResolveITypeName(generatorTypeAst.TypeName, out Exception exception);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResolveITypeName again check iTypeName as TypeName - can we use internal static Type ResolveTypeName(TypeName typeName, out Exception exception)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you might have something else like a generic or array type instead of a simple type.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarify!
Should we test these cases too (generic or array)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ast must be fully general which explains why you must use ResolveITypeName here.

In this specific case, it is extremely unlikely anyone would ever use a generic (which should work) or an array (which should be an error), so testing has marginal usefulness.

if (generatorType != null)
{
result = new ValidateSetAttribute(generatorType);
}
else
{
throw InterpreterError.NewInterpreterException(ast, typeof(RuntimeException), ast.Extent,
"TypeNotFound", ParserStrings.TypeNotFound, generatorTypeAst.TypeName.FullName, typeof(System.Management.Automation.IValidateSetValuesGenerator).FullName);
throw InterpreterError.NewInterpreterExceptionWithInnerException(
ast, typeof(RuntimeException), ast.Extent, "TypeNotFound", ParserStrings.TypeNotFound, exception,
generatorTypeAst.TypeName.FullName, typeof(System.Management.Automation.IValidateSetValuesGenerator).FullName);
}
}
else
Expand Down