Fix array type parsing in generic types (#19205)

This commit is contained in:
MartinGC94
2023-05-08 16:21:51 -07:00
committed by GitHub
parent 0b078175da
commit 1c8b58c0b4
2 changed files with 14 additions and 4 deletions
@@ -1346,7 +1346,7 @@ namespace System.Management.Automation.Language
case TokenKind.RBracket:
case TokenKind.Comma:
var elementType = new TypeName(typeName.Extent, typeName.Text);
return CompleteArrayTypeName(elementType, elementType, token);
return CompleteArrayTypeName(elementType, elementType, token, unBracketedGenericArg);
case TokenKind.LBracket:
case TokenKind.Identifier:
@@ -1496,7 +1496,7 @@ namespace System.Management.Automation.Language
if (token.Kind == TokenKind.LBracket)
{
SkipToken();
return CompleteArrayTypeName(result, openGenericType, NextToken());
return CompleteArrayTypeName(result, openGenericType, NextToken(), unbracketedGenericArg);
}
if (token.Kind == TokenKind.Comma && !unbracketedGenericArg)
@@ -1519,7 +1519,7 @@ namespace System.Management.Automation.Language
return result;
}
private ITypeName CompleteArrayTypeName(ITypeName elementType, TypeName typeForAssemblyQualification, Token firstTokenAfterLBracket)
private ITypeName CompleteArrayTypeName(ITypeName elementType, TypeName typeForAssemblyQualification, Token firstTokenAfterLBracket, bool unBracketedGenericArg)
{
while (true)
{
@@ -1592,7 +1592,9 @@ namespace System.Management.Automation.Language
}
token = PeekToken();
if (token.Kind == TokenKind.Comma)
// An array declared inside an unbracketed generic type argument cannot be assembly qualified
if (!unBracketedGenericArg && token.Kind == TokenKind.Comma)
{
SkipToken();
var assemblyName = _tokenizer.GetAssemblyNameSpec();
@@ -664,3 +664,11 @@ Describe "Parsing using statement with alias and linebreak and comma" -Tag CI {
}
}
}
It "Should correctly parse array types that are used as arguments without brackets in generic type" {
$tks = $null
$ers = $null
$Script = '[System.Tuple[System.String[],System.Int32[]]]'
$result = [System.Management.Automation.Language.Parser]::ParseInput($Script, [ref]$tks, [ref]$ers)
$result.EndBlock.Statements[0].PipelineElements[0].Expression.TypeName.FullName | Should -Be 'System.Tuple[System.String[],System.Int32[]]'
}